Skip to content

Commit

Permalink
Merge branch 'main' into nt/prod-params
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemberTang committed Oct 8, 2024
2 parents d0fcdc9 + 38e4423 commit f854bf6
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 22 deletions.
65 changes: 64 additions & 1 deletion packages/cloudbuster/src/digests.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { cloudbuster_fsbp_vulnerabilities } from '@prisma/client';
import { createDigestForAccount } from './digests';
import type { SecurityHubSeverity } from 'common/types';
import { createDigestForAccount, createDigestsFromFindings } from './digests';

describe('createDigestForAccount', () => {
it('should return nothing if no vulnerabilities are passed to it', () => {
Expand All @@ -23,6 +24,7 @@ describe('createDigestForAccount', () => {
title: 'test-title',
within_sla: false,
};

it('should return a digest with the correct fields', () => {
const actual = createDigestForAccount([testVuln]);
expect(actual).toEqual({
Expand Down Expand Up @@ -57,3 +59,64 @@ Remediation: [Documentation](https://example.com)`,
expect(actual).toBeUndefined();
});
});

function mockFinding(
aws_account_id: string,
severity: SecurityHubSeverity,
): cloudbuster_fsbp_vulnerabilities {
return {
aws_account_id,
title: 'mock title',
aws_account_name: 'mock-account',
arn: 'arn::mock::123',
remediation: 'https://mock.url/mock',
severity,
within_sla: true,
first_observed_at: new Date('2020-01-01'),
control_id: 'MOCK.1',
aws_region: 'eu-mock-1',
repo: null,
stack: null,
stage: null,
app: null,
};
}

describe('createDigestsFromFindings', () => {
it('should filter findings by severity', () => {
const findings = [
mockFinding('1', 'CRITICAL'),
mockFinding('2', 'HIGH'),
mockFinding('3', 'CRITICAL'),
];
const criticalDigests = createDigestsFromFindings(findings, 'CRITICAL');
expect(criticalDigests.length).toBe(2);

Check failure on line 93 in packages/cloudbuster/src/digests.test.ts

View workflow job for this annotation

GitHub Actions / test

createDigestsFromFindings › should filter findings by severity

expect(received).toBe(expected) // Object.is equality Expected: 2 Received: 0 at Object.<anonymous> (packages/cloudbuster/src/digests.test.ts:93:34)

const highDigests = createDigestsFromFindings(findings, 'HIGH');
expect(highDigests.length).toBe(1);
});
it('should create one digest per account', () => {
const findingsFromTwoAccounts = [
mockFinding('1', 'CRITICAL'),
mockFinding('2', 'CRITICAL'),
mockFinding('3', 'CRITICAL'),
];
const result = createDigestsFromFindings(
findingsFromTwoAccounts,
'CRITICAL',
);
expect(result.length).toBe(3);

Check failure on line 108 in packages/cloudbuster/src/digests.test.ts

View workflow job for this annotation

GitHub Actions / test

createDigestsFromFindings › should create one digest per account

expect(received).toBe(expected) // Object.is equality Expected: 3 Received: 0 at Object.<anonymous> (packages/cloudbuster/src/digests.test.ts:108:25)
});
it('should combine findings of the same account and severity into one digest', () => {
const findingsFromOneAccount = [
mockFinding('1', 'CRITICAL'),
mockFinding('1', 'CRITICAL'),
mockFinding('1', 'CRITICAL'),
];
const result = createDigestsFromFindings(
findingsFromOneAccount,
'CRITICAL',
);
expect(result.length).toBe(1);

Check failure on line 120 in packages/cloudbuster/src/digests.test.ts

View workflow job for this annotation

GitHub Actions / test

createDigestsFromFindings › should combine findings of the same account and severity into one digest

expect(received).toBe(expected) // Object.is equality Expected: 1 Received: 0 at Object.<anonymous> (packages/cloudbuster/src/digests.test.ts:120:25)
});
});
6 changes: 5 additions & 1 deletion packages/cloudbuster/src/digests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RequestedChannel } from '@guardian/anghammarad';
import type { Action, Anghammarad, NotifyParams } from '@guardian/anghammarad';
import type { cloudbuster_fsbp_vulnerabilities } from '@prisma/client';
import type { SecurityHubSeverity } from 'common/src/types';
import { type Config } from './config';
import { groupFindingsByAccount } from './findings';
import type { Digest } from './types';
Expand All @@ -10,8 +11,11 @@ import type { Digest } from './types';
*/
export function createDigestsFromFindings(
findings: cloudbuster_fsbp_vulnerabilities[],
severity: SecurityHubSeverity,
): Digest[] {
const groupedFindings = groupFindingsByAccount(findings);
const filteredFindings = findings.filter((f) => f.severity === severity);

const groupedFindings = groupFindingsByAccount(filteredFindings);

return Object.keys(groupedFindings)
.map((awsAccountId) =>
Expand Down
17 changes: 7 additions & 10 deletions packages/cloudbuster/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ import { getConfig } from './config';
import { createDigestsFromFindings, sendDigest } from './digests';
import { findingsToGuardianFormat } from './findings';

type LambdaHandlerProps = {
severities?: SecurityHubSeverity[];
};

export async function main(input: LambdaHandlerProps) {
// When manually invoking the function in AWS for testing,
// it can be cumbersome to manually type this object as an input.
// Therefore, fall back to default values.
const { severities = ['CRITICAL', 'HIGH'] } = input;
export async function main() {
const severities: SecurityHubSeverity[] = ['CRITICAL', 'HIGH'];

// *** SETUP ***
const config = await getConfig();
Expand All @@ -38,8 +31,12 @@ export async function main(input: LambdaHandlerProps) {
data: tableContents,
});

const digests = createDigestsFromFindings(tableContents);
const digests = createDigestsFromFindings(tableContents, 'CRITICAL');

const isTuesday = new Date().getDay() === 2;
if (isTuesday) {
digests.push(...createDigestsFromFindings(tableContents, 'HIGH'));
}
// *** NOTIFICATION SENDING ***
const anghammaradClient = new Anghammarad();

Expand Down
5 changes: 1 addition & 4 deletions packages/cloudbuster/src/run-locally.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ config({ path: `../../.env` }); // Load `.env` file at the root of the repositor
config({ path: `${homedir()}/.gu/service_catalogue/.env.local` });

if (require.main === module) {
void main({
// Using all severities in DEV for more data.
severities: ['CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'INFORMATION'],
});
void main();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Submit dependencies
id: submit
uses: scalacenter/sbt-dependency-submission@7ebd561e5280336d3d5b445a59013810ff79325e # v3.0.1
uses: scalacenter/sbt-dependency-submission@64084844d2b0a9b6c3765f33acde2fbe3f5ae7d3 # v3.1.0
- name: Log snapshot for user validation
id: validate
run: cat` +
Expand Down Expand Up @@ -59,11 +59,11 @@ jobs:
java-version: 17
- name: Submit dependencies
id: submit
uses: gradle/actions/dependency-submission@d9c87d481d55275bb5441eef3fe0e46805f9ef70 # v3.5.0
uses: gradle/actions/dependency-submission@d156388eb19639ec20ade50009f3d199ce1e2808 # v4.1.0
- name: Log snapshot for user validation
id: validate
run: cat ` + // Need to split this line to avoid errors due to new line produced in yaml
'/home/runner/work/repo2/repo2/dependency-graph-reports/update_dependency_graph_for_kotlin-dependency-graph.json\n | jq' +
'/home/runner/work/repo2/repo2/dependency-graph-reports/update_dependency_graph_for_gradle-dependency-graph.json\n | jq' +
String.raw`
permissions:
contents: write
Expand Down
6 changes: 3 additions & 3 deletions packages/dependency-graph-integrator/src/file-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function createLanguageSpecificWorkflowSteps(
{
name: 'Submit dependencies',
id: 'submit',
uses: 'scalacenter/sbt-dependency-submission@7ebd561e5280336d3d5b445a59013810ff79325e # v3.0.1',
uses: 'scalacenter/sbt-dependency-submission@64084844d2b0a9b6c3765f33acde2fbe3f5ae7d3 # v3.1.0',
},
{
name: 'Log snapshot for user validation',
Expand All @@ -47,12 +47,12 @@ function createLanguageSpecificWorkflowSteps(
{
name: 'Submit dependencies',
id: 'submit',
uses: 'gradle/actions/dependency-submission@d9c87d481d55275bb5441eef3fe0e46805f9ef70 # v3.5.0',
uses: 'gradle/actions/dependency-submission@d156388eb19639ec20ade50009f3d199ce1e2808 # v4.1.0',
},
{
name: 'Log snapshot for user validation',
id: 'validate',
run: `cat /home/runner/work/${repo}/${repo}/dependency-graph-reports/update_dependency_graph_for_kotlin-dependency-graph.json | jq`,
run: `cat /home/runner/work/${repo}/${repo}/dependency-graph-reports/update_dependency_graph_for_gradle-dependency-graph.json | jq`,
},
],
};
Expand Down

0 comments on commit f854bf6

Please sign in to comment.