Skip to content

Commit e4d25b2

Browse files
committedJan 25, 2017
secure flow of .env params configured
1 parent 47b0f58 commit e4d25b2

File tree

8 files changed

+53
-39
lines changed

8 files changed

+53
-39
lines changed
 

‎.env_test

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
REACT_APP_API_ENDPOINT="http://sandbox.gonebusy.com/api/v1"
2+
REACT_APP_API_ENDPOINT="v1"
3+
REACT_APP_TOKEN="Token af9094c6d46658e60cde12e34ad26979"
4+
5+
CI="true"

‎.example.env

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
REACT_APP_SERVICE_ID=7891245607
2-
REACT_APP_GONEBUSY_TOKEN="Token af9094c6d46658e60cde12e34ad26979"
3-
REACT_APP_API_HOST="http://sandbox.gonebusy.com"
4-
REACT_APP_API_PATH="/api/v1"
5-
REACT_APP_IS_PROXIED="true"
6-
REACT_APP_PROXY_HOST="http://localhost:3000"
1+
GONEBUSY_TOKEN="Token af9094c6d46658e60cde12e34ad26979"
2+
GONEBUSY_API_HOST="http://sandbox.gonebusy.com"
3+
GONEBUSY_API_PATH="/api/v1"
4+
GONEBUSY_IS_PROXIED="true"
5+
GONEBUSY_PROXY_HOST="http://localhost:3000"

‎.example.env_test

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REACT_APP_API_ENDPOINT="http://sandbox.gonebusy.com/api/v1"
2+
REACT_APP_TOKEN="Token af9094c6d46658e60cde12e34ad26979"
3+
4+
CI="true"

‎config/env.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
// injected into the application via DefinePlugin in Webpack configuration.
33

44
var REACT_APP = /^REACT_APP_/i;
5+
var clientParams = require('./gonebusy_env').client;
56

