handler.go 40.6 KB
Newer Older
Z
zryfish 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2020 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.
*/

Z
zryfish 已提交
17
package v1alpha2
H
hongming 已提交
18 19

import (
H
hongming 已提交
20
	"fmt"
R
Roland.Ma 已提交
21 22
	"strings"

H
hongming 已提交
23 24 25
	authuser "k8s.io/apiserver/pkg/authentication/user"
	"kubesphere.io/kubesphere/pkg/apiserver/request"
	"kubesphere.io/kubesphere/pkg/models/auth"
26

H
hongming 已提交
27
	"github.com/emicklei/go-restful"
H
hongming 已提交
28
	rbacv1 "k8s.io/api/rbac/v1"
H
hongming 已提交
29 30
	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/klog"
H
update  
hongming 已提交
31 32
	"kubesphere.io/kubesphere/pkg/api"
	iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
H
hongming 已提交
33
	"kubesphere.io/kubesphere/pkg/apiserver/authorization/authorizer"
H
hongming 已提交
34
	"kubesphere.io/kubesphere/pkg/apiserver/query"
H
hongming 已提交
35
	apirequest "kubesphere.io/kubesphere/pkg/apiserver/request"
H
update  
hongming 已提交
36
	"kubesphere.io/kubesphere/pkg/models/iam/am"
37
	"kubesphere.io/kubesphere/pkg/models/iam/group"
H
update  
hongming 已提交
38
	"kubesphere.io/kubesphere/pkg/models/iam/im"
H
hongming 已提交
39
	servererr "kubesphere.io/kubesphere/pkg/server/errors"
H
hongming 已提交
40 41
)

H
hongming 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
type Member struct {
	Username string `json:"username"`
	RoleRef  string `json:"roleRef"`
}

type GroupMember struct {
	UserName  string `json:"userName"`
	GroupName string `json:"groupName"`
}

type PasswordReset struct {
	CurrentPassword string `json:"currentPassword"`
	Password        string `json:"password"`
}

H
hongming 已提交
57
type iamHandler struct {
H
hongming 已提交
58 59
	am         am.AccessManagementInterface
	im         im.IdentityManagementInterface
60
	group      group.GroupOperator
H
hongming 已提交
61
	authorizer authorizer.Authorizer
H
hongming 已提交
62 63
}

H
hongming 已提交
64
func newIAMHandler(im im.IdentityManagementInterface, am am.AccessManagementInterface, group group.GroupOperator, authorizer authorizer.Authorizer) *iamHandler {
H
hongming 已提交
65
	return &iamHandler{
H
hongming 已提交
66 67
		am:         am,
		im:         im,
68
		group:      group,
H
hongming 已提交
69
		authorizer: authorizer,
H
hongming 已提交
70
	}
H
hongming 已提交
71 72
}

H
hongming 已提交
73
func (h *iamHandler) DescribeUser(request *restful.Request, response *restful.Response) {
H
hongming 已提交
74 75
	username := request.PathParameter("user")

H
update  
hongming 已提交
76 77
	user, err := h.im.DescribeUser(username)
	if err != nil {
H
hongming 已提交
78
		api.HandleInternalError(response, request, err)
H
update  
hongming 已提交
79 80 81
		return
	}

H
hongming 已提交
82
	globalRole, err := h.am.GetGlobalRoleOfUser(username)
H
hongming 已提交
83
	// ignore not found error
H
hongming 已提交
84 85
	if err != nil && !errors.IsNotFound(err) {
		api.HandleInternalError(response, request, err)
H
update  
hongming 已提交
86 87
		return
	}
H
hongming 已提交
88
	if globalRole != nil {
H
hongming 已提交
89
		user = appendGlobalRoleAnnotation(user, globalRole.Name)
H
hongming 已提交
90
	}
H
update  
hongming 已提交
91

H
hongming 已提交
92
	response.WriteEntity(user)
H
hongming 已提交
93 94
}

