[Cloud Security] convert status api router to be versioned (#159548)

This commit is contained in:
Ido Cohen 2023-07-03 13:04:17 +03:00 committed by GitHub
parent 80b602db4a
commit a6a8f5b9ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 88 additions and 41 deletions

View file

@ -14,6 +14,7 @@ export const journey = new Journey({
const response = await kibanaServer.request({
path: '/internal/cloud_security_posture/status?check=init',
method: 'GET',
headers: { 'elastic-api-version': '1' },
});
expect(response.status).to.eql(200);
expect(response.data).to.eql({ isPluginInitialized: true });

View file

@ -8,6 +8,8 @@
import { PostureTypes, VulnSeverity } from './types';
export const STATUS_ROUTE_PATH = '/internal/cloud_security_posture/status';
export const STATUS_API_CURRENT_VERSION = '1';
export const STATS_ROUTE_PATH = '/internal/cloud_security_posture/stats/{policy_template}';
export const VULNERABILITIES_DASHBOARD_ROUTE_PATH =

View file

@ -8,7 +8,7 @@
import { useQuery, type UseQueryOptions } from '@tanstack/react-query';
import { useKibana } from '../hooks/use_kibana';
import { type CspSetupStatus } from '../../../common/types';
import { STATUS_ROUTE_PATH } from '../../../common/constants';
import { STATUS_API_CURRENT_VERSION, STATUS_ROUTE_PATH } from '../../../common/constants';
const getCspSetupStatusQueryKey = 'csp_status_key';
@ -18,7 +18,7 @@ export const useCspSetupStatusApi = (
const { http } = useKibana().services;
return useQuery<CspSetupStatus, unknown, CspSetupStatus>(
[getCspSetupStatusQueryKey],
() => http.get<CspSetupStatus>(STATUS_ROUTE_PATH),
() => http.get<CspSetupStatus>(STATUS_ROUTE_PATH, { version: STATUS_API_CURRENT_VERSION }),
options
);
};

View file

@ -7,7 +7,6 @@
import { NewPackagePolicy } from '@kbn/fleet-plugin/common';
import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import pMap from 'p-map';
import { transformError } from '@kbn/securitysolution-es-utils';
import { GetCspRuleTemplateRequest, GetCspRuleTemplateResponse } from '../../../common/types';
import { CspRuleTemplate } from '../../../common/schemas';
@ -61,13 +60,9 @@ const findCspRuleTemplateHandler = async (
filter: getBenchmarkTypeFilter(benchmarkId),
});
const cspRulesTemplates = await pMap(
cspRulesTemplatesSo.saved_objects,
async (cspRuleTemplate) => {
return { ...cspRuleTemplate.attributes };
},
{ concurrency: 50 }
);
const cspRulesTemplates = cspRulesTemplatesSo.saved_objects.map((cspRuleTemplate) => {
return { ...cspRuleTemplate.attributes };
});
return {
items: cspRulesTemplates,

View file

@ -16,6 +16,7 @@ import type {
import moment from 'moment';
import { Installation, PackagePolicy } from '@kbn/fleet-plugin/common';
import { schema } from '@kbn/config-schema';
import { VersionedRoute } from '@kbn/core-http-server/src/versioning/types';
import {
CLOUD_SECURITY_POSTURE_PACKAGE_NAME,
STATUS_ROUTE_PATH,
@ -29,7 +30,12 @@ import {
LATEST_VULNERABILITIES_INDEX_DEFAULT_NS,
VULN_MGMT_POLICY_TEMPLATE,
} from '../../../common/constants';
import type { CspApiRequestHandlerContext, CspRouter, StatusResponseInfo } from '../../types';
import type {
CspApiRequestHandlerContext,
CspRequestHandlerContext,
CspRouter,
StatusResponseInfo,
} from '../../types';
import type {
CspSetupStatus,
CspStatusCode,
@ -328,44 +334,55 @@ export const statusQueryParamsSchema = schema.object({
check: schema.oneOf([schema.literal('all'), schema.literal('init')], { defaultValue: 'all' }),
});
export const defineGetCspStatusRoute = (router: CspRouter): void =>
router.get(
{
export const defineGetCspStatusRoute = (
router: CspRouter
): VersionedRoute<'get', CspRequestHandlerContext> =>
router.versioned
.get({
access: 'internal',
path: STATUS_ROUTE_PATH,
validate: { query: statusQueryParamsSchema },
options: {
tags: ['access:cloud-security-posture-read'],
},
},
async (context, request, response) => {
const cspContext = await context.csp;
try {
if (request.query.check === 'init') {
})
.addVersion(
{
version: '1',
validate: {
request: {
query: statusQueryParamsSchema,
},
},
},
async (context, request, response) => {
const cspContext = await context.csp;
try {
if (request.query.check === 'init') {
return response.ok({
body: {
isPluginInitialized: cspContext.isPluginInitialized(),
},
});
}
const status: CspSetupStatus = await getCspStatus({
...cspContext,
esClient: cspContext.esClient.asCurrentUser,
});
return response.ok({
body: {
isPluginInitialized: cspContext.isPluginInitialized(),
},
body: status,
});
} catch (err) {
cspContext.logger.error(`Error getting csp status`);
cspContext.logger.error(err);
const error = transformError(err);
return response.customError({
body: { message: error.message },
statusCode: error.statusCode,
});
}
const status = await getCspStatus({
...cspContext,
esClient: cspContext.esClient.asCurrentUser,
});
return response.ok({
body: status,
});
} catch (err) {
cspContext.logger.error(`Error getting csp status`);
cspContext.logger.error(err);
const error = transformError(err);
return response.customError({
body: { message: error.message },
statusCode: error.statusCode,
});
}
}
);
);
const getStatusResponse = (statusResponseInfo: StatusResponseInfo) => {
const {

View file

@ -48,6 +48,7 @@
"@kbn/shared-ux-router",
"@kbn/core-saved-objects-server",
"@kbn/share-plugin",
"@kbn/core-http-server",
],
"exclude": [
"target/**/*",

View file

@ -6,6 +6,7 @@
*/
import expect from '@kbn/expect';
import type { CspSetupStatus } from '@kbn/cloud-security-posture-plugin/common/types';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import {
FINDINGS_INDEX_DEFAULT_NS,
LATEST_FINDINGS_INDEX_DEFAULT_NS,
@ -105,6 +106,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
@ -131,6 +133,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
@ -157,6 +160,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import type { CspSetupStatus } from '@kbn/cloud-security-posture-plugin/common/types';
import {
FINDINGS_INDEX_DEFAULT_NS,
@ -71,6 +72,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
@ -89,6 +91,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
@ -107,6 +110,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import type { CspSetupStatus } from '@kbn/cloud-security-posture-plugin/common/types';
import {
FINDINGS_INDEX_DEFAULT_NS,
@ -70,6 +71,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
@ -88,6 +90,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
@ -106,6 +109,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);

View file

@ -6,6 +6,7 @@
*/
import expect from '@kbn/expect';
import type { CspSetupStatus } from '@kbn/cloud-security-posture-plugin/common/types';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { createPackagePolicy } from '../helper';
@ -50,6 +51,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
@ -72,6 +74,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
@ -94,6 +97,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import type { CspSetupStatus } from '@kbn/cloud-security-posture-plugin/common/types';
import {
BENCHMARK_SCORE_INDEX_DEFAULT_NS,
@ -79,6 +80,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertestWithoutAuth
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.auth(UNPRIVILEGED_USERNAME, 'changeme')
.expect(200);
@ -127,6 +129,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertestWithoutAuth
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.auth(UNPRIVILEGED_USERNAME, 'changeme')
.expect(200);
@ -157,6 +160,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertestWithoutAuth
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.auth(UNPRIVILEGED_USERNAME, 'changeme')
.expect(200);
@ -190,6 +194,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertestWithoutAuth
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.auth(UNPRIVILEGED_USERNAME, 'changeme')
.expect(200);

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import type { CspSetupStatus } from '@kbn/cloud-security-posture-plugin/common/types';
import { setupFleetAndAgents } from '../../../../fleet_api_integration/apis/agents/services';
import { generateAgent } from '../../../../fleet_api_integration/helpers';
@ -87,6 +88,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
expect(res.kspm.status).to.be('waiting_for_results');
@ -112,6 +114,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
expect(res.cspm.status).to.be('waiting_for_results');
@ -137,6 +140,7 @@ export default function (providerContext: FtrProviderContext) {
const { body: res }: { body: CspSetupStatus } = await supertest
.get(`/internal/cloud_security_posture/status`)
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.set('kbn-xsrf', 'xxxx')
.expect(200);
expect(res.vuln_mgmt.status).to.be('waiting_for_results');

View file

@ -6,6 +6,7 @@
*/
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import { data, MockTelemetryFindings } from './data';
import type { FtrProviderContext } from '../ftr_provider_context';
@ -26,6 +27,7 @@ export default function ({ getService }: FtrProviderContext) {
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');

View file

@ -6,6 +6,7 @@
*/
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import type { FtrProviderContext } from '../ftr_provider_context';
// Defined in CSP plugin
@ -27,6 +28,7 @@ export function CspDashboardPageProvider({ getService, getPageObjects }: FtrProv
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');

View file

@ -6,6 +6,7 @@
*/
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import type { FtrProviderContext } from '../ftr_provider_context';
// Defined in CSP plugin
@ -28,6 +29,7 @@ export function FindingsPageProvider({ getService, getPageObjects }: FtrProvider
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');