register.go 10.9 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 v1alpha2

import (
	"github.com/emicklei/go-restful"
	"github.com/emicklei/go-restful-openapi"
H
hongming 已提交
23
	rbacv1 "k8s.io/api/rbac/v1"
H
hongming 已提交
24 25 26 27 28
	"k8s.io/apimachinery/pkg/runtime/schema"
	"kubesphere.io/kubesphere/pkg/apiserver/iam"
	"kubesphere.io/kubesphere/pkg/apiserver/runtime"
	"kubesphere.io/kubesphere/pkg/errors"
	"kubesphere.io/kubesphere/pkg/models"
H
hongming 已提交
29 30
	"kubesphere.io/kubesphere/pkg/models/iam/policy"
	"net/http"
H
hongming 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
)

const GroupName = "iam.kubesphere.io"

var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha2"}

var (
	WebServiceBuilder = runtime.NewContainerBuilder(addWebService)
	AddToContainer    = WebServiceBuilder.AddToContainer
)

func addWebService(c *restful.Container) error {
	tags := []string{"IAM"}
	ws := runtime.NewWebService(GroupVersion)

H
hongming 已提交
46 47 48 49 50 51
	ok := "ok"
	pageableUserList := struct {
		Items      []models.User `json:"items"`
		TotalCount int           `json:"total_count"`
	}{}

H
hongming 已提交
52 53
	ws.Route(ws.POST("/authenticate").
		To(iam.TokenReviewHandler).
H
hongming 已提交
54
		Doc("TokenReview attempts to authenticate a token to a known user. Note: TokenReview requests may be cached by the webhook token authenticator plugin in the kube-apiserver.").
H
hongming 已提交
55
		Reads(iam.TokenReview{}).
H
hongming 已提交
56
		Returns(http.StatusOK, ok, iam.TokenReview{}).
H
hongming 已提交
57 58 59
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.POST("/login").
		To(iam.LoginHandler).
H
hongming 已提交
60
		Doc("KubeSphere APIs support token-based authentication via the Authtoken request header. The POST Login API is used to retrieve the authentication token. After the authentication token is obtained, it must be inserted into the Authtoken header for all requests.").
H
hongming 已提交
61
		Reads(iam.LoginRequest{}).
H
hongming 已提交
62
		Returns(http.StatusOK, ok, models.Token{}).
H
hongming 已提交
63
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
64 65
	ws.Route(ws.GET("/users/{username}").
		To(iam.DescribeUser).
H
hongming 已提交
66
		Doc("Describes the specified user.").
H
hongming 已提交
67
		Param(ws.PathParameter("username", "username")).
H
hongming 已提交
68
		Returns(http.StatusOK, ok, models.User{}).
H
hongming 已提交
69 70 71
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.POST("/users").
		To(iam.CreateUser).
H
hongming 已提交
72
		Doc("Create a user account.").
H
hongming 已提交
73
		Reads(models.User{}).
H
hongming 已提交
74
		Returns(http.StatusOK, ok, errors.Error{}).
H
hongming 已提交
75 76 77
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.DELETE("/users/{name}").
		To(iam.DeleteUser).
H
hongming 已提交
78
		Doc("Remove a specified user.").
H
hongming 已提交
79
		Param(ws.PathParameter("name", "username")).
H
hongming 已提交
80
		Returns(http.StatusOK, ok, errors.Error{}).
H
hongming 已提交
81 82 83
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.PUT("/users/{name}").
		To(iam.UpdateUser).
H
hongming 已提交
84
		Doc("Updates information about the specified user.").
H
hongming 已提交
85
		Param(ws.PathParameter("name", "username")).
H
hongming 已提交
86
		Reads(models.User{}).
H
hongming 已提交
87
		Returns(http.StatusOK, ok, errors.Error{}).
H
hongming 已提交
88 89 90
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/users/{name}/log").
		To(iam.UserLoginLog).
H
hongming 已提交
91
		Doc("This method is used to retrieve the \"login logs\" for the specified user.").
H
hongming 已提交
92
		Param(ws.PathParameter("name", "username")).
H
hongming 已提交
93 94 95 96
		Returns(http.StatusOK, ok, struct {
			LoginTime string `json:"login_time"`
			LoginIP   string `json:"login_ip"`
		}{}).
H
hongming 已提交
97 98
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/users").
H
hongming 已提交
99
		To(iam.ListUsers).
H
hongming 已提交
100 101
		Doc("List all users.").
		Returns(http.StatusOK, ok, pageableUserList).
H
hongming 已提交
102 103
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/groups").
H
hongming 已提交
104
		To(iam.ListGroups).
H
hongming 已提交
105 106
		Doc("List all user groups.").
		Returns(http.StatusOK, ok, []models.Group{}).
H
hongming 已提交
107 108
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/groups/{path}").
H
hongming 已提交
109
		To(iam.DescribeGroup).
H
hongming 已提交
110 111 112
		Doc("Describes the specified user group.").
		Param(ws.PathParameter("path", "user group path separated by colon.")).
		Returns(http.StatusOK, ok, models.Group{}).
H
hongming 已提交
113 114
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/groups/{path}/users").
H
hongming 已提交
115
		To(iam.ListGroupUsers).
H
hongming 已提交
116 117 118
		Doc("List all users in the specified user group.").
		Param(ws.PathParameter("path", "user group path separated by colon.")).
		Returns(http.StatusOK, ok, []models.User{}).
H
hongming 已提交
119 120 121
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.POST("/groups").
		To(iam.CreateGroup).
H
hongming 已提交
122
		Doc("Create a user group.").
H
hongming 已提交
123
		Reads(models.Group{}).
H
hongming 已提交
124
		Returns(http.StatusOK, ok, models.Group{}).
H
hongming 已提交
125 126 127
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.DELETE("/groups/{path}").
		To(iam.DeleteGroup).
H
hongming 已提交
128 129 130
		Doc("Delete a user group.").
		Param(ws.PathParameter("path", "user group path separated by colon.")).
		Returns(http.StatusOK, ok, errors.Error{}).
H
hongming 已提交
131 132 133
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.PUT("/groups/{path}").
		To(iam.UpdateGroup).
H
hongming 已提交
134 135 136 137
		Doc("Updates information about the user group.").
		Param(ws.PathParameter("path", "user group path separated by colon.")).
		Reads(models.Group{}).
		Returns(http.StatusOK, ok, models.Group{}).
H
hongming 已提交
138 139
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/users/{username}/roles").
H
hongming 已提交
140
		To(iam.ListUserRoles).
H
hongming 已提交
141
		Doc("This method is used to retrieve all the roles that are assigned to the user.").
H
hongming 已提交
142
		Param(ws.PathParameter("username", "username")).
H
hongming 已提交
143
		Returns(http.StatusOK, ok, iam.RoleList{}).
H
hongming 已提交
144
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
145 146
	ws.Route(ws.GET("/namespaces/{namespace}/roles").
		To(iam.ListRoles).
H
hongming 已提交
147 148 149 150 151 152
		Doc("This method is used to retrieve the roles that are assigned to the user in the specified namespace.").
		Param(ws.PathParameter("namespace", "kubernetes namespace")).
		Returns(http.StatusOK, ok, struct {
			Items      []rbacv1.Role `json:"items"`
			TotalCount int           `json:"total_count"`
		}{}).
H
hongming 已提交
153 154 155
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/clusterroles").
		To(iam.ListClusterRoles).
H
hongming 已提交
156 157 158 159 160
		Doc("List all cluster roles.").
		Returns(http.StatusOK, ok, struct {
			Items      []rbacv1.ClusterRole `json:"items"`
			TotalCount int                  `json:"total_count"`
		}{}).
H
hongming 已提交
161
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
162
	ws.Route(ws.GET("/namespaces/{namespace}/roles/{role}/users").
H
hongming 已提交
163
		To(iam.ListRoleUsers).
H
hongming 已提交
164 165
		Doc("This method is used to retrieve the users that are bind the role in the specified namespace.").
		Param(ws.PathParameter("namespace", "kubernetes namespace")).
H
hongming 已提交
166
		Param(ws.PathParameter("role", "role name")).
H
hongming 已提交
167
		Returns(http.StatusOK, ok, []models.User{}).
H
hongming 已提交
168 169
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/namespaces/{namespace}/users").
H
hongming 已提交
170
		To(iam.ListNamespaceUsers).
H
hongming 已提交
171 172 173
		Doc("List all users in the specified namespace").
		Param(ws.PathParameter("namespace", "kubernetes namespace")).
		Returns(http.StatusOK, ok, []models.User{}).
H
hongming 已提交
174 175
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/clusterroles/{clusterrole}/users").
H
hongming 已提交
176
		To(iam.ListClusterRoleUsers).
H
hongming 已提交
177
		Doc("List all users that are bind the cluster role.").
H
hongming 已提交
178
		Param(ws.PathParameter("clusterrole", "cluster role name")).
H
hongming 已提交
179
		Returns(http.StatusOK, ok, pageableUserList).
H
hongming 已提交
180 181
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/clusterroles/{clusterrole}/rules").
H
hongming 已提交
182
		To(iam.ListClusterRoleRules).
H
hongming 已提交
183
		Doc("List all policy rules of the specified cluster role.").
H
hongming 已提交
184
		Param(ws.PathParameter("clusterrole", "cluster role name")).
H
hongming 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		Returns(http.StatusOK, ok, []models.SimpleRule{}).
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/namespaces/{namespace}/roles/{role}/rules").
		To(iam.ListRoleRules).
		Doc("List all policy rules of the specified role.").
		Param(ws.PathParameter("namespace", "kubernetes namespace")).
		Param(ws.PathParameter("role", "role name")).
		Returns(http.StatusOK, ok, []models.SimpleRule{}).
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/devops/{devops}/roles/{role}/rules").
		To(iam.ListDevopsRoleRules).
		Doc("List all policy rules of the specified role.").
		Param(ws.PathParameter("devops", "devops project id")).
		Param(ws.PathParameter("role", "devops role name")).
		Returns(http.StatusOK, ok, []models.SimpleRule{}).
H
hongming 已提交
200 201
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/rulesmapping/clusterroles").
H
hongming 已提交
202
		To(iam.ClusterRulesMapping).
H
hongming 已提交
203 204
		Doc("Get the mapping relationships between cluster roles and policy rules.").
		Returns(http.StatusOK, ok, policy.ClusterRoleRuleMapping).
H
hongming 已提交
205 206
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/rulesmapping/roles").
H
hongming 已提交
207
		To(iam.RulesMapping).
H
hongming 已提交
208 209
		Doc("Get the mapping relationships between namespaced roles and policy rules.").
		Returns(http.StatusOK, ok, policy.RoleRuleMapping).
H
hongming 已提交
210
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
211 212
	ws.Route(ws.GET("/workspaces/{workspace}/roles").
		To(iam.ListWorkspaceRoles).
H
hongming 已提交
213
		Doc("List all workspace roles.").
H
hongming 已提交
214
		Param(ws.PathParameter("workspace", "workspace name")).
H
hongming 已提交
215 216 217 218
		Returns(http.StatusOK, ok, struct {
			Items      []rbacv1.ClusterRole `json:"items"`
			TotalCount int                  `json:"total_count"`
		}{}).
H
hongming 已提交
219 220 221
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/workspaces/{workspace}/roles/{role}").
		To(iam.DescribeWorkspaceRole).
H
hongming 已提交
222
		Doc("Describes the workspace role.").
H
hongming 已提交
223 224
		Param(ws.PathParameter("workspace", "workspace name")).
		Param(ws.PathParameter("role", "workspace role name")).
H
hongming 已提交
225
		Returns(http.StatusOK, ok, rbacv1.ClusterRole{}).
H
hongming 已提交
226 227 228
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/workspaces/{workspace}/roles/{role}/rules").
		To(iam.ListWorkspaceRoleRules).
H
hongming 已提交
229
		Doc("List all policy rules of the specified workspace role.").
H
hongming 已提交
230 231
		Param(ws.PathParameter("workspace", "workspace name")).
		Param(ws.PathParameter("role", "workspace role name")).
H
hongming 已提交
232
		Returns(http.StatusOK, ok, []models.SimpleRule{}).
H
hongming 已提交
233 234
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/workspaces/{workspace}/members").
H
hongming 已提交
235
		To(iam.ListWorkspaceUsers).
H
hongming 已提交
236
		Doc("List all members in the specified workspace.").
H
hongming 已提交
237
		Param(ws.PathParameter("workspace", "workspace name")).
H
hongming 已提交
238
		Returns(http.StatusOK, ok, pageableUserList).
H
hongming 已提交
239
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
240 241
	ws.Route(ws.POST("/workspaces/{workspace}/members").
		To(iam.InviteUser).
H
hongming 已提交
242
		Doc("Invite members to a workspace.").
H
hongming 已提交
243
		Param(ws.PathParameter("workspace", "workspace name")).
H
hongming 已提交
244 245
		Reads(models.User{}).
		Returns(http.StatusOK, ok, errors.Error{}).
H
hongming 已提交
246
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
247
	ws.Route(ws.DELETE("/workspaces/{workspace}/members/{username}").
H
hongming 已提交
248
		To(iam.RemoveUser).
H
hongming 已提交
249
		Doc("Remove members from workspace.").
H
hongming 已提交
250
		Param(ws.PathParameter("workspace", "workspace name")).
H
hongming 已提交
251
		Param(ws.PathParameter("name", "username")).
H
hongming 已提交
252
		Returns(http.StatusOK, ok, errors.Error{}).
H
hongming 已提交
253
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
254 255
	ws.Route(ws.GET("/workspaces/{workspace}/members/{username}").
		To(iam.DescribeWorkspaceUser).
H
hongming 已提交
256
		Doc("Describes the specified user.").
H
hongming 已提交
257 258
		Param(ws.PathParameter("workspace", "workspace name")).
		Param(ws.PathParameter("username", "username")).
H
hongming 已提交
259
		Returns(http.StatusOK, ok, models.User{}).
H
hongming 已提交
260 261 262 263
		Metadata(restfulspec.KeyOpenAPITags, tags))
	c.Add(ws)
	return nil
}