Skip to content

Commit 67f0228

Browse files
committed
Add OIDC tests for getCredentials
1 parent 3b2aaa3 commit 67f0228

File tree

1 file changed

+141
-3
lines changed

1 file changed

+141
-3
lines changed

src/start-proxy.test.ts

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ test("getCredentials throws an error when non-printable characters are used", as
281281
];
282282

283283
for (const invalidCredential of invalidCredentials) {
284-
const credentialsInput = Buffer.from(
285-
JSON.stringify([invalidCredential]),
286-
).toString("base64");
284+
const credentialsInput = toEncodedJSON([invalidCredential]);
287285

288286
t.throws(
289287
() =>
@@ -301,6 +299,146 @@ test("getCredentials throws an error when non-printable characters are used", as
301299
}
302300
});
303301

302+
const validAzureCredential: startProxyExports.AzureConfig = {
303+
tenant_id: "12345678-1234-1234-1234-123456789012",
304+
client_id: "abcdef01-2345-6789-abcd-ef0123456789",
305+
};
306+
307+
const validAwsCredential: startProxyExports.AWSConfig = {
308+
aws_region: "us-east-1",
309+
account_id: "123456789012",
310+
role_name: "MY_ROLE",
311+
domain: "MY_DOMAIN",
312+
domain_owner: "987654321098",
313+
audience: "custom-audience",
314+
};
315+
316+
const validJFrogCredential: startProxyExports.JFrogConfig = {
317+
jfrog_oidc_provider_name: "MY_PROVIDER",
318+
audience: "jfrog-audience",
319+
identity_mapping_name: "my-mapping",
320+
};
321+
322+
test("getCredentials throws an error when non-printable characters are used for Azure OIDC", (t) => {
323+
for (const key of Object.keys(validAzureCredential)) {
324+
const invalidAzureCredential = {
325+
...validAzureCredential,
326+
[key]: "123\x00",
327+
};
328+
const invalidCredential: startProxyExports.RawCredential = {
329+
type: "nuget_feed",
330+
host: `${key}.nuget.pkg.github.com`,
331+
...invalidAzureCredential,
332+
};
333+
const credentialsInput = toEncodedJSON([invalidCredential]);
334+
335+
t.throws(
336+
() =>
337+
startProxyExports.getCredentials(
338+
getRunnerLogger(true),
339+
undefined,
340+
credentialsInput,
341+
undefined,
342+
),
343+
{
344+
message:
345+
"Invalid credentials - fields must contain only printable characters",
346+
},
347+
);
348+
}
349+
});
350+
351+
test("getCredentials throws an error when non-printable characters are used for AWS OIDC", (t) => {
352+
for (const key of Object.keys(validAwsCredential)) {
353+
const invalidAwsCredential = {
354+
...validAwsCredential,
355+
[key]: "123\x00",
356+
};
357+
const invalidCredential: startProxyExports.RawCredential = {
358+
type: "nuget_feed",
359+
host: `${key}.nuget.pkg.github.com`,
360+
...invalidAwsCredential,
361+
};
362+
const credentialsInput = toEncodedJSON([invalidCredential]);
363+
364+
t.throws(
365+
() =>
366+
startProxyExports.getCredentials(
367+
getRunnerLogger(true),
368+
undefined,
369+
credentialsInput,
370+
undefined,
371+
),
372+
{
373+
message:
374+
"Invalid credentials - fields must contain only printable characters",
375+
},
376+
);
377+
}
378+
});
379+
380+
test("getCredentials throws an error when non-printable characters are used for JFrog OIDC", (t) => {
381+
for (const key of Object.keys(validJFrogCredential)) {
382+
const invalidJFrogCredential = {
383+
...validJFrogCredential,
384+
[key]: "123\x00",
385+
};
386+
const invalidCredential: startProxyExports.RawCredential = {
387+
type: "nuget_feed",
388+
host: `${key}.nuget.pkg.github.com`,
389+
...invalidJFrogCredential,
390+
};
391+
const credentialsInput = toEncodedJSON([invalidCredential]);
392+
393+
t.throws(
394+
() =>
395+
startProxyExports.getCredentials(
396+
getRunnerLogger(true),
397+
undefined,
398+
credentialsInput,
399+
undefined,
400+
),
401+
{
402+
message:
403+
"Invalid credentials - fields must contain only printable characters",
404+
},
405+
);
406+
}
407+
});
408+
409+
test("getCredentials accepts OIDC configurations", (t) => {
410+
const oidcConfigurations = [
411+
{
412+
type: "nuget_feed",
413+
host: "azure.pkg.github.com",
414+
...validAzureCredential,
415+
},
416+
{
417+
type: "nuget_feed",
418+
host: "aws.pkg.github.com",
419+
...validAwsCredential,
420+
},
421+
{
422+
type: "nuget_feed",
423+
host: "jfrog.pkg.github.com",
424+
...validJFrogCredential,
425+
},
426+
];
427+
428+
const credentials = startProxyExports.getCredentials(
429+
getRunnerLogger(true),
430+
undefined,
431+
toEncodedJSON(oidcConfigurations),
432+
KnownLanguage.csharp,
433+
);
434+
t.is(credentials.length, 3);
435+
436+
t.assert(credentials.every((c) => c.type === "nuget_feed"));
437+
t.assert(credentials.some((c) => startProxyExports.isAzureConfig(c)));
438+
t.assert(credentials.some((c) => startProxyExports.isAWSConfig(c)));
439+
t.assert(credentials.some((c) => startProxyExports.isJFrogConfig(c)));
440+
});
441+
304442
test("getCredentials logs a warning when a PAT is used without a username", async (t) => {
305443
const loggedMessages = [];
306444
const logger = getRecordingLogger(loggedMessages);

0 commit comments

Comments
 (0)