H
hongming 已提交
95
func (h *iamHandler) RetrieveMemberRoleTemplates(request *restful.Request, response *restful.Response) {
H
hongming 已提交
96

H
hongming 已提交
97
	if strings.HasSuffix(request.Request.URL.Path, iamv1alpha2.ResourcesPluralGlobalRole) {
H
hongming 已提交
98 99
		username := request.PathParameter("user")

H
hongming 已提交
100 101
		globalRole, err := h.am.GetGlobalRoleOfUser(username)
		if err != nil {
102 103 104 105 106
			// if role binding not exist return empty list
			if errors.IsNotFound(err) {
				response.WriteEntity([]interface{}{})
				return
			}
H
hongming 已提交
107 108 109 110 111 112 113 114 115 116 117 118
			api.HandleInternalError(response, request, err)
			return
		}

		result, err := h.am.ListGlobalRoles(&query.Query{
			Pagination: query.NoPagination,
			SortBy:     "",
			Ascending:  false,
			Filters:    map[query.Field]query.Value{iamv1alpha2.AggregateTo: query.Value(globalRole.Name)},
		})
		if err != nil {
			api.HandleInternalError(response, request, err)
H
hongming 已提交
119 120
			return
		}
H
hongming 已提交
121 122

		response.WriteEntity(result.Items)
H
hongming 已提交
123 124 125
		return
	}

H
hongming 已提交
126
	if strings.HasSuffix(request.Request.URL.Path, iamv1alpha2.ResourcesPluralClusterRole) {
H
hongming 已提交
127
		username := request.PathParameter("clustermember")
H
hongming 已提交
128 129
		clusterRole, err := h.am.GetClusterRoleOfUser(username)
		if err != nil {
130 131 132 133 134
			// if role binding not exist return empty list
			if errors.IsNotFound(err) {
				response.WriteEntity([]interface{}{})
				return
			}
H
hongming 已提交
135 136 137 138 139 140 141 142 143 144 145 146
			api.HandleInternalError(response, request, err)
			return
		}

		result, err := h.am.ListClusterRoles(&query.Query{
			Pagination: query.NoPagination,
			SortBy:     "",
			Ascending:  false,
			Filters:    map[query.Field]query.Value{iamv1alpha2.AggregateTo: query.Value(clusterRole.Name)},
		})
		if err != nil {
			api.HandleInternalError(response, request, err)
H
hongming 已提交
147 148
			return
		}
H
hongming 已提交
149 150

		response.WriteEntity(result.Items)
H
hongming 已提交
151 152 153
		return
	}

H
hongming 已提交
154 155
	if strings.HasSuffix(request.Request.URL.Path, iamv1alpha2.ResourcesPluralWorkspaceRole) {
		workspace := request.PathParameter("workspace")
H
hongming 已提交
156
		username := request.PathParameter("workspacemember")
157 158 159 160 161 162 163 164

		user, err := h.im.DescribeUser(username)
		if err != nil {
			api.HandleInternalError(response, request, err)
			return
		}

		workspaceRoles, err := h.am.GetWorkspaceRoleOfUser(username, user.Spec.Groups, workspace)
H
hongming 已提交
165
		if err != nil {
166 167 168 169 170
			// if role binding not exist return empty list
			if errors.IsNotFound(err) {
				response.WriteEntity([]interface{}{})
				return
			}
H
hongming 已提交
171 172 173
			api.HandleInternalError(response, request, err)
			return
		}
R
Roland.Ma 已提交
174
		templateRoles := make(map[string]*iamv1alpha2.WorkspaceRole)
175 176 177 178 179 180 181 182 183 184 185 186
		for _, role := range workspaceRoles {
			// merge template Role
			result, err := h.am.ListWorkspaceRoles(&query.Query{
				Pagination: query.NoPagination,
				SortBy:     "",
				Ascending:  false,
				Filters:    map[query.Field]query.Value{iamv1alpha2.AggregateTo: query.Value(role.Name)},
			})
			if err != nil {
				api.HandleInternalError(response, request, err)
				return
			}
H
hongming 已提交
187

188
			for _, obj := range result.Items {
R
Roland.Ma 已提交
189
				templateRole := obj.(*iamv1alpha2.WorkspaceRole)
190 191
				templateRoles[templateRole.Name] = templateRole
			}
H
hongming 已提交
192 193
		}

R
Roland.Ma 已提交
194
		results := make([]*iamv1alpha2.WorkspaceRole, 0, len(templateRoles))
195 196 197 198 199
		for _, value := range templateRoles {
			results = append(results, value)
		}

		response.WriteEntity(results)
H
hongming 已提交
200 201 202
		return
	}

H
hongming 已提交
203 204
	if strings.HasSuffix(request.Request.URL.Path, iamv1alpha2.ResourcesPluralRole) {
		namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
H
hongming 已提交
205
		username := request.PathParameter("member")
H
hongming 已提交
206 207 208 209
		if err != nil {
			api.HandleInternalError(response, request, err)
			return
		}
H
hongming 已提交
210

211 212 213 214 215
		user, err := h.im.DescribeUser(username)
		if err != nil {
			api.HandleInternalError(response, request, err)
			return
		}
H
hongming 已提交
216

217
		roles, err := h.am.GetNamespaceRoleOfUser(username, user.Spec.Groups, namespace)
H
hongming 已提交
218
		if err != nil {
H
hongming 已提交
219 220 221 222 223
			// if role binding not exist return empty list
			if errors.IsNotFound(err) {
				response.WriteEntity([]interface{}{})
				return
			}
H
hongming 已提交
224
			api.HandleInternalError(response, request, err)
H
hongming 已提交
225 226 227
			return
		}

228 229 230 231 232 233 234 235 236 237 238 239 240 241
		templateRoles := make(map[string]*rbacv1.Role)
		for _, role := range roles {
			// merge template Role
			result, err := h.am.ListRoles(namespace, &query.Query{
				Pagination: query.NoPagination,
				SortBy:     "",
				Ascending:  false,
				Filters:    map[query.Field]query.Value{iamv1alpha2.AggregateTo: query.Value(role.Name)},
			})

			if err != nil {
				api.HandleInternalError(response, request, err)
				return
			}
H
hongming 已提交
242

243 244 245 246
			for _, obj := range result.Items {
				templateRole := obj.(*rbacv1.Role)
				templateRoles[templateRole.Name] = templateRole
			}
H
hongming 已提交
247
		}
H
hongming 已提交
248

249 250 251 252 253 254
		results := make([]*rbacv1.Role, 0, len(templateRoles))
		for _, value := range templateRoles {
			results = append(results, value)
		}

		response.WriteEntity(results)
H
hongming 已提交
255 256
		return
	}
H
hongming 已提交
257
}
H
hongming 已提交
258

H
hongming 已提交
259
func (h *iamHandler) ListUsers(request *restful.Request, response *restful.Response) {
H
hongming 已提交
260
	queryParam := query.ParseQueryParameter(request)
H
hongming 已提交
261 262
	result, err := h.im.ListUsers(queryParam)
	if err != nil {
H
hongming 已提交
263
		api.HandleInternalError(response, request, err)
H
hongming 已提交
264 265 266 267 268
		return
	}
	for i, item := range result.Items {
		user := item.(*iamv1alpha2.User)
		user = user.DeepCopy()
H
hongming 已提交
269
		globalRole, err := h.am.GetGlobalRoleOfUser(user.Name)
H
hongming 已提交
270
		// ignore not found error
H
hongming 已提交
271
		if err != nil && !errors.IsNotFound(err) {
H
hongming 已提交
272
			api.HandleInternalError(response, request, err)
H
hongming 已提交
273 274
			return
		}
H
hongming 已提交
275
		if globalRole != nil {
H
hongming 已提交
276
			user = appendGlobalRoleAnnotation(user, globalRole.Name)
H
hongming 已提交
277 278 279
		}
		result.Items[i] = user
	}
H
hongming 已提交
280 281
	response.WriteEntity(result)
}
H
hongming 已提交
282

H
hongming 已提交
283 284 285 286 287 288 289
func appendGlobalRoleAnnotation(user *iamv1alpha2.User, globalRole string) *iamv1alpha2.User {
	if user.Annotations == nil {
		user.Annotations = make(map[string]string, 0)
	}
	user.Annotations[iamv1alpha2.GlobalRoleAnnotation] = globalRole
	return user
}
H
hongming 已提交
290

H
hongming 已提交
291
func (h *iamHandler) ListRoles(request *restful.Request, response *restful.Response) {
H
hongming 已提交
292 293
	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
	if err != nil {
H
hongming 已提交
294
		api.HandleError(response, request, err)
H
hongming 已提交
295 296 297 298
		return
	}

	queryParam := query.ParseQueryParameter(request)
H
hongming 已提交
299 300
	result, err := h.am.ListRoles(namespace, queryParam)
	if err != nil {
H
hongming 已提交
301
		api.HandleInternalError(response, request, err)
H
hongming 已提交
302
		return
H
update  
hongming 已提交
303
	}
H
hongming 已提交
304
	response.WriteEntity(result)
H
hongming 已提交
305
}
H
update  
hongming 已提交
306

H
hongming 已提交
307 308
func (h *iamHandler) ListClusterRoles(request *restful.Request, response *restful.Response) {
	queryParam := query.ParseQueryParameter(request)
H
hongming 已提交
309
	result, err := h.am.ListClusterRoles(queryParam)
H
update  
hongming 已提交
310
	if err != nil {
H
hongming 已提交
311
		api.HandleInternalError(response, request, err)
H
update  
hongming 已提交
312 313
		return
	}
H
hongming 已提交
314
	response.WriteEntity(result)
H
hongming 已提交
315
}
H
update  
hongming 已提交
316

