Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scan): print warnings on scan run #544

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 22 additions & 0 deletions src/Commands/RunScan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,26 @@ describe('RunScan', () => {
expect(act).toThrowError(SyntaxError);
});
});

describe('serializeWarnings', () => {
denis-maiorov-brightsec marked this conversation as resolved.
Show resolved Hide resolved
it('should print warnings correctly', () => {
// arrange
const input = [
{
message: 'Notice: Warning 1',
code: '1'
},
{
message: 'Notice: Warning 2',
code: '2'
}
];

// act
const result = RunScan.serializeWarnings(input);

// assert
expect(result).toEqual('Notice: Warning 1\nNotice: Warning 2');
});
});
});
13 changes: 11 additions & 2 deletions src/Commands/RunScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
ScanConfig,
Scans,
TestType,
ATTACK_PARAM_LOCATIONS_DEFAULT
ATTACK_PARAM_LOCATIONS_DEFAULT,
ScanWarning
} from '../Scan';
import { Helpers, logger } from '../Utils';
import { Arguments, Argv, CommandModule } from 'yargs';
Expand Down Expand Up @@ -37,6 +38,10 @@ export class RunScan implements CommandModule {
});
}

public static serializeWarnings(warnings: ScanWarning[]): string {
denis-maiorov-brightsec marked this conversation as resolved.
Show resolved Hide resolved
return `${warnings.map((warning) => warning.message).join('\n')}`;
}

public builder(argv: Argv): Argv {
return argv
.option('token', {
Expand Down Expand Up @@ -174,7 +179,7 @@ export class RunScan implements CommandModule {
try {
const scanManager: Scans = container.resolve(Scans);

const scanId: string = await scanManager.create({
const { id: scanId, warnings = [] } = await scanManager.create({
tests: args.test,
name: args.name,
module: args.module,
Expand All @@ -198,6 +203,10 @@ export class RunScan implements CommandModule {
// eslint-disable-next-line no-console
console.log(scanId);

if (warnings.length) {
logger.warn(RunScan.serializeWarnings(warnings));
}

process.exit(0);
} catch (e) {
logger.error(`Error during "scan:run": ${e.error || e.message}`);
Expand Down
2 changes: 1 addition & 1 deletion src/Scan/RestScans.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('RestScans', () => {
const result = await restScans.create(scanConfig);

// assert
expect(result).toEqual(postResponse.id);
expect(result).toEqual({ id: postResponse.id });
expect(parsedBody).toMatchObject({
discoveryTypes: expect.arrayContaining<Discovery>([expected])
});
Expand Down
9 changes: 5 additions & 4 deletions src/Scan/RestScans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
SourceType,
StorageFile,
SCAN_TESTS_TO_RUN_BY_DEFAULT,
ATTACK_PARAM_LOCATIONS_DEFAULT
ATTACK_PARAM_LOCATIONS_DEFAULT,
ScanCreateResponse
} from './Scans';
import { CliInfo } from '../Config';
import { ProxyFactory } from '../Utils';
Expand Down Expand Up @@ -56,15 +57,15 @@ export class RestScans implements Scans {
});
}

public async create(body: ScanConfig): Promise<string> {
public async create(body: ScanConfig): Promise<ScanCreateResponse> {
const scanConfig = await this.prepareScanConfig({ ...body });

const res = await this.client.post<{ id: string }>(
const res = await this.client.post<ScanCreateResponse>(
'/api/v1/scans',
scanConfig
);

return res.data.id;
return res.data;
}

public async retest(scanId: string): Promise<string> {
Expand Down
12 changes: 11 additions & 1 deletion src/Scan/Scans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,18 @@ export interface StorageFile {
type: SourceType;
}

export interface ScanWarning {
code: string;
message: string;
}

export interface ScanCreateResponse {
id: string;
warnings: ScanWarning[];
}

export interface Scans {
create(body: ScanConfig): Promise<string>;
create(body: ScanConfig): Promise<ScanCreateResponse>;

retest(scanId: string): Promise<string>;

Expand Down
Loading