/
roles.go
162 lines (141 loc) · 5.15 KB
/
roles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package kibana
import (
"fmt"
"net/http"
)
// RolesService handles communication with the roles related methods of the
// Kibana API
//
// Kibana API docs: https://www.elastic.co/guide/en/kibana/current/role-management-api.html
type RolesService struct {
client *Client
}
// Feature represents a single Kibana feature privileges
//
// Kibana API docs: https://www.elastic.co/guide/en/kibana/current/kibana-privileges.html
type Feature struct {
Discover []string `json:"discover,omitempty"`
Visualize []string `json:"visualize,omitempty"`
Dashboard []string `json:"dashboard,omitempty"`
DevTools []string `json:"dev_tools,omitempty"`
AdvancedSettings []string `json:"advancedSettings,omitempty"`
IndexPatterns []string `json:"indexPatterns,omitempty"`
Graph []string `json:"graph,omitempty"`
APM []string `json:"apm,omitempty"`
Maps []string `json:"maps,omitempty"`
Canvas []string `json:"canvas,omitempty"`
Infrastructure []string `json:"infrastructure,omitempty"`
Logs []string `json:"logs,omitempty"`
Uptime []string `json:"uptime,omitempty"`
}
// Privilege represents a Kibana feature privilege
//
// Kibana API docs: https://www.elastic.co/guide/en/kibana/current/kibana-privileges.html
type Privilege struct {
Base []string `json:"base"`
Feature *Feature `json:"feature"`
Spaces []string `json:"spaces"`
}
// ElasticsearchIndex represents an Elasticsearch index privilege
//
// Elasticsearch API docs: https://www.elastic.co/guide/en/elasticsearch/reference/7.17/defining-roles.html
type ElasticsearchIndex struct {
Names []string `json:"names"`
Privileges []string `json:"privileges"`
FieldSecurity struct {
Grant []string `json:"grant"`
Except []string `json:"except"`
} `json:"field_security"`
// TODO: Query
AllowRestrictedIndices bool `json:"allow_restricted_indices"`
}
// Role represents a Kibana Role
//
// Kibana API docs: https://www.elastic.co/guide/en/kibana/current/role-management-api.html
type Role struct {
Name string `json:"name"`
Metadata struct {
Version int `json:"version"`
} `json:"metadata"`
TransientMetadata struct {
Enabled bool `json:"enabled"`
} `json:"transient_metadata"`
Elasticsearch struct {
Cluster []string `json:"cluster"`
Indices *[]ElasticsearchIndex `json:"indices"`
RunAs []string `json:"run_as"`
} `json:"elasticsearch"`
Kibana *[]Privilege `json:"kibana"`
}
// CreateOrUpdateRoleOptions represents the available CreateOrUpdateRole() options
//
// Kibana API docs: https://www.elastic.co/guide/en/kibana/current/role-management-api-put.html
type CreateOrUpdateRoleOptions struct {
Metadata *struct {
Version *int `url:"version,omitempty" json:"version,omitempty"`
} `url:"metadata,omitempty" json:"metadata,omitempty"`
Elasticsearch *struct {
Cluster *[]string `url:"cluster,omitempty" json:"cluster,omitempty"`
Indices *[]ElasticsearchIndex `url:"indices,omitempty" json:"indices,omitempty"`
RunAs *[]string `url:"run_as,omitempty" json:"run_as,omitempty"`
} `url:"elasticsearch,omitempty" json:"elasticsearch,omitempty"`
Kibana *[]Privilege `url:"kibana" json:"kibana"`
}
// CreateOrUpdateRole creates or updates a Kibana role
//
// Kibana API docs: https://www.elastic.co/guide/en/kibana/current/role-management-api-put.html
func (s RolesService) CreateOrUpdateRole(rName string, opt *CreateOrUpdateRoleOptions, options ...RequestOptionFunc) (*Role, *Response, error) {
u := fmt.Sprintf("security/role/%s", rName)
req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
if err != nil {
return nil, nil, err
}
r := new(Role)
resp, err := s.client.Do(req, r)
if err != nil {
return nil, resp, err
}
return r, resp, err
}
// GetRole lists a single Kibana role
//
// Kibana API docs: https://www.elastic.co/guide/en/kibana/current/role-management-specific-api-get.html
func (s RolesService) GetRole(rName string, options ...RequestOptionFunc) (*Role, *Response, error) {
u := fmt.Sprintf("security/role/%s", rName)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
r := new(Role)
resp, err := s.client.Do(req, r)
if err != nil {
return nil, resp, err
}
return r, resp, err
}
// GetAllRoles lists all Kibana roles
//
// Kibana API docs: https://www.elastic.co/guide/en/kibana/current/role-management-api-get.html
func (s RolesService) GetAllRoles(options ...RequestOptionFunc) ([]*Role, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "security/role", nil, options)
if err != nil {
return nil, nil, err
}
var rs []*Role
resp, err := s.client.Do(req, &rs)
if err != nil {
return nil, resp, err
}
return rs, resp, err
}
// DeleteRole deletes a single Kibana role
//
// Kibana API docs: https://www.elastic.co/guide/en/kibana/current/role-management-api-delete.html
func (s RolesService) DeleteRole(rName string, options ...RequestOptionFunc) (*Response, error) {
u := fmt.Sprintf("security/role/%s", rName)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}