Skip to content

Commit 91cea46

Browse files
mgiancola9coopernetes
authored andcommitted
fix: add tls object to bypass certs
1 parent 708d100 commit 91cea46

File tree

6 files changed

+83
-49
lines changed

6 files changed

+83
-49
lines changed

config.schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@
7878
"type": "object"
7979
}
8080
}
81+
},
82+
"tls": {
83+
"description": "TLS configuration for secure connections",
84+
"type": "object",
85+
"properties": {
86+
"enabled": { "type": "boolean" },
87+
"key": { "type": "string" },
88+
"cert": { "type": "string" }
89+
},
90+
"required": ["enabled", "key", "cert"]
8191
}
8292
},
8393
"definitions": {

proxy.config.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,10 @@
9797
"urlShortener": "",
9898
"contactEmail": "",
9999
"csrfProtection": true,
100-
"plugins": []
100+
"plugins": [],
101+
"tls": {
102+
"enabled": true,
103+
"key": "certs/key.pem",
104+
"cert": "certs/cert.pem"
105+
}
101106
}

src/config/index.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import { existsSync, readFileSync } from 'fs';
22

33
import defaultSettings from '../../proxy.config.json';
44
import { configFile } from './file';
5-
import { Authentication, AuthorisedRepo, Database, TempPasswordConfig, UserSettings } from './types';
6-
5+
import {
6+
Authentication,
7+
AuthorisedRepo,
8+
Database,
9+
TempPasswordConfig,
10+
UserSettings,
11+
} from './types';
712

813
let _userSettings: UserSettings | null = null;
914
if (existsSync(configFile)) {
@@ -26,8 +31,9 @@ let _contactEmail: string = defaultSettings.contactEmail;
2631
let _csrfProtection: boolean = defaultSettings.csrfProtection;
2732
let _domains: Record<string, unknown> = defaultSettings.domains;
2833
// These are not always present in the default config file, so casting is required
29-
let _sslKeyPath: string = (defaultSettings as any).sslKeyPemPath;
30-
let _sslCertPath: string = (defaultSettings as any).sslCertPemPath;
34+
let _tlsEnabled = defaultSettings.tls.enabled;
35+
let _tlsKeyPemPath = defaultSettings.tls.key;
36+
let _tlsCertPemPath = defaultSettings.tls.cert;
3137

3238
// Get configured proxy URL
3339
export const getProxyUrl = () => {
@@ -170,26 +176,39 @@ export const getPlugins = () => {
170176
_plugins = _userSettings.plugins;
171177
}
172178
return _plugins;
173-
}
179+
};
174180

