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

Throw descriptive error message for absent .env #1546

Merged
merged 6 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/auth/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DB_DISABLE_SSL=
DB_ENABLE_SSL=
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if we should include this optional field in the examples or just skip it, I chose to include it

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I think it's good to include for completeness. Please add a note that the release process needs to include some manual changes to environment variables (the process is somewhat arduous)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will add it (by the way I am the release manager this week). Could you explain the process to me in person (or point me to related documentation)?

Copy link
Member

Choose a reason for hiding this comment

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

For sure. This is the best documentation so far, but I will take you through it in person and we can add a doc just for that process

Copy link
Member

Choose a reason for hiding this comment

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

DB_NAME=
DB_PASSWORD=
DB_URL=
Expand Down
2 changes: 1 addition & 1 deletion packages/data-api/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DB_DISABLE_SSL=
DB_ENABLE_SSL=
DB_NAME=
DB_PASSWORD=
DB_URL=
Expand Down
2 changes: 1 addition & 1 deletion packages/database/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DB_DISABLE_SSL=
DB_ENABLE_SSL=
DB_NAME=
DB_PASSWORD=
DB_URL=
Expand Down
57 changes: 35 additions & 22 deletions packages/database/src/getConnectionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,40 @@
* Copyright (c) 2017 Beyond Essential Systems Pty Ltd
*/

// Note: Must use function to guarantee environment variables have loaded.
const getServerConfig = () => ({
host: process.env.DB_URL,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
ssl:
process.env.DB_ENABLE_SSL === 'true'
? {
// Test server cannot turn on ssl, so sets the env to disable it
rejectUnauthorized: false,
}
: null,
});

const getCiConfig = () => ({
host: process.env.CI_TEST_DB_URL,
user: process.env.CI_TEST_DB_USER,
password: process.env.CI_TEST_DB_PASSWORD,
database: process.env.CI_TEST_DB_NAME,
ssl: null,
});

const validateEnv = () => {
['DB_URL', 'DB_USER', 'DB_PASSWORD', 'DB_NAME'].forEach(requiredField => {
if (!process.env[requiredField]) {
throw new Error(
`Invalid database connection configuration: please specify '${requiredField}' in a .env file`,
);
}
});
};

export const getConnectionConfig = () => {
return process.env.CI_NAME === 'codeship'
? {
host: process.env.CI_TEST_DB_URL,
user: process.env.CI_TEST_DB_USER,
password: process.env.CI_TEST_DB_PASSWORD,
database: process.env.CI_TEST_DB_NAME,
ssl: null,
}
: {
host: process.env.DB_URL,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
ssl:
process.env.DB_DISABLE_SSL === 'true'
? null
: {
// Test server cannot turn on ssl, so sets the env to disable it
rejectUnauthorized: false,
},
};
validateEnv();
// Note: Must use functions to guarantee environment variables have loaded
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed this is the case... In previous commits, I removed the comment (and used const) without a problem, but I was probably lucky

This functionality now fails if functions are not used to retrieve the .env variables

return process.env.CI_NAME === 'codeship' ? getCiConfig() : getServerConfig();
};
2 changes: 1 addition & 1 deletion packages/meditrak-server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AWS_SECRET_ACCESS_KEY=
CLIENT_SECRET=
CLIENT_USERNAME=
COUNTRY_REQUEST_EMAIL_ADDRESS=
DB_DISABLE_SSL=
DB_ENABLE_SSL=
DB_NAME=
DB_PASSWORD=
DB_URL=
Expand Down
2 changes: 1 addition & 1 deletion packages/web-config-server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AWS_ACCESS_KEY_ID=
AWS_ACCESS_KEY_SECRET=
CONFIG_SERVER_BASE_URL=
COOKIE_SECRET=
DB_DISABLE_SSL=
DB_ENABLE_SSL=
DB_NAME=
DB_PASSWORD=
DB_URL=
Expand Down
2 changes: 1 addition & 1 deletion packages/web-frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# only required for e2e tests
CYPRESS_TEST_USER_PASSWORD=
DB_DISABLE_SSL=
DB_ENABLE_SSL=
DB_NAME=
DB_PASSWORD=
DB_URL=
Expand Down