Skip to content

Commit

Permalink
[Cloud Security] Refactoring, removed duplicated code from tests (#19…
Browse files Browse the repository at this point in the history
…5492)

## Summary

Removed duplicated code cloud_security_posture_api_integration tests
folder


### Checklist

- [x] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
  • Loading branch information
kfirpeled authored Oct 8, 2024
1 parent 51b9be6 commit 776f05a
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 393 deletions.
12 changes: 5 additions & 7 deletions x-pack/test/cloud_security_posture_api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ import type { FtrConfigProviderContext } from '@kbn/test';
import { CLOUD_SECURITY_PLUGIN_VERSION } from '@kbn/cloud-security-posture-plugin/common/constants';

export default async function ({ readConfigFile }: FtrConfigProviderContext) {
const xpackFunctionalConfig = await readConfigFile(
require.resolve('../functional/config.base.js')
);
const xPackAPITestsConfig = await readConfigFile(require.resolve('../api_integration/config.ts'));

return {
...xpackFunctionalConfig.getAll(),
testFiles: [resolve(__dirname, './routes')],
...xPackAPITestsConfig.getAll(),
testFiles: [resolve(__dirname, './routes'), resolve(__dirname, './telemetry')],
junit: {
reportName: 'X-Pack Cloud Security Posture API Tests',
},
kbnTestServer: {
...xpackFunctionalConfig.get('kbnTestServer'),
...xPackAPITestsConfig.get('kbnTestServer'),
serverArgs: [
...xpackFunctionalConfig.get('kbnTestServer.serverArgs'),
...xPackAPITestsConfig.get('kbnTestServer.serverArgs'),
/**
* Package version is fixed (not latest) so FTR won't suddenly break when package is changed.
*
Expand Down
107 changes: 25 additions & 82 deletions x-pack/test/cloud_security_posture_api/routes/benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
X_ELASTIC_INTERNAL_ORIGIN_REQUEST,
} from '@kbn/core-http-common';
import {
BENCHMARK_SCORE_INDEX_DEFAULT_NS,
CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE,
LATEST_FINDINGS_INDEX_DEFAULT_NS,
} from '@kbn/cloud-security-posture-plugin/common/constants';
Expand All @@ -18,6 +17,7 @@ import Chance from 'chance';
import { CspBenchmarkRule } from '@kbn/cloud-security-posture-common/schema/rules/latest';
import { FtrProviderContext } from '../ftr_provider_context';
import { CspSecurityCommonProvider } from './helper/user_roles_utilites';
import { waitForPluginInitialized, EsIndexDataProvider } from '../utils';

const chance = new Chance();

Expand All @@ -28,9 +28,10 @@ export default function (providerContext: FtrProviderContext) {
const es = getService('es');
const kibanaServer = getService('kibanaServer');
const supertest = getService('supertest');
const log = getService('log');
const logger = getService('log');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const cspSecurity = CspSecurityCommonProvider(providerContext);
const findingsIndex = new EsIndexDataProvider(es, LATEST_FINDINGS_INDEX_DEFAULT_NS);

const getCspBenchmarkRules = async (benchmarkId: string): Promise<CspBenchmarkRule[]> => {
let cspBenchmarkRules: CspBenchmarkRule[] = [];
Expand Down Expand Up @@ -78,86 +79,21 @@ export default function (providerContext: FtrProviderContext) {
},
});

/**
* required before indexing findings
*/
const waitForPluginInitialized = (): Promise<void> =>
retry.try(async () => {
log.debug('Check CSP plugin is initialized');
const response = await supertest
.get('/internal/cloud_security_posture/status?check=init')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.expect(200);
expect(response.body).to.eql({ isPluginInitialized: true });
log.debug('CSP plugin is initialized');
});

const index = {
addFindings: async <T>(findingsMock: T[]) => {
await Promise.all(
findingsMock.map((findingsDoc) =>
es.index({
index: LATEST_FINDINGS_INDEX_DEFAULT_NS,
body: { ...findingsDoc, '@timestamp': new Date().toISOString() },
refresh: true,
})
)
);
},

addScores: async <T>(scoresMock: T[]) => {
await Promise.all(
scoresMock.map((scoreDoc) =>
es.index({
index: BENCHMARK_SCORE_INDEX_DEFAULT_NS,
body: { ...scoreDoc, '@timestamp': new Date().toISOString() },
refresh: true,
})
)
);
},

removeFindings: async () => {
const indexExists = await es.indices.exists({ index: LATEST_FINDINGS_INDEX_DEFAULT_NS });

if (indexExists) {
es.deleteByQuery({
index: LATEST_FINDINGS_INDEX_DEFAULT_NS,
query: { match_all: {} },
refresh: true,
});
}
},

removeScores: async () => {
const indexExists = await es.indices.exists({ index: BENCHMARK_SCORE_INDEX_DEFAULT_NS });

if (indexExists) {
es.deleteByQuery({
index: BENCHMARK_SCORE_INDEX_DEFAULT_NS,
query: { match_all: {} },
refresh: true,
});
}
},

deleteFindingsIndex: async () => {
const indexExists = await es.indices.exists({ index: LATEST_FINDINGS_INDEX_DEFAULT_NS });

if (indexExists) {
await es.indices.delete({ index: LATEST_FINDINGS_INDEX_DEFAULT_NS });
}
},
};

describe('GET /internal/cloud_security_posture/benchmarks', () => {
describe('Get Benchmark API', async () => {
beforeEach(async () => {
await index.removeFindings();
await findingsIndex.deleteAll();
await kibanaServer.savedObjects.clean({
types: ['cloud-security-posture-settings'],
});
await waitForPluginInitialized({ retry, logger, supertest });
});

afterEach(async () => {
await findingsIndex.deleteAll();
await kibanaServer.savedObjects.clean({
types: ['cloud-security-posture-settings'],
});
await waitForPluginInitialized();
});

it('Verify cspm benchmark score is updated when muting rules', async () => {
Expand All @@ -166,7 +102,7 @@ export default function (providerContext: FtrProviderContext) {

const cspmFinding = getMockFinding(benchmarkRules[0], 'passed');

await index.addFindings([cspmFinding]);
await findingsIndex.addBulk([cspmFinding]);

const { body: benchmarksBeforeMute } = await supertest
.get('/internal/cloud_security_posture/benchmarks')
Expand Down Expand Up @@ -219,7 +155,7 @@ export default function (providerContext: FtrProviderContext) {

const kspmFinding = getMockFinding(benchmarkRules[0], 'passed');

await index.addFindings([kspmFinding]);
await findingsIndex.addBulk([kspmFinding]);
const { body: benchmarksBeforeMute } = await supertest
.get('/internal/cloud_security_posture/benchmarks')
.set(ELASTIC_HTTP_VERSION_HEADER, '2')
Expand Down Expand Up @@ -268,11 +204,18 @@ export default function (providerContext: FtrProviderContext) {

describe('Get Benchmark API', async () => {
beforeEach(async () => {
await index.removeFindings();
await findingsIndex.deleteAll();
await kibanaServer.savedObjects.clean({
types: ['cloud-security-posture-settings'],
});
await waitForPluginInitialized({ retry, logger, supertest });
});

afterEach(async () => {
await findingsIndex.deleteAll();
await kibanaServer.savedObjects.clean({
types: ['cloud-security-posture-settings'],
});
await waitForPluginInitialized();
});

it('Calling Benchmark API as User with no read access to Security', async () => {
Expand All @@ -281,7 +224,7 @@ export default function (providerContext: FtrProviderContext) {

const cspmFinding1 = getMockFinding(benchmarkRules[0], 'passed');

await index.addFindings([cspmFinding1]);
await findingsIndex.addBulk([cspmFinding1]);

const { body: benchmarksResult } = await supertestWithoutAuth
.get('/internal/cloud_security_posture/benchmarks')
Expand All @@ -303,7 +246,7 @@ export default function (providerContext: FtrProviderContext) {

const cspmFinding1 = getMockFinding(benchmarkRules[0], 'passed');

await index.addFindings([cspmFinding1]);
await findingsIndex.addBulk([cspmFinding1]);

const { status } = await supertestWithoutAuth
.get('/internal/cloud_security_posture/benchmarks')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import type { CspBenchmarkRule } from '@kbn/cloud-security-posture-common/schema
import { generateBenchmarkRuleTags } from '@kbn/cloud-security-posture-plugin/common/utils/detection_rules';
import type { FtrProviderContext } from '../ftr_provider_context';
import { CspSecurityCommonProvider } from './helper/user_roles_utilites';
import { waitForPluginInitialized } from '../utils';

// eslint-disable-next-line import/no-default-export
export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const retry = getService('retry');
const supertest = getService('supertest');
const log = getService('log');
const logger = getService('log');
const kibanaServer = getService('kibanaServer');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const cspSecurity = CspSecurityCommonProvider(providerContext);
Expand Down Expand Up @@ -83,23 +84,9 @@ export default function (providerContext: FtrProviderContext) {
return detectionRule;
};

/**
* required before indexing findings
*/
const waitForPluginInitialized = (): Promise<void> =>
retry.try(async () => {
log.debug('Check CSP plugin is initialized');
const response = await supertest
.get('/internal/cloud_security_posture/status?check=init')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.expect(200);
expect(response.body).to.eql({ isPluginInitialized: true });
log.debug('CSP plugin is initialized');
});

describe('Verify update csp rules states API', async () => {
before(async () => {
await waitForPluginInitialized();
await waitForPluginInitialized({ retry, logger, supertest });
});

beforeEach(async () => {
Expand All @@ -108,6 +95,12 @@ export default function (providerContext: FtrProviderContext) {
});
});

afterEach(async () => {
await kibanaServer.savedObjects.clean({
types: ['cloud-security-posture-settings', 'alert'],
});
});

it('mute benchmark rules successfully', async () => {
const rule1 = await getRandomCspBenchmarkRule();
const rule2 = await getRandomCspBenchmarkRule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import { CSP_BENCHMARK_RULE_SAVED_OBJECT_TYPE } from '@kbn/cloud-security-postur
import type { CspBenchmarkRule } from '@kbn/cloud-security-posture-common/schema/rules/latest';
import type { FtrProviderContext } from '../ftr_provider_context';
import { CspSecurityCommonProvider } from './helper/user_roles_utilites';
import { waitForPluginInitialized } from '../utils';

// eslint-disable-next-line import/no-default-export
export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;
const retry = getService('retry');
const supertest = getService('supertest');
const log = getService('log');
const logger = getService('log');
const kibanaServer = getService('kibanaServer');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const cspSecurity = CspSecurityCommonProvider(providerContext);
Expand All @@ -42,23 +43,9 @@ export default function (providerContext: FtrProviderContext) {
return cspBenchmarkRules.saved_objects[randomIndex].attributes;
};

/**
* required before indexing findings
*/
const waitForPluginInitialized = (): Promise<void> =>
retry.try(async () => {
log.debug('Check CSP plugin is initialized');
const response = await supertest
.get('/internal/cloud_security_posture/status?check=init')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.expect(200);
expect(response.body).to.eql({ isPluginInitialized: true });
log.debug('CSP plugin is initialized');
});

describe('Tests get rules states API', async () => {
before(async () => {
await waitForPluginInitialized();
await waitForPluginInitialized({ retry, logger, supertest });
});

beforeEach(async () => {
Expand All @@ -67,6 +54,12 @@ export default function (providerContext: FtrProviderContext) {
});
});

afterEach(async () => {
await kibanaServer.savedObjects.clean({
types: ['cloud-security-posture-settings'],
});
});

it('get rules states successfully', async () => {
const rule1 = await getRandomCspBenchmarkRule();
const rule2 = await getRandomCspBenchmarkRule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,24 @@ import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import expect from '@kbn/expect';
import { FtrProviderContext } from '../ftr_provider_context';
import { CspSecurityCommonProvider } from './helper/user_roles_utilites';
import { waitForPluginInitialized } from '../utils';

// eslint-disable-next-line import/no-default-export
export default function (providerContext: FtrProviderContext) {
const { getService } = providerContext;

const retry = getService('retry');
const supertest = getService('supertest');
const log = getService('log');
const logger = getService('log');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const cspSecurity = CspSecurityCommonProvider(providerContext);

/**
* required before indexing findings
*/
const waitForPluginInitialized = (): Promise<void> =>
retry.try(async () => {
log.debug('Check CSP plugin is initialized');
const response = await supertest
.get('/internal/cloud_security_posture/status?check=init')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.expect(200);
expect(response.body).to.eql({ isPluginInitialized: true });
log.debug('CSP plugin is initialized');
});

describe('/internal/cloud_security_posture/detection_engine_rules/alerts/_status', () => {
describe('GET detection_engine_rules API with user that has specific access', async () => {
before(async () => {
await waitForPluginInitialized();
await waitForPluginInitialized({ retry, logger, supertest });
});

it('GET detection_engine_rules API with user with read access', async () => {
const { status } = await supertestWithoutAuth
.get(
Expand Down
1 change: 0 additions & 1 deletion x-pack/test/cloud_security_posture_api/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default function (providerContext: FtrProviderContext) {
await cspSecurity.createUsers();
});

loadTestFile(require.resolve('../telemetry/telemetry.ts'));
loadTestFile(require.resolve('./vulnerabilities_dashboard.ts'));
loadTestFile(require.resolve('./stats.ts'));
loadTestFile(require.resolve('./csp_benchmark_rules_bulk_update.ts'));
Expand Down
Loading

0 comments on commit 776f05a

Please sign in to comment.