tenant.go 5.8 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*

 Copyright 2019 The KubeSphere Authors.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

*/
package tenant

import (
H
hongming 已提交
21
	"fmt"
H
update  
hongming 已提交
22 23
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
H
hongming 已提交
24
	"k8s.io/apimachinery/pkg/runtime"
H
hongming 已提交
25
	"k8s.io/apiserver/pkg/authentication/user"
H
update  
hongming 已提交
26 27 28
	"k8s.io/klog"
	"kubesphere.io/kubesphere/pkg/api"
	tenantv1alpha1 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
H
hongming 已提交
29 30
	"kubesphere.io/kubesphere/pkg/apiserver/authorization/authorizer"
	"kubesphere.io/kubesphere/pkg/apiserver/authorization/authorizerfactory"
H
hongming 已提交
31
	"kubesphere.io/kubesphere/pkg/apiserver/query"
H
update  
hongming 已提交
32
	"kubesphere.io/kubesphere/pkg/informers"
H
update  
hongming 已提交
33
	"kubesphere.io/kubesphere/pkg/models/iam/am"
H
hongming 已提交
34 35
	resources "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
	resourcesv1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
H
hongming 已提交
36 37
)

H
hongming 已提交
38
type Interface interface {
H
hongming 已提交
39 40
	ListWorkspaces(user user.Info, query *query.Query) (*api.ListResult, error)
	ListNamespaces(user user.Info, workspace string, query *query.Query) (*api.ListResult, error)
H
hongming 已提交
41
}
H
hongming 已提交
42

H
hongming 已提交
43
type tenantOperator struct {
H
hongming 已提交
44 45 46
	am             am.AccessManagementInterface
	authorizer     authorizer.Authorizer
	resourceGetter *resourcesv1alpha3.ResourceGetter
H
hongming 已提交
47 48
}

H
hongming 已提交
49 50
func New(informers informers.InformerFactory) Interface {
	amOperator := am.NewAMOperator(informers)
H
hongming 已提交
51
	opaAuthorizer := authorizerfactory.NewOPAAuthorizer(amOperator)
H
update  
hongming 已提交
52
	return &tenantOperator{
H
hongming 已提交
53 54 55
		am:             amOperator,
		authorizer:     opaAuthorizer,
		resourceGetter: resourcesv1alpha3.NewResourceGetter(informers),
H
update  
hongming 已提交
56
	}
H
hongming 已提交
57 58
}

H
hongming 已提交
59
func (t *tenantOperator) ListWorkspaces(user user.Info, queryParam *query.Query) (*api.ListResult, error) {
H
hongming 已提交
60 61 62 63 64 65 66 67 68 69

	listWS := authorizer.AttributesRecord{
		User:       user,
		Verb:       "list",
		APIGroup:   "tenant.kubesphere.io",
		APIVersion: "v1alpha2",
		Resource:   "workspaces",
	}

	decision, _, err := t.authorizer.Authorize(listWS)
H
hongming 已提交
70

H
update  
hongming 已提交
71 72 73 74
	if err != nil {
		klog.Error(err)
		return nil, err
	}
H
hongming 已提交
75

H
hongming 已提交
76
	if decision == authorizer.DecisionAllow {
H
update  
hongming 已提交
77

H
hongming 已提交
78 79
		result, err := t.resourceGetter.List(tenantv1alpha1.ResourcePluralWorkspace, "", queryParam)

H
update  
hongming 已提交
80 81 82 83
		if err != nil {
			klog.Error(err)
			return nil, err
		}
H
hongming 已提交
84

H
hongming 已提交
85 86 87 88 89 90 91 92 93
		return result, nil
	}

	workspaceRoleBindings, err := t.am.ListWorkspaceRoleBindings(user.GetName(), "")

	if err != nil {
		klog.Error(err)
		return nil, err
	}
H
hongming 已提交
94

H
hongming 已提交
95
	workspaces := make([]runtime.Object, 0)
H
hongming 已提交
96

H
hongming 已提交
97
	for _, roleBinding := range workspaceRoleBindings {
H
hongming 已提交
98

H
hongming 已提交
99 100
		workspaceName := roleBinding.Labels[tenantv1alpha1.WorkspaceLabel]
		workspace, err := t.resourceGetter.Get(tenantv1alpha1.ResourcePluralWorkspace, "", workspaceName)
H
hongming 已提交
101

H
hongming 已提交
102 103 104 105 106 107 108 109 110 111 112 113
		if errors.IsNotFound(err) {
			klog.Warningf("workspace role: %+v found but workspace not exist", roleBinding.ObjectMeta)
			continue
		}

		if err != nil {
			klog.Error(err)
			return nil, err
		}

		if !contains(workspaces, workspace) {
			workspaces = append(workspaces, workspace)
H
update  
hongming 已提交
114
		}
H
hongming 已提交
115
	}
H
hongming 已提交
116

H
hongming 已提交
117 118 119 120 121 122 123
	result := resources.DefaultList(workspaces, queryParam, func(left runtime.Object, right runtime.Object, field query.Field) bool {
		return resources.DefaultObjectMetaCompare(left.(*tenantv1alpha1.Workspace).ObjectMeta, right.(*tenantv1alpha1.Workspace).ObjectMeta, field)
	}, func(workspace runtime.Object, filter query.Filter) bool {
		return resources.DefaultObjectMetaFilter(workspace.(*tenantv1alpha1.Workspace).ObjectMeta, filter)
	})

	return result, nil
H
hongming 已提交
124 125
}

