auth_user.go 7.4 KB

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