-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1020 from gchq/bugfix/BAI-1082-when-a-bad-connect…
…or-is-provided-no-way-to-tell-what-the-good-connectors-are Update errors for bad connectors
- Loading branch information
Showing
8 changed files
with
147 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,35 @@ | ||
import config from '../../../utils/v2/config.js' | ||
import { ConfigurationError } from '../../../utils/v2/error.js' | ||
import { BaseAuditConnector } from './Base.js' | ||
import { SillyAuditConnector } from './silly.js' | ||
import { StdoutAuditConnector } from './stdout.js' | ||
|
||
export const AuditKind = { | ||
Silly: 'silly', | ||
Stdout: 'stdout', | ||
} as const | ||
export type AuditKindKeys = (typeof AuditKind)[keyof typeof AuditKind] | ||
|
||
let auditConnector: undefined | BaseAuditConnector = undefined | ||
export function getAutheticationConnector(cache = true) { | ||
export function getAuditConnector(cache = true) { | ||
if (auditConnector && cache) { | ||
return auditConnector | ||
} | ||
|
||
switch (config.connectors.audit.kind) { | ||
case 'silly': | ||
case AuditKind.Silly: | ||
auditConnector = new SillyAuditConnector() | ||
break | ||
case 'stdout': | ||
case AuditKind.Stdout: | ||
auditConnector = new StdoutAuditConnector() | ||
break | ||
default: | ||
throw new Error('No valid audit connector provided.') | ||
throw ConfigurationError(`'${config.connectors.audit.kind}' is not a valid audit kind.`, { | ||
validKinds: Object.values(AuditKind), | ||
}) | ||
} | ||
|
||
return auditConnector | ||
} | ||
|
||
export default getAutheticationConnector() | ||
export default getAuditConnector() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
import config from '../../../utils/v2/config.js' | ||
import { ConfigurationError } from '../../../utils/v2/error.js' | ||
import { BaseAuthenticationConnector } from './Base.js' | ||
import { SillyAuthenticationConnector } from './silly.js' | ||
|
||
export const AuthenticationKind = { | ||
Silly: 'silly', | ||
} as const | ||
export type AuthenticationKindKeys = (typeof AuthenticationKind)[keyof typeof AuthenticationKind] | ||
|
||
let authenticationConnector: undefined | BaseAuthenticationConnector = undefined | ||
export function getAutheticationConnector(cache = true) { | ||
export function getAuthenticationConnector(cache = true) { | ||
if (authenticationConnector && cache) { | ||
return authenticationConnector | ||
} | ||
|
||
switch (config.connectors.authentication.kind) { | ||
case 'silly': | ||
case AuthenticationKind.Silly: | ||
authenticationConnector = new SillyAuthenticationConnector() | ||
break | ||
default: | ||
throw new Error('No valid authentication connector provided.') | ||
throw ConfigurationError(`'${config.connectors.authentication.kind}' is not a valid authentication kind.`, { | ||
validKinds: Object.values(AuthenticationKind), | ||
}) | ||
} | ||
|
||
return authenticationConnector | ||
} | ||
|
||
export default getAutheticationConnector() | ||
export default getAuthenticationConnector() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
import config from '../../../utils/v2/config.js' | ||
import { ConfigurationError } from '../../../utils/v2/error.js' | ||
import { BasicAuthorisationConnector } from './base.js' | ||
|
||
let authConnector: undefined | BasicAuthorisationConnector = undefined | ||
export const AuthorisationKind = { | ||
Basic: 'basic', | ||
} as const | ||
export type AuthorisationKindKeys = (typeof AuthorisationKind)[keyof typeof AuthorisationKind] | ||
|
||
let authorisationConnector: undefined | BasicAuthorisationConnector = undefined | ||
export function getAuthorisationConnector(cache = true): BasicAuthorisationConnector { | ||
if (authConnector && cache) { | ||
return authConnector | ||
if (authorisationConnector && cache) { | ||
return authorisationConnector | ||
} | ||
|
||
switch (config.connectors.authorisation.kind) { | ||
case 'basic': | ||
authConnector = new BasicAuthorisationConnector() | ||
case AuthorisationKind.Basic: | ||
authorisationConnector = new BasicAuthorisationConnector() | ||
break | ||
default: | ||
throw new Error('No valid authorisation connector provided.') | ||
throw ConfigurationError(`'${config.connectors.authorisation.kind}' is not a valid authorisation kind.`, { | ||
validKinds: Object.values(AuthorisationKind), | ||
}) | ||
} | ||
|
||
return authConnector | ||
return authorisationConnector | ||
} | ||
|
||
export default getAuthorisationConnector() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { describe, expect, test, vi } from 'vitest' | ||
|
||
import { getAuditConnector } from '../../../src/connectors/v2/audit/index.js' | ||
|
||
const configMock = vi.hoisted(() => ({ | ||
connectors: { | ||
audit: { | ||
kind: 'silly', | ||
}, | ||
}, | ||
})) | ||
vi.mock('../../../src/utils/v2/config.js', () => ({ | ||
__esModule: true, | ||
default: configMock, | ||
})) | ||
|
||
describe('connectors > audit', () => { | ||
test('silly', () => { | ||
const connector = getAuditConnector(false) | ||
expect(connector.constructor.name).toBe('SillyAuditConnector') | ||
}) | ||
|
||
test('stdout', () => { | ||
configMock.connectors.audit.kind = 'stdout' | ||
const connector = getAuditConnector(false) | ||
expect(connector.constructor.name).toBe('StdoutAuditConnector') | ||
}) | ||
|
||
test('invalid', () => { | ||
const invalidConnector = 'invalid' | ||
configMock.connectors.audit.kind = invalidConnector | ||
|
||
expect(() => getAuditConnector(false)).toThrowError(`'${invalidConnector}' is not a valid audit kind.`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters