Skip to content

Commit

Permalink
feat(*): rename cluster option to hostname in the Configuration
Browse files Browse the repository at this point in the history
closes #101
  • Loading branch information
derevnjuk committed May 25, 2022
1 parent 05e185b commit 375d9a6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/bus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To use the RabbitMQ Event Bus, pass the following options object to the construc
import { RMQEventBus, ExponentialBackoffRetryStrategy } from '@sec-tester/bus';

const config = new Configuration({
cluster: 'app.neuralegion.com'
hostname: 'app.neuralegion.com'
});

const repeaterId = 'your Repeater ID';
Expand Down
10 changes: 5 additions & 5 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ First, you need to generate a new instance of `Configuration`.
import { Configuration } from '@sec-tester/core';

const config = new Configuration({
cluster: 'app.neuralegion.com',
hostname: 'app.neuralegion.com',
credentials: {
token: 'your API key'
}
Expand All @@ -42,7 +42,7 @@ Configuration can be customized using the following options:

```ts
export interface ConfigurationOptions {
cluster: string;
hostname: string;
credentials?: Credentials;
logLevel?: LogLevel;
credentialProviders?: CredentialProvider[];
Expand All @@ -58,7 +58,7 @@ The default configuration is as follows:
}
```

#### cluster
#### hostname

- type: `string`

Expand All @@ -68,7 +68,7 @@ Set the application name (domain name), that is used to establish connection wit
import { Configuration } from '@sec-tester/core';

const config = new Configuration({
cluster: 'app.neuralegion.com'
hostname: 'app.neuralegion.com'
});
```

Expand All @@ -82,7 +82,7 @@ What level of logs to report. Any logs of a higher level than the setting are sh
import { Configuration, LogLevel } from '@sec-tester/core';

const config = new Configuration({
cluster: 'app.neuralegion.com',
hostname: 'app.neuralegion.com',
logLevel: LogLevel.ERROR
});
```
Expand Down
30 changes: 15 additions & 15 deletions packages/core/src/configuration/Configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ describe('Configuration', () => {
describe('constructor', () => {
it('should be a single instance', () => {
const configuration = new Configuration({
cluster: 'example.com'
hostname: 'example.com'
});
const configuration2 = configuration.container.resolve(Configuration);
expect(configuration).toBe(configuration2);
});

it('should throw if cluster is not passed', () =>
it('should throw if hostname is not passed', () =>
expect(
() =>
new Configuration({
cluster: ''
hostname: ''
})
).toThrow());

it('should throw an error if credentials or credential providers are not passed', () =>
expect(
() =>
new Configuration({
cluster: 'example.com',
hostname: 'example.com',
credentialProviders: []
})
).toThrow());

it('should return an expected name', () => {
const configuration = new Configuration({
cluster: 'example.com'
hostname: 'example.com'
});
const pathToRootPackageJson = resolve(
__dirname,
Expand All @@ -53,7 +53,7 @@ describe('Configuration', () => {

it('should return an expected version', () => {
const configuration = new Configuration({
cluster: 'example.com'
hostname: 'example.com'
});
const pathToPackageJson = resolve(__dirname, '../../package.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -66,7 +66,7 @@ describe('Configuration', () => {

it('should return an expected repeater version', () => {
const configuration = new Configuration({
cluster: 'example.com'
hostname: 'example.com'
});
const pathToPackageJson = resolve(__dirname, '../../package.json');
const {
Expand All @@ -81,7 +81,7 @@ describe('Configuration', () => {

it('should use options with default values', () => {
const config = new Configuration({
cluster: 'example.com'
hostname: 'example.com'
});

expect(config).toMatchObject({
Expand Down Expand Up @@ -156,20 +156,20 @@ describe('Configuration', () => {
'should generate correct api and bus for $input',
({ expected, input }) => {
const configuration = new Configuration({
cluster: input
hostname: input
});

expect(configuration).toMatchObject(expected);
}
);

it('should throw an error if cluster is wrong', () => {
it('should throw an error if hostname is wrong', () => {
expect(
() =>
new Configuration({
cluster: ':test'
hostname: ':test'
})
).toThrow("pass correct 'cluster' option");
).toThrow("pass correct 'hostname' option");
});
});

Expand All @@ -180,7 +180,7 @@ describe('Configuration', () => {
};
const configuration = new Configuration({
credentials,
cluster: 'app.neuralegion.com'
hostname: 'app.neuralegion.com'
});

await configuration.loadCredentials();
Expand All @@ -193,7 +193,7 @@ describe('Configuration', () => {
token: 'weobbz5.nexa.vennegtzr2h7urpxgtksetz2kwppdgj0'
};
const configuration = new Configuration({
cluster: 'app.neuralegion.com',
hostname: 'app.neuralegion.com',
credentialProviders: [instance(mockedProvider)]
});
when(mockedProvider.get()).thenResolve(credentials);
Expand All @@ -206,7 +206,7 @@ describe('Configuration', () => {

it('should throw an error if no one provider does not find credentials', async () => {
const configuration = new Configuration({
cluster: 'app.neuralegion.com',
hostname: 'app.neuralegion.com',
credentialProviders: [instance(mockedProvider)]
});
when(mockedProvider.get()).thenResolve(undefined);
Expand Down
27 changes: 14 additions & 13 deletions packages/core/src/configuration/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { version, secTester } from '../../package.json';
import { container, injectable } from 'tsyringe';

export interface ConfigurationOptions {
cluster: string;
hostname: string;
logLevel?: LogLevel;
credentials?: Credentials | CredentialsOptions;
credentialProviders?: CredentialProvider[];
Expand All @@ -19,7 +19,7 @@ export interface ConfigurationOptions {
@injectable()
export class Configuration {
private readonly SCHEMA_REGEXP = /^.+:\/\//;
private readonly CLUSTER_NORMALIZATION_REGEXP = /^(?!(?:\w+:)?\/\/)|^\/\//;
private readonly HOSTNAME_NORMALIZATION_REGEXP = /^(?!(?:\w+:)?\/\/)|^\/\//;

private _credentialProviders?: CredentialProvider[];

Expand Down Expand Up @@ -73,7 +73,7 @@ export class Configuration {
}

constructor({
cluster,
hostname,
credentials,
logLevel = LogLevel.ERROR,
credentialProviders = [new EnvCredentialProvider()]
Expand All @@ -90,11 +90,11 @@ export class Configuration {

this._credentialProviders = credentialProviders;

if (!cluster) {
throw new Error(`Please provide 'cluster' option.`);
if (!hostname) {
throw new Error(`Please provide 'hostname' option.`);
}

this.resolveUrls(cluster);
this.resolveUrls(hostname);

this._logLevel = logLevel;

Expand All @@ -116,18 +116,19 @@ export class Configuration {
}
}

private resolveUrls(cluster: string): void {
if (!this.SCHEMA_REGEXP.test(cluster)) {
cluster = cluster.replace(this.CLUSTER_NORMALIZATION_REGEXP, 'https://');
private resolveUrls(hostname: string): void {
if (!this.SCHEMA_REGEXP.test(hostname)) {
hostname = hostname.replace(
this.HOSTNAME_NORMALIZATION_REGEXP,
'https://'
);
}

let hostname = cluster;

try {
({ hostname } = new URL(cluster));
({ hostname } = new URL(hostname));
} catch {
throw new Error(
`Please make sure that you pass correct 'cluster' option.`
`Please make sure that you pass correct 'hostname' option.`
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/repeater/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To establish a secure connection between the Bright cloud engine and a target on

```ts
const configuration = new Configuration({
cluster: 'app.neuralegion.com'
hostname: 'app.neuralegion.com'
});

const repeaterFactory = new RepeaterFactory(configuration);
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('Scan', () => {

beforeAll(async () => {
const configuration = new Configuration({
cluster: 'app.neuralegion.com'
hostname: 'app.neuralegion.com'
});

repeater = await new RepeaterFactory(configuration).createRepeater();
Expand Down
10 changes: 5 additions & 5 deletions packages/runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ Then put obtained token into `BRIGHT_TOKEN` environment variable to make it acce

> Refer to `@sec-tester/core` package [documentation](https://github.com/NeuraLegion/sec-tester-js/tree/master/packages/core#credentials) for the details on alternative ways of configuring credential providers.
Once it is done, create a configuration object. Single required option is NeuraLegion `cluster` domain you are going to use, e.g. `app.neuralegion.com` as the main one:
Once it is done, create a configuration object. Single required option is NeuraLegion `hostname` domain you are going to use, e.g. `app.neuralegion.com` as the main one:

```ts
import { Configuration } from '@sec-tester/core';

const configuration = new Configuration({ cluster: 'app.neuralegion.com' });
const configuration = new Configuration({ hostname: 'app.neuralegion.com' });
```

### Setup runner
Expand All @@ -39,12 +39,12 @@ To set up a runner, create `SecRunner` instance passing a previously created con
import { Configuration } from '@sec-tester/core';
import { SecRunner } from '@sec-tester/runner';

const configuration = new Configuration({ cluster: 'app.neuralegion.com' });
const configuration = new Configuration({ hostname: 'app.neuralegion.com' });
const runner = new SecRunner(configuration);

// or

const runner2 = new SecRunner({ cluster: 'app.neuralegion.com' });
const runner2 = new SecRunner({ hostname: 'app.neuralegion.com' });
```

After that, you have to initialize a `SecRunner` instance:
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('/api', () => {
let scan!: SecScan;

beforeEach(async () => {
runner = new SecRunner({ cluster: 'app.neuralegion.com' });
runner = new SecRunner({ hostname: 'app.neuralegion.com' });

await runner.init();

Expand Down
2 changes: 1 addition & 1 deletion packages/scan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Configuration } from '@sec-tester/core';
import { ScanFactory } from '@sec-tester/scan';

const config = new Configuration({
cluster: 'app.neuralegion.com'
hostname: 'app.neuralegion.com'
});

const scanFactory = new ScanFactory(config);
Expand Down

0 comments on commit 375d9a6

Please sign in to comment.