auth_role.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2015 CoreOS, Inc.
  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. "encoding/json"
  18. "net/http"
  19. "net/url"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  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. // Add a role.
  50. AddRole(ctx context.Context, role string) error
  51. // Remove a role.
  52. RemoveRole(ctx context.Context, role string) error
  53. // Get role details.
  54. GetRole(ctx context.Context, role string) (*Role, error)
  55. // Grant a role some permission prefixes for the KV store.
  56. GrantRoleKV(ctx context.Context, role string, prefixes []string, permType PermissionType) (*Role, error)
  57. // Revoke some some permission prefixes for a role on the KV store.
  58. RevokeRoleKV(ctx context.Context, role string, prefixes []string, permType PermissionType) (*Role, error)
  59. // List 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 userList struct {
  101. Roles []string `json:"roles"`
  102. }
  103. if err = json.Unmarshal(body, &userList); err != nil {
  104. return nil, err
  105. }
  106. return userList.Roles, nil
  107. }
  108. func (r *httpAuthRoleAPI) AddRole(ctx context.Context, rolename string) error {
  109. role := &Role{
  110. Role: rolename,
  111. }
  112. return r.addRemoveRole(ctx, &authRoleAPIAction{
  113. verb: "PUT",
  114. name: rolename,
  115. role: role,
  116. })
  117. }
  118. func (r *httpAuthRoleAPI) RemoveRole(ctx context.Context, rolename string) error {
  119. return r.addRemoveRole(ctx, &authRoleAPIAction{
  120. verb: "DELETE",
  121. name: rolename,
  122. })
  123. }
  124. func (r *httpAuthRoleAPI) addRemoveRole(ctx context.Context, req *authRoleAPIAction) error {
  125. resp, body, err := r.client.Do(ctx, req)
  126. if err != nil {
  127. return err
  128. }
  129. if err := assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil {
  130. var sec authError
  131. err := json.Unmarshal(body, &sec)
  132. if err != nil {
  133. return err
  134. }
  135. return sec
  136. }
  137. return nil
  138. }
  139. func (r *httpAuthRoleAPI) GetRole(ctx context.Context, rolename string) (*Role, error) {
  140. return r.modRole(ctx, &authRoleAPIAction{
  141. verb: "GET",
  142. name: rolename,
  143. })
  144. }
  145. func buildRWPermission(prefixes []string, permType PermissionType) rwPermission {
  146. var out rwPermission
  147. switch permType {
  148. case ReadPermission:
  149. out.Read = prefixes
  150. case WritePermission:
  151. out.Write = prefixes
  152. case ReadWritePermission:
  153. out.Read = prefixes
  154. out.Write = prefixes
  155. }
  156. return out
  157. }
  158. func (r *httpAuthRoleAPI) GrantRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) {
  159. rwp := buildRWPermission(prefixes, permType)
  160. role := &Role{
  161. Role: rolename,
  162. Grant: &Permissions{
  163. KV: rwp,
  164. },
  165. }
  166. return r.modRole(ctx, &authRoleAPIAction{
  167. verb: "PUT",
  168. name: rolename,
  169. role: role,
  170. })
  171. }
  172. func (r *httpAuthRoleAPI) RevokeRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) {
  173. rwp := buildRWPermission(prefixes, permType)
  174. role := &Role{
  175. Role: rolename,
  176. Revoke: &Permissions{
  177. KV: rwp,
  178. },
  179. }
  180. return r.modRole(ctx, &authRoleAPIAction{
  181. verb: "PUT",
  182. name: rolename,
  183. role: role,
  184. })
  185. }
  186. func (r *httpAuthRoleAPI) modRole(ctx context.Context, req *authRoleAPIAction) (*Role, error) {
  187. resp, body, err := r.client.Do(ctx, req)
  188. if err != nil {
  189. return nil, err
  190. }
  191. if err = assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
  192. var sec authError
  193. err = json.Unmarshal(body, &sec)
  194. if err != nil {
  195. return nil, err
  196. }
  197. return nil, sec
  198. }
  199. var role Role
  200. if err = json.Unmarshal(body, &role); err != nil {
  201. return nil, err
  202. }
  203. return &role, nil
  204. }