Repository navigation
Expand file tree
/
Copy pathcredentials.mjs
More file actions
75 lines (69 loc) · 2.82 KB
/
Copy pathcredentials.mjs
File metadata and controls
75 lines (69 loc) · 2.82 KB
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
// Resolves the deploy credential. An explicit PRISMA_SERVICE_TOKEN wins;
// otherwise a repository connected through the Prisma Console exchanges the
// GitHub OIDC token of this run for a short-lived workspace token.
const EXCHANGE_PATH = "/v1/auth/github-actions/token";
const OIDC_AUDIENCE = "prisma-cloud";
const TIMEOUT_MS = 15_000;
async function fetchJsonWithRetry(fetchImpl, url, init) {
let lastError;
for (let attempt = 0; attempt < 2; attempt++) {
try {
const response = await fetchImpl(url, {
...init,
signal: AbortSignal.timeout(TIMEOUT_MS),
});
// 5xx is retryable; anything else is a definitive answer.
if (response.status >= 500) {
lastError = new Error(`${url} responded ${response.status}`);
continue;
}
return response;
} catch (error) {
lastError = error;
}
}
throw lastError;
}
/**
* Returns one of:
* { source: "env", token } explicit PRISMA_SERVICE_TOKEN
* { source: "oidc", token, workspaceId } exchanged short-lived token
* { source: "oidc", denied: true } the platform refused the exchange
* { source: "none" } no credential path available
* Throws on transient failures (network errors, 5xx after retry), which the
* caller must surface as a real failure rather than a quiet skip.
*/
export async function resolveCredential(env, apiUrl, { fetchImpl = fetch } = {}) {
const explicit = (env.PRISMA_SERVICE_TOKEN ?? "").trim();
if (explicit) return { source: "env", token: explicit };
const requestUrl = env.ACTIONS_ID_TOKEN_REQUEST_URL;
const requestToken = env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
if (!requestUrl || !requestToken) return { source: "none" };
const oidcResponse = await fetchJsonWithRetry(
fetchImpl,
`${requestUrl}&audience=${OIDC_AUDIENCE}`,
{ headers: { authorization: `Bearer ${requestToken}` } },
);
if (!oidcResponse.ok) {
throw new Error(`GitHub OIDC token request responded ${oidcResponse.status}`);
}
const oidcToken = (await oidcResponse.json()).value;
const exchangeResponse = await fetchJsonWithRetry(
fetchImpl,
`${apiUrl.replace(/\/$/, "")}${EXCHANGE_PATH}`,
{
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ token: oidcToken }),
},
);
// The exchange returns one generic 401 for every refusal (unknown repo,
// archived link, uninstalled app). That is an entitlement answer, not an
// outage, so it maps to the skip path.
if (exchangeResponse.status === 401) return { source: "oidc", denied: true };
if (!exchangeResponse.ok) {
throw new Error(`credential exchange responded ${exchangeResponse.status}`);
}
const { data } = await exchangeResponse.json();
return { source: "oidc", token: data.value, workspaceId: data.workspaceId };
}