auth_user.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. "path"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  22. )
  23. var (
  24. defaultV2AuthPrefix = "/v2/auth"
  25. )
  26. type User struct {
  27. User string `json:"user"`
  28. Password string `json:"password,omitempty"`
  29. Roles []string `json:"roles"`
  30. Grant []string `json:"grant,omitempty"`
  31. Revoke []string `json:"revoke,omitempty"`
  32. }
  33. func v2AuthURL(ep url.URL, action string, name string) *url.URL {
  34. if name != "" {
  35. ep.Path = path.Join(ep.Path, defaultV2AuthPrefix, action, name)
  36. return &ep
  37. }
  38. ep.Path = path.Join(ep.Path, defaultV2AuthPrefix, action)
  39. return &ep
  40. }
  41. // NewAuthAPI constructs a new AuthAPI that uses HTTP to
  42. // interact with etcd's general auth features.
  43. func NewAuthAPI(c Client) AuthAPI {
  44. return &httpAuthAPI{
  45. client: c,
  46. }
  47. }
  48. type AuthAPI interface {
  49. // Enable auth.
  50. Enable(ctx context.Context) error
  51. // Disable auth.
  52. Disable(ctx context.Context) error
  53. }
  54. type httpAuthAPI struct {
  55. client httpClient
  56. }
  57. func (s *httpAuthAPI) Enable(ctx context.Context) error {
  58. return s.enableDisable(ctx, &authAPIAction{"PUT"})
  59. }
  60. func (s *httpAuthAPI) Disable(ctx context.Context) error {
  61. return s.enableDisable(ctx, &authAPIAction{"DELETE"})
  62. }
  63. func (s *httpAuthAPI) enableDisable(ctx context.Context, req httpAction) error {
  64. resp, body, err := s.client.Do(ctx, req)
  65. if err != nil {
  66. return err
  67. }
  68. if err = assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil {
  69. var sec authError
  70. err = json.Unmarshal(body, &sec)
  71. if err != nil {
  72. return err
  73. }
  74. return sec
  75. }
  76. return nil
  77. }
  78. type authAPIAction struct {
  79. verb string
  80. }
  81. func (l *authAPIAction) HTTPRequest(ep url.URL) *http.Request {
  82. u := v2AuthURL(ep, "enable", "")
  83. req, _ := http.NewRequest(l.verb, u.String(), nil)
  84. return req
  85. }
  86. type authError struct {
  87. Message string `json:"message"`
  88. Code int `json:"-"`
  89. }
  90. func (e authError) Error() string {
  91. return e.Message
  92. }
  93. // NewAuthUserAPI constructs a new AuthUserAPI that uses HTTP to
  94. // interact with etcd's user creation and modification features.
  95. func NewAuthUserAPI(c Client) AuthUserAPI {
  96. return &httpAuthUserAPI{
  97. client: c,
  98. }
  99. }
  100. type AuthUserAPI interface {
  101. // Add a user.
  102. AddUser(ctx context.Context, username string, password string) error
  103. // Remove a user.
  104. RemoveUser(ctx context.Context, username string) error
  105. // Get user details.
  106. GetUser(ctx context.Context, username string) (*User, error)
  107. // Grant a user some permission roles.
  108. GrantUser(ctx context.Context, username string, roles []string) (*User, error)
  109. // Revoke some permission roles from a user.
  110. RevokeUser(ctx context.Context, username string, roles []string) (*User, error)
  111. // Change the user's password.
  112. ChangePassword(ctx context.Context, username string, password string) (*User, error)
  113. // List users.
  114. ListUsers(ctx context.Context) ([]string, error)
  115. }
  116. type httpAuthUserAPI struct {
  117. client httpClient
  118. }
  119. type authUserAPIAction struct {
  120. verb string
  121. username string
  122. user *User
  123. }
  124. type authUserAPIList struct{}
  125. func (list *authUserAPIList) HTTPRequest(ep url.URL) *http.Request {
  126. u := v2AuthURL(ep, "users", "")
  127. req, _ := http.NewRequest("GET", u.String(), nil)
  128. req.Header.Set("Content-Type", "application/json")
  129. return req
  130. }
  131. func (l *authUserAPIAction) HTTPRequest(ep url.URL) *http.Request {
  132. u := v2AuthURL(ep, "users", l.username)
  133. if l.user == nil {
  134. req, _ := http.NewRequest(l.verb, u.String(), nil)
  135. return req
  136. }
  137. b, err := json.Marshal(l.user)
  138. if err != nil {
  139. panic(err)
  140. }
  141. body := bytes.NewReader(b)
  142. req, _ := http.NewRequest(l.verb, u.String(), body)
  143. req.Header.Set("Content-Type", "application/json")
  144. return req
  145. }
  146. func (u *httpAuthUserAPI) ListUsers(ctx context.Context) ([]string, error) {
  147. resp, body, err := u.client.Do(ctx, &authUserAPIList{})
  148. if err != nil {
  149. return nil, err
  150. }
  151. if err = assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
  152. var sec authError
  153. err = json.Unmarshal(body, &sec)
  154. if err != nil {
  155. return nil, err
  156. }
  157. return nil, sec
  158. }
  159. var userList struct {
  160. Users []string `json:"users"`
  161. }
  162. if err = json.Unmarshal(body, &userList); err != nil {
  163. return nil, err
  164. }
  165. return userList.Users, nil
  166. }
  167. func (u *httpAuthUserAPI) AddUser(ctx context.Context, username string, password string) error {
  168. user := &User{
  169. User: username,
  170. Password: password,
  171. }
  172. return u.addRemoveUser(ctx, &authUserAPIAction{
  173. verb: "PUT",
  174. username: username,
  175. user: user,
  176. })
  177. }
  178. func (u *httpAuthUserAPI) RemoveUser(ctx context.Context, username string) error {
  179. return u.addRemoveUser(ctx, &authUserAPIAction{
  180. verb: "DELETE",
  181. username: username,
  182. })
  183. }
  184. func (u *httpAuthUserAPI) addRemoveUser(ctx context.Context, req *authUserAPIAction) error {
  185. resp, body, err := u.client.Do(ctx, req)
  186. if err != nil {
  187. return err
  188. }
  189. if err = assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil {
  190. var sec authError
  191. err = json.Unmarshal(body, &sec)
  192. if err != nil {
  193. return err
  194. }
  195. return sec
  196. }
  197. return nil
  198. }
  199. func (u *httpAuthUserAPI) GetUser(ctx context.Context, username string) (*User, error) {
  200. return u.modUser(ctx, &authUserAPIAction{
  201. verb: "GET",
  202. username: username,
  203. })
  204. }
  205. func (u *httpAuthUserAPI) GrantUser(ctx context.Context, username string, roles []string) (*User, error) {
  206. user := &User{
  207. User: username,
  208. Grant: roles,
  209. }
  210. return u.modUser(ctx, &authUserAPIAction{
  211. verb: "PUT",
  212. username: username,
  213. user: user,
  214. })
  215. }
  216. func (u *httpAuthUserAPI) RevokeUser(ctx context.Context, username string, roles []string) (*User, error) {
  217. user := &User{
  218. User: username,
  219. Revoke: roles,
  220. }
  221. return u.modUser(ctx, &authUserAPIAction{
  222. verb: "PUT",
  223. username: username,
  224. user: user,
  225. })
  226. }
  227. func (u *httpAuthUserAPI) ChangePassword(ctx context.Context, username string, password string) (*User, error) {
  228. user := &User{
  229. User: username,
  230. Password: password,
  231. }
  232. return u.modUser(ctx, &authUserAPIAction{
  233. verb: "PUT",
  234. username: username,
  235. user: user,
  236. })
  237. }
  238. func (u *httpAuthUserAPI) modUser(ctx context.Context, req *authUserAPIAction) (*User, error) {
  239. resp, body, err := u.client.Do(ctx, req)
  240. if err != nil {
  241. return nil, err
  242. }
  243. if err = assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
  244. var sec authError
  245. err = json.Unmarshal(body, &sec)
  246. if err != nil {
  247. return nil, err
  248. }
  249. return nil, sec
  250. }
  251. var user User
  252. if err = json.Unmarshal(body, &user); err != nil {
  253. return nil, err
  254. }
  255. return &user, nil
  256. }