auth_user.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. err = json.Unmarshal(body, &userList)
  163. if err != nil {
  164. return nil, err
  165. }
  166. return userList.Users, nil
  167. }
  168. func (u *httpAuthUserAPI) AddUser(ctx context.Context, username string, password string) error {
  169. user := &User{
  170. User: username,
  171. Password: password,
  172. }
  173. return u.addRemoveUser(ctx, &authUserAPIAction{
  174. verb: "PUT",
  175. username: username,
  176. user: user,
  177. })
  178. }
  179. func (u *httpAuthUserAPI) RemoveUser(ctx context.Context, username string) error {
  180. return u.addRemoveUser(ctx, &authUserAPIAction{
  181. verb: "DELETE",
  182. username: username,
  183. })
  184. }
  185. func (u *httpAuthUserAPI) addRemoveUser(ctx context.Context, req *authUserAPIAction) error {
  186. resp, body, err := u.client.Do(ctx, req)
  187. if err != nil {
  188. return err
  189. }
  190. if err := assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil {
  191. var sec authError
  192. err := json.Unmarshal(body, &sec)
  193. if err != nil {
  194. return err
  195. }
  196. return sec
  197. }
  198. return nil
  199. }
  200. func (u *httpAuthUserAPI) GetUser(ctx context.Context, username string) (*User, error) {
  201. return u.modUser(ctx, &authUserAPIAction{
  202. verb: "GET",
  203. username: username,
  204. })
  205. }
  206. func (u *httpAuthUserAPI) GrantUser(ctx context.Context, username string, roles []string) (*User, error) {
  207. user := &User{
  208. User: username,
  209. Grant: roles,
  210. }
  211. return u.modUser(ctx, &authUserAPIAction{
  212. verb: "PUT",
  213. username: username,
  214. user: user,
  215. })
  216. }
  217. func (u *httpAuthUserAPI) RevokeUser(ctx context.Context, username string, roles []string) (*User, error) {
  218. user := &User{
  219. User: username,
  220. Revoke: roles,
  221. }
  222. return u.modUser(ctx, &authUserAPIAction{
  223. verb: "PUT",
  224. username: username,
  225. user: user,
  226. })
  227. }
  228. func (u *httpAuthUserAPI) ChangePassword(ctx context.Context, username string, password string) (*User, error) {
  229. user := &User{
  230. User: username,
  231. Password: password,
  232. }
  233. return u.modUser(ctx, &authUserAPIAction{
  234. verb: "PUT",
  235. username: username,
  236. user: user,
  237. })
  238. }
  239. func (u *httpAuthUserAPI) modUser(ctx context.Context, req *authUserAPIAction) (*User, error) {
  240. resp, body, err := u.client.Do(ctx, req)
  241. if err != nil {
  242. return nil, err
  243. }
  244. if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
  245. var sec authError
  246. err := json.Unmarshal(body, &sec)
  247. if err != nil {
  248. return nil, err
  249. }
  250. return nil, sec
  251. }
  252. var user User
  253. err = json.Unmarshal(body, &user)
  254. if err != nil {
  255. return nil, err
  256. }
  257. return &user, nil
  258. }