Skip to content

Commit 0594156

Browse files
committed
Validate Cloudsmith OIDC configurations
1 parent f440495 commit 0594156

4 files changed

Lines changed: 69 additions & 6 deletions

File tree

lib/start-proxy-action.js

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/start-proxy.test.ts

Lines changed: 13 additions & 4 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 { UnvalidatedObject, validateSchema } from "./json";
1112
import { makeFromSchema } from "./json/testing-util";
1213
import { BuiltInLanguage } from "./languages";
1314
import { getRunnerLogger, Logger } from "./logging";
@@ -472,12 +473,20 @@ test("getCredentials accepts OIDC configurations", (t) => {
472473
toEncodedJSON(oidcConfigurations),
473474
BuiltInLanguage.csharp,
474475
);
475-
t.is(credentials.length, 3);
476+
t.is(credentials.length, startProxyExports.oidcSchemas.length);
476477

477478
t.assert(credentials.every((c) => c.type === "nuget_feed"));
478-
t.assert(credentials.some((c) => startProxyExports.isAzureConfig(c)));
479-
t.assert(credentials.some((c) => startProxyExports.isAWSConfig(c)));
480-
t.assert(credentials.some((c) => startProxyExports.isJFrogConfig(c)));
479+
480+
for (const oidcSchemaInfo of startProxyExports.oidcSchemas) {
481+
t.assert(
482+
credentials.some((c) =>
483+
validateSchema(
484+
oidcSchemaInfo.schema,
485+
c as unknown as UnvalidatedObject<any>,
486+
),
487+
),
488+
);
489+
}
481490
});
482491

483492
const getCredentialsMacro = test.macro({

src/start-proxy/types.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import test from "ava";
22

3+
import { makeFromSchema } from "../json/testing-util";
34
import { setupTests } from "../testing-utils";
45

56
import * as types from "./types";
@@ -107,6 +108,24 @@ test("credentialToStr - pretty-prints valid JFrog OIDC configurations", (t) => {
107108
);
108109
});
109110

111+
test("credentialToStr - pretty-prints valid Cloudsmith OIDC configurations", (t) => {
112+
const credential: types.Credential = {
113+
type: "maven_credential",
114+
url: "https://localhost",
115+
...(makeFromSchema(
116+
true,
117+
types.cloudsmithConfigSchema,
118+
) as types.CloudsmithConfig),
119+
};
120+
121+
const str = types.credentialToStr(credential);
122+
123+
t.is(
124+
"Type: maven_credential; Url: https://localhost; Cloudsmith Namespace: value-for-namespace; Cloudsmith Service Slug: value-for-service-slug; Cloudsmith API Host: value-for-api-host;",
125+
str,
126+
);
127+
});
128+
110129
test("credentialToStr - hides passwords", (t) => {
111130
const secret = "password123";
112131
const credential = {

src/start-proxy/types.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,33 @@ export function isJFrogConfig(
118118
return json.validateSchema(jfrogConfigSchema, config);
119119
}
120120

121+
/** A schema for Cloudsmith OIDC configurations. */
122+
export const cloudsmithConfigSchema = {
123+
namespace: json.string,
124+
"service-slug": json.string,
125+
"api-host": json.string,
126+
} as const satisfies json.Schema;
127+
128+
/** Configuration for Cloudsmith OIDC. */
129+
export type CloudsmithConfig = json.FromSchema<typeof cloudsmithConfigSchema>;
130+
131+
/** Decides whether `config` is a Cloudsmith OIDC configuration. */
132+
export function isCloudsmithConfig(
133+
config: UnvalidatedObject<AuthConfig>,
134+
): config is CloudsmithConfig {
135+
return json.validateSchema(cloudsmithConfigSchema, config);
136+
}
137+
121138
/** An array of all OIDC configuration schemas along with output-friendly names. */
122139
export const oidcSchemas = [
123140
{ schema: azureConfigSchema, name: "Azure" },
124141
{ schema: awsConfigSchema, name: "AWS" },
125142
{ schema: jfrogConfigSchema, name: "JFrog" },
143+
{ schema: cloudsmithConfigSchema, name: "Cloudsmith" },
126144
];
127145

128146
/** Represents all supported OIDC configurations. */
129-
export type OIDC = AzureConfig | AWSConfig | JFrogConfig;
147+
export type OIDC = AzureConfig | AWSConfig | JFrogConfig | CloudsmithConfig;
130148

131149
/** All authentication-related fields. */
132150
export type AuthConfig = UsernamePassword | Token | OIDC;
@@ -185,6 +203,10 @@ export function credentialToStr(credential: Credential): string {
185203
credential["identity-mapping-name"],
186204
);
187205
appendIfDefined("JFrog Audience", credential.audience);
206+
} else if (isCloudsmithConfig(credential)) {
207+
appendIfDefined("Cloudsmith Namespace", credential.namespace);
208+
appendIfDefined("Cloudsmith Service Slug", credential["service-slug"]);
209+
appendIfDefined("Cloudsmith API Host", credential["api-host"]);
188210
}
189211

190212
return result;

0 commit comments

Comments
 (0)