|
| 1 | +const { has } = require('ramda'); |
| 2 | +const convict = require('./convict'); |
| 3 | +const { createDebug } = require('./debug'); |
| 4 | + |
| 5 | +const DEBUG_KEY = 'config'; |
| 6 | + |
| 7 | +const debug = createDebug(DEBUG_KEY); |
| 8 | + |
| 9 | +const keyForValidatedConfigs = Symbol('validatedConfig'); |
| 10 | + |
| 11 | +const definition = { |
| 12 | + env: { |
| 13 | + doc: 'The environment the application is running in.', |
| 14 | + format: ['production', 'development', 'test'], |
| 15 | + default: 'development', |
| 16 | + env: 'NODE_ENV', |
| 17 | + }, |
| 18 | + origin: { |
| 19 | + doc: 'The HTTP origin of the host of the netlify-cms admin panel using this OAuth provider. ' + |
| 20 | + 'Multiple origin domains can be provided as an array of strings or a single comma-separated string. ' + |
| 21 | + 'You can provide only the domain part (`\'example.com\'`) which implies any protocol on any port or you can explicitly ' + |
| 22 | + 'specify a protocol and/or port (`\'https://example.com\'` or `\'http://example.com:8080\'`)', |
| 23 | + format: 'origin-list', |
| 24 | + default: null, |
| 25 | + allowEmpty: false, |
| 26 | + env: 'ORIGIN', |
| 27 | + }, |
| 28 | + completeUri: { |
| 29 | + doc: 'The URI (specified during the OAuth 2.0 authorization flow) that the `complete` handler is hosted at.', |
| 30 | + default: null, |
| 31 | + format: String, |
| 32 | + env: 'OAUTH_REDIRECT_URI', |
| 33 | + }, |
| 34 | + oauthProvider: { |
| 35 | + doc: 'The Git service / OAuth provider to use', |
| 36 | + default: 'github', |
| 37 | + format: ['github', 'gitlab'], |
| 38 | + env: 'OAUTH_PROVIDER', |
| 39 | + }, |
| 40 | + oauthClientID: { |
| 41 | + doc: 'The OAuth 2.0 Client ID received from the OAuth provider.', |
| 42 | + default: null, |
| 43 | + format: String, |
| 44 | + env: 'OAUTH_CLIENT_ID', |
| 45 | + }, |
| 46 | + oauthClientSecret: { |
| 47 | + doc: 'The OAuth 2.0 Client secret received from the OAuth provider.', |
| 48 | + default: null, |
| 49 | + format: String, |
| 50 | + env: 'OAUTH_CLIENT_SECRET', |
| 51 | + }, |
| 52 | + oauthTokenHost: { |
| 53 | + doc: 'The OAuth 2.0 token host URI for the OAuth provider. ' + |
| 54 | + 'If not provided, this will be guessed based on the provider. ' + |
| 55 | + 'You must provide this for GitHub enterprise.', |
| 56 | + default: '', |
| 57 | + format: String, |
| 58 | + env: 'OAUTH_TOKEN_HOST', |
| 59 | + }, |
| 60 | + oauthTokenPath: { |
| 61 | + doc: 'The relative URI to the OAuth 2.0 token endpoint for the OAuth provider. ' + |
| 62 | + 'If not provided, this will be guessed based on the provider.', |
| 63 | + default: '', |
| 64 | + format: String, |
| 65 | + env: 'OAUTH_TOKEN_PATH', |
| 66 | + }, |
| 67 | + oauthAuthorizePath: { |
| 68 | + doc: 'The relative URI to the OAuth 2.0 authorization endpoint for the OAuth provider. ' + |
| 69 | + 'If not provided, this will be guessed based on the provider.', |
| 70 | + default: '', |
| 71 | + format: String, |
| 72 | + env: 'OAUTH_AUTHORIZE_PATH', |
| 73 | + }, |
| 74 | + oauthScopes: { |
| 75 | + doc: 'The scopes to claim during the OAuth 2.0 authorization request with the OAuth provider. ' + |
| 76 | + 'If not provided, this will be guessed based on the provider with the goal to ensure the user has ' + |
| 77 | + 'read/write access to repositories.', |
| 78 | + default: '', |
| 79 | + format: String, |
| 80 | + env: 'OAUTH_SCOPES', |
| 81 | + }, |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * Mutates the provided object, marking it as a validated config so we can check for it later. |
| 86 | + * @param {object} config |
| 87 | + */ |
| 88 | +function markConfigAsValidated(config) { |
| 89 | + config[keyForValidatedConfigs] = true; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Determine if a given value is a validated and authentic config. |
| 94 | + * |
| 95 | + * @param {object} config |
| 96 | + * @return {boolean} |
| 97 | + */ |
| 98 | +function isConfigValidated(config) { |
| 99 | + return !!( |
| 100 | + config && |
| 101 | + typeof config === 'object' && |
| 102 | + has(keyForValidatedConfigs, config) && |
| 103 | + config[keyForValidatedConfigs] |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * @typedef {{ |
| 109 | + * skipAlreadyValidatedCheck?: boolean, |
| 110 | + * useEnv?: boolean, |
| 111 | + * useArgs?: boolean, |
| 112 | + * extraConvictOptions?: {}, |
| 113 | + * extraValidateOptions?: {} |
| 114 | + * }} CreateConfigOptions |
| 115 | + */ |
| 116 | + |
| 117 | +/** |
| 118 | + * @typedef {{get: function(string): *}} ConvictConfig |
| 119 | + */ |
| 120 | + |
| 121 | +/** |
| 122 | + * Create and validate a convict configuration instance for this package. |
| 123 | + * |
| 124 | + * @param {{}=} userConfig |
| 125 | + * @param {boolean=} skipAlreadyValidatedCheck Set to true to always try to reload and revalidate the provided config |
| 126 | + * @param {boolean=} useEnv Set to true to try to extract config values from environment variables |
| 127 | + * @param {boolean=} useArgs Set to true to try to extract config values from command line arguments |
| 128 | + * @param {{}=} extraConvictOptions Additional options to pass directly to convict |
| 129 | + * @param {{}=} extraValidateOptions Additional options to pass directly to convict's validate function. |
| 130 | + * @return {{}} The convict config instance (with i.e. a `get` method) |
| 131 | + */ |
| 132 | +function createConfig( |
| 133 | + userConfig = {}, |
| 134 | + { |
| 135 | + skipAlreadyValidatedCheck = false, |
| 136 | + useEnv = false, |
| 137 | + useArgs = false, |
| 138 | + extraConvictOptions = {}, |
| 139 | + extraValidateOptions = {}, |
| 140 | + } = {}, |
| 141 | +) { |
| 142 | + // If the config provided is already a validated config, we can just straight up return it. |
| 143 | + if (!skipAlreadyValidatedCheck && isConfigValidated(userConfig)) { |
| 144 | + return userConfig; |
| 145 | + } |
| 146 | + |
| 147 | + // Build out convict options |
| 148 | + const convictOptions = {}; |
| 149 | + if (!useEnv) { |
| 150 | + convictOptions.env = {}; |
| 151 | + } |
| 152 | + if (!useArgs) { |
| 153 | + convictOptions.args = []; |
| 154 | + } |
| 155 | + |
| 156 | + // Merge together options |
| 157 | + const processedOptions = { |
| 158 | + ...convictOptions, |
| 159 | + ...extraConvictOptions, |
| 160 | + }; |
| 161 | + |
| 162 | + // Build the config based on our definition and options |
| 163 | + const config = convict(definition, processedOptions); |
| 164 | + |
| 165 | + // Merge in the user config |
| 166 | + config.load(userConfig); |
| 167 | + |
| 168 | + // Validate the config; this throws if the config is invalid |
| 169 | + config.validate({ |
| 170 | + allowed: 'warn', |
| 171 | + output: debug, |
| 172 | + ...extraValidateOptions, |
| 173 | + }); |
| 174 | + |
| 175 | + // Mark the config as authentic and validated |
| 176 | + markConfigAsValidated(config); |
| 177 | + |
| 178 | + // Return it |
| 179 | + return config; |
| 180 | +} |
| 181 | + |
| 182 | +module.exports = { |
| 183 | + debug, |
| 184 | + DEBUG_KEY, |
| 185 | + definition, |
| 186 | + markConfigAsValidated, |
| 187 | + keyForValidatedConfigs, |
| 188 | + isConfigValidated, |
| 189 | + createConfig, |
| 190 | +}; |
0 commit comments