H
hongming 已提交
317 318 319 320 321 322
func (h *iamHandler) ListGlobalRoles(req *restful.Request, resp *restful.Response) {
	queryParam := query.ParseQueryParameter(req)
	result, err := h.am.ListGlobalRoles(queryParam)
	if err != nil {
		api.HandleInternalError(resp, req, err)
		return
H
update  
hongming 已提交
323 324 325
	}
	resp.WriteEntity(result)
}
H
hongming 已提交
326

H
hongming 已提交
327 328 329
func (h *iamHandler) ListNamespaceMembers(request *restful.Request, response *restful.Response) {
	queryParam := query.ParseQueryParameter(request)
	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
H
hongming 已提交
330

H
hongming 已提交
331
	if err != nil {
H
hongming 已提交
332
		api.HandleError(response, request, err)
H
hongming 已提交
333 334 335 336 337
		return
	}

	queryParam.Filters[iamv1alpha2.ScopeNamespace] = query.Value(namespace)
	result, err := h.im.ListUsers(queryParam)
H
hongming 已提交
338
	if err != nil {
H
hongming 已提交
339
		api.HandleInternalError(response, request, err)
H
hongming 已提交
340 341
		return
	}
H
hongming 已提交
342

H
hongming 已提交
343 344
	response.WriteEntity(result)
}
H
hongming 已提交
345

H
hongming 已提交
346
func (h *iamHandler) DescribeNamespaceMember(request *restful.Request, response *restful.Response) {
H
hongming 已提交
347
	username := request.PathParameter("member")
H
hongming 已提交
348 349
	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
	if err != nil {
H
hongming 已提交
350
		api.HandleError(response, request, err)
H
hongming 已提交
351 352
		return
	}
H
hongming 已提交
353

H
hongming 已提交
354
	queryParam := query.New()
H
hongming 已提交
355
	queryParam.Filters[query.FieldNames] = query.Value(username)
H
hongming 已提交
356
	queryParam.Filters[iamv1alpha2.ScopeNamespace] = query.Value(namespace)
H
hongming 已提交
357

H
hongming 已提交
358 359 360 361
	result, err := h.im.ListUsers(queryParam)
	if err != nil {
		api.HandleInternalError(response, request, err)
		return
H
hongming 已提交
362 363
	}

H
hongming 已提交
364 365 366 367 368
	if len(result.Items) == 0 {
		err := errors.NewNotFound(iamv1alpha2.Resource(iamv1alpha2.ResourcesSingularUser), username)
		api.HandleNotFound(response, request, err)
		return
	}
H
hongming 已提交
369

H
hongming 已提交
370
	response.WriteEntity(result.Items[0])
H
hongming 已提交
371 372
}

H
hongming 已提交
373 374 375
func (h *iamHandler) ListWorkspaceRoles(request *restful.Request, response *restful.Response) {
	queryParam := query.ParseQueryParameter(request)
	workspace := request.PathParameter("workspace")
H
hongming 已提交
376 377 378 379 380 381 382

	queryParam.Filters[iamv1alpha2.ScopeWorkspace] = query.Value(workspace)
	// shared workspace role template
	if string(queryParam.Filters[query.FieldLabel]) == fmt.Sprintf("%s=%s", iamv1alpha2.RoleTemplateLabel, "true") ||
		queryParam.Filters[iamv1alpha2.AggregateTo] != "" {
		delete(queryParam.Filters, iamv1alpha2.ScopeWorkspace)
	}
H
hongming 已提交
383 384 385 386 387 388

	result, err := h.am.ListWorkspaceRoles(queryParam)
	if err != nil {
		api.HandleInternalError(response, request, err)
		return
	}
H
hongming 已提交
389

H
hongming 已提交
390
	response.WriteEntity(result)
H
hongming 已提交
391 392
}

H
hongming 已提交
393
func (h *iamHandler) ListWorkspaceMembers(request *restful.Request, response *restful.Response) {
H
hongming 已提交
394 395
	queryParam := query.ParseQueryParameter(request)
	workspace := request.PathParameter("workspace")
H
hongming 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408
	queryParam.Filters[iamv1alpha2.ScopeWorkspace] = query.Value(workspace)

	result, err := h.im.ListUsers(queryParam)
	if err != nil {
		api.HandleInternalError(response, request, err)
		return
	}

	response.WriteEntity(result)
}

func (h *iamHandler) DescribeWorkspaceMember(request *restful.Request, response *restful.Response) {
	workspace := request.PathParameter("workspace")
H
hongming 已提交
409
	username := request.PathParameter("workspacemember")
H
hongming 已提交
410 411

	queryParam := query.New()
H
hongming 已提交
412
	queryParam.Filters[query.FieldNames] = query.Value(username)
H
hongming 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	queryParam.Filters[iamv1alpha2.ScopeWorkspace] = query.Value(workspace)

	result, err := h.im.ListUsers(queryParam)
	if err != nil {
		api.HandleInternalError(response, request, err)
		return
	}

	if len(result.Items) == 0 {
		err := errors.NewNotFound(iamv1alpha2.Resource(iamv1alpha2.ResourcesSingularUser), username)
		api.HandleNotFound(response, request, err)
		return
	}

	response.WriteEntity(result.Items[0])
}

func (h *iamHandler) UpdateWorkspaceRole(request *restful.Request, response *restful.Response) {
	workspace := request.PathParameter("workspace")
	workspaceRoleName := request.PathParameter("workspacerole")

	var workspaceRole iamv1alpha2.WorkspaceRole
	err := request.ReadEntity(&workspaceRole)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	if workspaceRoleName != workspaceRole.Name {
		err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", workspaceRole.Name, workspaceRoleName)
		api.HandleBadRequest(response, request, err)
		return
	}

	updated, err := h.am.CreateOrUpdateWorkspaceRole(workspace, &workspaceRole)
	if err != nil {
H
hongming 已提交
449
		api.HandleError(response, request, err)
H
hongming 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		return
	}

	response.WriteEntity(updated)
}

func (h *iamHandler) CreateWorkspaceRole(request *restful.Request, response *restful.Response) {
	workspace := request.PathParameter("workspace")

	var workspaceRole iamv1alpha2.WorkspaceRole
	err := request.ReadEntity(&workspaceRole)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	created, err := h.am.CreateOrUpdateWorkspaceRole(workspace, &workspaceRole)
	if err != nil {
H
hongming 已提交
468
		api.HandleError(response, request, err)
H
hongming 已提交
469 470 471 472 473 474 475 476 477
		return
	}

	response.WriteEntity(created)
}

