Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Customize sensors max days in past #170

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export const instanceConfigFields: IntegrationInstanceConfigFieldMap = {
mask: false,
optional: true,
},
sensorsMaxDaysInPast: {
type: 'string',
mask: false,
optional: true,
},
};

export interface IntegrationConfig extends IntegrationInstanceConfig {
Expand All @@ -71,6 +76,17 @@ export interface IntegrationConfig extends IntegrationInstanceConfig {
vulnerabilitiesLimit?: string;
devicesLimit?: string;
vulnerabilitiesMaxDaysInPast?: string;
sensorsMaxDaysInPast?: string;
}

function validateMaxDaysInPastValue(maxDaysInPastValue: number) {
if (
!isFinite(maxDaysInPastValue) ||
isNaN(maxDaysInPastValue) ||
maxDaysInPastValue <= 0
) {
throw new Error();
}
}

export async function validateInvocation({
Expand All @@ -92,14 +108,27 @@ export async function validateInvocation({
}

if (instance.config.vulnerabilitiesMaxDaysInPast) {
const number = Number(instance.config.vulnerabilitiesMaxDaysInPast);
if (!isFinite(number) || isNaN(number) || number <= 0) {
try {
validateMaxDaysInPastValue(
Number(instance.config.vulnerabilitiesMaxDaysInPast),
);
} catch (err) {
throw new IntegrationValidationError(
`Invalid vulnerabilitiesMaxDaysInPast: "${instance.config.vulnerabilitiesMaxDaysInPast}"`,
);
}
}

if (instance.config.sensorsMaxDaysInPast) {
try {
validateMaxDaysInPastValue(Number(instance.config.sensorsMaxDaysInPast));
} catch (err) {
throw new IntegrationValidationError(
`Invalid sensorsMaxDaysInPast: "${instance.config.sensorsMaxDaysInPast}"`,
);
}
}

const client = getOrCreateFalconAPIClient(instance.config, logger);
try {
await client.authenticate();
Expand Down
6 changes: 5 additions & 1 deletion src/steps/devices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ async function fetchDevices({

logger.info('Iterating devices...');

const timestamp = createFQLTimestamp(getDateInPast(50));
const maxDaysInPast = instance.config.sensorsMaxDaysInPast
? Number(instance.config.sensorsMaxDaysInPast)
: 50;

const timestamp = createFQLTimestamp(getDateInPast(maxDaysInPast));

await client.iterateDevices({
query: {
Expand Down
26 changes: 26 additions & 0 deletions src/validateInvocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,30 @@ describe('#validateInvocation', () => {
`Invalid vulnerabilitiesMaxDaysInPast: "I am not a number im just text"`,
);
});

test('fails to validate sensorsMaxDaysInPast contains invalid number', async () => {
const executionContext = createMockExecutionContext<IntegrationConfig>({
instanceConfig: {
...config,
sensorsMaxDaysInPast: '-1',
},
});

await expect(validateInvocation(executionContext)).rejects.toThrow(
`Invalid sensorsMaxDaysInPast: "-1"`,
);
});

test('fails to validate sensorsMaxDaysInPast is not a number', async () => {
const executionContext = createMockExecutionContext<IntegrationConfig>({
instanceConfig: {
...config,
sensorsMaxDaysInPast: 'I am not a number im just text',
},
});

await expect(validateInvocation(executionContext)).rejects.toThrow(
`Invalid sensorsMaxDaysInPast: "I am not a number im just text"`,
);
});
});