Skip to content

Commit

Permalink
feat: add get-entrypoints command
Browse files Browse the repository at this point in the history
  • Loading branch information
maksadbek committed Jun 26, 2024
1 parent 0d900f9 commit 3d7697c
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 2 deletions.
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]';
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);
} else {
// eslint-disable-next-line no-console
console.log(
entryPoints.map((entryPoint) => ({
id: entryPoint.id,
method: entryPoint.method,
url: entryPoint.url
}))
);
}

process.exit(0);
} catch (e) {
logger.error(`Error during "get-entrypoints": ${e.error || e.message}`);
process.exit(1);
}
}
}
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;

0 comments on commit 3d7697c

Please sign in to comment.