func (h *iamHandler) DeleteWorkspaceRole(request *restful.Request, response *restful.Response) {
	workspace := request.PathParameter("workspace")
	workspaceRoleName := request.PathParameter("workspacerole")
H
hongming 已提交
478

H
hongming 已提交
479
	err := h.am.DeleteWorkspaceRole(workspace, workspaceRoleName)
H
hongming 已提交
480
	if err != nil {
H
hongming 已提交
481
		api.HandleError(response, request, err)
H
hongming 已提交
482 483
		return
	}
H
hongming 已提交
484

H
hongming 已提交
485 486 487
	response.WriteEntity(servererr.None)
}

H
hongming 已提交
488
func (h *iamHandler) CreateUser(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
489
	var user iamv1alpha2.User
H
hongming 已提交
490
	err := req.ReadEntity(&user)
H
hongming 已提交
491
	if err != nil {
H
hongming 已提交
492
		api.HandleBadRequest(resp, req, err)
H
hongming 已提交
493 494
		return
	}
H
hongming 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	operator, ok := request.UserFrom(req.Request.Context())
	if ok && operator.GetName() == iamv1alpha2.PreRegistrationUser {
		extra := operator.GetExtra()
		// The token used for registration must contain additional information
		if len(extra[iamv1alpha2.ExtraIdentityProvider]) != 1 || len(extra[iamv1alpha2.ExtraUID]) != 1 {
			err = errors.NewBadRequest("invalid registration token")
			api.HandleBadRequest(resp, req, err)
			return
		}
		if user.Labels == nil {
			user.Labels = make(map[string]string)
		}
		user.Labels[iamv1alpha2.IdentifyProviderLabel] = extra[iamv1alpha2.ExtraIdentityProvider][0]
		user.Labels[iamv1alpha2.OriginUIDLabel] = extra[iamv1alpha2.ExtraUID][0]
		// default role
		delete(user.Annotations, iamv1alpha2.GlobalRoleAnnotation)
	}
H
hongming 已提交
512

H
hongming 已提交
513
	globalRole := user.Annotations[iamv1alpha2.GlobalRoleAnnotation]
H
hongming 已提交
514
	delete(user.Annotations, iamv1alpha2.GlobalRoleAnnotation)
H
hongming 已提交
515 516
	if globalRole != "" {
		if _, err = h.am.GetGlobalRole(globalRole); err != nil {
H
hongming 已提交
517
			api.HandleError(resp, req, err)
H
hongming 已提交
518
			return
H
hongming 已提交
519 520 521
		}
	}

H
hongming 已提交
522 523
	created, err := h.im.CreateUser(&user)
	if err != nil {
H
hongming 已提交
524
		api.HandleError(resp, req, err)
H
hongming 已提交
525 526 527 528
		return
	}

	if globalRole != "" {
H
hongming 已提交
529
		if err := h.am.CreateGlobalRoleBinding(user.Name, globalRole); err != nil {
H
hongming 已提交
530
			api.HandleError(resp, req, err)
H
hongming 已提交
531 532 533 534 535 536 537
			return
		}
	}

	// ensure encrypted password will not be output
	created.Spec.EncryptedPassword = ""

H
hongming 已提交
538
	resp.WriteEntity(created)
H
hongming 已提交
539 540
}

H
hongming 已提交
541
func (h *iamHandler) UpdateUser(request *restful.Request, response *restful.Response) {
H
hongming 已提交
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
	username := request.PathParameter("user")

	var user iamv1alpha2.User

	err := request.ReadEntity(&user)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	if username != user.Name {
		err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", user.Name, username)
		api.HandleBadRequest(response, request, err)
		return
	}

	globalRole := user.Annotations[iamv1alpha2.GlobalRoleAnnotation]
	delete(user.Annotations, iamv1alpha2.GlobalRoleAnnotation)

	updated, err := h.im.UpdateUser(&user)
	if err != nil {
H
hongming 已提交
563
		api.HandleError(response, request, err)
H
hongming 已提交
564 565 566
		return
	}

H
hongming 已提交
567 568 569 570
	operator, ok := apirequest.UserFrom(request.Request.Context())
	if globalRole != "" && ok {
		err = h.updateGlobalRoleBinding(operator, updated, globalRole)
		if err != nil {
H
hongming 已提交
571
			api.HandleError(response, request, err)
H
hongming 已提交
572 573
			return
		}
H
hongming 已提交
574
		updated = appendGlobalRoleAnnotation(updated, globalRole)
H
hongming 已提交
575 576 577 578 579
	}

	response.WriteEntity(updated)
}

H
hongming 已提交
580 581
func (h *iamHandler) ModifyPassword(request *restful.Request, response *restful.Response) {
	username := request.PathParameter("user")
H
hongming 已提交
582
	var passwordReset PasswordReset
H
hongming 已提交
583 584 585 586 587 588 589
	err := request.ReadEntity(&passwordReset)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	operator, ok := apirequest.UserFrom(request.Request.Context())
H
hongming 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

	if !ok {
		err = errors.NewInternalError(fmt.Errorf("cannot obtain user info"))
		api.HandleInternalError(response, request, err)
		return
	}

	userManagement := authorizer.AttributesRecord{
		Resource:        "users/password",
		Verb:            "update",
		ResourceScope:   apirequest.GlobalScope,
		ResourceRequest: true,
		User:            operator,
	}

	decision, _, err := h.authorizer.Authorize(userManagement)
	if err != nil {
		api.HandleInternalError(response, request, err)
		return
	}

	// only the user manager can modify the password without verifying the old password
	// if old password is defined must be verified
	if decision != authorizer.DecisionAllow || passwordReset.CurrentPassword != "" {
Z
zryfish 已提交
614
		if err = h.im.PasswordVerify(username, passwordReset.CurrentPassword); err != nil {
H
hongming 已提交
615
			if err == auth.IncorrectPasswordError {
H
hongming 已提交
616
				err = errors.NewBadRequest("incorrect old password")
H
hongming 已提交
617
			}
H
hongming 已提交
618
			api.HandleError(response, request, err)
H
hongming 已提交
619 620 621 622 623 624
			return
		}
	}

	err = h.im.ModifyPassword(username, passwordReset.Password)
	if err != nil {
H
hongming 已提交
625
		api.HandleError(response, request, err)
H
hongming 已提交
626 627
		return
	}
H
hongming 已提交
628

H
hongming 已提交
629 630 631
	response.WriteEntity(servererr.None)
}

H
hongming 已提交
632
func (h *iamHandler) DeleteUser(request *restful.Request, response *restful.Response) {
H
hongming 已提交
633 634 635 636
	username := request.PathParameter("user")

	err := h.im.DeleteUser(username)
	if err != nil {
H
hongming 已提交
637
		api.HandleError(response, request, err)
H
hongming 已提交
638
		return
H
hongming 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
	}

	response.WriteEntity(servererr.None)
}

