Skip to content

Commit

Permalink
[Osquery] Update cypress runner to latest Fleet changes (elastic#124398)
Browse files Browse the repository at this point in the history
(cherry picked from commit afbe790)
  • Loading branch information
patrykkopycinski authored and kibanamachine committed Feb 3, 2022
1 parent 47a2516 commit 3b2fbc8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 50 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/osquery/cypress/tasks/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 4 additions & 11 deletions x-pack/test/osquery_cypress/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,24 @@ 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 {
private params: AgentManagerParams;
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() {
Expand Down
9 changes: 5 additions & 4 deletions x-pack/test/osquery_cypress/artifact_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
46 changes: 32 additions & 14 deletions x-pack/test/osquery_cypress/fleet_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
// 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 response = await axios.post(
`${this.esConfig.esHost}/_security/service/elastic/fleet-server/credential/token`
`${this.config.kibanaUrl}/api/fleet/service_tokens`,
{},
this.requestOptions
);
const serviceToken = response.data.token.value;
const serviceToken = response.data.value;
const artifact = `docker.elastic.co/beats/elastic-agent:${await getLatestVersion()}`;
this.log.info(artifact);

Expand All @@ -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',
});
Expand Down
41 changes: 24 additions & 17 deletions x-pack/test/osquery_cypress/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) => {
Expand All @@ -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();
}
}

Expand Down
4 changes: 2 additions & 2 deletions x-pack/test/osquery_cypress/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/
import axios from 'axios';
import { ElasticsearchConfig } from './fleet_server';
import { AgentManagerParams } from './agent';

interface Roles {
[roleName: string]: {
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit 3b2fbc8

Please sign in to comment.