Skip to content

Commit

Permalink
Merge pull request #568 from aternosorg/base64-credentials
Browse files Browse the repository at this point in the history
Support passing google cloud credentials as base64 encoded JSON when using environment variables
  • Loading branch information
JulianVennen authored May 4, 2023
2 parents fac3f5b + 0fa0f3c commit c11e673
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ These credentials are used for the following apis if you enabled them in the con
- Cloud Vision
- Cloud Logging

### Base64 Encoded
If you're using environment variables it is recommended to base64 encode the credentials JSON object and set them in the
environment variable `MODBOT_GOOGLE_CLOUD_CREDENTIALS`. If this variable is set ModBot will ignore the other variables.

#### Client Email
| type | config file | environment |
|--------|----------------------------------------|------------------------------------------------|
Expand Down
17 changes: 13 additions & 4 deletions src/bot/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ export class Config {

async load() {
if (process.env.MODBOT_USE_ENV) {
let googleCloudCredentials = process.env.MODBOT_GOOGLE_CLOUD_CREDENTIALS;
if (googleCloudCredentials) {
googleCloudCredentials = JSON.parse((new Buffer(googleCloudCredentials, 'base64')).toString('ascii'));
}
else {
googleCloudCredentials = {
client_email: process.env.MODBOT_GOOGLE_CLOUD_CREDENTIALS_CLIENT_EMAIL,
private_key: process.env.MODBOT_GOOGLE_CLOUD_CREDENTIALS_PRIVATE_KEY?.replaceAll('\\n', '\n'),
};
}


// load settings from env
this.#data = {
authToken: process.env.MODBOT_AUTH_TOKEN,
Expand All @@ -99,10 +111,7 @@ export class Config {
},
googleApiKey: process.env.MODBOT_GOOGLE_API_KEY,
googleCloud: {
credentials: {
client_email: process.env.MODBOT_GOOGLE_CLOUD_CREDENTIALS_CLIENT_EMAIL,
private_key: process.env.MODBOT_GOOGLE_CLOUD_CREDENTIALS_PRIVATE_KEY
},
credentials: googleCloudCredentials,
vision: {
enabled: this.#parseBooleanFromEnv(process.env.MODBOT_GOOGLE_CLOUD_VISION_ENABLED)
},
Expand Down

0 comments on commit c11e673

Please sign in to comment.