func (h *iamHandler) CreateGlobalRole(request *restful.Request, response *restful.Response) {

	var globalRole iamv1alpha2.GlobalRole
	err := request.ReadEntity(&globalRole)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	created, err := h.am.CreateOrUpdateGlobalRole(&globalRole)
	if err != nil {
H
hongming 已提交
655
		api.HandleError(response, request, err)
H
hongming 已提交
656 657 658 659 660 661 662 663 664 665
		return
	}

	response.WriteEntity(created)
}

func (h *iamHandler) DeleteGlobalRole(request *restful.Request, response *restful.Response) {
	globalRole := request.PathParameter("globalrole")
	err := h.am.DeleteGlobalRole(globalRole)
	if err != nil {
H
hongming 已提交
666
		api.HandleError(response, request, err)
H
hongming 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
		return
	}

	response.WriteEntity(servererr.None)
}

func (h *iamHandler) UpdateGlobalRole(request *restful.Request, response *restful.Response) {
	globalRoleName := request.PathParameter("globalrole")

	var globalRole iamv1alpha2.GlobalRole
	err := request.ReadEntity(&globalRole)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	if globalRoleName != globalRole.Name {
		err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", globalRole.Name, globalRoleName)
		api.HandleBadRequest(response, request, err)
		return
	}

	updated, err := h.am.CreateOrUpdateGlobalRole(&globalRole)
	if err != nil {
H
hongming 已提交
691
		api.HandleError(response, request, err)
H
hongming 已提交
692 693 694 695 696 697 698 699 700 701
		return
	}

	response.WriteEntity(updated)
}

func (h *iamHandler) DescribeGlobalRole(request *restful.Request, response *restful.Response) {
	globalRoleName := request.PathParameter("globalrole")
	globalRole, err := h.am.GetGlobalRole(globalRoleName)
	if err != nil {
H
hongming 已提交
702
		api.HandleError(response, request, err)
H
hongming 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
		return
	}
	response.WriteEntity(globalRole)
}

func (h *iamHandler) CreateClusterRole(request *restful.Request, response *restful.Response) {
	var clusterRole rbacv1.ClusterRole
	err := request.ReadEntity(&clusterRole)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	created, err := h.am.CreateOrUpdateClusterRole(&clusterRole)
	if err != nil {
H
hongming 已提交
718
		api.HandleError(response, request, err)
H
hongming 已提交
719 720 721 722 723 724 725 726 727 728 729
		return
	}

	response.WriteEntity(created)
}

func (h *iamHandler) DeleteClusterRole(request *restful.Request, response *restful.Response) {
	clusterrole := request.PathParameter("clusterrole")

	err := h.am.DeleteClusterRole(clusterrole)
	if err != nil {
H
hongming 已提交
730
		api.HandleError(response, request, err)
H
hongming 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
		return
	}

	response.WriteEntity(servererr.None)
}

func (h *iamHandler) UpdateClusterRole(request *restful.Request, response *restful.Response) {
	clusterRoleName := request.PathParameter("clusterrole")

	var clusterRole rbacv1.ClusterRole

	err := request.ReadEntity(&clusterRole)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	if clusterRoleName != clusterRole.Name {
		err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", clusterRole.Name, clusterRoleName)
		api.HandleBadRequest(response, request, err)
		return
	}

	updated, err := h.am.CreateOrUpdateClusterRole(&clusterRole)
	if err != nil {
H
hongming 已提交
756
		api.HandleError(response, request, err)
H
hongming 已提交
757 758 759 760 761 762 763 764 765 766
		return
	}

	response.WriteEntity(updated)
}

func (h *iamHandler) DescribeClusterRole(request *restful.Request, response *restful.Response) {
	clusterRoleName := request.PathParameter("clusterrole")
	clusterRole, err := h.am.GetClusterRole(clusterRoleName)
	if err != nil {
H
hongming 已提交
767
		api.HandleError(response, request, err)
H
hongming 已提交
768 769 770 771 772 773 774 775 776 777
		return
	}
	response.WriteEntity(clusterRole)
}

func (h *iamHandler) DescribeWorkspaceRole(request *restful.Request, response *restful.Response) {
	workspace := request.PathParameter("workspace")
	workspaceRoleName := request.PathParameter("workspacerole")
	workspaceRole, err := h.am.GetWorkspaceRole(workspace, workspaceRoleName)
	if err != nil {
H
hongming 已提交
778
		api.HandleError(response, request, err)
H
hongming 已提交
779 780 781 782 783 784 785 786 787
		return
	}
	response.WriteEntity(workspaceRole)
}

func (h *iamHandler) CreateNamespaceRole(request *restful.Request, response *restful.Response) {

	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
	if err != nil {
H
hongming 已提交
788
		api.HandleError(response, request, err)
H
hongming 已提交
789 790 791 792 793 794 795 796 797 798 799 800
		return
	}

	var role rbacv1.Role
	err = request.ReadEntity(&role)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	created, err := h.am.CreateOrUpdateNamespaceRole(namespace, &role)
	if err != nil {
H
hongming 已提交
801
		api.HandleError(response, request, err)
H
hongming 已提交
802 803 804 805 806 807 808 809 810
		return
	}

	response.WriteEntity(created)
}

func (h *iamHandler) DeleteNamespaceRole(request *restful.Request, response *restful.Response) {
	role := request.PathParameter("role")

H
hongming 已提交
811
	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
H
hongming 已提交
812
	if err != nil {
H
hongming 已提交
813
		api.HandleError(response, request, err)
H
hongming 已提交
814 815 816 817 818
		return
	}

	err = h.am.DeleteNamespaceRole(namespace, role)
	if err != nil {
H
hongming 已提交
819
		api.HandleError(response, request, err)
H
hongming 已提交
820 821 822 823 824 825 826 827
		return
	}

	response.WriteEntity(servererr.None)
}

func (h *iamHandler) UpdateNamespaceRole(request *restful.Request, response *restful.Response) {
	roleName := request.PathParameter("role")
H
hongming 已提交
828
	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
H
hongming 已提交
829
	if err != nil {
H
hongming 已提交
830
		api.HandleError(response, request, err)
H
hongming 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
		return
	}

	var role rbacv1.Role
	err = request.ReadEntity(&role)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	if roleName != role.Name {
		err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", role.Name, roleName)
		api.HandleBadRequest(response, request, err)
		return
	}

	updated, err := h.am.CreateOrUpdateNamespaceRole(namespace, &role)
	if err != nil {
H
hongming 已提交
849
		api.HandleError(response, request, err)
H
hongming 已提交
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
		return
	}

	response.WriteEntity(updated)
}

