diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index daccf271ac..5fb13338ca 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -278,5 +278,5 @@ jobs: run: npm install - name: npm build - run: npm run build:prod + run: npm run build:cloud diff --git a/.gitignore b/.gitignore index a032f143f6..f78b6c9983 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,4 @@ dist/ *.swp build/ !dev-server.shared.pem -config/development.json +config/local.json diff --git a/README.md b/README.md index bb5cda4299..b22e9c57f9 100644 --- a/README.md +++ b/README.md @@ -44,20 +44,23 @@ npm install ENV=production npm run build:watch ``` -You can also manually adjusting your API endpoint settings by adding `config/development.json` overriding any of the values in `config/base.json`. For example: +You can also manually adjusting your API endpoint settings by adding `config/local.json` overriding any of the following values: -```typescript +```json { "proxyApi": "http://your-api-url", "proxyIdentity": "http://your-identity-url", "proxyEvents": "http://your-events-url", "proxyNotifications": "http://your-notifications-url", "proxyPortal": "http://your-portal-url", - "allowedHosts": ["hostnames-to-allow-in-webpack"] + "allowedHosts": ["hostnames-to-allow-in-webpack"], + "urls": { + + } } ``` -To pick up the overrides in the newly created `config/development.json` file, run the app with: +Where the `urls` object is defined by the [Urls type in jslib](https://github.com/bitwarden/jslib/blob/master/common/src/abstractions/environment.service.ts). To pick up the overrides in the newly created `config/local.json` file, run the app with: ``` npm run build:dev:watch diff --git a/config.js b/config.js index 961644de9d..dee8904f33 100644 --- a/config.js +++ b/config.js @@ -1,26 +1,29 @@ function load(envName) { - const envOverrides = { - 'production': () => require('./config/production.json'), - 'qa': () => require('./config/qa.json'), - 'development': () => require('./config/development.json'), - }; - - const baseConfig = require('./config/base.json'); - const overrideConfig = envOverrides.hasOwnProperty(envName) ? envOverrides[envName]() : {}; - return { - ...baseConfig, - ...overrideConfig + ...require('./config/base.json'), + ...loadConfig(envName), + ...loadConfig('local'), }; } function log(configObj) { - const repeatNum = 50 - console.log(`${"=".repeat(repeatNum)}\nenvConfig`) - Object.entries(configObj).map(([key, value]) => { - console.log(` ${key}: ${value}`) - }) - console.log(`${"=".repeat(repeatNum)}`) + const repeatNum = 50; + console.log(`${"=".repeat(repeatNum)}\nenvConfig`); + console.log(JSON.stringify(configObj, null, 2)); + console.log(`${"=".repeat(repeatNum)}`); +} + +function loadConfig(configName) { + try { + return require(`./config/${configName}.json`); + } catch (e) { + if (e instanceof Error && e.code === "MODULE_NOT_FOUND") { + return {}; + } + else { + throw e; + } + } } module.exports = { diff --git a/config/base.json b/config/base.json index fdbb8f99d3..c8b0606aed 100644 --- a/config/base.json +++ b/config/base.json @@ -1,8 +1,3 @@ { - "proxyApi": "http://localhost:4000", - "proxyIdentity": "http://localhost:33656", - "proxyEvents": "http://localhost:46273", - "proxyNotifications": "http://localhost:61840", - "proxyPortal": "http://localhost:52313", - "allowedHosts": [] + "urls": {} } diff --git a/config/cloud.json b/config/cloud.json new file mode 100644 index 0000000000..b11942a255 --- /dev/null +++ b/config/cloud.json @@ -0,0 +1,7 @@ +{ + "urls": { + "icons": "https://icons.bitwarden.net", + "notifications": "https://notifications.bitwarden.com", + "enterprise": "https://portal.bitwarden.com" + } +} diff --git a/config/development.json b/config/development.json new file mode 100644 index 0000000000..9903790597 --- /dev/null +++ b/config/development.json @@ -0,0 +1,11 @@ +{ + "proxyApi": "http://localhost:4000", + "proxyIdentity": "http://localhost:33656", + "proxyEvents": "http://localhost:46273", + "proxyNotifications": "http://localhost:61840", + "proxyEnterprise": "http://localhost:52313", + "allowedHosts": [], + "urls": { + "enterprise": "http://localhost:52313" + } +} diff --git a/config/production.json b/config/production.json deleted file mode 100644 index 7da9dd948c..0000000000 --- a/config/production.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "proxyApi": "https://api.bitwarden.com", - "proxyIdentity": "https://identity.bitwarden.com", - "proxyEvents": "https://events.bitwarden.com", - "proxyNotifications": "https://notifications.bitwarden.com", - "proxyPortal": "https://portal.bitwarden.com" -} diff --git a/config/qa.json b/config/qa.json index 502cf59fee..3afc3803ae 100644 --- a/config/qa.json +++ b/config/qa.json @@ -1,7 +1,8 @@ { - "proxyApi": "https://api.qa.bitwarden.com", - "proxyIdentity": "https://identity.qa.bitwarden.com", - "proxyEvents": "https://events.qa.bitwarden.com", - "proxyNotifications": "https://notifications.qa.bitwarden.com", - "proxyPortal": "https://portal.qa.bitwarden.com" + "urls": { + "icons": "https://icons.qa.bitwarden.pw", + "notifications": "https://notifications.qa.bitwarden.pw", + "enterprise": "https://portal.qa.bitwarden.pw" + } + } diff --git a/config/self-hosted.json b/config/self-hosted.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/config/self-hosted.json @@ -0,0 +1 @@ +{} diff --git a/package.json b/package.json index e3555115a6..3637f46b4c 100644 --- a/package.json +++ b/package.json @@ -18,18 +18,15 @@ "build:dev": "cross-env ENV=development npm run build", "build:dev:watch": "cross-env ENV=development npm run build:watch", "build:qa": "cross-env NODE_ENV=production ENV=qa npm run build", - "build:qa:watch": "cross-env NODE_ENV=production ENV=qa npm run build:watch", - "build:prod": "cross-env NODE_ENV=production ENV=production npm run build", - "build:prod:oss": "cross-env NODE_ENV=production ENV=production npm run build:oss", - "build:prod:watch": "cross-env NODE_ENV=production ENV=production npm run build:watch", - "build:selfhost": "cross-env SELF_HOST=true npm run build:watch", - "build:selfhost:watch": "cross-env SELF_HOST=true npm run build:watch", - "build:selfhost:prod": "cross-env SELF_HOST=true NODE_ENV=production npm run build", - "build:selfhost:prod:oss": "cross-env SELF_HOST=true NODE_ENV=production npm run build:oss", - "build:selfhost:prod:watch": "cross-env SELF_HOST=true NODE_ENV=production npm run build:watch", + "build:cloud": "cross-env NODE_ENV=production ENV=cloud npm run build", + "build:cloud:oss": "cross-env NODE_ENV=production ENV=cloud npm run build:oss", + "build:selfhost": "cross-env ENV=selfhosted npm run build:watch", + "build:selfhost:watch": "cross-env ENV=selfhosted npm run build:watch", + "build:selfhost:prod": "cross-env ENV=selfhosted NODE_ENV=production npm run build", + "build:selfhost:prod:oss": "cross-env ENV=selfhosted NODE_ENV=production npm run build:oss", "clean:l10n": "git push origin --delete l10n_master", - "dist": "npm run build:prod && gulp postdist", - "dist:oss": "npm run build:prod:oss && gulp postdist", + "dist": "npm run build:cloud && gulp postdist", + "dist:oss": "npm run build:cloud:oss && gulp postdist", "dist:selfhost": "npm run build:selfhost:prod && gulp postdist", "dist:selfhost:oss": "npm run build:selfhost:prod:oss && gulp postdist", "deploy": "npm run dist && gh-pages -d build", diff --git a/src/app/services/services.module.ts b/src/app/services/services.module.ts index 59352afa40..c0c820c617 100644 --- a/src/app/services/services.module.ts +++ b/src/app/services/services.module.ts @@ -62,7 +62,7 @@ import { CipherService as CipherServiceAbstraction } from 'jslib-common/abstract import { CollectionService as CollectionServiceAbstraction } from 'jslib-common/abstractions/collection.service'; import { CryptoService as CryptoServiceAbstraction } from 'jslib-common/abstractions/crypto.service'; import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from 'jslib-common/abstractions/cryptoFunction.service'; -import { EnvironmentService as EnvironmentServiceAbstraction } from 'jslib-common/abstractions/environment.service'; +import { EnvironmentService as EnvironmentServiceAbstraction, Urls } from 'jslib-common/abstractions/environment.service'; import { EventService as EventLoggingServiceAbstraction } from 'jslib-common/abstractions/event.service'; import { ExportService as ExportServiceAbstraction } from 'jslib-common/abstractions/export.service'; import { FileUploadService as FileUploadServiceAbstraction } from 'jslib-common/abstractions/fileUpload.service'; @@ -147,16 +147,9 @@ export function initFactory(): Function { return async () => { await (storageService as HtmlStorageService).init(); - if (process.env.ENV !== 'production' || platformUtilsService.isSelfHost()) { - environmentService.setUrls({ base: window.location.origin }, false); - } else { - environmentService.setUrls({ - base: window.location.origin, - icons: 'https://icons.bitwarden.net', - notifications: 'https://notifications.bitwarden.com', - enterprise: 'https://portal.bitwarden.com', - }, false); - } + const urls = process.env.URLS as Urls; + urls.base ??= window.location.origin; + environmentService.setUrls(urls, false); setTimeout(() => notificationsService.init(), 3000); diff --git a/src/app/settings/add-credit.component.ts b/src/app/settings/add-credit.component.ts index 919465a353..0751edfe80 100644 --- a/src/app/settings/add-credit.component.ts +++ b/src/app/settings/add-credit.component.ts @@ -47,7 +47,7 @@ export class AddCreditComponent implements OnInit { constructor(private userService: UserService, private apiService: ApiService, private platformUtilsService: PlatformUtilsService) { - if (process.env.ENV !== 'production' || platformUtilsService.isDev()) { + if (process.env.ENV !== 'cloud' || platformUtilsService.isDev()) { this.ppButtonFormAction = WebConstants.paypal.buttonActionSandbox; this.ppButtonBusinessId = WebConstants.paypal.businessIdSandbox; } diff --git a/src/app/settings/payment.component.ts b/src/app/settings/payment.component.ts index b6203d59e7..8470fefe0a 100644 --- a/src/app/settings/payment.component.ts +++ b/src/app/settings/payment.component.ts @@ -67,7 +67,7 @@ export class PaymentComponent implements OnInit { this.stripeScript.src = 'https://js.stripe.com/v3/'; this.stripeScript.async = true; this.stripeScript.onload = () => { - this.stripe = (window as any).Stripe(process.env.ENV === 'production' && !platformUtilsService.isDev() ? + this.stripe = (window as any).Stripe(process.env.ENV === 'cloud' && !platformUtilsService.isDev() ? WebConstants.stripeLiveKey : WebConstants.stripeTestKey); this.stripeElements = this.stripe.elements(); this.setStripeElement(); @@ -126,7 +126,7 @@ export class PaymentComponent implements OnInit { if (this.method === PaymentMethodType.PayPal) { window.setTimeout(() => { (window as any).braintree.dropin.create({ - authorization: process.env.ENV === 'production' ? + authorization: process.env.ENV === 'cloud' ? WebConstants.btProductionKey : WebConstants.btSandboxKey, container: '#bt-dropin-container', paymentOptionPriority: ['paypal'], diff --git a/src/services/webPlatformUtils.service.ts b/src/services/webPlatformUtils.service.ts index ecf00b2604..de67e9a1f1 100644 --- a/src/services/webPlatformUtils.service.ts +++ b/src/services/webPlatformUtils.service.ts @@ -253,7 +253,7 @@ export class WebPlatformUtilsService implements PlatformUtilsService { } isSelfHost(): boolean { - return process.env.SELF_HOST.toString() === 'true'; + return process.env.ENV.toString() === 'selfhosted'; } copyToClipboard(text: string, options?: any): void | boolean { diff --git a/webpack.config.js b/webpack.config.js index cd67c6f63a..0d5b88962e 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -13,7 +13,7 @@ const config = require('./config.js'); const ENV = process.env.ENV == null ? 'development' : process.env.ENV; const NODE_ENV = process.env.NODE_ENV == null ? 'development' : process.env.NODE_ENV; -const envConfig = config.load(process.env.ENV); +const envConfig = config.load(ENV); config.log(envConfig); const moduleRules = [ @@ -141,9 +141,9 @@ const plugins = [ new webpack.EnvironmentPlugin({ 'ENV': ENV, 'NODE_ENV': NODE_ENV === 'production' ? 'production' : 'development', - 'SELF_HOST': process.env.SELF_HOST === 'true' ? true : false, 'APPLICATION_VERSION': pjson.version, 'CACHE_TAG': Math.random().toString(36).substring(7), + 'URLS': envConfig['urls'] ?? {}, }), new AngularCompilerPlugin({ tsConfigPath: 'tsconfig.json', @@ -154,7 +154,7 @@ const plugins = [ // ref: https://webpack.js.org/configuration/dev-server/#devserver let certSuffix = fs.existsSync('dev-server.local.pem') ? '.local' : '.shared'; -const devServer = { +const devServer = ENV !== 'development' ? {} : { https: { key: fs.readFileSync('dev-server' + certSuffix + '.pem'), cert: fs.readFileSync('dev-server' + certSuffix + '.pem'), @@ -186,7 +186,7 @@ const devServer = { changeOrigin: true }, '/portal': { - target: envConfig['proxyPortal'], + target: envConfig['proxyEnterprise'], pathRewrite: {'^/portal' : ''}, secure: false, changeOrigin: true