mirror of
https://github.com/actions/create-github-app-token.git
synced 2026-12-01 14:55:26 +00:00
185 lines
7.4 KiB
JavaScript
185 lines
7.4 KiB
JavaScript
// Base for all `main` tests.
|
||
// @ts-check
|
||
import { createServer } from "node:http";
|
||
|
||
import { createMockAgent } from "./mock-agent.js";
|
||
|
||
export const DEFAULT_ENV = {
|
||
GITHUB_REPOSITORY_OWNER: "actions",
|
||
GITHUB_REPOSITORY: "actions/create-github-app-token",
|
||
// inputs are set as environment variables with the prefix INPUT_
|
||
// https://docs.github.com/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
|
||
"INPUT_GITHUB-API-URL": "https://api.github.com",
|
||
"INPUT_SKIP-TOKEN-REVOKE": "false",
|
||
"INPUT_CLIENT-ID": "Iv1.0123456789abcdef",
|
||
// This key is invalidated. It’s from https://github.com/octokit/auth-app.js/issues/465#issuecomment-1564998327.
|
||
"INPUT_PRIVATE-KEY": `-----BEGIN RSA PRIVATE KEY-----
|
||
MIIEowIBAAKCAQEA280nfuUM9w00Ib9E2rvZJ6Qu3Ua3IqR34ZlK53vn/Iobn2EL
|
||
Z9puc5Q/nFBU15NKwHyQNb+OG2hTCkjd1Xi9XPzEOH1r42YQmTGq8YCkUSkk6KZA
|
||
5dnhLwN9pFquT9fQgrf4r1D5GJj3rqvj8JDr1sBmunArqY5u4gziSrIohcjLIZV0
|
||
cIMz/RUIMe/EAsNeiwzEteHAtf/WpMs+OfF94SIUrDlkPr0H0H3DER8l1HZAvE0e
|
||
eD3ZJ6njrF6UHQWDVrekSTB0clpVTTU9TMpe+gs2nnFww9G8As+WsW8xHVjVipJy
|
||
AwqBhiR+s7wlcbh2i0NQqt8GL9/jIFTmleiwsQIDAQABAoIBAHNyP8pgl/yyzKzk
|
||
/0871wUBMTQ7zji91dGCaFtJM0HrcDK4D/uOOPEv7nE1qDpKPLr5Me1pHUS7+NGw
|
||
EAPtlNhgUtew2JfppdIwyi5qeOPADoi7ud6AH8xHsxg+IMwC+JuP8WhzyU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8zXfsigXCiejnS664oAuX8sQZID
|
||
x3WQZRiXlWejSMUAHuMwXrhGlltF3lw83+xAjnqsVp75kGS6OH61
|
||
-----END RSA PRIVATE KEY-----`,
|
||
// The Actions runner sets all inputs to empty strings if not set.
|
||
"INPUT_PERMISSION-ADMINISTRATION": "",
|
||
};
|
||
|
||
// Authenticate with GitHub Actions OIDC instead of a private key.
|
||
export const OIDC_ENV = {
|
||
...DEFAULT_ENV,
|
||
"INPUT_PRIVATE-KEY": "",
|
||
ACTIONS_ID_TOKEN_REQUEST_TOKEN: "runner-token",
|
||
};
|
||
|
||
/**
|
||
* Starts a local server that mimics the Actions OIDC token endpoint, because
|
||
* `core.getIDToken()` uses `@actions/http-client`, which `MockAgent` can't intercept.
|
||
*/
|
||
async function startOidcServer() {
|
||
/** @type {string[]} */
|
||
const requests = [];
|
||
const server = createServer((request, response) => {
|
||
requests.push(`${request.method} ${request.url}`);
|
||
const authorized = request.headers.authorization === "Bearer runner-token";
|
||
response.writeHead(authorized ? 200 : 401, {
|
||
"content-type": "application/json",
|
||
});
|
||
response.end(JSON.stringify(authorized ? { value: "oidc-token" } : { message: "Unauthorized" }));
|
||
});
|
||
await new Promise((resolve) => server.listen(0, "127.0.0.1", () => resolve(undefined)));
|
||
const { port } = /** @type {import("node:net").AddressInfo} */ (server.address());
|
||
process.env.ACTIONS_ID_TOKEN_REQUEST_URL = `http://127.0.0.1:${port}/token?api-version=2.0`;
|
||
|
||
// `core.getIDToken()` logs the token URL, so replace the random port to keep snapshots stable.
|
||
const write = process.stdout.write.bind(process.stdout);
|
||
process.stdout.write = /** @type {any} */ (
|
||
(/** @type {unknown} */ chunk, /** @type {any[]} */ ...args) =>
|
||
write(
|
||
typeof chunk === "string" ? chunk.replaceAll(`127.0.0.1:${port}`, "127.0.0.1:<port>") : chunk,
|
||
...args
|
||
)
|
||
);
|
||
|
||
return {
|
||
requests,
|
||
close: () => new Promise((resolve) => server.close(() => resolve(undefined))),
|
||
};
|
||
}
|
||
|
||
export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
|
||
for (const [key, value] of Object.entries(env)) {
|
||
process.env[key] = value;
|
||
}
|
||
|
||
// Set up mocking
|
||
const baseUrl = new URL(env["INPUT_GITHUB-API-URL"]);
|
||
const basePath = baseUrl.pathname === "/" ? "" : baseUrl.pathname;
|
||
const mockAgent = createMockAgent({ enableCallHistory: true });
|
||
const mockPool = mockAgent.get(baseUrl.origin);
|
||
|
||
// Calling `auth({ type: "app" })` to obtain a JWT doesn’t make network requests, so no need to intercept.
|
||
|
||
// Without a private key, the action authenticates with an OIDC token and passes `client_id`.
|
||
const oidcServer = env["INPUT_PRIVATE-KEY"] ? undefined : await startOidcServer();
|
||
const clientIdQuery = oidcServer ? `?client_id=${env["INPUT_CLIENT-ID"]}` : "";
|
||
// JWT creation is not idempotent, so the `authorization` header is only asserted for OIDC.
|
||
const authorization = oidcServer ? { authorization: "Bearer oidc-token" } : {};
|
||
|
||
// Mock installation ID and app slug request
|
||
const mockInstallationId = "123456";
|
||
const mockAppSlug = "github-actions";
|
||
const owner = env.INPUT_OWNER ?? env.GITHUB_REPOSITORY_OWNER;
|
||
const currentRepoName = env.GITHUB_REPOSITORY.split("/")[1];
|
||
const firstRepositoryInput =
|
||
(env.INPUT_REPOSITORIES ?? currentRepoName)
|
||
.split(/[\n,]+/)
|
||
.map((repository) => repository.trim())
|
||
.find(Boolean) ?? currentRepoName;
|
||
const repo = encodeURIComponent(
|
||
firstRepositoryInput.split("/").pop() || firstRepositoryInput
|
||
);
|
||
|
||
mockPool
|
||
.intercept({
|
||
path: `${basePath}/repos/${owner}/${repo}/installation${clientIdQuery}`,
|
||
method: "GET",
|
||
headers: {
|
||
accept: "application/vnd.github.v3+json",
|
||
"user-agent": "actions/create-github-app-token",
|
||
...authorization,
|
||
},
|
||
})
|
||
.reply(
|
||
200,
|
||
{ id: mockInstallationId, app_slug: mockAppSlug },
|
||
{ headers: { "content-type": "application/json" } }
|
||
);
|
||
|
||
// Mock installation access token request
|
||
const mockInstallationAccessToken =
|
||
"ghs_16C7e42F292c6912E7710c838347Ae178B4a"; // This token is invalidated. It’s from https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app.
|
||
const mockExpiresAt = "2016-07-11T22:14:10Z";
|
||
|
||
mockPool
|
||
.intercept({
|
||
path: `${basePath}/app/installations/${mockInstallationId}/access_tokens${clientIdQuery}`,
|
||
method: "POST",
|
||
headers: {
|
||
accept: "application/vnd.github.v3+json",
|
||
"user-agent": "actions/create-github-app-token",
|
||
...authorization,
|
||
},
|
||
})
|
||
.reply(
|
||
201,
|
||
{ token: mockInstallationAccessToken, expires_at: mockExpiresAt },
|
||
{ headers: { "content-type": "application/json" } }
|
||
);
|
||
|
||
// Run the callback
|
||
cb(mockPool);
|
||
|
||
// Run the main script
|
||
const { default: promise } = await import("../main.js");
|
||
await promise;
|
||
|
||
if (oidcServer) {
|
||
await oidcServer.close();
|
||
console.log("--- OIDC REQUESTS ---");
|
||
console.log(oidcServer.requests.join("\n"));
|
||
}
|
||
|
||
console.log("--- REQUESTS ---");
|
||
const calls = mockAgent
|
||
.getCallHistory()
|
||
.calls()
|
||
.map((call) => {
|
||
const route = `${call.method} ${call.path}${new URL(call.fullUrl).search}`;
|
||
if (call.method === "GET") return route;
|
||
|
||
return `${route}\n${call.body}`;
|
||
});
|
||
|
||
console.log(calls.join("\n"));
|
||
}
|