175-
export const getSSLKeyPath = () => {
181+
export const getTLSKeyPemPath = () => {
176182
if (_userSettings && _userSettings.sslKeyPemPath) {
177-
_sslKeyPath = _userSettings.sslKeyPemPath;
183+
console.log(
184+
'Warning: sslKeyPemPath setting is replaced with tls.key setting in proxy.config.json & will be deprecated in a future release',
185+
);
186+
_tlsKeyPemPath = _userSettings.sslKeyPemPath;
178187
}
179-
if (!_sslKeyPath) {
180-
return '../../certs/key.pem';
188+
if (_userSettings?.tls && _userSettings?.tls?.key) {
189+
_tlsKeyPemPath = _userSettings.tls.key;
181190
}
182-
return _sslKeyPath;
191+
return _tlsKeyPemPath;
183192
};
184193

185-
export const getSSLCertPath = () => {
194+
export const getTLSCertPemPath = () => {
186195
if (_userSettings && _userSettings.sslCertPemPath) {
187-
_sslCertPath = _userSettings.sslCertPemPath;
196+
console.log(
197+
'Warning: sslCertPemPath setting is replaced with tls.cert setting in proxy.config.json & will be deprecated in a future release',
198+
);
199+
_tlsCertPemPath = _userSettings.sslCertPemPath;
188200
}
189-
if (!_sslCertPath) {
190-
return '../../certs/cert.pem';
201+
if (_userSettings?.tls && _userSettings?.tls?.cert) {
202+
_tlsCertPemPath = _userSettings.tls.cert;
203+
}
204+
return _tlsCertPemPath;
205+
};
206+
207+
export const getTLSEnabled = () => {
208+
if (_userSettings && _userSettings.tls && _userSettings.tls.enabled) {
209+
_tlsEnabled = _userSettings.tls.enabled;
191210
}
192-
return _sslCertPath;
211+
return _tlsEnabled;
193212
};
194213

195214
export const getDomains = () => {

src/config/types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ export interface UserSettings {
77
api: Record<string, any>;
88
cookieSecret: string;
99
sessionMaxAgeHours: number;
10-
sslKeyPemPath?: string; // Optional (not in config.schema.json)
11-
sslCertPemPath?: string; // Optional (not in config.schema.json)
10+
tls?: TLSConfig;
11+
sslCertPemPath?: string; // deprecated
12+
sslKeyPemPath?: string; // deprecated
1213
plugins: any[];
1314
commitConfig: Record<string, unknown>;
1415
attestationConfig: Record<string, unknown>;
@@ -19,6 +20,12 @@ export interface UserSettings {
1920
domains: Record<string, unknown>;
2021
}
2122

23+
export interface TLSConfig {
24+
enabled?: boolean;
25+
cert?: string;
26+
key?: string;
27+
}
28+
2229
export interface AuthorisedRepo {
2330
project: string;
2431
name: string;

src/proxy/index.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ import { router } from './routes';
88
import {
99
getAuthorisedList,
1010
getPlugins,
11-
getSSLCertPath,
12-
getSSLKeyPath
11+
getTLSKeyPemPath,
12+
getTLSCertPemPath,
13+
getTLSEnabled,
1314
} from '../config';
14-
import {
15-
addUserCanAuthorise,
16-
addUserCanPush,
17-
createRepo,
18-
getRepos
19-
} from '../db';
15+
import { addUserCanAuthorise, addUserCanPush, createRepo, getRepos } from '../db';
2016
import { PluginLoader } from '../plugin';
2117
import chain from './chain';
2218
import { Repo } from '../db/types';
@@ -28,8 +24,8 @@ const options = {
2824
inflate: true,
2925
limit: '100000kb',
3026
type: '*/*',
31-
key: fs.readFileSync(path.join(__dirname, getSSLKeyPath())),
32-
cert: fs.readFileSync(path.join(__dirname, getSSLCertPath())),
27+
key: getTLSEnabled() ? fs.readFileSync(path.join(__dirname, getTLSKeyPemPath())) : undefined,
28+
cert: getTLSEnabled() ? fs.readFileSync(path.join(__dirname, getTLSCertPemPath())) : undefined,
3329
};
3430

3531
const proxyPreparations = async () => {
@@ -66,15 +62,17 @@ const start = async () => {
6662
http.createServer(options as any, app).listen(proxyHttpPort, () => {
6763
console.log(`HTTP Proxy Listening on ${proxyHttpPort}`);
6864
});
69-
https.createServer(options, app).listen(proxyHttpsPort, () => {
70-
console.log(`HTTPS Proxy Listening on ${proxyHttpsPort}`);
71-
});
72-
65+
// Start HTTPS server only if TLS is enabled
66+
if (getTLSEnabled()) {
67+
https.createServer(options, app).listen(proxyHttpsPort, () => {
68+
console.log(`HTTPS Proxy Listening on ${proxyHttpsPort}`);
69+
});
70+
}
7371
return app;
7472
};
7573

7674
export default {
7775
proxyPreparations,
7876
createApp,
79-
start
77+
start,
8078
};

test/testConfig.test.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe('default configuration', function () {
1616
expect(config.getDatabase()).to.be.eql(defaultSettings.sink[0]);
1717
expect(config.getTempPasswordConfig()).to.be.eql(defaultSettings.tempPassword);
1818
expect(config.getAuthorisedList()).to.be.eql(defaultSettings.authorisedList);
19-
expect(config.getSSLKeyPath()).to.be.eql("../../certs/key.pem");
20-
expect(config.getSSLCertPath()).to.be.eql("../../certs/cert.pem");
19+
expect(config.getTLSKeyPemPath()).to.be.eql(defaultSettings.tls.key);
20+
expect(config.getTLSCertPemPath()).to.be.eql(defaultSettings.tls.cert);
2121
});
2222
after(function () {
2323
delete require.cache[require.resolve('../src/config')];
@@ -94,15 +94,17 @@ describe('user configuration', function () {
9494

9595
it('should override default settings for SSL certificate', function () {
9696
const user = {
97-
sslKeyPemPath: "my-key.pem",
98-
sslCertPemPath: "my-cert.pem"
97+
tls: {
98+
key: 'my-key.pem',
99+
cert: 'my-cert.pem',
100+
},
99101
};
100102
fs.writeFileSync(tempUserFile, JSON.stringify(user));
101103

102104
const config = require('../src/config');
103105

104-
expect(config.getSSLKeyPath()).to.be.eql(user.sslKeyPemPath);
105-
expect(config.getSSLCertPath()).to.be.eql(user.sslCertPemPath);
106+
expect(config.getTLSKeyPemPath()).to.be.eql(user.tls.key);
107+
expect(config.getTLSCertPemPath()).to.be.eql(user.tls.cert);
106108
});
107109

108110
afterEach(function () {
@@ -116,21 +118,14 @@ describe('validate config files', function () {
116118
const config = require('../src/config/file');
117119

118120
it('all valid config files should pass validation', function () {
119-
const validConfigFiles = [
120-
'proxy.config.valid-1.json',
121-
'proxy.config.valid-2.json',
122-
];
121+
const validConfigFiles = ['proxy.config.valid-1.json', 'proxy.config.valid-2.json'];
123122
for (const testConfigFile of validConfigFiles) {
124-
expect(config.validate(path.join(__dirname, fixtures, testConfigFile))).to
125-
.be.true;
123+
expect(config.validate(path.join(__dirname, fixtures, testConfigFile))).to.be.true;
126124
}
127125
});
128126

129127
it('all invalid config files should fail validation', function () {
130-
const invalidConfigFiles = [
131-
'proxy.config.invalid-1.json',
132-
'proxy.config.invalid-2.json',
133-
];
128+
const invalidConfigFiles = ['proxy.config.invalid-1.json', 'proxy.config.invalid-2.json'];
134129
for (const testConfigFile of invalidConfigFiles) {
135130
const test = function () {
136131
config.validate(path.join(__dirname, fixtures, testConfigFile));

0 commit comments

Comments
 (0)