Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ describe("resolveArgosToken", () => {

it("throws when the GitHub OIDC endpoint fails", async () => {
server.use(
http.get(MOCK_OIDC_URL, () => HttpResponse.json(null, { status: 500 })),
http.get(MOCK_OIDC_URL, () => HttpResponse.json(null, { status: 403 })),
);
await expect(resolveArgosToken(baseConfig)).rejects.toThrow(
"Failed to fetch GitHub Actions OIDC token: 500",
"Failed to fetch GitHub Actions OIDC token: 403",
);
});

Expand Down
73 changes: 62 additions & 11 deletions packages/core/src/github-actions-oidc.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { http, HttpResponse } from "msw";
import {
isGitHubActionsOidcAvailable,
Expand Down Expand Up @@ -77,18 +77,69 @@ describe("exchangeGitHubActionsOidcToken", () => {
expect(token).toBe(MOCK_ARGOS_TOKEN);
});

it("throws when the GitHub OIDC endpoint returns an error", async () => {
server.use(
http.get(MOCK_OIDC_URL, () => {
return HttpResponse.json(null, { status: 500 });
}),
);
await expect(
exchangeGitHubActionsOidcToken({
describe("when the GitHub OIDC endpoint returns an error", () => {
// Skips the waits between retries.
beforeEach(() => {
vi.useFakeTimers({ toFake: ["setTimeout"] });
});
afterEach(() => {
vi.useRealTimers();
});

/** Answers `status` to the first `times` token requests, then a token. */
function failOidcRequests(status: number, times = Infinity) {
let requestCount = 0;
server.use(
http.get(MOCK_OIDC_URL, () => {
requestCount++;
if (requestCount <= times) {
return HttpResponse.json(null, { status });
}
return HttpResponse.json({ value: MOCK_OIDC_TOKEN, count: 1 });
}),
);
return { getRequestCount: () => requestCount };
}

async function exchangeSkippingWaits() {
const result = exchangeGitHubActionsOidcToken({
apiBaseUrl: "https://api.argos-ci.com/v2/",
config: baseConfig,
}),
).rejects.toThrow("Failed to fetch GitHub Actions OIDC token: 500");
});
let settled = false;
result.then(
() => (settled = true),
() => (settled = true),
);
// Each wait is scheduled only once its failed response has arrived.
while (!settled) {
await new Promise((resolve) => setImmediate(resolve));
await vi.runOnlyPendingTimersAsync();
}
return result;
}

it("retries a server error", async () => {
const oidc = failOidcRequests(503, 1);
await expect(exchangeSkippingWaits()).resolves.toBe(MOCK_ARGOS_TOKEN);
expect(oidc.getRequestCount()).toBe(2);
});

it("throws once the retries for server errors run out", async () => {
const oidc = failOidcRequests(503);
await expect(exchangeSkippingWaits()).rejects.toThrow(
"Failed to fetch GitHub Actions OIDC token: 503",
);
expect(oidc.getRequestCount()).toBe(4);
});

it("does not retry a client error", async () => {
const oidc = failOidcRequests(403);
await expect(exchangeSkippingWaits()).rejects.toThrow(
"Failed to fetch GitHub Actions OIDC token: 403",
);
expect(oidc.getRequestCount()).toBe(1);
});
});

it("throws when the Argos API exchange returns an error", async () => {
Expand Down
41 changes: 32 additions & 9 deletions packages/core/src/github-actions-oidc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createClient, throwAPIError } from "@argos-ci/api-client";
import pRetry from "p-retry";
import type { Config } from "./config";
import { debug } from "./debug";

/**
* Check if GitHub Actions OIDC is available for auto-detection.
Expand All @@ -25,18 +27,39 @@ async function fetchOidcToken(args: { audience: string }): Promise<string> {
const url = new URL(process.env.ACTIONS_ID_TOKEN_REQUEST_URL);
url.searchParams.set("audience", args.audience);

const response = await fetch(url.toString(), {
headers: {
Authorization: `Bearer ${process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN}`,
Accept: "application/json; api-version=2.0",
"Content-Type": "application/json",
const getErrorMessage = (response: Response) =>
`Failed to fetch GitHub Actions OIDC token: ${response.status} ${response.statusText}`;

// One 5xx would fail the whole upload. `@actions/core` retries this
// endpoint too.
const response = await pRetry(
async () => {
const response = await fetch(url.toString(), {
headers: {
Authorization: `Bearer ${process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN}`,
Accept: "application/json; api-version=2.0",
"Content-Type": "application/json",
},
});
if (response.status >= 500) {
throw new Error(getErrorMessage(response));
}
return response;
},
});
{
retries: 3,
onFailedAttempt: ({ error, retriesLeft, retryDelay }) => {
if (retriesLeft > 0) {
debug(
`${error.message}, retrying in ${retryDelay}ms (${retriesLeft} left)`,
);
}
},
},
);

if (!response.ok) {
throw new Error(
`Failed to fetch GitHub Actions OIDC token: ${response.status} ${response.statusText}`,
);
throw new Error(getErrorMessage(response));
}

const data = (await response.json()) as { value?: string };
Expand Down