Skip to content

Commit 530fcb3

Browse files
committed
Group OIDC schemas into an array
1 parent 2acf819 commit 530fcb3

6 files changed

Lines changed: 51 additions & 52 deletions

File tree

lib/start-proxy-action.js

Lines changed: 11 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/json/testing-util.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as json from ".";
2+
3+
export function makeFromSchema<S extends json.Schema>(
4+
includeOptional: boolean,
5+
schema: S,
6+
): json.FromSchema<S> {
7+
const result = {};
8+
for (const [key, validator] of Object.entries(schema)) {
9+
if (!validator.required && !includeOptional) {
10+
continue;
11+
}
12+
result[key] = `value-for-${key}`;
13+
}
14+
return result as json.FromSchema<S>;
15+
}

src/start-proxy.test.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import sinon from "sinon";
88
import * as apiClient from "./api-client";
99
import * as defaults from "./defaults.json";
1010
import { setUpFeatureFlagTests } from "./feature-flags/testing-util";
11+
import { makeFromSchema } from "./json/testing-util";
1112
import { BuiltInLanguage } from "./languages";
1213
import { getRunnerLogger, Logger } from "./logging";
1314
import * as startProxyExports from "./start-proxy";
@@ -457,23 +458,13 @@ test("getCredentials throws an error when non-printable characters are used for
457458
});
458459

459460
test("getCredentials accepts OIDC configurations", (t) => {
460-
const oidcConfigurations = [
461-
{
461+
const oidcConfigurations = startProxyExports.oidcSchemas.map(
462+
(schemaInfo) => ({
462463
type: "nuget_feed",
463-
host: "azure.pkg.github.com",
464-
...validAzureCredential,
465-
},
466-
{
467-
type: "nuget_feed",
468-
host: "aws.pkg.github.com",
469-
...validAwsCredential,
470-
},
471-
{
472-
type: "nuget_feed",
473-
host: "jfrog.pkg.github.com",
474-
...validJFrogCredential,
475-
},
476-
];
464+
host: `${schemaInfo.name.toLowerCase()}.pkg.github.com`,
465+
...makeFromSchema(true, schemaInfo.schema),
466+
}),
467+
);
477468

478469
const credentials = startProxyExports.getCredentials(
479470
getRunnerLogger(true),

src/start-proxy/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ export function isJFrogConfig(
118118
return json.validateSchema(jfrogConfigSchema, config);
119119
}
120120

121+
/** An array of all OIDC configuration schemas along with output-friendly names. */
122+
export const oidcSchemas = [
123+
{ schema: azureConfigSchema, name: "Azure" },
124+
{ schema: awsConfigSchema, name: "AWS" },
125+
{ schema: jfrogConfigSchema, name: "JFrog" },
126+
];
127+
121128
/** Represents all supported OIDC configurations. */
122129
export type OIDC = AzureConfig | AWSConfig | JFrogConfig;
123130

src/start-proxy/validation.test.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
11
import test from "ava";
22

33
import * as json from "../json";
4+
import { makeFromSchema } from "../json/testing-util";
45
import { setupTests } from "../testing-utils";
56

67
import * as types from "./types";
78
import { getAuthConfig } from "./validation";
89

910
setupTests(test);
1011

11-
function makeFromSchema(
12-
includeOptional: boolean,
13-
schema: json.Schema,
14-
): json.FromSchema<typeof schema> {
15-
const result = {};
16-
for (const [key, validator] of Object.entries(schema)) {
17-
if (!validator.required && !includeOptional) {
18-
continue;
19-
}
20-
result[key] = `value-for-${key}`;
21-
}
22-
return result;
23-
}
24-
25-
const schemaTests = [
26-
{ schema: types.azureConfigSchema, name: "isAzureConfig" },
27-
{ schema: types.awsConfigSchema, name: "isAWSConfig" },
28-
{ schema: types.jfrogConfigSchema, name: "isJFrogConfig" },
29-
] as Array<{ schema: json.Schema; name: string }>;
30-
31-
for (const schemaTest of schemaTests) {
12+
for (const schemaTest of types.oidcSchemas) {
3213
for (const includeOptional of [true, false]) {
3314
const minimalName = includeOptional ? "full" : "minimal";
3415

@@ -39,7 +20,7 @@ for (const schemaTest of schemaTests) {
3920
getAuthConfig({
4021
...config,
4122
unexpected: "unexpected-value",
42-
} as json.UnvalidatedObject<types.AuthConfig>),
23+
} as unknown as json.UnvalidatedObject<types.AuthConfig>),
4324
config,
4425
);
4526
});

src/start-proxy/validation.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ export function getAuthConfig(
3030
): AuthConfig {
3131
// Start by checking for the OIDC configurations, since they have required properties
3232
// which we can use to identify them.
33-
if (types.isAzureConfig(config)) {
34-
return cloneCredential(types.azureConfigSchema, config);
35-
} else if (types.isAWSConfig(config)) {
36-
return cloneCredential(types.awsConfigSchema, config);
37-
} else if (types.isJFrogConfig(config)) {
38-
return cloneCredential(types.jfrogConfigSchema, config);
39-
} else if (types.isToken(config)) {
33+
for (const oidcSchema of types.oidcSchemas) {
34+
if (json.validateSchema(oidcSchema.schema, config)) {
35+
return cloneCredential(oidcSchema.schema, config);
36+
}
37+
}
38+
39+
// Otherwise, try the basic configuration types.
40+
if (types.isToken(config)) {
4041
// There are three scenarios for non-OIDC authentication based on the registry type:
4142
//
4243
// 1. `username`+`token`

0 commit comments

Comments
 (0)