You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xpack_security_change_password.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2012-2018 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "github.com/olivere/elastic/v7/uritemplates"
  13. )
  14. // XPackSecurityChangePasswordService changes a native user's password.
  15. //
  16. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.1/security-api-change-password.html.
  17. type XPackSecurityChangePasswordService struct {
  18. client *Client
  19. pretty *bool // pretty format the returned JSON response
  20. human *bool // return human readable values for statistics
  21. errorTrace *bool // include the stack trace of returned errors
  22. filterPath []string // list of filters used to reduce the response
  23. headers http.Header // custom request-level HTTP headers
  24. username string
  25. password string
  26. refresh string
  27. body interface{}
  28. }
  29. // NewXPackSecurityChangePasswordService creates a new XPackSecurityChangePasswordService.
  30. func NewXPackSecurityChangePasswordService(client *Client) *XPackSecurityChangePasswordService {
  31. return &XPackSecurityChangePasswordService{
  32. client: client,
  33. }
  34. }
  35. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  36. func (s *XPackSecurityChangePasswordService) Pretty(pretty bool) *XPackSecurityChangePasswordService {
  37. s.pretty = &pretty
  38. return s
  39. }
  40. // Human specifies whether human readable values should be returned in
  41. // the JSON response, e.g. "7.5mb".
  42. func (s *XPackSecurityChangePasswordService) Human(human bool) *XPackSecurityChangePasswordService {
  43. s.human = &human
  44. return s
  45. }
  46. // ErrorTrace specifies whether to include the stack trace of returned errors.
  47. func (s *XPackSecurityChangePasswordService) ErrorTrace(errorTrace bool) *XPackSecurityChangePasswordService {
  48. s.errorTrace = &errorTrace
  49. return s
  50. }
  51. // FilterPath specifies a list of filters used to reduce the response.
  52. func (s *XPackSecurityChangePasswordService) FilterPath(filterPath ...string) *XPackSecurityChangePasswordService {
  53. s.filterPath = filterPath
  54. return s
  55. }
  56. // Header adds a header to the request.
  57. func (s *XPackSecurityChangePasswordService) Header(name string, value string) *XPackSecurityChangePasswordService {
  58. if s.headers == nil {
  59. s.headers = http.Header{}
  60. }
  61. s.headers.Add(name, value)
  62. return s
  63. }
  64. // Headers specifies the headers of the request.
  65. func (s *XPackSecurityChangePasswordService) Headers(headers http.Header) *XPackSecurityChangePasswordService {
  66. s.headers = headers
  67. return s
  68. }
  69. // Username is name of the user to change.
  70. func (s *XPackSecurityChangePasswordService) Username(username string) *XPackSecurityChangePasswordService {
  71. s.username = username
  72. return s
  73. }
  74. // Password is the new value of the password.
  75. func (s *XPackSecurityChangePasswordService) Password(password string) *XPackSecurityChangePasswordService {
  76. s.password = password
  77. return s
  78. }
  79. // Refresh, if "true" (the default), refreshes the affected shards to make this operation
  80. // visible to search, if "wait_for" then wait for a refresh to make this operation visible
  81. // to search, if "false" then do nothing with refreshes.
  82. func (s *XPackSecurityChangePasswordService) Refresh(refresh string) *XPackSecurityChangePasswordService {
  83. s.refresh = refresh
  84. return s
  85. }
  86. // Body specifies the password. Use a string or a type that will get serialized as JSON.
  87. func (s *XPackSecurityChangePasswordService) Body(body interface{}) *XPackSecurityChangePasswordService {
  88. s.body = body
  89. return s
  90. }
  91. // buildURL builds the URL for the operation.
  92. func (s *XPackSecurityChangePasswordService) buildURL() (string, url.Values, error) {
  93. // Build URL
  94. path, err := uritemplates.Expand("/_xpack/security/user/{username}/_password", map[string]string{
  95. "username": s.username,
  96. })
  97. if err != nil {
  98. return "", url.Values{}, err
  99. }
  100. // Add query string parameters
  101. params := url.Values{}
  102. if v := s.pretty; v != nil {
  103. params.Set("pretty", fmt.Sprint(*v))
  104. }
  105. if v := s.human; v != nil {
  106. params.Set("human", fmt.Sprint(*v))
  107. }
  108. if v := s.errorTrace; v != nil {
  109. params.Set("error_trace", fmt.Sprint(*v))
  110. }
  111. if len(s.filterPath) > 0 {
  112. params.Set("filter_path", strings.Join(s.filterPath, ","))
  113. }
  114. if v := s.refresh; v != "" {
  115. params.Set("refresh", v)
  116. }
  117. return path, params, nil
  118. }
  119. // Validate checks if the operation is valid.
  120. func (s *XPackSecurityChangePasswordService) Validate() error {
  121. var invalid []string
  122. if s.username == "" {
  123. invalid = append(invalid, "Userame")
  124. }
  125. if s.password == "" && s.body == nil {
  126. invalid = append(invalid, "Body")
  127. }
  128. if len(invalid) > 0 {
  129. return fmt.Errorf("missing required fields: %v", invalid)
  130. }
  131. return nil
  132. }
  133. // Do executes the operation.
  134. func (s *XPackSecurityChangePasswordService) Do(ctx context.Context) (*XPackSecurityChangeUserPasswordResponse, error) {
  135. // Check pre-conditions
  136. if err := s.Validate(); err != nil {
  137. return nil, err
  138. }
  139. // Get URL for request
  140. path, params, err := s.buildURL()
  141. if err != nil {
  142. return nil, err
  143. }
  144. var body interface{}
  145. if s.body != nil {
  146. body = s.body
  147. } else {
  148. body = map[string]interface{}{
  149. "password": s.password,
  150. }
  151. }
  152. // Get HTTP response
  153. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  154. Method: "POST",
  155. Path: path,
  156. Params: params,
  157. Body: body,
  158. Headers: s.headers,
  159. })
  160. if err != nil {
  161. return nil, err
  162. }
  163. // Return operation response
  164. ret := new(XPackSecurityChangeUserPasswordResponse)
  165. if err := json.Unmarshal(res.Body, ret); err != nil {
  166. return nil, err
  167. }
  168. return ret, nil
  169. }
  170. // XPackSecurityChangeUserPasswordResponse is the response of
  171. // XPackSecurityChangePasswordService.Do.
  172. //
  173. // A successful call returns an empty JSON structure: {}.
  174. type XPackSecurityChangeUserPasswordResponse struct {
  175. }