func (h *iamHandler) CreateWorkspaceMembers(request *restful.Request, response *restful.Response) {
	workspace := request.PathParameter("workspace")

	var members []Member
	err := request.ReadEntity(&members)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	for _, member := range members {
R
Roland.Ma 已提交
867
		err := h.am.CreateUserWorkspaceRoleBinding(member.Username, workspace, member.RoleRef)
H
hongming 已提交
868
		if err != nil {
H
hongming 已提交
869
			api.HandleError(response, request, err)
H
hongming 已提交
870 871 872 873
			return
		}
	}

H
hongming 已提交
874
	response.WriteEntity(members)
H
hongming 已提交
875 876 877 878
}

func (h *iamHandler) RemoveWorkspaceMember(request *restful.Request, response *restful.Response) {
	workspace := request.PathParameter("workspace")
H
hongming 已提交
879
	username := request.PathParameter("workspacemember")
H
hongming 已提交
880 881 882

	err := h.am.RemoveUserFromWorkspace(username, workspace)
	if err != nil {
H
hongming 已提交
883
		api.HandleError(response, request, err)
H
hongming 已提交
884 885 886 887 888 889 890 891
		return
	}

	response.WriteEntity(servererr.None)
}

func (h *iamHandler) UpdateWorkspaceMember(request *restful.Request, response *restful.Response) {
	workspace := request.PathParameter("workspace")
H
hongming 已提交
892
	username := request.PathParameter("workspacemember")
H
hongming 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906

	var member Member
	err := request.ReadEntity(&member)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	if username != member.Username {
		err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", member.Username, username)
		api.HandleBadRequest(response, request, err)
		return
	}

R
Roland.Ma 已提交
907
	err = h.am.CreateUserWorkspaceRoleBinding(member.Username, workspace, member.RoleRef)
H
hongming 已提交
908
	if err != nil {
H
hongming 已提交
909
		api.HandleError(response, request, err)
H
hongming 已提交
910 911 912
		return
	}

H
hongming 已提交
913
	response.WriteEntity(member)
H
hongming 已提交
914 915 916 917 918 919
}

func (h *iamHandler) CreateNamespaceMembers(request *restful.Request, response *restful.Response) {

	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
	if err != nil {
H
hongming 已提交
920
		api.HandleError(response, request, err)
H
hongming 已提交
921 922 923 924 925 926 927 928 929 930 931
		return
	}

	var members []Member
	err = request.ReadEntity(&members)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	for _, member := range members {
H
hongming 已提交
932
		err := h.am.CreateNamespaceRoleBinding(member.Username, namespace, member.RoleRef)
H
hongming 已提交
933
		if err != nil {
H
hongming 已提交
934
			api.HandleError(response, request, err)
H
hongming 已提交
935 936 937 938
			return
		}
	}

H
hongming 已提交
939
	response.WriteEntity(members)
H
hongming 已提交
940 941 942
}

func (h *iamHandler) UpdateNamespaceMember(request *restful.Request, response *restful.Response) {
H
hongming 已提交
943
	username := request.PathParameter("member")
H
hongming 已提交
944
	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
H
hongming 已提交
945
	if err != nil {
H
hongming 已提交
946
		api.HandleError(response, request, err)
H
hongming 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
		return
	}

	var member Member
	err = request.ReadEntity(&member)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	if username != member.Username {
		err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", member.Username, username)
		api.HandleBadRequest(response, request, err)
		return
	}

H
hongming 已提交
963
	err = h.am.CreateNamespaceRoleBinding(member.Username, namespace, member.RoleRef)
H
hongming 已提交
964
	if err != nil {
H
hongming 已提交
965
		api.HandleError(response, request, err)
H
hongming 已提交
966 967 968
		return
	}

H
hongming 已提交
969
	response.WriteEntity(member)
H
hongming 已提交
970 971 972
}

func (h *iamHandler) RemoveNamespaceMember(request *restful.Request, response *restful.Response) {
H
hongming 已提交
973
	username := request.PathParameter("member")
H
hongming 已提交
974
	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
H
hongming 已提交
975
	if err != nil {
H
hongming 已提交
976
		api.HandleError(response, request, err)
H
hongming 已提交
977 978 979 980 981
		return
	}

	err = h.am.RemoveUserFromNamespace(username, namespace)
	if err != nil {
H
hongming 已提交
982
		api.HandleError(response, request, err)
H
hongming 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
		return
	}

	response.WriteEntity(servererr.None)
}

func (h *iamHandler) CreateClusterMembers(request *restful.Request, response *restful.Response) {
	var members []Member
	err := request.ReadEntity(&members)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	for _, member := range members {
H
hongming 已提交
998
		err := h.am.CreateClusterRoleBinding(member.Username, member.RoleRef)
H
hongming 已提交
999
		if err != nil {
H
hongming 已提交
1000
			api.HandleError(response, request, err)
H
hongming 已提交
1001 1002 1003 1004
			return
		}
	}

H
hongming 已提交
1005
	response.WriteEntity(members)
H
hongming 已提交
1006 1007 1008
}

func (h *iamHandler) RemoveClusterMember(request *restful.Request, response *restful.Response) {
H
hongming 已提交
1009
	username := request.PathParameter("clustermember")
H
hongming 已提交
1010 1011 1012

	err := h.am.RemoveUserFromCluster(username)
	if err != nil {
H
hongming 已提交
1013
		api.HandleError(response, request, err)
H
hongming 已提交
1014 1015 1016 1017 1018 1019 1020
		return
	}

	response.WriteEntity(servererr.None)
}

func (h *iamHandler) UpdateClusterMember(request *restful.Request, response *restful.Response) {
H
hongming 已提交
1021
	username := request.PathParameter("clustermember")
H
hongming 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

	var member Member
	err := request.ReadEntity(&member)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	if username != member.Username {
		err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", member.Username, username)
		api.HandleBadRequest(response, request, err)
		return
	}

H
hongming 已提交
1036
	err = h.am.CreateClusterRoleBinding(member.Username, member.RoleRef)
H
hongming 已提交
1037
	if err != nil {
H
hongming 已提交
1038
		api.HandleError(response, request, err)
H
hongming 已提交
1039 1040 1041
		return
	}

H
hongming 已提交
1042
	response.WriteEntity(member)
H
hongming 已提交
1043 1044 1045
}

func (h *iamHandler) DescribeClusterMember(request *restful.Request, response *restful.Response) {
H
hongming 已提交
1046
	username := request.PathParameter("clustermember")
H
hongming 已提交
1047 1048

	queryParam := query.New()
H
hongming 已提交
1049
	queryParam.Filters[query.FieldNames] = query.Value(username)
H
hongming 已提交
1050
	queryParam.Filters[iamv1alpha2.ScopeCluster] = "true"
H
hongming 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

	result, err := h.im.ListUsers(queryParam)
	if err != nil {
		api.HandleInternalError(response, request, err)
		return
	}

	if len(result.Items) == 0 {
		err := errors.NewNotFound(iamv1alpha2.Resource(iamv1alpha2.ResourcesSingularUser), username)
		api.HandleNotFound(response, request, err)
		return
	}

	response.WriteEntity(result.Items[0])
}

