auth_user.go 7.3 KB

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