mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[Cloud Security][serverless] metering code polishing
This commit is contained in:
parent
bc9706577e
commit
b058d21e84
5 changed files with 28 additions and 20 deletions
|
@ -16,6 +16,7 @@ xpack.securitySolutionServerless.productTypes:
|
|||
[
|
||||
{ product_line: 'security', product_tier: 'complete' },
|
||||
{ product_line: 'endpoint', product_tier: 'complete' },
|
||||
{ product_line: 'cloud', product_tier: 'complete' },
|
||||
]
|
||||
|
||||
xpack.securitySolution.offeringSettings: {
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
import type { Logger } from '@kbn/core/server';
|
||||
import { ProductLine } from '../../common/product';
|
||||
import { getCloudSecurityUsageRecord } from './cloud_security_metering_task';
|
||||
import { CLOUD_DEFEND, CNVM, CSPM, KSPM } from './constants';
|
||||
|
@ -21,16 +22,12 @@ export const cloudSecurityMetringCallback = async ({
|
|||
}: MeteringCallbackInput): Promise<UsageRecord[]> => {
|
||||
const projectId = cloudSetup?.serverless?.projectId || 'missing_project_id';
|
||||
|
||||
if (!cloudSetup?.serverless?.projectId) {
|
||||
logger.error('no project id found');
|
||||
}
|
||||
|
||||
const tier: Tier = getCloudProductTier(config);
|
||||
const tier: Tier = getCloudProductTier(config, logger);
|
||||
|
||||
try {
|
||||
const cloudSecuritySolutions: CloudSecuritySolutions[] = [CSPM, KSPM, CNVM, CLOUD_DEFEND];
|
||||
|
||||
const cloudSecurityUsageRecords = await Promise.all(
|
||||
const promiseResults = await Promise.allSettled(
|
||||
cloudSecuritySolutions.map((cloudSecuritySolution) =>
|
||||
getCloudSecurityUsageRecord({
|
||||
esClient,
|
||||
|
@ -44,21 +41,33 @@ export const cloudSecurityMetringCallback = async ({
|
|||
)
|
||||
);
|
||||
|
||||
// remove any potential undefined values from the array,
|
||||
return cloudSecurityUsageRecords
|
||||
.filter((record) => record !== undefined && record.length > 0)
|
||||
.flatMap((record) => record) as UsageRecord[];
|
||||
const cloudSecurityUsageRecords: UsageRecord[] = [];
|
||||
promiseResults.forEach((result) => {
|
||||
if (result.status === 'fulfilled') {
|
||||
if (result.value !== undefined && result.value.length > 0) {
|
||||
cloudSecurityUsageRecords.push(...result.value);
|
||||
}
|
||||
} else {
|
||||
// Handle or log the rejection reason
|
||||
logger.error(`Promise rejected with reason: ${result.reason}`);
|
||||
}
|
||||
});
|
||||
|
||||
return cloudSecurityUsageRecords;
|
||||
} catch (err) {
|
||||
logger.error(`Failed to fetch Cloud Security metering data ${err}`);
|
||||
logger.error(`Failed to process Cloud Security metering data ${err}`);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export const getCloudProductTier = (config: ServerlessSecurityConfig): Tier => {
|
||||
export const getCloudProductTier = (config: ServerlessSecurityConfig, logger: Logger): Tier => {
|
||||
const cloud = config.productTypes.find(
|
||||
(productType) => productType.product_line === ProductLine.cloud
|
||||
);
|
||||
const tier = cloud ? cloud.product_tier : 'none';
|
||||
if (tier === 'none') {
|
||||
logger.error(`Failed to fetch cloud product tier, config: ${JSON.stringify(config)}`);
|
||||
}
|
||||
|
||||
return tier;
|
||||
};
|
||||
|
|
|
@ -167,7 +167,7 @@ describe('should return the relevant product tier', () => {
|
|||
],
|
||||
} as unknown as ServerlessSecurityConfig;
|
||||
|
||||
const tier = getCloudProductTier(serverlessSecurityConfig);
|
||||
const tier = getCloudProductTier(serverlessSecurityConfig, logger);
|
||||
|
||||
expect(tier).toBe('complete');
|
||||
});
|
||||
|
@ -273,7 +273,7 @@ describe('should return the relevant product tier', () => {
|
|||
productTypes: [{ product_line: 'endpoint', product_tier: 'complete' }],
|
||||
} as unknown as ServerlessSecurityConfig;
|
||||
|
||||
const tier = getCloudProductTier(serverlessSecurityConfig);
|
||||
const tier = getCloudProductTier(serverlessSecurityConfig, logger);
|
||||
|
||||
expect(tier).toBe('none');
|
||||
});
|
||||
|
|
|
@ -222,14 +222,12 @@ const getSearchStartDate = (lastSuccessfulReport: Date): Date => {
|
|||
const initialDate = new Date();
|
||||
const thresholdDate = new Date(initialDate.getTime() - THRESHOLD_MINUTES * 60 * 1000);
|
||||
|
||||
let lastSuccessfulReport1;
|
||||
|
||||
if (lastSuccessfulReport) {
|
||||
lastSuccessfulReport1 = new Date(lastSuccessfulReport);
|
||||
const lastSuccessfulReportDate = new Date(lastSuccessfulReport);
|
||||
|
||||
const searchFrom =
|
||||
lastSuccessfulReport && lastSuccessfulReport1 > thresholdDate
|
||||
? lastSuccessfulReport1
|
||||
lastSuccessfulReport && lastSuccessfulReportDate > thresholdDate
|
||||
? lastSuccessfulReportDate
|
||||
: thresholdDate;
|
||||
return searchFrom;
|
||||
}
|
||||
|
|
|
@ -102,7 +102,7 @@ export class SecurityUsageReportingTask {
|
|||
params: { version: this.version },
|
||||
});
|
||||
} catch (e) {
|
||||
this.logger.debug(`Error scheduling task, received ${e.message}`);
|
||||
this.logger.error(`Error scheduling task ${this.taskType}, received ${e.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue