1package elastic
2
3import (
4	"context"
5	"testing"
6)
7
8const (
9	testRoleBody = `{
10		"cluster" : [ "all" ],
11		"indices" : [
12			{
13				"names" : [ "index1", "index2" ],
14				"privileges" : [ "all" ],
15				"field_security" : {
16					"grant" : [ "title", "body" ]
17				}
18			}
19		],
20		"applications" : [ ],
21		"run_as" : [ "other_user" ],
22		"global" : {
23			"application": {
24			  "manage": {
25				  "applications": [ "my-test-app" ]
26			  }
27			}
28		  },
29		"metadata" : {
30			"version" : 1
31		},
32		"transient_metadata": {
33			"enabled": true
34		}
35	  }`
36
37	testRoleMappingBody = `{
38		"enabled": false,
39		"roles": [
40			"user"
41		],
42		"rules": {
43			"all": [
44				{
45					"field": {
46					"username": "esadmin"
47					}
48				},
49				{
50					"field": {
51					"groups": "cn=admins,dc=example,dc=com"
52					}
53				}
54			]
55		},
56		"metadata": {
57			"version": 1
58		}
59	  }`
60
61	testWatchBody = `{
62		"trigger" : {
63			"schedule" : { "cron" : "0 0/1 * * * ?" }
64		},
65		"input" : {
66			"search" : {
67				"request" : {
68					"indices" : [
69						"elastic-test"
70					],
71					"body" : {
72						"query" : {
73							"bool" : {
74								"must" : {
75									"match": {
76										 "response": 404
77									}
78								},
79								"filter" : {
80									"range": {
81										"@timestamp": {
82											"from": "{{ctx.trigger.scheduled_time}}||-5m",
83											"to": "{{ctx.trigger.triggered_time}}"
84										}
85									}
86								}
87							}
88						}
89					}
90				}
91			}
92		},
93		"condition" : {
94			"compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
95		},
96		"actions" : {
97			"email_admin" : {
98				"email" : {
99					"to" : "admin@domain.host.com",
100					"subject" : "404 recently encountered"
101				}
102			}
103		}
104	}`
105)
106
107func TestXpackInfo(t *testing.T) {
108	client := setupTestClientForXpackSecurity(t)
109	tagline := "You know, for X"
110
111	// Get xpack info
112	info, err := client.XPackInfo().Do(context.Background())
113	if err != nil {
114		t.Fatal(err)
115	}
116	if info == &(XPackInfoServiceResponse{}) {
117		t.Errorf("expected data from response; got empty response")
118	}
119	if info.Tagline != tagline {
120		t.Errorf("expected %s as a tagline; received %s", tagline, info.Tagline)
121	}
122}
123
124func TestXPackSecurityRole(t *testing.T) {
125	client := setupTestClientForXpackSecurity(t)
126
127	xpack_info, err := client.XPackInfo().Do(context.Background())
128	if err != nil {
129		t.Fatal(err)
130	}
131	if !xpack_info.Features.Security.Enabled {
132		t.Skip("skip due to deactivated xpack security")
133	}
134
135	roleName := "my-role"
136
137	// Add a role
138	_, err = client.XPackSecurityPutRole(roleName).Body(testRoleBody).Do(context.Background())
139	if err != nil {
140		t.Fatal(err)
141	}
142	defer func() {
143		client.XPackSecurityDeleteRole(roleName).Do(context.Background())
144	}()
145
146	// Get a role
147	role, err := client.XPackSecurityGetRole(roleName).Do(context.Background())
148	if err != nil {
149		t.Fatal(err)
150	}
151	if len(*role) == 0 {
152		t.Errorf("expected len(Mappings) > 0; got empty")
153	}
154	if _, ok := (*role)[roleName]; !ok {
155		t.Errorf("expected role mapping %s; key did not exist", roleName)
156	}
157	if role == &(XPackSecurityGetRoleResponse{}) {
158		t.Errorf("expected data from response; got empty response")
159	}
160
161	// Delete a role
162	deletedRole, err := client.XPackSecurityDeleteRole(roleName).Do(context.Background())
163	if err != nil {
164		t.Fatal(err)
165	}
166	if !deletedRole.Found {
167		t.Error("expected test role to be found; was not found")
168	}
169
170}
171
172func TestXPackSecurityRoleMapping(t *testing.T) {
173	client := setupTestClientForXpackSecurity(t)
174
175	xpack_info, err := client.XPackInfo().Do(context.Background())
176	if err != nil {
177		t.Fatal(err)
178	}
179	if !xpack_info.Features.Security.Enabled {
180		t.Skip("skip due to deactivated xpack security")
181	}
182
183	roleMappingName := "my-role-mapping"
184
185	// Add a role mapping
186	_, err = client.XPackSecurityPutRoleMapping(roleMappingName).Body(testRoleMappingBody).Do(context.Background())
187	if err != nil {
188		t.Fatal(err)
189	}
190	defer func() {
191		client.XPackSecurityDeleteRoleMapping(roleMappingName).Do(context.Background())
192	}()
193
194	// Get a role mapping
195	roleMappings, err := client.XPackSecurityGetRoleMapping(roleMappingName).Do(context.Background())
196	if err != nil {
197		t.Fatal(err)
198	}
199	if len(*roleMappings) == 0 {
200		t.Errorf("expected len(Mappings) > 0; got empty")
201	}
202	if _, ok := (*roleMappings)[roleMappingName]; !ok {
203		t.Errorf("expected role mapping %s; key did not exist", roleMappingName)
204	}
205	if roleMappings == &(XPackSecurityGetRoleMappingResponse{}) {
206		t.Errorf("expected data from response; got empty response")
207	}
208
209	// Delete a role mapping
210	_, err = client.XPackSecurityDeleteRoleMapping(roleMappingName).Do(context.Background())
211	if err != nil {
212		t.Fatal(err)
213	}
214
215}
216
217func TestXPackWatcher(t *testing.T) {
218	client := setupTestClientAndCreateIndex(t, SetURL("http://elastic:elastic@localhost:9210"))
219
220	xpack_info, err := client.XPackInfo().Do(context.Background())
221	if err != nil {
222		t.Fatal(err)
223	}
224	if !xpack_info.Features.Watcher.Enabled {
225		t.Skip("skip due to deactivated xpack watcher")
226	}
227
228	// Add a watch
229	watchName := "my-watch"
230	_, err = client.XPackWatchPut(watchName).Body(testWatchBody).Do(context.Background())
231	if err != nil {
232		if IsForbidden(err) {
233			t.Skipf("skip due to missing license: %v", err)
234		}
235		t.Fatal(err)
236	}
237	defer func() {
238		client.XPackWatchDelete(watchName).Do(context.Background())
239	}()
240
241	// Get a watch
242	watch, err := client.XPackWatchGet(watchName).Do(context.Background())
243	if err != nil {
244		t.Fatal(err)
245	}
246	if watch.Found == false {
247		t.Errorf("expected watch.Found == true; got false")
248	}
249	if want, have := watchName, watch.Id; want != have {
250		t.Errorf("expected watch.Id == %q; got %q", want, have)
251	}
252
253	// Exec a watch
254	execution, err := client.XPackWatchExecute().Id(watchName).Do(context.Background())
255	if err != nil {
256		t.Fatal(err)
257	}
258	if want, have := watchName, execution.WatchRecord.WatchId; want != have {
259		t.Errorf("expected execution.WatchId == %q; got %q", want, have)
260	}
261	if want, have := "execution_not_needed", execution.WatchRecord.State; want != have {
262		t.Errorf("expected execution.state == %q; got %q", want, have)
263	}
264
265	// Ack a watch
266	ack, err := client.XPackWatchAck(watchName).Do(context.Background())
267	if err != nil {
268		t.Fatal(err)
269	}
270	if ack.Status.State == nil {
271		t.Errorf("expected ack.status != nil; got %v", ack.Status.State)
272	}
273
274	// Activate a watch
275	_, err = client.XPackWatchActivate(watchName).Do(context.Background())
276	if err != nil {
277		t.Fatal(err)
278	}
279	watch, err = client.XPackWatchGet(watchName).Do(context.Background())
280	if err != nil {
281		t.Fatal(err)
282	}
283	if want, have := true, watch.Status.State.Active; want != have {
284		t.Errorf("expected watch.Status.State.Active == %v; got %v", want, have)
285	}
286
287	// Deactivate the watch
288	_, err = client.XPackWatchDeactivate(watchName).Do(context.Background())
289	if err != nil {
290		t.Fatal(err)
291	}
292	watch, err = client.XPackWatchGet(watchName).Do(context.Background())
293	if err != nil {
294		t.Fatal(err)
295	}
296	if want, have := false, watch.Status.State.Active; want != have {
297		t.Errorf("expected watch.Status.State.Active == %v; got %v", want, have)
298	}
299
300	// Stop the watch
301	_, err = client.XPackWatchStop().Do(context.Background())
302	if err != nil {
303		t.Fatal(err)
304	}
305	stats, err := client.XPackWatchStats().Do(context.Background())
306	if err != nil {
307		t.Fatal(err)
308	}
309	if have := stats.Stats[0].WatcherState; have != "stopping" && have != "stopped" {
310		t.Errorf("expected stats.WatcherState == %q (or %q); got %q", "stopping", "stopped", have)
311	}
312
313	// Start again
314	start, err := client.XPackWatchStart().Do(context.Background())
315	if err != nil {
316		t.Fatal(err)
317	}
318	stats, err = client.XPackWatchStats().Do(context.Background())
319	if err != nil {
320		t.Fatal(err)
321	}
322	if want, have := true, start.Acknowledged; want != have {
323		t.Errorf("expected start.Acknowledged == %v; got %v", want, have)
324	}
325
326	// Restart
327	restart, err := client.XPackWatchRestart().Do(context.Background())
328	if err != nil {
329		t.Fatal(err)
330	}
331	if want, have := true, restart.Acknowledged; want != have {
332		t.Errorf("expected stats.WatcherState == %v; got %v", want, have)
333	}
334}
335