auth_user.go 7.5 KB

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