Skip to content

Commit 4ee4567

Browse files
committed
Simplify credential cloning in getAuthConfig
1 parent c8e26e2 commit 4ee4567

2 files changed

Lines changed: 35 additions & 42 deletions

File tree

lib/start-proxy-action.js

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

src/start-proxy/validation.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,39 @@ import * as core from "@actions/core";
33
import * as json from "../json";
44
import { isDefined } from "../util";
55

6-
import type {
7-
AuthConfig,
8-
AWSConfig,
9-
AzureConfig,
10-
JFrogConfig,
11-
Token,
12-
UsernamePassword,
13-
} from "./types";
6+
import type { AuthConfig, Token, UsernamePassword } from "./types";
147
import * as types from "./types";
158

9+
/** Constructs a new object from `obj` with only keys that exist in `schema`. */
10+
export function cloneCredential<
11+
T extends json.FromSchema<S>,
12+
S extends json.Schema,
13+
>(schema: json.Schema, obj: T): T {
14+
const result = {};
15+
16+
for (const key of Object.keys(schema)) {
17+
// Skip keys that don't exist or don't have a value.
18+
if (!isDefined(obj[key])) {
19+
continue;
20+
}
21+
result[key] = obj[key];
22+
}
23+
24+
return result as T;
25+
}
26+
1627
/** Extracts an `AuthConfig` value from `config`. */
1728
export function getAuthConfig(
1829
config: json.UnvalidatedObject<AuthConfig>,
1930
): AuthConfig {
2031
// Start by checking for the OIDC configurations, since they have required properties
2132
// which we can use to identify them.
2233
if (types.isAzureConfig(config)) {
23-
return {
24-
"tenant-id": config["tenant-id"],
25-
"client-id": config["client-id"],
26-
} satisfies AzureConfig;
34+
return cloneCredential(types.azureConfigSchema, config);
2735
} else if (types.isAWSConfig(config)) {
28-
return {
29-
"aws-region": config["aws-region"],
30-
"account-id": config["account-id"],
31-
"role-name": config["role-name"],
32-
domain: config.domain,
33-
"domain-owner": config["domain-owner"],
34-
audience: config.audience,
35-
} satisfies AWSConfig;
36+
return cloneCredential(types.awsConfigSchema, config);
3637
} else if (types.isJFrogConfig(config)) {
37-
return {
38-
"jfrog-oidc-provider-name": config["jfrog-oidc-provider-name"],
39-
"identity-mapping-name": config["identity-mapping-name"],
40-
audience: config.audience,
41-
} satisfies JFrogConfig;
38+
return cloneCredential(types.jfrogConfigSchema, config);
4239
} else if (types.isToken(config)) {
4340
// There are three scenarios for non-OIDC authentication based on the registry type:
4441
//

0 commit comments

Comments
 (0)