auth_user.go 7.7 KB

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