Make environment variables higher precedence than config (#15858)

Environment variables are now considered as higher-precedence than
configuration by our build system. This means that if the same value is
set in `.metamaskrc` and in an environment variable, the environment
variable is what will be used. Previously the reverse was true, the
configuration would take precedence.

It is conventional for CLI tools to consider environment variables as
higher precedence than configuration. This makes our build system less
surprising for most people.
feature/default_network_editable
Mark Stacey 2 years ago committed by GitHub
parent 441e894e9a
commit f465089c2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/scripts/lib/setupSentry.js
  2. 68
      development/build/config.js

@ -9,7 +9,9 @@ import extractEthjsErrorMessage from './extractEthjsErrorMessage';
// Destructuring breaks the inlining of the environment variables // Destructuring breaks the inlining of the environment variables
const METAMASK_DEBUG = process.env.METAMASK_DEBUG; const METAMASK_DEBUG = process.env.METAMASK_DEBUG;
const METAMASK_ENVIRONMENT = process.env.METAMASK_ENVIRONMENT; const METAMASK_ENVIRONMENT = process.env.METAMASK_ENVIRONMENT;
const SENTRY_DSN_DEV = process.env.SENTRY_DSN_DEV; const SENTRY_DSN_DEV =
process.env.SENTRY_DSN_DEV ||
'https://f59f3dd640d2429d9d0e2445a87ea8e1@sentry.io/273496';
const METAMASK_BUILD_TYPE = process.env.METAMASK_BUILD_TYPE; const METAMASK_BUILD_TYPE = process.env.METAMASK_BUILD_TYPE;
const IN_TEST = process.env.IN_TEST; const IN_TEST = process.env.IN_TEST;
/* eslint-enable prefer-destructuring */ /* eslint-enable prefer-destructuring */

@ -3,6 +3,33 @@ const { readFile } = require('fs/promises');
const ini = require('ini'); const ini = require('ini');
const { BuildType } = require('../lib/build-type'); const { BuildType } = require('../lib/build-type');
const commonConfigurationPropertyNames = ['PUBNUB_PUB_KEY', 'PUBNUB_SUB_KEY'];
const configurationPropertyNames = [
...commonConfigurationPropertyNames,
'COLLECTIBLES_V1',
'INFURA_PROJECT_ID',
'ONBOARDING_V2',
'PHISHING_WARNING_PAGE_URL',
'PORTFOLIO_URL',
'SEGMENT_HOST',
'SEGMENT_WRITE_KEY',
'SENTRY_DSN_DEV',
'SIWE_V1',
'SWAPS_USE_DEV_APIS',
];
const productionConfigurationPropertyNames = [
...commonConfigurationPropertyNames,
'INFURA_BETA_PROJECT_ID',
'INFURA_FLASK_PROJECT_ID',
'INFURA_PROD_PROJECT_ID',
'SEGMENT_BETA_WRITE_KEY',
'SEGMENT_FLASK_WRITE_KEY',
'SEGMENT_PROD_WRITE_KEY',
'SENTRY_DSN',
];
/** /**
* Get configuration for non-production builds. * Get configuration for non-production builds.
* *
@ -20,22 +47,17 @@ async function getConfig() {
throw error; throw error;
} }
} }
const environmentVariables = {};
for (const propertyName of configurationPropertyNames) {
if (process.env[propertyName]) {
environmentVariables[propertyName] = process.env[propertyName];
}
}
return { return {
COLLECTIBLES_V1: process.env.COLLECTIBLES_V1,
INFURA_PROJECT_ID: process.env.INFURA_PROJECT_ID,
ONBOARDING_V2: process.env.ONBOARDING_V2,
PHISHING_WARNING_PAGE_URL: process.env.PHISHING_WARNING_PAGE_URL,
PORTFOLIO_URL: process.env.PORTFOLIO_URL,
PUBNUB_PUB_KEY: process.env.PUBNUB_PUB_KEY,
PUBNUB_SUB_KEY: process.env.PUBNUB_SUB_KEY,
SEGMENT_HOST: process.env.SEGMENT_HOST,
SEGMENT_WRITE_KEY: process.env.SEGMENT_WRITE_KEY,
SENTRY_DSN_DEV:
process.env.SENTRY_DSN_DEV ??
'https://f59f3dd640d2429d9d0e2445a87ea8e1@sentry.io/273496',
SIWE_V1: process.env.SIWE_V1,
SWAPS_USE_DEV_APIS: process.env.SWAPS_USE_DEV_APIS,
...ini.parse(configContents), ...ini.parse(configContents),
...environmentVariables,
}; };
} }
@ -61,17 +83,17 @@ async function getProductionConfig(buildType) {
throw error; throw error;
} }
} }
const environmentVariables = {};
for (const propertyName of productionConfigurationPropertyNames) {
if (process.env[propertyName]) {
environmentVariables[propertyName] = process.env[propertyName];
}
}
const prodConfig = { const prodConfig = {
INFURA_BETA_PROJECT_ID: process.env.INFURA_BETA_PROJECT_ID,
INFURA_FLASK_PROJECT_ID: process.env.INFURA_FLASK_PROJECT_ID,
INFURA_PROD_PROJECT_ID: process.env.INFURA_PROD_PROJECT_ID,
PUBNUB_PUB_KEY: process.env.PUBNUB_PUB_KEY,
PUBNUB_SUB_KEY: process.env.PUBNUB_SUB_KEY,
SEGMENT_BETA_WRITE_KEY: process.env.SEGMENT_BETA_WRITE_KEY,
SEGMENT_FLASK_WRITE_KEY: process.env.SEGMENT_FLASK_WRITE_KEY,
SEGMENT_PROD_WRITE_KEY: process.env.SEGMENT_PROD_WRITE_KEY,
SENTRY_DSN: process.env.SENTRY_DSN,
...ini.parse(prodConfigContents), ...ini.parse(prodConfigContents),
...environmentVariables,
}; };
const requiredEnvironmentVariables = { const requiredEnvironmentVariables = {

Loading…
Cancel
Save