Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 4a5dd2d

Browse files
committed
filter the resource and fix some bugs for common users
1 parent 363b274 commit 4a5dd2d

File tree

10 files changed

+78
-44
lines changed

10 files changed

+78
-44
lines changed

pkg/apiserver/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ func (r *APIRequestInfoResolver) GetAPIRequestInfo(req *http.Request) (APIReques
535535
if len(currentParts) > 1 {
536536
requestInfo.Tenant = currentParts[1]
537537

538-
// if there is another step after the namespace name and it is not a known namespace subresource
538+
// if there is another step after the tenant name and it is not a known tenant subresource
539539
// move currentParts to include it as a resource in its own right
540540
if len(currentParts) > 2 {
541541
currentParts = currentParts[2:]

pkg/apiserver/resthandler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/kubernetes/pkg/api/errors"
3131
"k8s.io/kubernetes/pkg/api/rest"
3232
"k8s.io/kubernetes/pkg/api/unversioned"
33+
"k8s.io/kubernetes/pkg/auth/authorizer"
3334
"k8s.io/kubernetes/pkg/fields"
3435
"k8s.io/kubernetes/pkg/runtime"
3536
"k8s.io/kubernetes/pkg/util"
@@ -280,9 +281,8 @@ func ListResource(r rest.Lister, rw rest.Watcher, scope RequestScope, forceWatch
280281
return
281282
}
282283
//
283-
url := req.Request.URL.String()
284-
userinfo, _ := api.UserFrom(ctx)
285-
if strings.Index(url, "https://") == 0 && userinfo.GetName() != api.UserAdmin {
284+
userinfo, ok := api.UserFrom(ctx)
285+
if ok && !authorizer.IsWhiteListedUser(userinfo.GetName()) {
286286
tenant := api.TenantValue(ctx)
287287
if err := filterListInTenant(result, tenant, scope.Kind, scope.Namer); err != nil {
288288
errorJSON(err, scope.Codec, w)
@@ -848,7 +848,7 @@ func filterListInTenant(obj runtime.Object, tenant string, kind string, namer Sc
848848
}
849849
}
850850
}
851-
} else if kind == "Namespace" || kind == "Network" {
851+
} else if kind == "Namespace" || kind == "Network" || kind == "Pod" || kind == "Serviceaccount" || kind == "Secret" {
852852
for i := range items {
853853
if name, err := namer.ObjectTenant(items[i]); err == nil {
854854
if tenant == name {

pkg/auth/authorizer/helper.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package authorizer
18+
19+
import (
20+
"strings"
21+
22+
"k8s.io/kubernetes/pkg/api"
23+
)
24+
25+
func IsWhiteListedUser(username string) bool {
26+
if strings.HasPrefix(username, "system:serviceaccount:") {
27+
return true
28+
}
29+
whiteList := map[string]bool{
30+
api.UserAdmin: true,
31+
"kubelet": true,
32+
"kube_proxy": true,
33+
"system:scheduler": true,
34+
"system:controller_manager": true,
35+
"system:logging": true,
36+
"system:monitoring": true,
37+
}
38+
return whiteList[username]
39+
}

pkg/auth/authorizer/keystone/keystone.go

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package keystone
1818

1919
import (
2020
"errors"
21-
"strings"
2221

2322
"k8s.io/kubernetes/pkg/api"
2423
"k8s.io/kubernetes/pkg/auth/authorizer"
@@ -97,24 +96,8 @@ func (ka *keystoneAuthorizer) Authorize(a authorizer.Attributes) (string, error)
9796
var (
9897
tenantName string
9998
ns *api.Namespace
99+
err error
100100
)
101-
if strings.HasPrefix(a.GetUserName(), "system:serviceaccount:") {
102-
return "", nil
103-
}
104-
if isWhiteListedUser(a.GetUserName()) {
105-
return "", nil
106-
}
107-
108-
authConfig := &authConfig{
109-
AuthUrl: ka.authUrl,
110-
Username: a.GetUserName(),
111-
Password: a.GetPassword(),
112-
}
113-
osClient, err := newOpenstackClient(authConfig)
114-
if err != nil {
115-
glog.Errorf("%v", err)
116-
return "", err
117-
}
118101
if a.GetNamespace() != "" {
119102
ns, err = ka.kubeClient.Namespaces().Get(a.GetNamespace())
120103
if err != nil {
@@ -130,6 +113,25 @@ func (ka *keystoneAuthorizer) Authorize(a authorizer.Attributes) (string, error)
130113
tenantName = te.Name
131114
}
132115
}
116+
if authorizer.IsWhiteListedUser(a.GetUserName()) {
117+
return tenantName, nil
118+
} else {
119+
if !a.IsReadOnly() && a.GetResource() == "tenants" {
120+
return "", errors.New("only admin can write tenant")
121+
}
122+
}
123+
124+
authConfig := &authConfig{
125+
AuthUrl: ka.authUrl,
126+
Username: a.GetUserName(),
127+
Password: a.GetPassword(),
128+
}
129+
osClient, err := newOpenstackClient(authConfig)
130+
if err != nil {
131+
glog.Errorf("%v", err)
132+
return "", err
133+
}
134+
133135
tenant, err := osClient.getTenant()
134136
if err != nil {
135137
glog.Errorf("%v", err)
@@ -141,19 +143,6 @@ func (ka *keystoneAuthorizer) Authorize(a authorizer.Attributes) (string, error)
141143
return "", errors.New("Keystone authorization failed")
142144
}
143145

144-
func isWhiteListedUser(username string) bool {
145-
whiteList := map[string]bool{
146-
api.UserAdmin: true,
147-
"kubelet": true,
148-
"kube_proxy": true,
149-
"system:scheduler": true,
150-
"system:controller_manager": true,
151-
"system:logging": true,
152-
"system:monitoring": true,
153-
}
154-
return whiteList[username]
155-
}
156-
157146
func (osClient *OpenstackClient) getTenant() (tenant *tenants.Tenant, err error) {
158147
tenantList := make([]tenants.Tenant, 0)
159148
opts := tenants.ListOpts{}

pkg/controller/serviceaccount/serviceaccounts_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ func (e *ServiceAccountsController) createServiceAccount(name, namespace string)
211211
serviceAccount := &api.ServiceAccount{}
212212
serviceAccount.Name = name
213213
serviceAccount.Namespace = namespace
214+
ns, err := e.getNamespace(namespace)
215+
if err != nil {
216+
glog.Error(err)
217+
return
218+
}
219+
serviceAccount.Tenant = ns.Tenant
214220
if _, err := e.client.ServiceAccounts(namespace).Create(serviceAccount); err != nil {
215221
glog.Error(err)
216222
}

pkg/controller/serviceaccount/tokens_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ func (e *TokensController) createSecret(serviceAccount *api.ServiceAccount) erro
305305
ObjectMeta: api.ObjectMeta{
306306
Name: secret.Strategy.GenerateName(fmt.Sprintf("%s-token-", serviceAccount.Name)),
307307
Namespace: serviceAccount.Namespace,
308+
Tenant: serviceAccount.Tenant,
308309
Annotations: map[string]string{
309310
api.ServiceAccountNameKey: serviceAccount.Name,
310311
api.ServiceAccountUIDKey: string(serviceAccount.UID),

pkg/kubectl/cmd/create.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,18 @@ func RunCreate(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *C
8787
if err != nil {
8888
return err
8989
}
90+
9091
cmdTenant, enforceTenant, err := f.DefaultTenant()
9192
if err != nil {
9293
return err
9394
}
95+
fmt.Printf("tenant input is %s\n", cmdTenant)
9496
mapper, typer := f.Object()
9597
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
9698
Schema(schema).
9799
ContinueOnError().
98-
NamespaceParam(cmdNamespace).DefaultNamespace().
99-
TenantParam(cmdTenant).DefaultTenant().
100+
NamespaceParam(cmdNamespace).
101+
TenantParam(cmdTenant).
100102
FilenameParam(enforceTenant, enforceNamespace, options.Filenames...).
101103
Flatten().
102104
Do()

pkg/kubectl/cmd/get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
134134
isWatch, isWatchOnly := cmdutil.GetFlagBool(cmd, "watch"), cmdutil.GetFlagBool(cmd, "watch-only")
135135
if isWatch || isWatchOnly {
136136
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
137-
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
137+
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(true).
138138
TenantParam(cmdTenant).DefaultTenant().
139139
FilenameParam(enforceTenant, enforceNamespace, options.Filenames...).
140140
SelectorParam(selector).
@@ -189,7 +189,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
189189
}
190190

191191
b := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
192-
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
192+
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(true).
193193
FilenameParam(enforceTenant, enforceNamespace, options.Filenames...).
194194
SelectorParam(selector).
195195
ResourceTypeOrNameArgs(true, args...).

pkg/kubectl/resource/visitor.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,6 @@ func RequireTenant(tenant string) VisitorFunc {
601601
if err != nil {
602602
return err
603603
}
604-
if !info.Namespaced() {
605-
return nil
606-
}
607604
if len(info.Tenant) == 0 {
608605
info.Tenant = tenant
609606
UpdateObjectTenant(info, nil)

pkg/kubectl/resource_printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ func printNetworkList(list *api.NetworkList, w io.Writer, withNamespace bool, wi
10521052

10531053
func printTenant(item *api.Tenant, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
10541054
if withNamespace {
1055-
return fmt.Errorf("namespace is not namespaced")
1055+
return fmt.Errorf("tenant is not namespaced")
10561056
}
10571057

10581058
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", item.Name, labels.FormatLabels(item.Labels), item.Status.Phase, translateTimestamp(item.CreationTimestamp)); err != nil {

0 commit comments

Comments
 (0)