Skip to content

Commit 0fad3a4

Browse files
committed
feat: mongo connection string & cookie secret from env vars
- allow for the setting of configuration for MongoDB database connection string as well as cookie secret via environment variables as an alternative to JSON-based configuration Useful in environments that split config between secrets (ie. stored in a secret manager) and plain app configurations such as commitConfig, attestation, etc. that may not be considered sensitive & wouldn't be appropriate in a secret manager.
1 parent 75fb0e6 commit 0fad3a4

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/config/env.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ export type ServerConfig = {
33
GIT_PROXY_HTTPS_SERVER_PORT: string | number;
44
GIT_PROXY_UI_HOST: string;
55
GIT_PROXY_UI_PORT: string | number;
6+
GIT_PROXY_COOKIE_SECRET: string | undefined;
7+
GIT_PROXY_MONGO_CONNECTION_STRING: string;
68
};
79

810
const {
911
GIT_PROXY_SERVER_PORT = 8000,
1012
GIT_PROXY_HTTPS_SERVER_PORT = 8443,
1113
GIT_PROXY_UI_HOST = 'http://localhost',
1214
GIT_PROXY_UI_PORT = 8080,
15+
GIT_PROXY_COOKIE_SECRET,
16+
GIT_PROXY_MONGO_CONNECTION_STRING = 'mongodb://localhost:27017/git-proxy',
1317
} = process.env;
1418

1519
export const serverConfig: ServerConfig = {
1620
GIT_PROXY_SERVER_PORT,
1721
GIT_PROXY_HTTPS_SERVER_PORT,
1822
GIT_PROXY_UI_HOST,
1923
GIT_PROXY_UI_PORT,
24+
GIT_PROXY_COOKIE_SECRET,
25+
GIT_PROXY_MONGO_CONNECTION_STRING,
2026
};

src/config/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync, readFileSync } from 'fs';
22

33
import defaultSettings from '../../proxy.config.json';
4+
import { serverConfig } from './env';
45
import { configFile, validate } from './file';
56
import { ConfigLoader, Configuration } from './ConfigLoader';
67
import {
@@ -23,7 +24,7 @@ let _apiAuthentication: Authentication[] = defaultSettings.apiAuthentication;
2324
let _tempPassword: TempPasswordConfig = defaultSettings.tempPassword;
2425
let _proxyUrl = defaultSettings.proxyUrl;
2526
let _api: Record<string, unknown> = defaultSettings.api;
26-
let _cookieSecret: string = defaultSettings.cookieSecret;
27+
let _cookieSecret: string = serverConfig.GIT_PROXY_COOKIE_SECRET || defaultSettings.cookieSecret;
2728
let _sessionMaxAgeHours: number = defaultSettings.sessionMaxAgeHours;
2829
let _plugins: any[] = defaultSettings.plugins;
2930
let _commitConfig: Record<string, any> = defaultSettings.commitConfig;
@@ -82,6 +83,10 @@ export const getDatabase = () => {
8283
if (ix) {
8384
const db = _database[ix];
8485
if (db.enabled) {
86+
// if mongodb is configured and connection string unspecified, fallback to env var
87+
if (db.type === 'mongo' && !db.connectionString) {
88+
db.connectionString = serverConfig.GIT_PROXY_MONGO_CONNECTION_STRING;
89+
}
8590
return db;
8691
}
8792
}
@@ -92,7 +97,7 @@ export const getDatabase = () => {
9297

9398
/**
9499
* Get the list of enabled authentication methods
95-
*
100+
*
96101
* At least one authentication method must be enabled.
97102
* @return {Authentication[]} List of enabled authentication methods
98103
*/
@@ -104,15 +109,15 @@ export const getAuthMethods = (): Authentication[] => {
104109
const enabledAuthMethods = _authentication.filter((auth) => auth.enabled);
105110

106111
if (enabledAuthMethods.length === 0) {
107-
throw new Error("No authentication method enabled");
112+
throw new Error('No authentication method enabled');
108113
}
109114

110115
return enabledAuthMethods;
111116
};
112117

113118
/**
114119
* Get the list of enabled authentication methods for API endpoints
115-
*
120+
*
116121
* If no API authentication methods are enabled, all endpoints are public.
117122
* @return {Authentication[]} List of enabled authentication methods
118123
*/
@@ -121,10 +126,10 @@ export const getAPIAuthMethods = (): Authentication[] => {
121126
_apiAuthentication = _userSettings.apiAuthentication;
122127
}
123128

124-
const enabledAuthMethods = _apiAuthentication.filter(auth => auth.enabled);
129+
const enabledAuthMethods = _apiAuthentication.filter((auth) => auth.enabled);
125130

126131
if (enabledAuthMethods.length === 0) {
127-
console.log("Warning: No authentication method enabled for API endpoints.");
132+
console.log('Warning: No authentication method enabled for API endpoints.');
128133
}
129134

130135
return enabledAuthMethods;

test/testConfig.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ describe('default configuration', function () {
3737
describe('user configuration', function () {
3838
let tempDir;
3939
let tempUserFile;
40+
let oldEnv;
4041

4142
beforeEach(function () {
43+
delete require.cache[require.resolve('../src/config/env')];
44+
oldEnv = { ...process.env };
4245
tempDir = fs.mkdtempSync('gitproxy-test');
4346
tempUserFile = path.join(tempDir, 'test-settings.json');
4447
require('../src/config/file').configFile = tempUserFile;
@@ -258,9 +261,34 @@ describe('user configuration', function () {
258261
expect(config.getAPIs()).to.be.eql(user.api);
259262
});
260263

264+
it('should override default settings for cookieSecret if env var is used', function () {
265+
fs.writeFileSync(tempUserFile, '{}');
266+
process.env.GIT_PROXY_COOKIE_SECRET = 'test-cookie-secret'
267+
268+
const config = require('../src/config');
269+
expect(config.getCookieSecret()).to.equal('test-cookie-secret');
270+
});
271+
272+
it('should override default settings for mongo connection string if env var is used', function () {
273+
const user = {
274+
sink: [
275+
{
276+
type: 'mongo',
277+
enabled: true,
278+
}
279+
]
280+
};
281+
fs.writeFileSync(tempUserFile, JSON.stringify(user));
282+
process.env.GIT_PROXY_MONGO_CONNECTION_STRING = 'mongodb://example.com:27017/test';
283+
284+
const config = require('../src/config');
285+
expect(config.getDatabase().connectionString).to.equal('mongodb://example.com:27017/test');
286+
});
287+
261288
afterEach(function () {
262289
fs.rmSync(tempUserFile);
263290
fs.rmdirSync(tempDir);
291+
process.env = oldEnv;
264292
delete require.cache[require.resolve('../src/config')];
265293
});
266294
});

0 commit comments

Comments
 (0)