am.go 5.4 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 21 22
/*

 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 iam

import (
	"github.com/emicklei/go-restful"
	"k8s.io/api/rbac/v1"
H
hongming 已提交
23 24
	k8serr "k8s.io/apimachinery/pkg/api/errors"
	"kubesphere.io/kubesphere/pkg/params"
H
hongming 已提交
25 26 27 28 29 30 31 32
	"net/http"
	"sort"

	"kubesphere.io/kubesphere/pkg/errors"
	"kubesphere.io/kubesphere/pkg/models/iam"
	"kubesphere.io/kubesphere/pkg/models/iam/policy"
)

H
hongming 已提交
33
type RoleList struct {
H
hongming 已提交
34
	ClusterRoles []*v1.ClusterRole `json:"clusterRole" protobuf:"bytes,2,rep,name=clusterRoles"`
H
hongming 已提交
35 36 37
	Roles        []*v1.Role        `json:"roles" protobuf:"bytes,2,rep,name=roles"`
}

H
hongming 已提交
38
func ListRoleUsers(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
39
	roleName := req.PathParameter("role")
H
hongming 已提交
40
	namespace := req.PathParameter("namespace")
H
hongming 已提交
41

H
hongming 已提交
42
	users, err := iam.RoleUsers(namespace, roleName)
H
hongming 已提交
43 44

	if err != nil {
H
hongming 已提交
45
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
H
hongming 已提交
46 47 48
		return
	}

H
hongming 已提交
49 50 51 52 53 54 55 56
	resp.WriteAsJson(users)
}

func ListClusterRoles(req *restful.Request, resp *restful.Response) {
	conditions, err := params.ParseConditions(req.QueryParameter(params.ConditionsParam))
	orderBy := req.QueryParameter(params.OrderByParam)
	limit, offset := params.ParsePaging(req.QueryParameter(params.PagingParam))
	reverse := params.ParseReverse(req)
H
hongming 已提交
57 58

	if err != nil {
H
hongming 已提交
59
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
H
hongming 已提交
60 61 62
		return
	}

H
hongming 已提交
63 64 65 66 67 68 69 70 71
	result, err := iam.ListClusterRoles(conditions, orderBy, reverse, limit, offset)

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

	resp.WriteAsJson(result)

H
hongming 已提交
72 73
}

H
hongming 已提交
74
func ListRoles(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
75
	namespace := req.PathParameter("namespace")
H
hongming 已提交
76 77 78 79
	conditions, err := params.ParseConditions(req.QueryParameter(params.ConditionsParam))
	orderBy := req.QueryParameter(params.OrderByParam)
	limit, offset := params.ParsePaging(req.QueryParameter(params.PagingParam))
	reverse := params.ParseReverse(req)
H
hongming 已提交
80

H
hongming 已提交
81 82 83 84 85 86
	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
		return
	}

	result, err := iam.ListRoles(namespace, conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
87 88 89 90 91 92

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
93 94
	resp.WriteAsJson(result)

H
hongming 已提交
95 96
}

H
hongming 已提交
97 98
// List users by namespace
func ListNamespaceUsers(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
99 100 101 102 103 104 105 106 107 108

	namespace := req.PathParameter("namespace")

	users, err := iam.NamespaceUsers(namespace)

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
109
	// sort by time by default
H
hongming 已提交
110
	sort.Slice(users, func(i, j int) bool {
H
hongming 已提交
111
		return users[i].RoleBindTime.After(*users[j].RoleBindTime)
H
hongming 已提交
112 113 114 115 116
	})

	resp.WriteAsJson(users)
}

H
hongming 已提交
117
func ListUserRoles(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
118 119 120

	username := req.PathParameter("username")

H
hongming 已提交
121
	roles, err := iam.GetUserRoles("", username)
H
hongming 已提交
122 123 124 125 126 127

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
128
	_, clusterRoles, err := iam.GetUserClusterRoles(username)
H
hongming 已提交
129 130 131 132 133 134

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
135
	roleList := RoleList{}
H
hongming 已提交
136 137 138 139 140 141
	roleList.Roles = roles
	roleList.ClusterRoles = clusterRoles

	resp.WriteAsJson(roleList)
}

H
hongming 已提交
142
func RulesMapping(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
143 144 145 146
	rules := policy.RoleRuleMapping
	resp.WriteAsJson(rules)
}

H
hongming 已提交
147
func ClusterRulesMapping(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
148 149 150 151
	rules := policy.ClusterRoleRuleMapping
	resp.WriteAsJson(rules)
}

H
hongming 已提交
152
func ListClusterRoleRules(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
153
	clusterRoleName := req.PathParameter("clusterrole")
H
hongming 已提交
154
	rules, err := iam.GetClusterRoleSimpleRules(clusterRoleName)
H
hongming 已提交
155 156 157 158
	if err != nil {
		resp.WriteError(http.StatusInternalServerError, err)
		return
	}
H
hongming 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	resp.WriteAsJson(rules)
}

func ListClusterRoleUsers(req *restful.Request, resp *restful.Response) {
	clusterRoleName := req.PathParameter("clusterrole")
	conditions, err := params.ParseConditions(req.QueryParameter(params.ConditionsParam))
	orderBy := req.QueryParameter(params.OrderByParam)
	limit, offset := params.ParsePaging(req.QueryParameter(params.PagingParam))
	reverse := params.ParseReverse(req)

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
		return
	}

	result, err := iam.ListClusterRoleUsers(clusterRoleName, conditions, orderBy, reverse, limit, offset)

H
hongming 已提交
176
	if err != nil {
H
hongming 已提交
177 178 179 180 181
		if k8serr.IsNotFound(err) {
			resp.WriteError(http.StatusNotFound, err)
		} else {
			resp.WriteError(http.StatusInternalServerError, err)
		}
H
hongming 已提交
182 183 184
		return
	}

H
hongming 已提交
185
	resp.WriteAsJson(result)
H
hongming 已提交
186 187
}

H
hongming 已提交
188 189 190
func ListRoleRules(req *restful.Request, resp *restful.Response) {
	namespaceName := req.PathParameter("namespace")
	roleName := req.PathParameter("role")
H
hongming 已提交
191

H
hongming 已提交
192
	rules, err := iam.GetRoleSimpleRules(namespaceName, roleName)
H
hongming 已提交
193 194

	if err != nil {
H
hongming 已提交
195
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
H
hongming 已提交
196 197 198
		return
	}

H
hongming 已提交
199
	resp.WriteAsJson(rules)
H
hongming 已提交
200
}