-
Notifications
You must be signed in to change notification settings - Fork 52
/
spandx.config.js
executable file
·213 lines (183 loc) · 7.03 KB
/
spandx.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*global module, process, require, __dirname*/
const tryRequire = require('try-require');
const lodash = require('lodash');
const base64 = require('base-64');
const jwt = require('jsonwebtoken');
const cookie = require('cookie');
const fs = require('fs');
const url = require('url-parse');
const localhost = (process.env.PLATFORM === 'linux') ? 'localhost' : 'host.docker.internal';
const protocol = (process.env.SSL === 'true') ? 'https' : 'http';
const port = process.env.PORT || 8002;
const useCloud = process.env.USE_CLOUD;
const PORTAL_BACKEND_MARKER = 'PORTAL_BACKEND_MARKER';
const keycloakPubkeys = {
prod: fs.readFileSync(__dirname + '/certs/keycloak.prod.cert', 'utf8'),
stage: fs.readFileSync(__dirname + '/certs/keycloak.stage.cert', 'utf8'),
qa: fs.readFileSync(__dirname + '/certs/keycloak.qa.cert', 'utf8')
};
const buildUser = input => {
const user = {
entitlements: {
insights: { is_entitled: true },
smart_management: { is_entitled: true },
openshift: { is_entitled: true },
hybrid: { is_entitled: true },
migrations: { is_entitled: true },
ansible: { is_entitled: true }
},
identity: {
account_number: input.account_number,
type: 'User',
user: {
username: input.username,
email: input.email,
first_name: input.first_name,
last_name: input.last_name,
is_active: true,
is_org_admin: input.is_org_admin,
is_internal: input.is_internal,
locale: input.locale
},
internal: {
org_id: input.account_id
}
}
};
return user;
};
const envMap = {
ci: {
keycloakPubkey: keycloakPubkeys.qa,
target: `https://ci.${useCloud ? 'cloud' : 'console'}.redhat.com`,
str: 'ci'
},
qa: {
keycloakPubkey: keycloakPubkeys.qa,
target: `https://qa.${useCloud ? 'cloud' : 'console'}.redhat.com`,
str: 'qa'
},
stage: {
keycloakPubkey: keycloakPubkeys.stage,
target: `https://${useCloud ? 'cloud' : 'console'}.stage.redhat.com`,
str: 'stage'
},
prod: {
keycloakPubkey: keycloakPubkeys.prod,
target: `https://${useCloud ? 'cloud' : 'console'}.redhat.com`,
str: 'prod'
}
};
const authPlugin = (req, res, target) => {
let env = envMap.prod;
switch (req.headers['x-spandx-origin'].replace(/http[s]?:\/\//, '').replace(':1337', '')) {
case 'ci.foo.redhat.com': env = envMap.ci; break;
case 'qa.foo.redhat.com': env = envMap.qa; break;
case 'stage.foo.redhat.com': env = envMap.stage; break;
case 'prod.foo.redhat.com': env = envMap.prod; break;
default: env = false;
}
if (target && target.includes(PORTAL_BACKEND_MARKER)) {
target = target.replace(PORTAL_BACKEND_MARKER, env.target);
console.log(` --> mangled ${PORTAL_BACKEND_MARKER} to ${target}`);
}
const noop = { then: (cb) => { cb(target); } };
if (!req || !req.headers || !req.headers.cookie) { return noop; } // no cookies short circut
const cookies = cookie.parse(req.headers.cookie);
if (!cookies.cs_jwt) { return noop; } // no rh_jwt short circut
return new Promise (function (resolve, reject) {
jwt.verify(cookies.cs_jwt, env.keycloakPubkey, {}, function jwtVerifyPromise(err, decoded) {
if (err) { resolve(target); return; } // silently miss on error
const user = buildUser(decoded);
const unicodeUser = new Buffer(JSON.stringify(user), "utf8");
req.headers["x-rh-identity"] = unicodeUser.toString("base64");
resolve(target);
});
});
};
const defaults = {
routerPlugin: authPlugin,
bs: {
notify: false,
https: {
key: __dirname + '/ssl/key.pem',
cert: __dirname + '/ssl/cert.pem'
}
},
esi: {
allowedHosts: [
/^https:\/\/.*(cloud|console).redhat.com$/
]
},
host: {
'ci.foo.redhat.com': 'ci.foo.redhat.com',
'qa.foo.redhat.com': 'qa.foo.redhat.com',
'stage.foo.redhat.com': 'stage.foo.redhat.com',
'prod.foo.redhat.com': 'prod.foo.redhat.com'
},
port: process.env.SPANDX_PORT || 1337,
open: false,
startPath: '/',
verbose: true,
routes: {}
};
if (process.env.LOCAL_CHROME === 'true') {
defaults.routes['/apps/chrome'] = '/chrome/';
defaults.routes['/beta/apps/chrome'] = '/chrome/';
} else {
defaults.routes['/apps/chrome'] = { host: PORTAL_BACKEND_MARKER };
defaults.routes['/apps/beta/chrome'] = { host: PORTAL_BACKEND_MARKER };
}
defaults.routes['/'] = { host: PORTAL_BACKEND_MARKER };
const CUSTOM_CONF_PATH = process.env.CUSTOM_CONF_PATH || '/config/spandx.config.js';
if (process.env.CUSTOM_CONF === 'true') {
if (!fs.existsSync(CUSTOM_CONF_PATH)) {
console.log(`Error: CUSTOM_CONF is set to true but custom conf file is missing (container looks at ${CUSTOM_CONF_PATH})`);
process.exit(1);
}
try {
fs.readFileSync(CUSTOM_CONF_PATH, 'utf8');
} catch (e) {
console.log(`Error: CUSTOM_CONF is set to true but custom conf file is unreadable`);
console.log('This may be a simple permissions error or an selinux error');
console.log('Hint: if it is selinux try `sudo chcon -Rt svirt_sandbox_file_t` on the custom config file');
console.log('\n\nDetails:');
console.log(e);
process.exit(1);
}
}
const custom = tryRequire(CUSTOM_CONF_PATH) || {};
let foundHostDockerInternal = false;
if (process.env.NO_LOCALHOST_REWRITE !== 'true') {
lodash.forOwn(custom.routes, function (route) {
if (route.host) {
const u = url(route.host);
if (u.hostname.indexOf('host.docker.internal') === 0) {
foundHostDockerInternal = true;
}
if (u.hostname.indexOf('localhost') === 0) {
u.set('hostname', localhost);
route.host = u.toString();
}
}
});
if (foundHostDockerInternal) {
console.log();
console.log('Warning: the usage of host.docker.internal in your overide config is deprecated');
console.log('The proxy container will substitute localhost -> host.docker.internal for you');
console.log('If you really want to use this and ignore this warning rerun with NO_LOCALHOST_REWRITE=true');
console.log('This will be an error in 3.2.x');
console.log();
}
}
const ret = lodash.defaultsDeep(custom, defaults);
console.log('#################################');
console.log(`# Insights Proxy version: ${require('./package.json').version} #`);
console.log('#################################');
console.log('\nUsing this Spandx config:');
console.log(ret);
console.log('\nFor more info see: https://github.com/redhataccess/spandx\n');
process.on('SIGINT', function() {
process.exit();
});
module.exports = ret;