Skip to content

Commit 09924ce

Browse files
authored
feat: Extract Credential from 'authorization' header and set it as 'A… (#94)
1 parent d98f852 commit 09924ce

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

charts/internal-gateway/files/conf.d/s3-gateway.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ server {
3232
proxy_read_timeout 60s;
3333
proxy_send_timeout 60s;
3434

35+
js_set $auth_header auth.setAuthHeader;
36+
37+
proxy_set_header Authorization $auth_header;
38+
3539
proxy_pass http://{{ index $vals "codefresh" "serviceEndpoints" "cfapi-auth" "svc" }}:{{ index $vals "codefresh" "serviceEndpoints" "cfapi-auth" "port" }};
3640
}
3741
}
Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
11
function account_name(r) {
2+
// Get the variable "auth_entity"
23
const auth_entity = r.variables["auth_entity"];
3-
const b64decoded = Buffer.from(auth_entity, 'base64');
4-
const json = JSON.parse(b64decoded);
5-
const account_name = json.authenticatedEntity.activeAccount.name;
4+
if (!auth_entity) {
5+
r.error("account_name: auth_entity variable is missing");
6+
return "default";
7+
}
8+
r.error("account_name: auth_entity = " + auth_entity);
69

7-
return account_name;
10+
// Attempt to base64-decode it
11+
var decoded;
12+
try {
13+
decoded = Buffer.from(auth_entity, 'base64').toString('utf8');
14+
r.error("account_name: decoded auth_entity = " + decoded);
15+
} catch (e) {
16+
r.error("account_name: error decoding auth_entity: " + e);
17+
return "default";
18+
}
19+
20+
// Try to parse the decoded value as JSON
21+
var json;
22+
try {
23+
json = JSON.parse(decoded);
24+
r.error("account_name: parsed JSON = " + JSON.stringify(json));
25+
} catch (e) {
26+
r.error("account_name: JSON parse error: " + e);
27+
return "default";
28+
}
29+
30+
// Extract the account name
31+
if (json.activeAccount && json.activeAccount.name) {
32+
r.error("account_name: extracted account name = " + json.activeAccount.name);
33+
return json.activeAccount.name;
34+
} else {
35+
r.error("account_name: activeAccount.name not found in JSON");
36+
return "default";
37+
}
38+
}
39+
40+
function setAuthHeader(r) {
41+
let auth = r.headersIn['authorization'];
42+
if (auth) {
43+
// Look for the pattern: Credential=<value>/...
44+
let matches = auth.match(/Credential=([^\/]+)\//);
45+
if (matches && matches.length > 1) {
46+
return matches[1];
47+
}
48+
}
49+
return "";
850
}
951

10-
export default {account_name};
52+
export default { account_name, setAuthHeader };

0 commit comments

Comments
 (0)