Files
shields/core/base-service/errors.js
Paul Melnikow ce0ddf93fc Inject secrets into the services (#3652)
This is a reworking of #3410 based on some feedback @calebcartwright left on that PR.

The goals of injecting the secrets are threefold:

1. Simplify testing
2. Be consistent with all of the other config (which is injected)
3. Encapsulate the sensitive auth-related code in one place so it can be studied and tested thoroughly

- Rather than add more code to BaseService to handle authorization logic, it delegates that to an AuthHelper class.
- When the server starts, it fetches the credentials from `config` and injects them into `BaseService.register()` which passes them to `invoke()`.
- In `invoke()` the service's auth configuration is checked (`static get auth()`, much like `static get route()`).
- If the auth config is present, an AuthHelper instance is created and attached to the new instance.
- Then within the service, the password, basic auth config, or bearer authentication can be accessed via e.g. `this.authHelper.basicAuth` and passed to `this._requestJson()` and friends.
- Everything is being done very explicitly, so it should be very clear where and how the configured secrets are being used.
- Testing different configurations of services can now be done by injecting the config into `invoke()` in `.spec` files instead of mocking global state in the service tests as was done before. See the new Jira spec files for a good example of this.

Ref #3393
2019-07-09 23:14:36 -04:00

132 lines
2.8 KiB
JavaScript

'use strict'
class ShieldsRuntimeError extends Error {
get name() {
return 'ShieldsRuntimeError'
}
get defaultPrettyMessage() {
throw new Error('Must implement abstract method')
}
constructor(props = {}, message) {
super(message)
this.prettyMessage = props.prettyMessage || this.defaultPrettyMessage
if (props.underlyingError) {
this.stack = props.underlyingError.stack
}
}
}
const defaultNotFoundError = 'not found'
class NotFound extends ShieldsRuntimeError {
get name() {
return 'NotFound'
}
get defaultPrettyMessage() {
return defaultNotFoundError
}
constructor(props = {}) {
const prettyMessage = props.prettyMessage || defaultNotFoundError
const message =
prettyMessage === defaultNotFoundError
? 'Not Found'
: `Not Found: ${prettyMessage}`
super(props, message)
this.response = props.response
}
}
class InvalidResponse extends ShieldsRuntimeError {
get name() {
return 'InvalidResponse'
}
get defaultPrettyMessage() {
return 'invalid'
}
constructor(props = {}) {
const message = props.underlyingError
? `Invalid Response: ${props.underlyingError.message}`
: 'Invalid Response'
super(props, message)
this.response = props.response
}
}
class Inaccessible extends ShieldsRuntimeError {
get name() {
return 'Inaccessible'
}
get defaultPrettyMessage() {
return 'inaccessible'
}
constructor(props = {}) {
const message = props.underlyingError
? `Inaccessible: ${props.underlyingError.message}`
: 'Inaccessible'
super(props, message)
this.response = props.response
}
}
class ImproperlyConfigured extends ShieldsRuntimeError {
get name() {
return 'ImproperlyConfigured'
}
get defaultPrettyMessage() {
return 'improperly configured'
}
constructor(props = {}) {
const message = props.underlyingError
? `ImproperlyConfigured: ${props.underlyingError.message}`
: 'ImproperlyConfigured'
super(props, message)
this.response = props.response
}
}
class InvalidParameter extends ShieldsRuntimeError {
get name() {
return 'InvalidParameter'
}
get defaultPrettyMessage() {
return 'invalid parameter'
}
constructor(props = {}) {
const message = props.underlyingError
? `Invalid Parameter: ${props.underlyingError.message}`
: 'Invalid Parameter'
super(props, message)
this.response = props.response
}
}
class Deprecated extends ShieldsRuntimeError {
get name() {
return 'Deprecated'
}
get defaultPrettyMessage() {
return 'no longer available'
}
constructor(props) {
const message = 'Deprecated'
super(props, message)
}
}
module.exports = {
ShieldsRuntimeError,
NotFound,
ImproperlyConfigured,
InvalidResponse,
Inaccessible,
InvalidParameter,
Deprecated,
}