From 4d0bec12bf135c51b73a57a17b1b338c224fde17 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Wed, 11 Feb 2026 17:10:39 +0000 Subject: [PATCH] Rename types --- lib/start-proxy-action.js | 11 ++++++--- src/start-proxy-action.ts | 9 ++++--- src/start-proxy.ts | 21 ++++++++++------ src/start-proxy/reachability.test.ts | 8 +++--- src/start-proxy/reachability.ts | 10 ++++---- src/start-proxy/types.ts | 37 ++++++++++++++++++---------- 6 files changed, 58 insertions(+), 38 deletions(-) diff --git a/lib/start-proxy-action.js b/lib/start-proxy-action.js index 35885a94f..0fbe48638 100644 --- a/lib/start-proxy-action.js +++ b/lib/start-proxy-action.js @@ -80948,7 +80948,7 @@ var require_Credential = __commonJS({ "use strict"; Object.defineProperty(exports2, "__esModule", { value: true }); exports2.Credential = void 0; - var Credential = class { + var Credential2 = class { /** * Creates a RequestPolicy object. * @@ -80959,7 +80959,7 @@ var require_Credential = __commonJS({ throw new Error("Method should be implemented in children classes."); } }; - exports2.Credential = Credential; + exports2.Credential = Credential2; } }); @@ -82088,7 +82088,7 @@ var require_Credential2 = __commonJS({ "use strict"; Object.defineProperty(exports2, "__esModule", { value: true }); exports2.Credential = void 0; - var Credential = class { + var Credential2 = class { /** * Creates a RequestPolicy object. * @@ -82099,7 +82099,7 @@ var require_Credential2 = __commonJS({ throw new Error("Method should be implemented in children classes."); } }; - exports2.Credential = Credential; + exports2.Credential = Credential2; } }); @@ -121565,6 +121565,9 @@ function getCredentials(logger, registrySecrets, registriesCredentials, language if (e === null || typeof e !== "object") { throw new ConfigurationError("Invalid credentials - must be an object"); } + if (!isDefined2(e.type)) { + throw new ConfigurationError("Invalid credentials - must have a type"); + } if (isDefined2(e.password)) { core10.setSecret(e.password); } diff --git a/src/start-proxy-action.ts b/src/start-proxy-action.ts index db1bbebf4..a9b355eaa 100644 --- a/src/start-proxy-action.ts +++ b/src/start-proxy-action.ts @@ -19,8 +19,8 @@ import { ProxyInfo, sendFailedStatusReport, sendSuccessStatusReport, - ValidCredential, - ValidRegistry, + Credential, + Registry, } from "./start-proxy"; import { checkConnections } from "./start-proxy/reachability"; import { ActionName, sendUnhandledErrorStatusReport } from "./status-report"; @@ -40,7 +40,8 @@ type BasicAuthCredentials = { }; type ProxyConfig = { - all_credentials: ValidCredential[]; + /** The validated configurations for the proxy. */ + all_credentials: Credential[]; ca: CertificateAuthority; proxy_auth?: BasicAuthCredentials; }; @@ -243,7 +244,7 @@ async function startProxy( core.setOutput("proxy_port", port.toString()); core.setOutput("proxy_ca_certificate", config.ca.cert); - const registry_urls: ValidRegistry[] = config.all_credentials + const registry_urls: Registry[] = config.all_credentials .filter((credential) => credential.url !== undefined) .map((credential) => ({ type: credential.type, diff --git a/src/start-proxy.ts b/src/start-proxy.ts index 347038d82..4e4b62c91 100644 --- a/src/start-proxy.ts +++ b/src/start-proxy.ts @@ -15,9 +15,9 @@ import { KnownLanguage } from "./languages"; import { Logger } from "./logging"; import { Address, - Credential, + RawCredential, Registry, - ValidCredential, + Credential, } from "./start-proxy/types"; import { ActionName, @@ -234,7 +234,7 @@ const LANGUAGE_TO_REGISTRY_TYPE: Partial> = { * * @throws A `ConfigurationError` if the `Registry` value contains neither a `url` or `host` field. */ -function getRegistryAddress(registry: Registry): Address { +function getRegistryAddress(registry: Partial): Address { if (isDefined(registry.url)) { return { url: registry.url, @@ -261,7 +261,7 @@ export function getCredentials( registrySecrets: string | undefined, registriesCredentials: string | undefined, language: KnownLanguage | undefined, -): ValidCredential[] { +): Credential[] { const registryTypeForLanguage = language ? LANGUAGE_TO_REGISTRY_TYPE[language] : undefined; @@ -279,9 +279,9 @@ export function getCredentials( } // Parse and validate the credentials - let parsed: Credential[]; + let parsed: RawCredential[]; try { - parsed = JSON.parse(credentialsStr) as Credential[]; + parsed = JSON.parse(credentialsStr) as RawCredential[]; } catch { // Don't log the error since it might contain sensitive information. logger.error("Failed to parse the credentials data."); @@ -295,12 +295,17 @@ export function getCredentials( ); } - const out: ValidCredential[] = []; + const out: Credential[] = []; for (const e of parsed) { if (e === null || typeof e !== "object") { throw new ConfigurationError("Invalid credentials - must be an object"); } + // The configuration must have a type. + if (!isDefined(e.type)) { + throw new ConfigurationError("Invalid credentials - must have a type"); + } + // Mask credentials to reduce chance of accidental leakage in logs. if (isDefined(e.password)) { core.setSecret(e.password); @@ -450,7 +455,7 @@ export async function getDownloadUrl( * * @param c The credential to convert to a string. */ -export function credentialToStr(c: Credential): string { +export function credentialToStr(c: RawCredential): string { return `Type: ${c.type}; Host: ${c.host}; Url: ${c.url} Username: ${ c.username }; Password: ${c.password !== undefined}; Token: ${c.token !== undefined}`; diff --git a/src/start-proxy/reachability.test.ts b/src/start-proxy/reachability.test.ts index 4871b7228..84cd4f9dc 100644 --- a/src/start-proxy/reachability.test.ts +++ b/src/start-proxy/reachability.test.ts @@ -11,22 +11,22 @@ import { ReachabilityBackend, ReachabilityError, } from "./reachability"; -import { ProxyInfo, ValidRegistry } from "./types"; +import { ProxyInfo, Registry } from "./types"; setupTests(test); class MockReachabilityBackend implements ReachabilityBackend { - public async checkConnection(_registry: ValidRegistry): Promise { + public async checkConnection(_registry: Registry): Promise { return 200; } } -const mavenRegistry: ValidRegistry = { +const mavenRegistry: Registry = { type: "maven_registry", url: "https://repo.maven.apache.org/maven2/", }; -const nugetFeed: ValidRegistry = { +const nugetFeed: Registry = { type: "nuget_feed", url: "https://api.nuget.org/v3/index.json", }; diff --git a/src/start-proxy/reachability.ts b/src/start-proxy/reachability.ts index fd14e92e6..06c9a2b64 100644 --- a/src/start-proxy/reachability.ts +++ b/src/start-proxy/reachability.ts @@ -5,7 +5,7 @@ import { HttpsProxyAgent } from "https-proxy-agent"; import { Logger } from "../logging"; import { getErrorMessage } from "../util"; -import { getAddressString, ProxyInfo, ValidRegistry } from "./types"; +import { getAddressString, ProxyInfo, Registry } from "./types"; export class ReachabilityError extends Error { constructor(public readonly statusCode?: number | undefined) { @@ -25,7 +25,7 @@ export interface ReachabilityBackend { * @param registry The registry to try and reach. * @returns The successful status code (in the `<400` range). */ - checkConnection: (registry: ValidRegistry) => Promise; + checkConnection: (registry: Registry) => Promise; } class NetworkReachabilityBackend implements ReachabilityBackend { @@ -38,7 +38,7 @@ class NetworkReachabilityBackend implements ReachabilityBackend { this.agent = new HttpsProxyAgent(`http://${proxy.host}:${proxy.port}`); } - public async checkConnection(registry: ValidRegistry): Promise { + public async checkConnection(registry: Registry): Promise { return new Promise((resolve, reject) => { const req = https.request( getAddressString(registry), @@ -78,8 +78,8 @@ export async function checkConnections( logger: Logger, proxy: ProxyInfo, backend?: ReachabilityBackend, -): Promise> { - const result: Set = new Set(); +): Promise> { + const result: Set = new Set(); // Don't do anything if there are no registries. if (proxy.registries.length === 0) return result; diff --git a/src/start-proxy/types.ts b/src/start-proxy/types.ts index 859cccefd..0fb67f21c 100644 --- a/src/start-proxy/types.ts +++ b/src/start-proxy/types.ts @@ -1,14 +1,28 @@ -export interface Credential extends Registry { - username?: string; - password?: string; - token?: string; -} +/** + * After parsing configurations from JSON, we don't know whether all the keys we expect are + * present or not. This type is used to represent such values, which we expect to be + * `Credential` values, but haven't validated yet. + */ +export type RawCredential = Partial; -export interface Registry { +/** + * A package registry configuration includes identifying information as well as + * authentication credentials. + */ +export type Credential = { + /** The username needed to authenticate to the package registry, if any. */ + username?: string; + /** The password needed to authenticate to the package registry, if any. */ + password?: string; + /** The token needed to authenticate to the package registry, if any. */ + token?: string; +} & Registry; + +/** A package registry is identified by its type and address. */ +export type Registry = { + /** The type of the package registry. */ type: string; - host?: string; - url?: string; -} +} & Address; // If a registry has an `url`, then that takes precedence over the `host` which may or may // not be defined. @@ -39,12 +53,9 @@ export function getAddressString(address: Address): string { } } -export type ValidRegistry = T & Address; -export type ValidCredential = ValidRegistry; - export interface ProxyInfo { host: string; port: number; cert: string; - registries: ValidRegistry[]; + registries: Registry[]; }