Skip to content

Instantly share code, notes, and snippets.

@azasypkin
Last active February 12, 2025 13:50
Show Gist options
  • Save azasypkin/6f18413d0cb73c9c988d09c25b780029 to your computer and use it in GitHub Desktop.
Save azasypkin/6f18413d0cb73c9c988d09c25b780029 to your computer and use it in GitHub Desktop.
Kibana mass login
#!/usr/bin/env node
'use strict';
import autocannon from 'autocannon';
import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator';
const USERS_COUNT = process.env.AZ_USERS_COUNT || 3;
const SESSIONS_PER_USER_COUNT = process.env.AZ_SESSIONS_PER_USER_COUNT || 1000;
const MAX_CONNECTIONS = process.env.AZ_MAX_CONNECTIONS || 100;
const KIBANA_HOST = process.env.AZ_KIBANA_HOST || 'http://localhost:5601';
const USERNAME = 'elastic';
const PASSWORD = 'changeme';
const USE_RANDOM_USERS = process.env.AZ_USE_RANDOM_USERS !== 'false';
const BASIC_PROVIDER_NAME = 'basic';
async function runInParallel(url, headers, requests) {
return new Promise((resolve, reject) => {
autocannon({
url,
amount: requests.length,
connections: Math.min(MAX_CONNECTIONS, requests.length),
method: 'POST',
headers,
requests: [{
setupRequest: (req) => {
const { body, path } = requests.pop();
req.body = body;
req.path = path;
return req;
}
}]
}, (err, res) => {
if (err) {
reject(err)
} else {
resolve(res.statusCodeStats)
}
});
});
}
(async function main() {
const usernames = USE_RANDOM_USERS
? [...Array(USERS_COUNT).keys()].map(() => uniqueNamesGenerator({ dictionaries: [adjectives, animals], length: 2 }))
: Array(USERS_COUNT).fill(USERNAME, 0, USERS_COUNT);
const httpHeaders = {
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'kbn-xsrf': true,
'x-elastic-internal-origin': 'Kibana'
};
if (USE_RANDOM_USERS) {
const userCreateRequests = usernames.map((username) => {
const fullName = username.split('_').map((namePart) => namePart.charAt(0).toUpperCase() + namePart.slice(1)).join(' ');
return {
path: `${KIBANA_HOST}/internal/security/users/${username}`,
body: JSON.stringify({
username,
password: PASSWORD,
full_name: fullName,
email: `${username}@profiles.elastic.co`,
roles: ['superuser'],
enabled: true
})
};
});
console.log('Generated users: ', await runInParallel(
KIBANA_HOST,
{ ...httpHeaders, Authorization: `Basic ${Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64')}` },
userCreateRequests
));
}
const userLoginRequests = usernames.flatMap((username) => {
return Array(SESSIONS_PER_USER_COUNT).fill({
path: `${KIBANA_HOST}/internal/security/login`,
body: JSON.stringify({
providerType: 'basic',
providerName: BASIC_PROVIDER_NAME,
currentURL: `${KIBANA_HOST}/login`,
params: { username, password: PASSWORD }
})
}, 0, SESSIONS_PER_USER_COUNT);
});
console.log('Logged in users: ', await runInParallel(KIBANA_HOST, httpHeaders, userLoginRequests));
})();
{
"version": "1.0.0",
"bin": "./index.js",
"name": "kibana-mass-login",
"type": "module",
"dependencies": {
"autocannon": "^7.15.0",
"unique-names-generator": "^4.7.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment