[Osquery] Update cypress runner to latest Fleet changes (#124398)

This commit is contained in:
Patryk Kopyciński 2022-02-03 09:44:29 +01:00 committed by GitHub
parent 63013b9dff
commit afbe790725
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 51 deletions

View file

@ -13,10 +13,10 @@ import {
DATA_COLLECTION_SETUP_STEP,
} from '../screens/integrations';
export const addIntegration = (agent = 'Default fleet') => {
export const addIntegration = (agentPolicy = 'Default Fleet Server policy') => {
cy.getBySel(ADD_POLICY_BTN).click();
cy.getBySel(DATA_COLLECTION_SETUP_STEP).find('.euiLoadingSpinner').should('not.exist');
cy.getBySel('comboBoxInput').click().type(`${agent} {downArrow} {enter}`);
cy.getBySel('agentPolicySelect').select(agentPolicy);
cy.getBySel(CREATE_PACKAGE_POLICY_SAVE_BTN).click();
// sometimes agent is assigned to default policy, sometimes not
closeModalIfVisible();

View file

@ -11,11 +11,12 @@ import { ChildProcess, spawn } from 'child_process';
import { getLatestVersion } from './artifact_manager';
import { Manager } from './resource_manager';
interface AgentManagerParams {
export interface AgentManagerParams {
user: string;
password: string;
kibanaUrl: string;
esHost: string;
esPort: string;
}
export class AgentManager extends Manager {
@ -23,19 +24,11 @@ export class AgentManager extends Manager {
private log: ToolingLog;
private agentProcess?: ChildProcess;
private requestOptions: AxiosRequestConfig;
constructor(params: AgentManagerParams, log: ToolingLog) {
constructor(params: AgentManagerParams, log: ToolingLog, requestOptions: AxiosRequestConfig) {
super();
this.log = log;
this.params = params;
this.requestOptions = {
headers: {
'kbn-xsrf': 'kibana',
},
auth: {
username: this.params.user,
password: this.params.password,
},
};
this.requestOptions = requestOptions;
}
public async setup() {

View file

@ -5,10 +5,11 @@
* 2.0.
*/
import axios from 'axios';
import { last } from 'lodash';
// import axios from 'axios';
// import { last } from 'lodash';
export async function getLatestVersion(): Promise<string> {
const response: any = await axios('https://artifacts-api.elastic.co/v1/versions');
return last(response.data.versions as string[]) || '8.1.0-SNAPSHOT';
return '8.0.0-SNAPSHOT';
// const response: any = await axios('https://artifacts-api.elastic.co/v1/versions');
// return last(response.data.versions as string[]) || '8.1.0-SNAPSHOT';
}

View file

@ -7,34 +7,49 @@
import { ChildProcess, spawn } from 'child_process';
import { ToolingLog } from '@kbn/dev-utils';
import axios from 'axios';
import axios, { AxiosRequestConfig } from 'axios';
import { Manager } from './resource_manager';
import { getLatestVersion } from './artifact_manager';
export interface ElasticsearchConfig {
esHost: string;
user: string;
password: string;
port: string;
}
import { AgentManagerParams } from './agent';
export class FleetManager extends Manager {
private fleetProcess?: ChildProcess;
private esConfig: ElasticsearchConfig;
private config: AgentManagerParams;
private log: ToolingLog;
constructor(esConfig: ElasticsearchConfig, log: ToolingLog) {
private requestOptions: AxiosRequestConfig;
constructor(config: AgentManagerParams, log: ToolingLog, requestOptions: AxiosRequestConfig) {
super();
this.esConfig = esConfig;
this.config = config;
this.log = log;
this.requestOptions = requestOptions;
}
public async setup(): Promise<void> {
this.log.info('Setting fleet up');
return new Promise(async (res, rej) => {
try {
const response = await axios.post(
`${this.esConfig.esHost}/_security/service/elastic/fleet-server/credential/token`
// default fleet server policy no longer created by default
const {
data: {
item: { id: policyId },
},
} = await axios.post(
`${this.config.kibanaUrl}/api/fleet/agent_policies`,
{
name: 'Default Fleet Server policy',
description: '',
namespace: 'default',
monitoring_enabled: ['logs', 'metrics'],
has_fleet_server: true,
},
this.requestOptions
);
const serviceToken = response.data.token.value;
const response = await axios.post(
`${this.config.kibanaUrl}/api/fleet/service_tokens`,
{},
this.requestOptions
);
const serviceToken = response.data.value;
const artifact = `docker.elastic.co/beats/elastic-agent:${await getLatestVersion()}`;
this.log.info(artifact);
@ -49,12 +64,15 @@ export class FleetManager extends Manager {
'--env',
'FLEET_SERVER_ENABLE=true',
'--env',
`FLEET_SERVER_ELASTICSEARCH_HOST=http://${host}:${this.esConfig.port}`,
`FLEET_SERVER_ELASTICSEARCH_HOST=http://${host}:${this.config.esPort}`,
'--env',
`FLEET_SERVER_SERVICE_TOKEN=${serviceToken}`,
'--env',
`FLEET_SERVER_POLICY=${policyId}`,
'--rm',
artifact,
];
this.log.info('docker ' + args.join(' '));
this.fleetProcess = spawn('docker', args, {
stdio: 'inherit',
});

View file

@ -12,7 +12,10 @@ import { withProcRunner } from '@kbn/dev-utils';
import { FtrProviderContext } from './ftr_provider_context';
import { AgentManager } from './agent';
import {
// AgentManager,
AgentManagerParams,
} from './agent';
import { FleetManager } from './fleet_server';
async function withFleetAgent(
@ -23,25 +26,29 @@ async function withFleetAgent(
const config = getService('config');
const esHost = Url.format(config.get('servers.elasticsearch'));
const esConfig = {
const params: AgentManagerParams = {
user: config.get('servers.elasticsearch.username'),
password: config.get('servers.elasticsearch.password'),
esHost,
port: config.get('servers.elasticsearch.port'),
esPort: config.get('servers.elasticsearch.port'),
kibanaUrl: Url.format({
protocol: config.get('servers.kibana.protocol'),
hostname: config.get('servers.kibana.hostname'),
port: config.get('servers.kibana.port'),
}),
};
const fleetManager = new FleetManager(esConfig, log);
const agentManager = new AgentManager(
{
...esConfig,
kibanaUrl: Url.format({
protocol: config.get('servers.kibana.protocol'),
hostname: config.get('servers.kibana.hostname'),
port: config.get('servers.kibana.port'),
}),
const requestOptions = {
headers: {
'kbn-xsrf': 'kibana',
},
log
);
auth: {
username: params.user,
password: params.password,
},
};
const fleetManager = new FleetManager(params, log, requestOptions);
// const agentManager = new AgentManager(params, log, requestOptions);
// Since the managers will create uncaughtException event handlers we need to exit manually
process.on('uncaughtException', (err) => {
@ -50,13 +57,13 @@ async function withFleetAgent(
process.exit(1);
});
await agentManager.setup();
// await agentManager.setup();
await fleetManager.setup();
try {
await runner({});
} finally {
fleetManager.cleanup();
agentManager.cleanup();
// agentManager.cleanup();
}
}

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import axios from 'axios';
import { ElasticsearchConfig } from './fleet_server';
import { AgentManagerParams } from './agent';
interface Roles {
[roleName: string]: {
@ -83,7 +83,7 @@ export const USERS: Users = {
},
};
export const setupUsers = async (config: ElasticsearchConfig) => {
export const setupUsers = async (config: AgentManagerParams) => {
const { esHost, user: username, password } = config;
const params = {
auth: { username, password },