auth_role.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "bytes"
  17. "context"
  18. "encoding/json"
  19. "net/http"
  20. "net/url"
  21. )
  22. type Role struct {
  23. Role string `json:"role"`
  24. Permissions Permissions `json:"permissions"`
  25. Grant *Permissions `json:"grant,omitempty"`
  26. Revoke *Permissions `json:"revoke,omitempty"`
  27. }
  28. type Permissions struct {
  29. KV rwPermission `json:"kv"`
  30. }
  31. type rwPermission struct {
  32. Read []string `json:"read"`
  33. Write []string `json:"write"`
  34. }
  35. type PermissionType int
  36. const (
  37. ReadPermission PermissionType = iota
  38. WritePermission
  39. ReadWritePermission
  40. )
  41. // NewAuthRoleAPI constructs a new AuthRoleAPI that uses HTTP to
  42. // interact with etcd's role creation and modification features.
  43. func NewAuthRoleAPI(c Client) AuthRoleAPI {
  44. return &httpAuthRoleAPI{
  45. client: c,
  46. }
  47. }
  48. type AuthRoleAPI interface {
  49. // AddRole adds a role.
  50. AddRole(ctx context.Context, role string) error
  51. // RemoveRole removes a role.
  52. RemoveRole(ctx context.Context, role string) error
  53. // GetRole retrieves role details.
  54. GetRole(ctx context.Context, role string) (*Role, error)
  55. // GrantRoleKV grants a role some permission prefixes for the KV store.
  56. GrantRoleKV(ctx context.Context, role string, prefixes []string, permType PermissionType) (*Role, error)
  57. // RevokeRoleKV revokes some permission prefixes for a role on the KV store.
  58. RevokeRoleKV(ctx context.Context, role string, prefixes []string, permType PermissionType) (*Role, error)
  59. // ListRoles lists roles.
  60. ListRoles(ctx context.Context) ([]string, error)
  61. }
  62. type httpAuthRoleAPI struct {
  63. client httpClient
  64. }
  65. type authRoleAPIAction struct {
  66. verb string
  67. name string
  68. role *Role
  69. }
  70. type authRoleAPIList struct{}
  71. func (list *authRoleAPIList) HTTPRequest(ep url.URL) *http.Request {
  72. u := v2AuthURL(ep, "roles", "")
  73. req, _ := http.NewRequest("GET", u.String(), nil)
  74. req.Header.Set("Content-Type", "application/json")
  75. return req
  76. }
  77. func (l *authRoleAPIAction) HTTPRequest(ep url.URL) *http.Request {
  78. u := v2AuthURL(ep, "roles", l.name)
  79. if l.role == nil {
  80. req, _ := http.NewRequest(l.verb, u.String(), nil)
  81. return req
  82. }
  83. b, err := json.Marshal(l.role)
  84. if err != nil {
  85. panic(err)
  86. }
  87. body := bytes.NewReader(b)
  88. req, _ := http.NewRequest(l.verb, u.String(), body)
  89. req.Header.Set("Content-Type", "application/json")
  90. return req
  91. }
  92. func (r *httpAuthRoleAPI) ListRoles(ctx context.Context) ([]string, error) {
  93. resp, body, err := r.client.Do(ctx, &authRoleAPIList{})
  94. if err != nil {
  95. return nil, err
  96. }
  97. if err = assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
  98. return nil, err
  99. }
  100. var roleList struct {
  101. Roles []Role `json:"roles"`
  102. }
  103. if err = json.Unmarshal(body, &roleList); err != nil {
  104. return nil, err
  105. }
  106. ret := make([]string, 0, len(roleList.Roles))
  107. for _, r := range roleList.Roles {
  108. ret = append(ret, r.Role)
  109. }
  110. return ret, nil
  111. }
  112. func (r *httpAuthRoleAPI) AddRole(ctx context.Context, rolename string) error {
  113. role := &Role{
  114. Role: rolename,
  115. }
  116. return r.addRemoveRole(ctx, &authRoleAPIAction{
  117. verb: "PUT",
  118. name: rolename,
  119. role: role,
  120. })
  121. }
  122. func (r *httpAuthRoleAPI) RemoveRole(ctx context.Context, rolename string) error {
  123. return r.addRemoveRole(ctx, &authRoleAPIAction{
  124. verb: "DELETE",
  125. name: rolename,
  126. })
  127. }
  128. func (r *httpAuthRoleAPI) addRemoveRole(ctx context.Context, req *authRoleAPIAction) error {
  129. resp, body, err := r.client.Do(ctx, req)
  130. if err != nil {
  131. return err
  132. }
  133. if err := assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil {
  134. var sec authError
  135. err := json.Unmarshal(body, &sec)
  136. if err != nil {
  137. return err
  138. }
  139. return sec
  140. }
  141. return nil
  142. }
  143. func (r *httpAuthRoleAPI) GetRole(ctx context.Context, rolename string) (*Role, error) {
  144. return r.modRole(ctx, &authRoleAPIAction{
  145. verb: "GET",
  146. name: rolename,
  147. })
  148. }
  149. func buildRWPermission(prefixes []string, permType PermissionType) rwPermission {
  150. var out rwPermission
  151. switch permType {
  152. case ReadPermission:
  153. out.Read = prefixes
  154. case WritePermission:
  155. out.Write = prefixes
  156. case ReadWritePermission:
  157. out.Read = prefixes
  158. out.Write = prefixes
  159. }
  160. return out
  161. }
  162. func (r *httpAuthRoleAPI) GrantRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) {
  163. rwp := buildRWPermission(prefixes, permType)
  164. role := &Role{
  165. Role: rolename,
  166. Grant: &Permissions{
  167. KV: rwp,
  168. },
  169. }
  170. return r.modRole(ctx, &authRoleAPIAction{
  171. verb: "PUT",
  172. name: rolename,
  173. role: role,
  174. })
  175. }
  176. func (r *httpAuthRoleAPI) RevokeRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) {
  177. rwp := buildRWPermission(prefixes, permType)
  178. role := &Role{
  179. Role: rolename,
  180. Revoke: &Permissions{
  181. KV: rwp,
  182. },
  183. }
  184. return r.modRole(ctx, &authRoleAPIAction{
  185. verb: "PUT",
  186. name: rolename,
  187. role: role,
  188. })
  189. }
  190. func (r *httpAuthRoleAPI) modRole(ctx context.Context, req *authRoleAPIAction) (*Role, error) {
  191. resp, body, err := r.client.Do(ctx, req)
  192. if err != nil {
  193. return nil, err
  194. }
  195. if err = assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
  196. var sec authError
  197. err = json.Unmarshal(body, &sec)
  198. if err != nil {
  199. return nil, err
  200. }
  201. return nil, sec
  202. }
  203. var role Role
  204. if err = json.Unmarshal(body, &role); err != nil {
  205. return nil, err
  206. }
  207. return &role, nil
  208. }