Skip to content

Commit

Permalink
[Cloud Security][serverless] metering code polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenIdo authored Jan 18, 2024
1 parent bc97065 commit b058d21
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
1 change: 1 addition & 0 deletions config/serverless.security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down Expand Up @@ -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');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
};

Expand Down

0 comments on commit b058d21

Please sign in to comment.