feat: add github_action to ua string
This commit is contained in:
parent
f8d102ad7d
commit
15f02495fa
3 changed files with 42 additions and 42 deletions
26
dist/index.js
generated
vendored
26
dist/index.js
generated
vendored
|
|
@ -71011,24 +71011,20 @@ var MAX_TAG_VALUE_LENGTH = 256;
|
||||||
var SANITIZATION_CHARACTER = "_";
|
var SANITIZATION_CHARACTER = "_";
|
||||||
var SPECIAL_CHARS_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]+/;
|
var SPECIAL_CHARS_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]+/;
|
||||||
var USER_AGENT_PREFIX = "configure-aws-credentials-for-github-actions";
|
var USER_AGENT_PREFIX = "configure-aws-credentials-for-github-actions";
|
||||||
var RUN_ID_PATTERN = /^[0-9]{1,20}$/;
|
var UA_FIELDS = [
|
||||||
var ATTEMPT_PATTERN = /^[0-9]{1,10}$/;
|
{ env: "GITHUB_ACTION", label: "action", pattern: /^[A-Za-z0-9_-]{1,128}$/ },
|
||||||
|
{ env: "GITHUB_RUN_ID", label: "run_id", pattern: /^[0-9]{1,20}$/ },
|
||||||
|
{ env: "GITHUB_RUN_ATTEMPT", label: "attempt", pattern: /^[0-9]{1,10}$/ }
|
||||||
|
];
|
||||||
function buildCustomUserAgent() {
|
function buildCustomUserAgent() {
|
||||||
const tokens = [[USER_AGENT_PREFIX]];
|
const tokens = [[USER_AGENT_PREFIX]];
|
||||||
const runId = process.env.GITHUB_RUN_ID;
|
for (const { env, label, pattern } of UA_FIELDS) {
|
||||||
const attempt = process.env.GITHUB_RUN_ATTEMPT;
|
const value = process.env[env];
|
||||||
if (runId !== void 0) {
|
if (value === void 0) continue;
|
||||||
if (RUN_ID_PATTERN.test(runId)) {
|
if (pattern.test(value)) {
|
||||||
tokens.push(["md", `run_id#${runId}`]);
|
tokens.push(["md", `${label}#${value}`]);
|
||||||
} else {
|
} else {
|
||||||
warning("GITHUB_RUN_ID has unexpected format; omitting from User-Agent");
|
warning(`${env} has unexpected format; omitting from User-Agent`);
|
||||||
}
|
|
||||||
}
|
|
||||||
if (attempt !== void 0) {
|
|
||||||
if (ATTEMPT_PATTERN.test(attempt)) {
|
|
||||||
tokens.push(["md", `attempt#${attempt}`]);
|
|
||||||
} else {
|
|
||||||
warning("GITHUB_RUN_ATTEMPT has unexpected format; omitting from User-Agent");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tokens;
|
return tokens;
|
||||||
|
|
|
||||||
|
|
@ -8,25 +8,21 @@ const MAX_TAG_VALUE_LENGTH = 256;
|
||||||
const SANITIZATION_CHARACTER = '_';
|
const SANITIZATION_CHARACTER = '_';
|
||||||
const SPECIAL_CHARS_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]+/;
|
const SPECIAL_CHARS_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]+/;
|
||||||
const USER_AGENT_PREFIX = 'configure-aws-credentials-for-github-actions';
|
const USER_AGENT_PREFIX = 'configure-aws-credentials-for-github-actions';
|
||||||
const RUN_ID_PATTERN = /^[0-9]{1,20}$/;
|
const UA_FIELDS: ReadonlyArray<{ env: string; label: string; pattern: RegExp }> = [
|
||||||
const ATTEMPT_PATTERN = /^[0-9]{1,10}$/;
|
{ env: 'GITHUB_ACTION', label: 'action', pattern: /^[A-Za-z0-9_-]{1,128}$/ },
|
||||||
|
{ env: 'GITHUB_RUN_ID', label: 'run_id', pattern: /^[0-9]{1,20}$/ },
|
||||||
|
{ env: 'GITHUB_RUN_ATTEMPT', label: 'attempt', pattern: /^[0-9]{1,10}$/ },
|
||||||
|
];
|
||||||
|
|
||||||
export function buildCustomUserAgent(): UserAgent {
|
export function buildCustomUserAgent(): UserAgent {
|
||||||
const tokens: UserAgent = [[USER_AGENT_PREFIX]];
|
const tokens: UserAgent = [[USER_AGENT_PREFIX]];
|
||||||
const runId = process.env.GITHUB_RUN_ID;
|
for (const { env, label, pattern } of UA_FIELDS) {
|
||||||
const attempt = process.env.GITHUB_RUN_ATTEMPT;
|
const value = process.env[env];
|
||||||
if (runId !== undefined) {
|
if (value === undefined) continue;
|
||||||
if (RUN_ID_PATTERN.test(runId)) {
|
if (pattern.test(value)) {
|
||||||
tokens.push(['md', `run_id#${runId}`]);
|
tokens.push(['md', `${label}#${value}`]);
|
||||||
} else {
|
} else {
|
||||||
core.warning('GITHUB_RUN_ID has unexpected format; omitting from User-Agent');
|
core.warning(`${env} has unexpected format; omitting from User-Agent`);
|
||||||
}
|
|
||||||
}
|
|
||||||
if (attempt !== undefined) {
|
|
||||||
if (ATTEMPT_PATTERN.test(attempt)) {
|
|
||||||
tokens.push(['md', `attempt#${attempt}`]);
|
|
||||||
} else {
|
|
||||||
core.warning('GITHUB_RUN_ATTEMPT has unexpected format; omitting from User-Agent');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tokens;
|
return tokens;
|
||||||
|
|
|
||||||
|
|
@ -1218,9 +1218,7 @@ describe('Configure AWS Credentials', {}, () => {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: any required to mock private method
|
// biome-ignore lint/suspicious/noExplicitAny: any required to mock private method
|
||||||
vi.spyOn(CredentialsClient.prototype as any, 'loadCredentials').mockRejectedValue(
|
vi.spyOn(CredentialsClient.prototype as any, 'loadCredentials').mockRejectedValue(new Error('network glitch'));
|
||||||
new Error('network glitch'),
|
|
||||||
);
|
|
||||||
await run();
|
await run();
|
||||||
expect(core.setFailed).toHaveBeenCalled();
|
expect(core.setFailed).toHaveBeenCalled();
|
||||||
expect(core.info).not.toHaveBeenCalledWith(expect.stringContaining('Retry'));
|
expect(core.info).not.toHaveBeenCalledWith(expect.stringContaining('Retry'));
|
||||||
|
|
@ -1267,13 +1265,15 @@ describe('Configure AWS Credentials', {}, () => {
|
||||||
return (client.stsClient.config as any).customUserAgent;
|
return (client.stsClient.config as any).customUserAgent;
|
||||||
}
|
}
|
||||||
|
|
||||||
it('includes run_id and attempt tokens when env vars are valid', async () => {
|
it('includes action, run_id and attempt tokens when env vars are valid', async () => {
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
|
process.env.GITHUB_ACTION = '__run_2';
|
||||||
process.env.GITHUB_RUN_ID = '16412345678';
|
process.env.GITHUB_RUN_ID = '16412345678';
|
||||||
process.env.GITHUB_RUN_ATTEMPT = '1';
|
process.env.GITHUB_RUN_ATTEMPT = '1';
|
||||||
const ua = await getCustomUserAgent();
|
const ua = await getCustomUserAgent();
|
||||||
expect(ua).toEqual([
|
expect(ua).toEqual([
|
||||||
['configure-aws-credentials-for-github-actions'],
|
['configure-aws-credentials-for-github-actions'],
|
||||||
|
['md', 'action#__run_2'],
|
||||||
['md', 'run_id#16412345678'],
|
['md', 'run_id#16412345678'],
|
||||||
['md', 'attempt#1'],
|
['md', 'attempt#1'],
|
||||||
]);
|
]);
|
||||||
|
|
@ -1282,6 +1282,7 @@ describe('Configure AWS Credentials', {}, () => {
|
||||||
|
|
||||||
it('omits tokens when env vars are unset, with no warning', async () => {
|
it('omits tokens when env vars are unset, with no warning', async () => {
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
|
delete process.env.GITHUB_ACTION;
|
||||||
const ua = await getCustomUserAgent();
|
const ua = await getCustomUserAgent();
|
||||||
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
|
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
|
||||||
expect(core.warning).not.toHaveBeenCalled();
|
expect(core.warning).not.toHaveBeenCalled();
|
||||||
|
|
@ -1289,26 +1290,33 @@ describe('Configure AWS Credentials', {}, () => {
|
||||||
|
|
||||||
it('warns and skips when env vars are malformed', async () => {
|
it('warns and skips when env vars are malformed', async () => {
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
|
process.env.GITHUB_ACTION = '$(curl evil)';
|
||||||
process.env.GITHUB_RUN_ID = '$(curl evil)';
|
process.env.GITHUB_RUN_ID = '$(curl evil)';
|
||||||
process.env.GITHUB_RUN_ATTEMPT = '1; rm -rf /';
|
process.env.GITHUB_RUN_ATTEMPT = '1; rm -rf /';
|
||||||
const ua = await getCustomUserAgent();
|
const ua = await getCustomUserAgent();
|
||||||
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
|
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
|
||||||
expect(core.warning).toHaveBeenCalledWith(
|
expect(core.warning).toHaveBeenCalledWith('GITHUB_ACTION has unexpected format; omitting from User-Agent');
|
||||||
'GITHUB_RUN_ID has unexpected format; omitting from User-Agent',
|
expect(core.warning).toHaveBeenCalledWith('GITHUB_RUN_ID has unexpected format; omitting from User-Agent');
|
||||||
);
|
expect(core.warning).toHaveBeenCalledWith('GITHUB_RUN_ATTEMPT has unexpected format; omitting from User-Agent');
|
||||||
expect(core.warning).toHaveBeenCalledWith(
|
expect(core.warning).toHaveBeenCalledTimes(3);
|
||||||
'GITHUB_RUN_ATTEMPT has unexpected format; omitting from User-Agent',
|
|
||||||
);
|
|
||||||
expect(core.warning).toHaveBeenCalledTimes(2);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('warns and skips when env vars exceed the length bound', async () => {
|
it('warns and skips when env vars exceed the length bound', async () => {
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
|
process.env.GITHUB_ACTION = 'a'.repeat(200);
|
||||||
process.env.GITHUB_RUN_ID = '1'.repeat(50);
|
process.env.GITHUB_RUN_ID = '1'.repeat(50);
|
||||||
process.env.GITHUB_RUN_ATTEMPT = '1'.repeat(50);
|
process.env.GITHUB_RUN_ATTEMPT = '1'.repeat(50);
|
||||||
const ua = await getCustomUserAgent();
|
const ua = await getCustomUserAgent();
|
||||||
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
|
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
|
||||||
expect(core.warning).toHaveBeenCalledTimes(2);
|
expect(core.warning).toHaveBeenCalledTimes(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects GITHUB_ACTION containing whitespace or other characters', async () => {
|
||||||
|
vi.resetModules();
|
||||||
|
process.env.GITHUB_ACTION = 'has space';
|
||||||
|
const ua = await getCustomUserAgent();
|
||||||
|
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
|
||||||
|
expect(core.warning).toHaveBeenCalledWith('GITHUB_ACTION has unexpected format; omitting from User-Agent');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets AWS_EXECUTION_ENV to GitHubActions when unset', async () => {
|
it('sets AWS_EXECUTION_ENV to GitHubActions when unset', async () => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue