auth_role.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. err = json.Unmarshal(body, &userList)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return userList.Roles, nil
  108. }
  109. func (r *httpAuthRoleAPI) AddRole(ctx context.Context, rolename string) error {
  110. role := &Role{
  111. Role: rolename,
  112. }
  113. return r.addRemoveRole(ctx, &authRoleAPIAction{
  114. verb: "PUT",
  115. name: rolename,
  116. role: role,
  117. })
  118. }
  119. func (r *httpAuthRoleAPI) RemoveRole(ctx context.Context, rolename string) error {
  120. return r.addRemoveRole(ctx, &authRoleAPIAction{
  121. verb: "DELETE",
  122. name: rolename,
  123. })
  124. }
  125. func (r *httpAuthRoleAPI) addRemoveRole(ctx context.Context, req *authRoleAPIAction) error {
  126. resp, body, err := r.client.Do(ctx, req)
  127. if err != nil {
  128. return err
  129. }
  130. if err := assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil {
  131. var sec authError
  132. err := json.Unmarshal(body, &sec)
  133. if err != nil {
  134. return err
  135. }
  136. return sec
  137. }
  138. return nil
  139. }
  140. func (r *httpAuthRoleAPI) GetRole(ctx context.Context, rolename string) (*Role, error) {
  141. return r.modRole(ctx, &authRoleAPIAction{
  142. verb: "GET",
  143. name: rolename,
  144. })
  145. }
  146. func buildRWPermission(prefixes []string, permType PermissionType) rwPermission {
  147. var out rwPermission
  148. switch permType {
  149. case ReadPermission:
  150. out.Read = prefixes
  151. case WritePermission:
  152. out.Write = prefixes
  153. case ReadWritePermission:
  154. out.Read = prefixes
  155. out.Write = prefixes
  156. }
  157. return out
  158. }
  159. func (r *httpAuthRoleAPI) GrantRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) {
  160. rwp := buildRWPermission(prefixes, permType)
  161. role := &Role{
  162. Role: rolename,
  163. Grant: &Permissions{
  164. KV: rwp,
  165. },
  166. }
  167. return r.modRole(ctx, &authRoleAPIAction{
  168. verb: "PUT",
  169. name: rolename,
  170. role: role,
  171. })
  172. }
  173. func (r *httpAuthRoleAPI) RevokeRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) {
  174. rwp := buildRWPermission(prefixes, permType)
  175. role := &Role{
  176. Role: rolename,
  177. Revoke: &Permissions{
  178. KV: rwp,
  179. },
  180. }
  181. return r.modRole(ctx, &authRoleAPIAction{
  182. verb: "PUT",
  183. name: rolename,
  184. role: role,
  185. })
  186. }
  187. func (r *httpAuthRoleAPI) modRole(ctx context.Context, req *authRoleAPIAction) (*Role, error) {
  188. resp, body, err := r.client.Do(ctx, req)
  189. if err != nil {
  190. return nil, err
  191. }
  192. if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
  193. var sec authError
  194. err := json.Unmarshal(body, &sec)
  195. if err != nil {
  196. return nil, err
  197. }
  198. return nil, sec
  199. }
  200. var role Role
  201. err = json.Unmarshal(body, &role)
  202. if err != nil {
  203. return nil, err
  204. }
  205. return &role, nil
  206. }