-
Notifications
You must be signed in to change notification settings - Fork 8k
/
add_monitor.ts
160 lines (136 loc) · 5.39 KB
/
add_monitor.ts
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { omit } from 'lodash';
import expect from '@kbn/expect';
import { secretKeys } from '@kbn/synthetics-plugin/common/constants/monitor_management';
import { HTTPFields } from '@kbn/synthetics-plugin/common/runtime_types';
import { API_URLS } from '@kbn/synthetics-plugin/common/constants';
import { ALL_SPACES_ID } from '@kbn/security-plugin/common/constants';
import { format as formatUrl } from 'url';
import supertest from 'supertest';
import { serviceApiKeyPrivileges } from '@kbn/synthetics-plugin/server/synthetics_service/get_api_key';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { getFixtureJson } from './helper/get_fixture_json';
export default function ({ getService }: FtrProviderContext) {
describe('[POST] /internal/uptime/service/monitors', function () {
this.tags('skipCloud');
const supertestAPI = getService('supertest');
let _httpMonitorJson: HTTPFields;
let httpMonitorJson: HTTPFields;
before(() => {
_httpMonitorJson = getFixtureJson('http_monitor');
});
beforeEach(() => {
httpMonitorJson = _httpMonitorJson;
});
it('returns the newly added monitor', async () => {
const newMonitor = httpMonitorJson;
const apiResponse = await supertestAPI
.post(API_URLS.SYNTHETICS_MONITORS)
.set('kbn-xsrf', 'true')
.send(newMonitor);
expect(apiResponse.body.attributes).eql(omit(newMonitor, secretKeys));
});
it('returns bad request if payload is invalid for HTTP monitor', async () => {
// Delete a required property to make payload invalid
const newMonitor = { ...httpMonitorJson, 'check.request.headers': undefined };
const apiResponse = await supertestAPI
.post(API_URLS.SYNTHETICS_MONITORS)
.set('kbn-xsrf', 'true')
.send(newMonitor);
expect(apiResponse.status).eql(400);
});
it('returns bad request if monitor type is invalid', async () => {
const newMonitor = { ...httpMonitorJson, type: 'invalid-data-steam' };
const apiResponse = await supertestAPI
.post(API_URLS.SYNTHETICS_MONITORS)
.set('kbn-xsrf', 'true')
.send(newMonitor);
expect(apiResponse.status).eql(400);
expect(apiResponse.body.message).eql('Monitor type is invalid');
});
it('can create monitor with API key with proper permissions', async () => {
await supertestAPI
.post('/internal/security/api_key')
.set('kbn-xsrf', 'xxx')
.send({
name: 'test_api_key',
expiration: '12d',
kibana_role_descriptors: {
uptime_save: {
elasticsearch: serviceApiKeyPrivileges,
kibana: [
{
base: [],
spaces: [ALL_SPACES_ID],
feature: {
uptime: ['all'],
},
},
],
},
},
})
.expect(200)
.then(async (response: Record<string, any>) => {
const { name, encoded: apiKey } = response.body;
expect(name).to.eql('test_api_key');
const config = getService('config');
const { hostname, protocol, port } = config.get('servers.kibana');
const kibanaServerUrl = formatUrl({ hostname, protocol, port });
const supertestNoAuth = supertest(kibanaServerUrl);
const apiResponse = await supertestNoAuth
.post(API_URLS.SYNTHETICS_MONITORS)
.auth(name, apiKey)
.set('kbn-xsrf', 'true')
.set('Authorization', `ApiKey ${apiKey}`)
.send(httpMonitorJson);
expect(apiResponse.status).eql(200);
});
});
it('can not create monitor with API key without proper permissions', async () => {
await supertestAPI
.post('/internal/security/api_key')
.set('kbn-xsrf', 'xxx')
.send({
name: 'test_api_key',
expiration: '12d',
kibana_role_descriptors: {
uptime_save: {
elasticsearch: serviceApiKeyPrivileges,
kibana: [
{
base: [],
spaces: [ALL_SPACES_ID],
feature: {
uptime: ['read'],
},
},
],
},
},
})
.expect(200)
.then(async (response: Record<string, any>) => {
const { name, encoded: apiKey } = response.body;
expect(name).to.eql('test_api_key');
const config = getService('config');
const { hostname, protocol, port } = config.get('servers.kibana');
const kibanaServerUrl = formatUrl({ hostname, protocol, port });
const supertestNoAuth = supertest(kibanaServerUrl);
const apiResponse = await supertestNoAuth
.post(API_URLS.SYNTHETICS_MONITORS)
.auth(name, apiKey)
.set('kbn-xsrf', 'true')
.set('Authorization', `ApiKey ${apiKey}`)
.send(httpMonitorJson);
expect(apiResponse.status).eql(403);
expect(apiResponse.body.message).eql('Unable to create synthetics-monitor');
});
});
});
}