Test
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_get_role.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/url"
  10. "github.com/olivere/elastic/uritemplates"
  11. )
  12. // XPackSecurityGetRoleService retrieves a role by its name.
  13. // See https://www.elastic.co/guide/en/elasticsearch/reference/6.4/security-api-get-role.html.
  14. type XPackSecurityGetRoleService struct {
  15. client *Client
  16. pretty bool
  17. name string
  18. }
  19. // NewXPackSecurityGetRoleService creates a new XPackSecurityGetRoleService.
  20. func NewXPackSecurityGetRoleService(client *Client) *XPackSecurityGetRoleService {
  21. return &XPackSecurityGetRoleService{
  22. client: client,
  23. }
  24. }
  25. // Name is name of the role to retrieve.
  26. func (s *XPackSecurityGetRoleService) Name(name string) *XPackSecurityGetRoleService {
  27. s.name = name
  28. return s
  29. }
  30. // Pretty indicates that the JSON response be indented and human readable.
  31. func (s *XPackSecurityGetRoleService) Pretty(pretty bool) *XPackSecurityGetRoleService {
  32. s.pretty = pretty
  33. return s
  34. }
  35. // buildURL builds the URL for the operation.
  36. func (s *XPackSecurityGetRoleService) buildURL() (string, url.Values, error) {
  37. // Build URL
  38. path, err := uritemplates.Expand("/_xpack/security/role/{name}", map[string]string{
  39. "name": s.name,
  40. })
  41. if err != nil {
  42. return "", url.Values{}, err
  43. }
  44. // Add query string parameters
  45. params := url.Values{}
  46. if s.pretty {
  47. params.Set("pretty", "true")
  48. }
  49. return path, params, nil
  50. }
  51. // Validate checks if the operation is valid.
  52. func (s *XPackSecurityGetRoleService) Validate() error {
  53. var invalid []string
  54. if s.name == "" {
  55. invalid = append(invalid, "Name")
  56. }
  57. if len(invalid) > 0 {
  58. return fmt.Errorf("missing required fields: %v", invalid)
  59. }
  60. return nil
  61. }
  62. // Do executes the operation.
  63. func (s *XPackSecurityGetRoleService) Do(ctx context.Context) (*XPackSecurityGetRoleResponse, error) {
  64. // Check pre-conditions
  65. if err := s.Validate(); err != nil {
  66. return nil, err
  67. }
  68. // Get URL for request
  69. path, params, err := s.buildURL()
  70. if err != nil {
  71. return nil, err
  72. }
  73. // Get HTTP response
  74. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  75. Method: "GET",
  76. Path: path,
  77. Params: params,
  78. })
  79. if err != nil {
  80. return nil, err
  81. }
  82. // Return operation response
  83. ret := XPackSecurityGetRoleResponse{}
  84. if err := json.Unmarshal(res.Body, &ret); err != nil {
  85. return nil, err
  86. }
  87. return &ret, nil
  88. }
  89. // XPackSecurityGetRoleResponse is the response of XPackSecurityGetRoleService.Do.
  90. type XPackSecurityGetRoleResponse map[string]XPackSecurityRole
  91. // XPackSecurityRole is the role object.
  92. //
  93. // The Java source for this struct is defined here:
  94. // https://github.com/elastic/elasticsearch/blob/6.x/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java
  95. type XPackSecurityRole struct {
  96. Cluster []string `json:"cluster"`
  97. Indices []XPackSecurityIndicesPermissions `json:"indices"`
  98. Applications []XPackSecurityApplicationPrivileges `json:"applications"`
  99. RunAs []string `json:"run_as"`
  100. Global map[string]interface{} `json:"global"`
  101. Metadata map[string]interface{} `json:"metadata"`
  102. TransientMetadata map[string]interface{} `json:"transient_metadata"`
  103. }
  104. // XPackSecurityApplicationPrivileges is the application privileges object
  105. type XPackSecurityApplicationPrivileges struct {
  106. Application string `json:"application"`
  107. Privileges []string `json:"privileges"`
  108. Ressources []string `json:"resources"`
  109. }
  110. // XPackSecurityIndicesPermissions is the indices permission object
  111. type XPackSecurityIndicesPermissions struct {
  112. Names []string `json:"names"`
  113. Privileges []string `json:"privileges"`
  114. FieldSecurity interface{} `json:"field_security"`
  115. Query string `json:"query"`
  116. }