Skip to content

Commit 7270ea5

Browse files
authored
Merge pull request #10 from softrams/IDDOC-61984
2 parents e564e30 + 67def69 commit 7270ea5

File tree

6 files changed

+95
-134
lines changed

6 files changed

+95
-134
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,5 @@ dist
105105

106106
# Local Development
107107
localDevelopment
108-
docker-compose.yaml
108+
docker-compose.yaml
109+
.DS_Store

.nvmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lts/Iron
2+
# specify that we want the LTS version of node.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "http://softrams.io/schemas/prefect-auth-proxy-permitted-routes.json",
4+
"type": "object",
5+
"properties": {"$schema":true},
6+
"patternProperties": {
7+
"(GET|POST|PUT|DELETE)": {
8+
"type": "array",
9+
"description": "HTTP METHOD with array of filter strings for permitted routes for the METHOD",
10+
"items": {
11+
"type": "string",
12+
"description": "Pattern to match against for the URL"
13+
},
14+
"examples": [["*","*/filters"],["*/filters","*/count"]]
15+
}
16+
17+
},
18+
"additionalProperties": false
19+
}

index.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ const config = {
3232
PORT: process.env.PORT || 3000,
3333
LOG_LEVEL_OVERRIDE_DURATION: process.env.LOG_LEVEL_OVERRIDE_DURATION || 300,
3434
ENV: process.env.ENV || "NA",
35+
PERMITTED_ROUTES_FILE: process.env.PERMITTED_ROUTES_FILE || "./config/permitted-routes.json",
36+
PERMITTED_ROUTES_JSON: process.env.PERMITTED_ROUTES_JSON || ""
3537
};
3638

37-
if (fs.existsSync("./config/permitted-routes.json")) {
38-
config.PERMITTED_ROUTES = JSON.parse(fs.readFileSync("./config/permitted-routes.json"));
39+
if (fs.existsSync(config.PERMITTED_ROUTES_FILE)) {
40+
if (config.PERMITTED_ROUTES_JSON) {
41+
config.PERMITTED_ROUTES = JSON.parse(config.PERMITTED_ROUTES_JSON)
42+
}
43+
else {
44+
config.PERMITTED_ROUTES = JSON.parse(fs.readFileSync(config.PERMITTED_ROUTES_FILE));
45+
}
3946
}
4047

4148
// #endregion
@@ -46,19 +53,19 @@ const gLogFunc = console.log;
4653
const gWarnFunc = console.warn;
4754

4855
function initLogLevels(level) {
49-
console.debug = () => {};
50-
console.trace = () => {};
51-
console.info = () => {};
52-
console.warn = () => {};
53-
console.log = () => {};
56+
console.debug = () => { };
57+
console.trace = () => { };
58+
console.info = () => { };
59+
console.warn = () => { };
60+
console.log = () => { };
5461

5562
if (level === "warn") {
56-
console.log = () => {};
63+
console.log = () => { };
5764
console.warn = gWarnFunc;
5865
}
5966
if (level === "error") {
60-
console.log = () => {};
61-
console.warn = () => {};
67+
console.log = () => { };
68+
console.warn = () => { };
6269
}
6370
if (level === "info") {
6471
console.log = gLogFunc;
@@ -179,7 +186,12 @@ async function fetchAPIKeysInfo(key) {
179186
}
180187
});
181188
}
182-
189+
/**
190+
*
191+
* @param {String} url "Url to validate against route rules"
192+
* @param {Array} routes "Allowed or Permitted routes to match against"
193+
* @returns
194+
*/
183195
const checkRoutes = (url, routes) => {
184196
if (!routes || !routes.length) {
185197
return false;
@@ -203,7 +215,6 @@ const allowPassthrough = (url, method, acl) => {
203215
if (config.ALLOW_PUBLIC_ACCESS) {
204216
return true;
205217
}
206-
207218
// check permitted routes
208219
if (config.PERMITTED_ROUTES && config.PERMITTED_ROUTES[method]?.length && checkRoutes(url, config.PERMITTED_ROUTES[method])) {
209220
return true;
@@ -213,7 +224,6 @@ const allowPassthrough = (url, method, acl) => {
213224
if (checkRoutes(url, acl?.ops)) {
214225
return true;
215226
}
216-
217227
return false;
218228
};
219229
// #endregion
@@ -257,6 +267,7 @@ app.use(async (req, res, next) => {
257267
}
258268

259269
if (!allowPassthrough(req.url, req.method, req.acl)) {
270+
console.warn(`AUTH-PROXY-AUDIT: ${req.url}`)
260271
return res.status(401).send("Unauthorized");
261272
} else {
262273
next();
@@ -325,6 +336,9 @@ app.use(
325336
// Proxy request to end point
326337
const writeBody = (bodyData) => {
327338
proxyReq.setHeader("Content-Length", Buffer.byteLength(bodyData));
339+
if (config.LOG_LEVEL === "debug") {
340+
proxyReq.removeHeader('Accept-Encoding')
341+
}
328342
proxyReq.write(bodyData);
329343
};
330344

0 commit comments

Comments
 (0)