func (h *iamHandler) ListClusterMembers(request *restful.Request, response *restful.Response) {
	queryParam := query.ParseQueryParameter(request)
H
hongming 已提交
1069
	queryParam.Filters[iamv1alpha2.ScopeCluster] = "true"
H
hongming 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

	result, err := h.im.ListUsers(queryParam)
	if err != nil {
		api.HandleInternalError(response, request, err)
		return
	}

	response.WriteEntity(result)
}

func (h *iamHandler) DescribeNamespaceRole(request *restful.Request, response *restful.Response) {
	roleName := request.PathParameter("role")
	namespace, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
	if err != nil {
H
hongming 已提交
1084
		api.HandleError(response, request, err)
H
hongming 已提交
1085 1086 1087 1088 1089
		return
	}

	role, err := h.am.GetNamespaceRole(namespace, roleName)
	if err != nil {
H
hongming 已提交
1090
		api.HandleError(response, request, err)
H
hongming 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
		return
	}

	response.WriteEntity(role)
}

// resolve the namespace which controlled by the devops project
func (h *iamHandler) resolveNamespace(namespace string, devops string) (string, error) {
	if devops == "" {
		return namespace, nil
	}
H
hongming 已提交
1102
	return h.am.GetDevOpsRelatedNamespace(devops)
H
hongming 已提交
1103
}
H
hongming 已提交
1104

H
hongming 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
func (h *iamHandler) PatchWorkspaceRole(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
	workspaceRoleName := request.PathParameter("workspacerole")

	var workspaceRole iamv1alpha2.WorkspaceRole
	err := request.ReadEntity(&workspaceRole)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	workspaceRole.Name = workspaceRoleName
	patched, err := h.am.PatchWorkspaceRole(workspaceName, &workspaceRole)
	if err != nil {
H
hongming 已提交
1119
		api.HandleError(response, request, err)
H
hongming 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
		return
	}

	response.WriteEntity(patched)
}

func (h *iamHandler) PatchGlobalRole(request *restful.Request, response *restful.Response) {
	globalRoleName := request.PathParameter("globalrole")

	var globalRole iamv1alpha2.GlobalRole
	err := request.ReadEntity(&globalRole)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	globalRole.Name = globalRoleName
	patched, err := h.am.PatchGlobalRole(&globalRole)
	if err != nil {
H
hongming 已提交
1139
		api.HandleError(response, request, err)
H
hongming 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
		return
	}

	response.WriteEntity(patched)
}

func (h *iamHandler) PatchNamespaceRole(request *restful.Request, response *restful.Response) {
	roleName := request.PathParameter("role")
	namespaceName, err := h.resolveNamespace(request.PathParameter("namespace"), request.PathParameter("devops"))
	if err != nil {
H
hongming 已提交
1150
		api.HandleError(response, request, err)
H
hongming 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
		return
	}

	var role rbacv1.Role
	err = request.ReadEntity(&role)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	role.Name = roleName
	patched, err := h.am.PatchNamespaceRole(namespaceName, &role)
	if err != nil {
H
hongming 已提交
1164
		api.HandleError(response, request, err)
H
hongming 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
		return
	}

	response.WriteEntity(patched)
}

func (h *iamHandler) PatchClusterRole(request *restful.Request, response *restful.Response) {
	clusterRoleName := request.PathParameter("clusterrole")

	var clusterRole rbacv1.ClusterRole
	err := request.ReadEntity(&clusterRole)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	clusterRole.Name = clusterRoleName
	patched, err := h.am.PatchClusterRole(&clusterRole)
	if err != nil {
H
hongming 已提交
1184
		api.HandleError(response, request, err)
H
hongming 已提交
1185 1186 1187 1188 1189 1190
		return
	}

	response.WriteEntity(patched)
}

H
hongming 已提交
1191
func (h *iamHandler) updateGlobalRoleBinding(operator authuser.Info, user *iamv1alpha2.User, globalRole string) error {
H
hongming 已提交
1192 1193 1194 1195 1196 1197 1198

	oldGlobalRole, err := h.am.GetGlobalRoleOfUser(user.Name)
	if err != nil && !errors.IsNotFound(err) {
		klog.Error(err)
		return err
	}

H
hongming 已提交
1199
	if oldGlobalRole != nil && oldGlobalRole.Name == globalRole {
H
hongming 已提交
1200 1201 1202
		return nil
	}

H
hongming 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
	userManagement := authorizer.AttributesRecord{
		Resource:        iamv1alpha2.ResourcesPluralUser,
		Verb:            "update",
		ResourceScope:   apirequest.GlobalScope,
		ResourceRequest: true,
		User:            operator,
	}
	decision, _, err := h.authorizer.Authorize(userManagement)
	if err != nil {
		klog.Error(err)
		return err
	}
	if decision != authorizer.DecisionAllow {
H
hongming 已提交
1216 1217
		err = errors.NewForbidden(iamv1alpha2.Resource(iamv1alpha2.ResourcesSingularUser),
			user.Name, fmt.Errorf("update global role binding is not allowed"))
H
hongming 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
		klog.Warning(err)
		return err
	}
	if err := h.am.CreateGlobalRoleBinding(user.Name, globalRole); err != nil {
		klog.Error(err)
		return err
	}
	return nil
}

Z
zryfish 已提交
1228 1229 1230
func (h *iamHandler) ListUserLoginRecords(request *restful.Request, response *restful.Response) {
	username := request.PathParameter("user")
	queryParam := query.ParseQueryParameter(request)
H
hongming 已提交
1231
	result, err := h.im.ListLoginRecords(username, queryParam)
Z
zryfish 已提交
1232
	if err != nil {
H
hongming 已提交
1233
		api.HandleError(response, request, err)
Z
zryfish 已提交
1234 1235 1236 1237 1238
		return
	}
	response.WriteEntity(result)
}

1239 1240 1241 1242 1243 1244
func (h *iamHandler) ListWorkspaceGroups(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
	queryParam := query.ParseQueryParameter(request)
	result, err := h.group.ListGroups(workspaceName, queryParam)

	if err != nil {
H
hongming 已提交
1245
		api.HandleError(response, request, err)
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
		return
	}

	response.WriteEntity(result)
}

func (h *iamHandler) CreateGroup(request *restful.Request, response *restful.Response) {
	workspace := request.PathParameter("workspace")
	var group iamv1alpha2.Group

	err := request.ReadEntity(&group)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	created, err := h.group.CreateGroup(workspace, &group)
	if err != nil {
H
hongming 已提交
1264
		api.HandleError(response, request, err)
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
		return
	}

	response.WriteEntity(created)
}

