-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathsetup-custom-kibana-user-role.ts
252 lines (216 loc) · 6.33 KB
/
setup-custom-kibana-user-role.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
/* eslint-disable no-console */
import yaml from 'js-yaml';
import axios, { AxiosRequestConfig, AxiosError } from 'axios';
import fs from 'fs';
import { union, difference, once } from 'lodash';
import path from 'path';
import { argv } from 'yargs';
const config = yaml.safeLoad(
fs.readFileSync(
path.join(__filename, '../../../../../../../config/kibana.dev.yml'),
'utf8'
)
);
const GITHUB_USERNAME = argv.username as string;
const KIBANA_INDEX = config['kibana.index'] as string;
const TASK_MANAGER_INDEX = config['xpack.task_manager.index'] as string;
const ELASTICSEARCH_USERNAME = (argv.esUsername as string) || 'elastic';
const ELASTICSEARCH_PASSWORD = (argv.esPassword ||
config['elasticsearch.password']) as string;
const KIBANA_BASE_URL = (argv.baseUrl as string) || 'http://localhost:5601';
interface User {
username: string;
roles: string[];
full_name?: string;
email?: string;
enabled?: boolean;
}
const getKibanaBasePath = once(async () => {
try {
await axios.request({ url: KIBANA_BASE_URL, maxRedirects: 0 });
} catch (e) {
const err = e as AxiosError;
const { location } = err.response?.headers;
const isBasePath = RegExp(/^\/\w{3}$/).test(location);
return isBasePath ? location : '';
}
return '';
});
init().catch(e => {
if (e.response) {
console.log(
JSON.stringify({ request: e.config, response: e.response.data }, null, 2)
);
return;
}
console.log(e);
});
async function init() {
// kibana.index must be different from `.kibana`
if (KIBANA_INDEX === '.kibana') {
console.log(
'kibana.dev.yml: Please use a custom "kibana.index". Example: "kibana.index: .kibana-john"'
);
return;
}
if (!KIBANA_INDEX.startsWith('.kibana')) {
console.log(
'kibana.dev.yml: "kibana.index" must be prefixed with `.kibana`. Example: "kibana.index: .kibana-john"'
);
return;
}
if (TASK_MANAGER_INDEX && !TASK_MANAGER_INDEX.startsWith('.kibana')) {
console.log(
'kibana.dev.yml: "xpack.task_manager.index" must be prefixed with `.kibana`. Example: "xpack.task_manager.index: .kibana-task-manager-john"'
);
return;
}
if (!GITHUB_USERNAME) {
console.log(
'Please specify your github username with `--username <username>` '
);
return;
}
const isEnabled = await isSecurityEnabled();
if (!isEnabled) {
console.log('Security must be enabled!');
return;
}
const KIBANA_READ_ROLE = `kibana_read_${GITHUB_USERNAME}`;
const KIBANA_WRITE_ROLE = `kibana_write_${GITHUB_USERNAME}`;
// create roles
await createRole({ roleName: KIBANA_READ_ROLE, privilege: 'read' });
await createRole({ roleName: KIBANA_WRITE_ROLE, privilege: 'all' });
// read/write access to all apps + apm index access
await createOrUpdateUser({
username: 'apm_write_user',
roles: ['apm_user', KIBANA_WRITE_ROLE]
});
// read access to all apps + apm index access
await createOrUpdateUser({
username: 'apm_read_user',
roles: ['apm_user', KIBANA_READ_ROLE]
});
// read/write access to all apps (no apm index access)
await createOrUpdateUser({
username: 'kibana_write_user',
roles: [KIBANA_WRITE_ROLE]
});
}
async function isSecurityEnabled() {
interface XPackInfo {
features: { security?: { allow_rbac: boolean } };
}
const { features } = await callKibana<XPackInfo>({
url: `/api/xpack/v1/info`
});
return features.security?.allow_rbac;
}
async function callKibana<T>(options: AxiosRequestConfig): Promise<T> {
const basePath = await getKibanaBasePath();
const { data } = await axios.request({
...options,
baseURL: KIBANA_BASE_URL + basePath,
auth: {
username: ELASTICSEARCH_USERNAME,
password: ELASTICSEARCH_PASSWORD
},
headers: { 'kbn-xsrf': 'true', ...options.headers }
});
return data;
}
async function createRole({
roleName,
privilege
}: {
roleName: string;
privilege: 'read' | 'all';
}) {
const role = await getRole(roleName);
if (role) {
console.log(`Skipping: Role "${roleName}" already exists`);
return;
}
await callKibana({
method: 'PUT',
url: `/api/security/role/${roleName}`,
data: {
metadata: { version: 1 },
elasticsearch: { cluster: [], indices: [] },
kibana: [{ base: [privilege], feature: {}, spaces: ['*'] }]
}
});
console.log(`Created role "${roleName}" with privilege "${privilege}"`);
}
async function createOrUpdateUser(newUser: User) {
const existingUser = await getUser(newUser.username);
if (!existingUser) {
return createUser(newUser);
}
return updateUser(existingUser, newUser);
}
async function createUser(newUser: User) {
const user = await callKibana<User>({
method: 'POST',
url: `/api/security/v1/users/${newUser.username}`,
data: {
...newUser,
enabled: true,
password: ELASTICSEARCH_PASSWORD
}
});
console.log(`User "${newUser.username}" was created`);
return user;
}
async function updateUser(existingUser: User, newUser: User) {
const { username } = newUser;
const allRoles = union(existingUser.roles, newUser.roles);
const hasAllRoles = difference(allRoles, existingUser.roles).length === 0;
if (hasAllRoles) {
console.log(
`Skipping: User "${username}" already has neccesarry roles: "${newUser.roles}"`
);
return;
}
// assign role to user
await callKibana({
method: 'POST',
url: `/api/security/v1/users/${username}`,
data: { ...existingUser, roles: allRoles }
});
console.log(`User "${username}" was updated`);
}
async function getUser(username: string) {
try {
return await callKibana<User>({
url: `/api/security/v1/users/${username}`
});
} catch (e) {
const err = e as AxiosError;
// return empty if user doesn't exist
if (err.response?.status === 404) {
return null;
}
throw e;
}
}
async function getRole(roleName: string) {
try {
return await callKibana({
method: 'GET',
url: `/api/security/role/${roleName}`
});
} catch (e) {
const err = e as AxiosError;
// return empty if role doesn't exist
if (err.response?.status === 404) {
return null;
}
throw e;
}
}