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(entrypoints): add command to list entrypoints #555

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 69 additions & 0 deletions src/Commands/GetEntryPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { EntryPoint, EntryPoints, RestProjectsOptions } from '../EntryPoint';
import { logger } from '../Utils';
import { Arguments, Argv, CommandModule } from 'yargs';
import { container } from 'tsyringe';

export class GetEntryPoints implements CommandModule {
public readonly command = 'get-entrypoints [options]';
derevnjuk marked this conversation as resolved.
Show resolved Hide resolved
public readonly describe = 'get all entrypoints of the project.';

public builder(argv: Argv): Argv {
return argv
.option('token', {
alias: 't',
describe: 'Bright API-key',
requiresArg: true,
demandOption: true
})
.option('project', {
alias: 'p',
describe: 'ID of the project',
requiresArg: true,
demandOption: true
})
.option('verbose', {
describe: 'Enable verbose mode',
boolean: true,
default: false
})
.middleware((args: Arguments) =>
container.register<RestProjectsOptions>(RestProjectsOptions, {
useValue: {
insecure: args.insecure as boolean,
baseURL: args.api as string,
apiKey: args.token as string,
proxyURL: (args.proxyExternal ?? args.proxy) as string
}
})
);
}

public async handler(args: Arguments): Promise<void> {
const entryPointsManager: EntryPoints = container.resolve(EntryPoints);

try {
const entryPoints: EntryPoint[] = await entryPointsManager.entrypoints(
args.project as string
);

if (args.verbose) {
// eslint-disable-next-line no-console
console.log(entryPoints);
derevnjuk marked this conversation as resolved.
Show resolved Hide resolved
} else {
// eslint-disable-next-line no-console
console.log(
entryPoints.map((entryPoint) => ({
id: entryPoint.id,
method: entryPoint.method,
url: entryPoint.url
}))
);
derevnjuk marked this conversation as resolved.
Show resolved Hide resolved
}

process.exit(0);
derevnjuk marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
logger.error(`Error during "get-entrypoints": ${e.error || e.message}`);
process.exit(1);
derevnjuk marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
1 change: 1 addition & 0 deletions src/Commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export { StopScan } from './StopScan';
export { PollingScanStatus } from './PollingScanStatus';
export { RunRepeater } from './RunRepeater';
export { Configure } from './Configure';
export { GetEntryPoints } from './GetEntryPoints';
6 changes: 6 additions & 0 deletions src/Config/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
RestScans,
Scans
} from '../Scan';
import { EntryPoints, RestEntryPoints } from '../EntryPoint';
import {
Archives,
DefaultParserFactory,
Expand Down Expand Up @@ -166,6 +167,11 @@ container
{ lifecycle: Lifecycle.Singleton }
)
.register(Scans, { useClass: RestScans }, { lifecycle: Lifecycle.Singleton })
.register(
EntryPoints,
{ useClass: RestEntryPoints },
{ lifecycle: Lifecycle.Singleton }
)
.register(
Archives,
{ useClass: RestArchives },
Expand Down
22 changes: 22 additions & 0 deletions src/EntryPoint/EntryPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface EntryPoints {
entrypoints(projectId: string): Promise<EntryPoint[]>;
}

export const EntryPoints: unique symbol = Symbol('EntryPoints');

export interface EntryPoint {
id: string;
method: string;
url: string;
responseStatus: number;
connectivity: string;
lastUpdated: string;
lastEdited: string;
lastValidated: string;
parametersCount: number;
responseTime: number;
status: string;
openIssuesCount: number;
closedIssuesCount: number;
createdAt: string;
}
60 changes: 60 additions & 0 deletions src/EntryPoint/RestEntryPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { EntryPoints, EntryPoint } from './EntryPoints';
import { ProxyFactory } from '../Utils';
import axios, { Axios } from 'axios';
import { inject, injectable } from 'tsyringe';
import http from 'node:http';
import https from 'node:https';

export interface RestProjectsOptions {
baseURL: string;
apiKey: string;
timeout?: number;
insecure?: boolean;
proxyURL?: string;
}

export const RestProjectsOptions: unique symbol = Symbol('RestProjectsOptions');

@injectable()
export class RestEntryPoints implements EntryPoints {
private readonly client: Axios;

constructor(
@inject(ProxyFactory) private readonly proxyFactory: ProxyFactory,
@inject(RestProjectsOptions)
{
baseURL,
apiKey,
insecure,
proxyURL,
timeout = 10000
}: RestProjectsOptions
) {
const {
httpAgent = new http.Agent(),
httpsAgent = new https.Agent({ rejectUnauthorized: !insecure })
} = proxyURL
? this.proxyFactory.createProxy({
proxyUrl: proxyURL,
rejectUnauthorized: !insecure
})
: {};

this.client = axios.create({
baseURL,
timeout,
httpAgent,
httpsAgent,
responseType: 'json',
headers: { authorization: `Api-Key ${apiKey}` }
});
}

public async entrypoints(projectId: string): Promise<EntryPoint[]> {
const res = await this.client.get(
`/api/v2/projects/${projectId}/entry-points`
);

return res.data.items;
}
}
2 changes: 2 additions & 0 deletions src/EntryPoint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './EntryPoints';
export * from './RestEntryPoints';
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
StopScan,
UploadArchive,
VersionCommand,
Configure
Configure,
GetEntryPoints
} from './Commands';
import { CliBuilder, container } from './Config';

Expand All @@ -24,6 +25,7 @@ container.resolve(CliBuilder).build({
new RetestScan(),
new StopScan(),
new UploadArchive(),
new Configure()
new Configure(),
new GetEntryPoints()
]
}).argv;
Loading