func (h *iamHandler) DescribeGroup(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
	groupName := request.PathParameter("group")
	ns, err := h.group.DescribeGroup(workspaceName, groupName)

	if err != nil {
H
hongming 已提交
1277
		api.HandleError(response, request, err)
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
		return
	}

	response.WriteEntity(ns)
}

func (h *iamHandler) DeleteGroup(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
	groupName := request.PathParameter("group")

	err := h.group.DeleteGroup(workspaceName, groupName)
	if err != nil {
H
hongming 已提交
1290
		api.HandleError(response, request, err)
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
		return
	}

	response.WriteEntity(servererr.None)
}

func (h *iamHandler) UpdateGroup(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
	groupName := request.PathParameter("group")

	var group iamv1alpha2.Group
	err := request.ReadEntity(&group)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	if groupName != group.Name {
		err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", group.Name, groupName)
		api.HandleBadRequest(response, request, err)
		return
	}

	updated, err := h.group.UpdateGroup(workspaceName, &group)
	if err != nil {
H
hongming 已提交
1316
		api.HandleError(response, request, err)
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
		return
	}

	response.WriteEntity(updated)
}

func (h *iamHandler) PatchGroup(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
	groupName := request.PathParameter("group")

	var group iamv1alpha2.Group
	err := request.ReadEntity(&group)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

	group.Name = groupName
	patched, err := h.group.PatchGroup(workspaceName, &group)
	if err != nil {
H
hongming 已提交
1337
		api.HandleError(response, request, err)
1338 1339 1340 1341 1342 1343 1344 1345 1346
		return
	}

	response.WriteEntity(patched)
}

func (h *iamHandler) ListGroupBindings(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
	queryParam := query.ParseQueryParameter(request)
R
Roland.Ma 已提交
1347
	result, err := h.group.ListGroupBindings(workspaceName, queryParam)
1348
	if err != nil {
H
hongming 已提交
1349
		api.HandleError(response, request, err)
1350 1351 1352 1353 1354 1355
		return
	}

	response.WriteEntity(result)
}

R
Roland.Ma 已提交
1356 1357
func (h *iamHandler) ListGroupRoleBindings(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
R
Roland.Ma 已提交
1358 1359
	queryParam := query.ParseQueryParameter(request)
	result, err := h.am.ListGroupRoleBindings(workspaceName, queryParam)
R
Roland.Ma 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
	if err != nil {
		api.HandleInternalError(response, request, err)
		return
	}

	response.WriteEntity(result)
}

func (h *iamHandler) CreateRoleBinding(request *restful.Request, response *restful.Response) {
	namespace := request.PathParameter("namespace")
	var roleBindings []rbacv1.RoleBinding
	err := request.ReadEntity(&roleBindings)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

H
hongming 已提交
1377
	var results []rbacv1.RoleBinding
R
Roland.Ma 已提交
1378
	for _, item := range roleBindings {
R
Roland.Ma 已提交
1379
		r, err := h.am.CreateRoleBinding(namespace, &item)
R
Roland.Ma 已提交
1380
		if err != nil {
H
hongming 已提交
1381
			api.HandleError(response, request, err)
R
Roland.Ma 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
			return
		}
		results = append(results, *r)
	}

	response.WriteEntity(results)
}

func (h *iamHandler) DeleteRoleBinding(request *restful.Request, response *restful.Response) {
	name := request.PathParameter("rolebinding")
	namespace := request.PathParameter("namespace")

R
Roland.Ma 已提交
1394
	err := h.am.DeleteRoleBinding(namespace, name)
R
Roland.Ma 已提交
1395
	if err != nil {
H
hongming 已提交
1396
		api.HandleError(response, request, err)
R
Roland.Ma 已提交
1397 1398 1399 1400 1401 1402 1403 1404
		return
	}

	response.WriteEntity(servererr.None)
}

func (h *iamHandler) ListGroupWorkspaceRoleBindings(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
R
Roland.Ma 已提交
1405 1406
	queryParam := query.ParseQueryParameter(request)
	result, err := h.am.ListGroupWorkspaceRoleBindings(workspaceName, queryParam)
R
Roland.Ma 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
	if err != nil {
		api.HandleInternalError(response, request, err)
		return
	}

	response.WriteEntity(result)
}

func (h *iamHandler) CreateWorkspaceRoleBinding(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")

	var roleBindings []iamv1alpha2.WorkspaceRoleBinding
	err := request.ReadEntity(&roleBindings)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

H
hongming 已提交
1425
	var results []iamv1alpha2.WorkspaceRoleBinding
R
Roland.Ma 已提交
1426
	for _, item := range roleBindings {
R
Roland.Ma 已提交
1427
		r, err := h.am.CreateWorkspaceRoleBinding(workspaceName, &item)
R
Roland.Ma 已提交
1428
		if err != nil {
H
hongming 已提交
1429
			api.HandleError(response, request, err)
R
Roland.Ma 已提交
1430 1431 1432 1433 1434 1435
			return
		}
		results = append(results, *r)
	}

	response.WriteEntity(results)
1436 1437
}

R
Roland.Ma 已提交
1438 1439 1440 1441
func (h *iamHandler) DeleteWorkspaceRoleBinding(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
	name := request.PathParameter("rolebinding")

R
Roland.Ma 已提交
1442
	err := h.am.DeleteWorkspaceRoleBinding(workspaceName, name)
R
Roland.Ma 已提交
1443
	if err != nil {
H
hongming 已提交
1444
		api.HandleError(response, request, err)
R
Roland.Ma 已提交
1445 1446 1447 1448
		return
	}

	response.WriteEntity(servererr.None)
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
}

func (h *iamHandler) CreateGroupBinding(request *restful.Request, response *restful.Response) {

	workspace := request.PathParameter("workspace")

	var members []GroupMember
	err := request.ReadEntity(&members)
	if err != nil {
		api.HandleBadRequest(response, request, err)
		return
	}

H
hongming 已提交
1462
	var results []iamv1alpha2.GroupBinding
1463
	for _, item := range members {
R
Roland.Ma 已提交
1464
		b, err := h.group.CreateGroupBinding(workspace, item.GroupName, item.UserName)
1465
		if err != nil {
H
hongming 已提交
1466
			api.HandleError(response, request, err)
1467 1468
			return
		}
R
Roland.Ma 已提交
1469
		results = append(results, *b)
1470 1471
	}

R
Roland.Ma 已提交
1472
	response.WriteEntity(results)
1473 1474 1475 1476 1477 1478 1479 1480
}

func (h *iamHandler) DeleteGroupBinding(request *restful.Request, response *restful.Response) {
	workspaceName := request.PathParameter("workspace")
	name := request.PathParameter("groupbinding")

	err := h.group.DeleteGroupBinding(workspaceName, name)
	if err != nil {
H
hongming 已提交
1481
		api.HandleError(response, request, err)
1482 1483 1484 1485 1486
		return
	}

	response.WriteEntity(servererr.None)
}