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

Validate the configuration path for Google Cloud Storage at start time #73

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
37 changes: 36 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow

import fs from 'fs';
import convict from 'convict';
import { getLogger } from './log';

const log = getLogger('config');

class ConfigurationError extends Error {
name = 'ConfigurationError';
}

function loadConfig() {
const conf = convict({
env: {
Expand Down Expand Up @@ -54,7 +59,37 @@ function loadConfig() {

conf.validate();

return conf.getProperties();
const resolvedConfig = conf.getProperties();

validateConfigValues(resolvedConfig);

return resolvedConfig;
}

// This checks if the configured values are valid, and throws otherwise.
function validateConfigValues(config: Config) {
const { googleAuthenticationFilePath } = config;

if (
googleAuthenticationFilePath &&
googleAuthenticationFilePath !== 'MOCKED'
) {
// If some other value is set, we check it ahead of time so that the lib
// doesn't output an error later.
// Note that an empty string is a valid value: in that case the lib will try
// to find the authentication information from other means.
try {
fs.accessSync(googleAuthenticationFilePath, fs.constants.R_OK);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the synchronous version of the function because this is running at start time.

} catch (e) {
log.critical(
'gcs_configuration_error',
`The authentication file '${googleAuthenticationFilePath}' is missing or not readable.`
);
throw new ConfigurationError(
`The authentication file is missing or not readable.`
);
}
}
}

type Config = {|
Expand Down
32 changes: 32 additions & 0 deletions test/unit/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow

import fs from 'fs';
import { config, loadConfigForTestsOnly as loadConfig } from '../../src/config';

describe('config.js', () => {
Expand Down Expand Up @@ -35,4 +37,34 @@ describe('config.js', () => {
expect(() => loadConfig()).toThrow();
process.env.NODE_ENV = 'test';
});

it('accepts mocked google authentication paths', () => {
process.env.GCS_AUTHENTICATION_PATH = 'MOCKED';
expect(loadConfig().googleAuthenticationFilePath).toEqual('MOCKED');
delete process.env.GCS_AUTHENTICATION_PATH;
});

it('accepts valid google authentication paths', () => {
const filePath = '/path/to/file';
jest.spyOn(fs, 'accessSync').mockReturnValue();
process.env.GCS_AUTHENTICATION_PATH = filePath;
expect(loadConfig().googleAuthenticationFilePath).toEqual(filePath);
delete process.env.GCS_AUTHENTICATION_PATH;
});

it('rejects google authentication paths that are not readable', () => {
const filePath = '/path/to/file';
jest.spyOn(process.stdout, 'write').mockImplementation(() => {});
jest.spyOn(fs, 'accessSync').mockImplementation(() => {
throw new Error();
});
process.env.GCS_AUTHENTICATION_PATH = filePath;
expect(() => loadConfig()).toThrow(
'The authentication file is missing or not readable.'
);
expect(process.stdout.write).toHaveBeenCalledWith(
expect.stringContaining('CRITICAL')
);
delete process.env.GCS_AUTHENTICATION_PATH;
});
});