Skip to content

Commit

Permalink
Merge branch 'master' into feat/unified-error-output
Browse files Browse the repository at this point in the history
  • Loading branch information
aborovsky committed Oct 28, 2024
2 parents bc84307 + 6e57c57 commit b753c8d
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 391 deletions.
7 changes: 0 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ RUN set -eux; \
chmod -R g+rwX /home/node; \
chown -R 1000 /home/node

RUN set -eux; \
apk upgrade --no-cache; \
apk add --no-cache libcap; \
rm -rf /var/cache/apk/*

RUN setcap 'cap_net_raw+ep' $(which node)

# change workgin dir
WORKDIR $HOME/

Expand Down
620 changes: 300 additions & 320 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@brightsec/cli",
"version": "12.3.0",
"version": "12.6.0",
"private": false,
"repository": {
"type": "git",
Expand Down
22 changes: 20 additions & 2 deletions src/Commands/GetEntryPoints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import 'reflect-metadata';
import { Logger, logger } from '../Utils';
import { GetEntryPoints } from './GetEntryPoints';
import { EntryPoints } from '../EntryPoint';
import { mock, reset, spy, instance, when, anything, verify } from 'ts-mockito';
import {
mock,
reset,
spy,
instance,
when,
anything,
verify,
objectContaining
} from 'ts-mockito';
import { container } from 'tsyringe';
import { Arguments } from 'yargs';

Expand Down Expand Up @@ -34,14 +43,23 @@ describe('GetEntryPoints', () => {
it('should correctly pass config from args', async () => {
const args = {
project: '1',
limit: 10,
verbose: true,
pretty: true,
connectivity: ['connected', 'ok'],
status: ['open', 'closed', 'failed'],
_: [],
$0: ''
} as Arguments;

when(processSpy.exit(anything())).thenReturn(undefined);
when(
mockedEntryPoints.entrypoints({ projectId: '1', limit: 10 })
mockedEntryPoints.entrypoints(
objectContaining({
projectId: '1',
limit: 10
})
)
).thenResolve([
{
id: '1',
Expand Down
28 changes: 16 additions & 12 deletions src/Commands/GetEntryPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export class GetEntryPoints implements CommandModule {
describe: 'Limit the number of entrypoints',
default: 10
})
.option('pretty', {
describe: 'Pretty print the output',
boolean: true,
default: false
})
.option('connectivity', {
describe: 'Filter by connectivity',
array: true,
Expand All @@ -53,7 +58,8 @@ export class GetEntryPoints implements CommandModule {
insecure: args.insecure as boolean,
baseURL: args.api as string,
apiKey: args.token as string,
proxyURL: (args.proxyBright ?? args.proxy) as string
proxyURL: (args.proxyBright ?? args.proxy) as string,
timeout: args.timeout as number
}
})
);
Expand All @@ -70,20 +76,18 @@ export class GetEntryPoints implements CommandModule {
status: args.status as string[]
});

if (args.verbose) {
// eslint-disable-next-line no-console
console.log('%j', entryPoints);
} else {
// eslint-disable-next-line no-console
console.log(
'%j',
entryPoints.map((entryPoint) => ({
const ep = args.verbose
? entryPoints
: entryPoints.map((entryPoint) => ({
id: entryPoint.id,
method: entryPoint.method,
url: entryPoint.url
}))
);
}
}));

// eslint-disable-next-line no-console
console.log(
args.pretty ? JSON.stringify(ep, null, 2) : JSON.stringify(ep)
);

process.exitCode = 0;
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/PollingScanStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export class PollingScanStatus implements CommandModule {
insecure: args.insecure as boolean,
baseURL: args.api as string,
apiKey: args.token as string,
proxyURL: (args.proxyBright ?? args.proxy) as string
proxyURL: (args.proxyBright ?? args.proxy) as string,
timeout: args.timeout as number
}
})
);
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/RetestScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class RetestScan implements CommandModule {
insecure: args.insecure as boolean,
baseURL: args.api as string,
apiKey: args.token as string,
proxyURL: (args.proxyBright ?? args.proxy) as string
proxyURL: (args.proxyBright ?? args.proxy) as string,
timeout: args.timeout as number
}
})
);
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/RunRepeater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ export class RunRepeater implements CommandModule {
daemon: 'remove-daemon',
ntlm: [
'proxy',
'proxy-external',
'proxy-internal',
'proxy-bright',
'proxy-target',
'experimental-connection-reuse'
]
})
Expand Down Expand Up @@ -205,7 +205,7 @@ export class RunRepeater implements CommandModule {
useValue: {
uri: args.repeaterServer as string,
token: args.token as string,
connectTimeout: 10000,
connectTimeout: args.timeout as number,
proxyUrl: (args.proxyBright ?? args.proxy) as string,
insecure: args.insecure as boolean
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/RunScan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('RunScan', () => {

// assert
verify(processSpy.exit(1)).once();
verify(loggerSpy.error(`Error during "scan:run": ${errMessage}`)).once();
verify(loggerSpy.error(`Error during "scan:run": ${errMessage}.`)).once();
});

it('should display warnings when present', async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/RunScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ export class RunScan implements CommandModule {
insecure: args.insecure as boolean,
baseURL: args.api as string,
apiKey: args.token as string,
proxyURL: (args.proxyBright ?? args.proxy) as string
proxyURL: (args.proxyBright ?? args.proxy) as string,
timeout: args.timeout as number
}
})
);
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/StopScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class StopScan implements CommandModule {
insecure: args.insecure as boolean,
baseURL: args.api as string,
apiKey: args.token as string,
proxyURL: (args.proxyBright ?? args.proxy) as string
proxyURL: (args.proxyBright ?? args.proxy) as string,
timeout: args.timeout as number
}
})
);
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/UploadArchive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export class UploadArchive implements CommandModule {
insecure: args.insecure as boolean,
baseURL: args.api as string,
apiKey: args.token as string,
proxyURL: (args.proxyBright ?? args.proxy) as string
proxyURL: (args.proxyBright ?? args.proxy) as string,
timeout: args.timeout as number
}
});
});
Expand Down
19 changes: 16 additions & 3 deletions src/Config/CliBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SystemConfigManager } from './SystemConfigManager';
import { CliInfo } from './CliInfo';
import { Arguments, Argv, CommandModule } from 'yargs';
import { init, runWithAsyncContext, setContext } from '@sentry/node';
import ms from 'ms';

export interface CliBuilderOptions {
info: CliInfo;
Expand Down Expand Up @@ -65,19 +66,31 @@ export class CliBuilder {
'Specify a proxy URL to route all traffic through. This should be an HTTP(S), SOCKS4, or SOCKS5 URL. By default, if you specify SOCKS://<URL>, then SOCKS5h is applied.'
})
.option('proxy-bright', {
alias: 'proxy-external',
requiresArg: true,
describe:
'Specify a proxy URL to route all outbound traffic through. For more information, see the --proxy option.'
})
.option('proxy-target', {
alias: 'proxy-internal',
requiresArg: true,
describe:
'Specify a proxy URL to route all inbound traffic through. For more information, see the --proxy option.'
})
.option('timeout', {
describe:
'Request timeout in seconds or a duration string (e.g. 10s, 1m, 1h, 10h, 1y).',
default: 30,
coerce(arg: string) {
// if arg is not a number, then it's a duration string
// convert duration string to milliseconds
if (isNaN(+arg)) {
return ms(arg);
}

return +arg * 1000;
}
})
.conflicts({
proxy: ['proxy-internal', 'proxy-external'],
proxy: ['proxy-bright', 'proxy-target'],
hostname: 'cluster'
})
.middleware((args: Arguments) => {
Expand Down
8 changes: 1 addition & 7 deletions src/EntryPoint/RestEntryPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ export class RestEntryPoints implements EntryPoints {
constructor(
@inject(ProxyFactory) private readonly proxyFactory: ProxyFactory,
@inject(RestProjectsOptions)
{
baseURL,
apiKey,
insecure,
proxyURL,
timeout = 10000
}: RestProjectsOptions
{ baseURL, apiKey, insecure, proxyURL, timeout }: RestProjectsOptions
) {
const {
httpAgent = new http.Agent(),
Expand Down
31 changes: 5 additions & 26 deletions src/Scan/BasePolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Polling } from './Polling';
import { Breakpoint } from './Breakpoint';
import { Backoff, logger } from '../Utils';
import { PollingConfig } from './PollingFactory';
import ms from 'ms';
import axios from 'axios';
import { ok } from 'node:assert';

Expand Down Expand Up @@ -33,9 +32,7 @@ export class BasePolling implements Polling {
}

if (this.options.interval) {
const interval = this.toMilliseconds(this.options.interval);

if (interval < this.defaultInterval) {
if (this.options.interval < this.defaultInterval) {
logger.warn(`Warning: polling interval is too small.`);
logger.warn(`The recommended way to set polling interval to 10s.`);
}
Expand Down Expand Up @@ -69,13 +66,9 @@ export class BasePolling implements Polling {
clearTimeout(this.timeoutDescriptor);
}

private setTimeout(timeout: number | string = this.options.timeout): void {
const timeoutInMs: number = this.toMilliseconds(timeout);
this.timeoutDescriptor = setTimeout(
() => (this._active = false),
timeoutInMs
);
logger.debug(`The polling timeout has been set to %d ms.`, timeoutInMs);
private setTimeout(timeout: number = this.options.timeout): void {
this.timeoutDescriptor = setTimeout(() => (this._active = false), timeout);
logger.debug(`The polling timeout has been set to %d ms.`, timeout);
}

private async *poll(): AsyncIterableIterator<ScanState> {
Expand All @@ -96,19 +89,6 @@ export class BasePolling implements Polling {
}
}

private toMilliseconds(time: string | number): number {
if (typeof time === 'string') {
const milliseconds = ms(time);
if (!milliseconds) {
return;
}

return milliseconds;
} else if (typeof time === 'number') {
return time;
}
}

private isRedundant(status: ScanStatus): boolean {
return (
status === ScanStatus.DONE ||
Expand All @@ -119,8 +99,7 @@ export class BasePolling implements Polling {
}

private delay(): Promise<void> {
const interval =
this.toMilliseconds(this.options.interval) ?? this.defaultInterval;
const interval = this.options.interval ?? this.defaultInterval;

return new Promise<void>((resolve) => setTimeout(resolve, interval));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Scan/PollingFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Polling } from './Polling';
import { BreakpointType } from './BreakpointType';

export interface PollingConfig {
timeout?: number | string;
timeout?: number;
interval?: number;
breakpoint: BreakpointType;
scanId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/Scan/RestScans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class RestScans implements Scans {
@inject(delay(() => CliInfo)) private readonly info: CliInfo,
@inject(ProxyFactory) private readonly proxyFactory: ProxyFactory,
@inject(RestScansOptions)
{ baseURL, apiKey, insecure, proxyURL, timeout = 10000 }: RestScansOptions
{ baseURL, apiKey, insecure, proxyURL, timeout }: RestScansOptions
) {
const {
httpAgent = new http.Agent(),
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/ErrorMessageBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export class ErrorMessageBuilder {
| { error: unknown; message: string }
| { error: any; command: string }
): string {
const message = '';
'message' in params ? params.message : `Error during "${params.command}"`;
const message =
'message' in params ? params.message : `Error during "${params.command}"`;

const errMessage =
typeof params.error === 'string'
Expand Down
1 change: 1 addition & 0 deletions src/Utils/Helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ describe('Helpers', () => {
expect(actual).toEqual([TestEnum.TEST, TestEnum.PROD]);
});
});

describe('parseHeaders', () => {
it('should return empty object', () => {
// arrange
Expand Down

0 comments on commit b753c8d

Please sign in to comment.