H
hongming 已提交
126
func (t *tenantOperator) ListNamespaces(user user.Info, workspace string, queryParam *query.Query) (*api.ListResult, error) {
H
hongming 已提交
127 128 129 130 131 132 133 134 135

	listNSInWS := authorizer.AttributesRecord{
		User:       user,
		Verb:       "list",
		APIGroup:   "",
		APIVersion: "v1",
		Workspace:  workspace,
		Resource:   "namespaces",
	}
H
update  
hongming 已提交
136

H
hongming 已提交
137
	decision, _, err := t.authorizer.Authorize(listNSInWS)
H
hongming 已提交
138 139

	if err != nil {
H
update  
hongming 已提交
140
		klog.Error(err)
H
hongming 已提交
141 142 143
		return nil, err
	}

H
hongming 已提交
144
	if decision == authorizer.DecisionAllow {
H
hongming 已提交
145

H
hongming 已提交
146 147 148
		queryParam.Filters[query.FieldLabel] = query.Value(fmt.Sprintf("%s:%s", tenantv1alpha1.WorkspaceLabel, workspace))

		result, err := t.resourceGetter.List("namespaces", "", queryParam)
H
hongming 已提交
149

H
update  
hongming 已提交
150 151 152 153
		if err != nil {
			klog.Error(err)
			return nil, err
		}
H
hongming 已提交
154

H
hongming 已提交
155 156 157 158
		return result, nil
	}

	roleBindings, err := t.am.ListRoleBindings(user.GetName(), "")
H
hongming 已提交
159

H
hongming 已提交
160 161 162 163
	if err != nil {
		klog.Error(err)
		return nil, err
	}
H
hongming 已提交
164

H
hongming 已提交
165
	namespaces := make([]runtime.Object, 0)
H
hongming 已提交
166

H
hongming 已提交
167 168 169
	for _, roleBinding := range roleBindings {
		namespaceName := roleBinding.Namespace
		namespace, err := t.resourceGetter.Get("namespaces", "", namespaceName)
H
hongming 已提交
170

H
hongming 已提交
171 172 173
		if errors.IsNotFound(err) {
			klog.Warningf("workspace role: %+v found but workspace not exist", roleBinding.ObjectMeta)
			continue
H
update  
hongming 已提交
174 175
		}

H
hongming 已提交
176 177 178 179
		if err != nil {
			klog.Error(err)
			return nil, err
		}
H
update  
hongming 已提交
180

H
hongming 已提交
181 182
		if !contains(namespaces, namespace) {
			namespaces = append(namespaces, namespace)
H
update  
hongming 已提交
183
		}
H
hongming 已提交
184 185
	}

H
hongming 已提交
186 187 188 189 190 191
	result := resources.DefaultList(namespaces, queryParam, func(left runtime.Object, right runtime.Object, field query.Field) bool {
		return resources.DefaultObjectMetaCompare(left.(*corev1.Namespace).ObjectMeta, right.(*corev1.Namespace).ObjectMeta, field)
	}, func(object runtime.Object, filter query.Filter) bool {
		namespace := object.(*corev1.Namespace).ObjectMeta
		if workspaceLabel, ok := namespace.Labels[tenantv1alpha1.WorkspaceLabel]; !ok || workspaceLabel != workspace {
			return false
H
hongming 已提交
192
		}
H
hongming 已提交
193 194
		return resources.DefaultObjectMetaFilter(namespace, filter)
	})
H
hongming 已提交
195

H
hongming 已提交
196
	return result, nil
H
update  
hongming 已提交
197 198
}

H
hongming 已提交
199 200 201 202 203
func contains(objects []runtime.Object, object runtime.Object) bool {
	for _, item := range objects {
		if item == object {
			return true
		}
H
update  
hongming 已提交
204
	}
H
hongming 已提交
205
	return false
H
hongming 已提交
206
}