67
function getClientEnvironment(publicUrl) {
8+
var envData = Object.assign({}, process.env, clientParams);
9+
710
var processEnv = Object
8-
.keys(process.env)
11+
.keys(envData)
912
.filter(key => REACT_APP.test(key))
1013
.reduce((env, key) => {
11-
env[key] = JSON.stringify(process.env[key]);
14+
env[key] = JSON.stringify(envData[key]);
1215
return env;
1316
}, {
1417
// Useful for determining whether we’re running in production mode.
1518
// Most importantly, it switches React into the correct mode.
1619
'NODE_ENV': JSON.stringify(
17-
process.env.NODE_ENV || 'development'
20+
envData.NODE_ENV || 'development'
1821
),
1922
// Useful for resolving the correct path to static assets in `public`.
2023
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.

‎config/gonebusy_env.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
const url = require('url');
22
const env = process.env;
3-
const reactAppServiceId = env['REACT_APP_SERVICE_ID'];
4-
const reactAppGonebusyToken = env['REACT_APP_GONEBUSY_TOKEN'];
5-
const gonebusyApiHost = env['REACT_APP_API_HOST'];
6-
const gonebusyApiPath = env['REACT_APP_API_PATH'];
7-
const gonebusyIsProxied = env['REACT_APP_IS_PROXIED'];
8-
const gonebusyProxyHost = env['REACT_APP_PROXY_HOST'];
93

10-
const is_proxied = !!(gonebusyIsProxied && JSON.parse(gonebusyIsProxied));
4+
const envToken = env['GONEBUSY_TOKEN'];
5+
const envApiHost = env['GONEBUSY_API_HOST'];
6+
const envApiPath = env['GONEBUSY_API_PATH'];
7+
const envIsProxied = env['GONEBUSY_IS_PROXIED'];
8+
const envProxyHost = env['GONEBUSY_PROXY_HOST'];
119

12-
const clientApiEndpoint = url.resolve((is_proxied ? gonebusyProxyHost : gonebusyApiHost) || '', gonebusyApiPath);
13-
const clientToken = is_proxied ? 'none' : reactAppGonebusyToken;
14-
const middlewareProxyHost = is_proxied ? gonebusyApiHost : undefined;
15-
const middlewareToken = is_proxied ? reactAppGonebusyToken : undefined;
10+
const is_proxied = !!(envIsProxied && JSON.parse(envIsProxied));
1611

17-
console.log("to change the way we process .env so that it won't appear in plain JS", is_proxied);
12+
const clientApiEndpoint = url.resolve((is_proxied ? envProxyHost : envApiHost) || '', envApiPath);
13+
const clientToken = is_proxied ? 'none' : envToken;
14+
15+
const middlewareProxyHost = is_proxied ? envApiHost : undefined;
16+
const middlewareToken = is_proxied ? envToken : undefined;
1817

1918
module.exports = {
20-
service_id: reactAppServiceId,
21-
clientApiEndpoint,
22-
clientToken,
23-
middlewareProxyHost,
24-
middlewarePath: gonebusyApiPath,
25-
middlewareToken,
19+
client: {
20+
REACT_APP_API_ENDPOINT: clientApiEndpoint,
21+
REACT_APP_TOKEN: clientToken
22+
},
23+
middleware: {
24+
proxy: middlewareProxyHost,
25+
path: envApiPath,
26+
token: middlewareToken
27+
}
2628
};

‎scripts/start.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ function addMiddleware(devServer) {
156156
// `proxy` lets you to specify a fallback server during development.
157157
// Every unrecognized request will be forwarded to it.
158158

159-
const gonebusy_env = require('../config/gonebusy_env');
160-
const proxy = gonebusy_env['middlewareProxyHost'];
161-
const token = gonebusy_env['middlewareToken'];
162-
const middlewarePath = gonebusy_env['middlewarePath'];
159+
var envParams = require('../config/gonebusy_env').middleware;
160+
var proxy = envParams.proxy;
161+
var token = envParams.token;
162+
var apiPath = envParams.path;
163163

164164
devServer.use(historyApiFallback({
165165
// Paths with dots should still use the history fallback.
@@ -192,7 +192,7 @@ function addMiddleware(devServer) {
192192
// Tip: use https://jex.im/regulex/ to visualize the regex
193193
// var mayProxy = /^(?!\/(index\.html$|.*\.hot-update\.json$|sockjs-node\/)).*$/;
194194
// var mayProxy = /^\/api.*$/;
195-
var mayProxy = new RegExp('^' + middlewarePath + '.*$');
195+
var mayProxy = new RegExp('^' + apiPath + '.*$');
196196

197197
// Pass the scope regex both to Express and to the middleware for proxying
198198
// of both HTTP and WebSockets to work without false positives.

‎scripts/test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ process.env.PUBLIC_URL = '';
55
// if this file is missing. dotenv will never modify any environment variables
66
// that have already been set.
77
// https://github.com/motdotla/dotenv
8-
require('dotenv').config({silent: true});
8+
require('dotenv').config({
9+
silent: true,
10+
path: './.env_test'
11+
});
912

1013
const jest = require('jest');
1114
const argv = process.argv.slice(2);
1215

16+
// console.log(process.env);
17+
1318
// Watch unless on CI or in coverage mode
1419
if (!process.env.CI && argv.indexOf('--coverage') < 0) {
20+
console.log('got inside');
1521
argv.push('--watch');
1622
}
1723

‎src/lib/BusyAdapter.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ import gonebusy, { CreateBookingBody } from 'gonebusy-nodejs-client/lib';
22
import { Promise } from 'bluebird';
33
import Scheduler from './Scheduler';
44

5-
import gonebusyEnv from '../../config/gonebusy_env';
6-
75
const ServicesController = Promise.promisifyAll(gonebusy.ServicesController);
86
const BookingsController = Promise.promisifyAll(gonebusy.BookingsController);
97

10-
const {
11-
clientToken: authorization,
12-
clientApiEndpoint
13-
} = gonebusyEnv;
8+
const { REACT_APP_TOKEN: authorization, REACT_APP_API_ENDPOINT: clientApiEndpoint } = process.env;
149

1510
gonebusy.configuration.BASEURI = clientApiEndpoint;
1611

0 commit comments

Comments
 (0)