Skip to content

Commit 4d44b57

Browse files
committed
Type that registries must have either an url or a host
1 parent 700fc11 commit 4d44b57

File tree

6 files changed

+126
-47
lines changed

6 files changed

+126
-47
lines changed

lib/start-proxy-action.js

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

src/start-proxy-action.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import { KnownLanguage } from "./languages";
1111
import { getActionsLogger, Logger } from "./logging";
1212
import { getRepositoryNwo } from "./repository";
1313
import {
14-
Credential,
1514
credentialToStr,
1615
getCredentials,
1716
getProxyBinaryPath,
1817
getSafeErrorMessage,
1918
parseLanguage,
2019
ProxyInfo,
21-
Registry,
2220
sendFailedStatusReport,
2321
sendSuccessStatusReport,
22+
ValidCredential,
23+
ValidRegistry,
2424
} from "./start-proxy";
2525
import { checkConnections } from "./start-proxy/reachability";
2626
import { ActionName, sendUnhandledErrorStatusReport } from "./status-report";
@@ -40,7 +40,7 @@ type BasicAuthCredentials = {
4040
};
4141

4242
type ProxyConfig = {
43-
all_credentials: Credential[];
43+
all_credentials: ValidCredential[];
4444
ca: CertificateAuthority;
4545
proxy_auth?: BasicAuthCredentials;
4646
};
@@ -243,7 +243,7 @@ async function startProxy(
243243
core.setOutput("proxy_port", port.toString());
244244
core.setOutput("proxy_ca_certificate", config.ca.cert);
245245

246-
const registry_urls: Registry[] = config.all_credentials
246+
const registry_urls: ValidRegistry[] = config.all_credentials
247247
.filter((credential) => credential.url !== undefined)
248248
.map((credential) => ({
249249
type: credential.type,

src/start-proxy.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import { Config } from "./config-utils";
1313
import * as defaults from "./defaults.json";
1414
import { KnownLanguage } from "./languages";
1515
import { Logger } from "./logging";
16-
import { Credential } from "./start-proxy/types";
16+
import {
17+
Address,
18+
Credential,
19+
Registry,
20+
ValidCredential,
21+
} from "./start-proxy/types";
1722
import {
1823
ActionName,
1924
createStatusReportBase,
@@ -223,6 +228,31 @@ const LANGUAGE_TO_REGISTRY_TYPE: Partial<Record<KnownLanguage, string[]>> = {
223228
go: ["goproxy_server", "git_source"],
224229
} as const;
225230

231+
/**
232+
* Extracts an `Address` value from the given `Registry` value by determining whether it has
233+
* a `url` value, or no `url` value but a `host` value.
234+
*
235+
* @throws A `ConfigurationError` if the `Registry` value contains neither a `url` or `host` field.
236+
*/
237+
function getRegistryAddress(registry: Registry): Address {
238+
if (isDefined(registry.url)) {
239+
return {
240+
url: registry.url,
241+
host: registry.host,
242+
};
243+
} else if (isDefined(registry.host)) {
244+
return {
245+
url: undefined,
246+
host: registry.host,
247+
};
248+
} else {
249+
// The proxy needs one of these to work. If both are defined, the url has the precedence.
250+
throw new ConfigurationError(
251+
"Invalid credentials - must specify host or url",
252+
);
253+
}
254+
}
255+
226256
// getCredentials returns registry credentials from action inputs.
227257
// It prefers `registries_credentials` over `registry_secrets`.
228258
// If neither is set, it returns an empty array.
@@ -231,7 +261,7 @@ export function getCredentials(
231261
registrySecrets: string | undefined,
232262
registriesCredentials: string | undefined,
233263
language: KnownLanguage | undefined,
234-
): Credential[] {
264+
): ValidCredential[] {
235265
const registryTypeForLanguage = language
236266
? LANGUAGE_TO_REGISTRY_TYPE[language]
237267
: undefined;
@@ -265,7 +295,7 @@ export function getCredentials(
265295
);
266296
}
267297

268-
const out: Credential[] = [];
298+
const out: ValidCredential[] = [];
269299
for (const e of parsed) {
270300
if (e === null || typeof e !== "object") {
271301
throw new ConfigurationError("Invalid credentials - must be an object");
@@ -279,12 +309,7 @@ export function getCredentials(
279309
core.setSecret(e.token);
280310
}
281311

282-
if (!isDefined(e.url) && !isDefined(e.host)) {
283-
// The proxy needs one of these to work. If both are defined, the url has the precedence.
284-
throw new ConfigurationError(
285-
"Invalid credentials - must specify host or url",
286-
);
287-
}
312+
const address = getRegistryAddress(e);
288313

289314
// Filter credentials based on language if specified. `type` is the registry type.
290315
// E.g., "maven_feed" for Java/Kotlin, "nuget_repository" for C#.
@@ -327,11 +352,10 @@ export function getCredentials(
327352

328353
out.push({
329354
type: e.type,
330-
host: e.host,
331-
url: e.url,
332355
username: e.username,
333356
password: e.password,
334357
token: e.token,
358+
...address,
335359
});
336360
}
337361
return out;

src/start-proxy/reachability.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ import {
1111
ReachabilityBackend,
1212
ReachabilityError,
1313
} from "./reachability";
14-
import { ProxyInfo, Registry } from "./types";
14+
import { ProxyInfo, ValidRegistry } from "./types";
1515

1616
setupTests(test);
1717

1818
class MockReachabilityBackend implements ReachabilityBackend {
19-
public async checkConnection(_registry: Registry): Promise<number> {
19+
public async checkConnection(_registry: ValidRegistry): Promise<number> {
2020
return 200;
2121
}
2222
}
2323

24-
const mavenRegistry: Registry = {
24+
const mavenRegistry: ValidRegistry = {
2525
type: "maven_registry",
2626
url: "https://repo.maven.apache.org/maven2/",
2727
};
2828

29-
const nugetFeed: Registry = {
29+
const nugetFeed: ValidRegistry = {
3030
type: "nuget_feed",
3131
url: "https://api.nuget.org/v3/index.json",
3232
};

src/start-proxy/reachability.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { HttpsProxyAgent } from "https-proxy-agent";
55
import { Logger } from "../logging";
66
import { getErrorMessage } from "../util";
77

8-
import { ProxyInfo, Registry } from "./types";
8+
import { getAddressString, ProxyInfo, Registry, ValidRegistry } from "./types";
99

1010
export class ReachabilityError extends Error {
1111
constructor(
@@ -34,7 +34,7 @@ export interface ReachabilityBackend {
3434
* @param registry The registry to try and reach.
3535
* @returns The successful status code (in the `<400` range).
3636
*/
37-
checkConnection: (registry: Registry) => Promise<number>;
37+
checkConnection: (registry: ValidRegistry) => Promise<number>;
3838
}
3939

4040
class NetworkReachabilityBackend implements ReachabilityBackend {
@@ -47,10 +47,10 @@ class NetworkReachabilityBackend implements ReachabilityBackend {
4747
this.agent = new HttpsProxyAgent(`http://${proxy.host}:${proxy.port}`);
4848
}
4949

50-
public async checkConnection(registry: Registry): Promise<number> {
50+
public async checkConnection(registry: ValidRegistry): Promise<number> {
5151
return new Promise((resolve, reject) => {
5252
const req = https.request(
53-
registry.url as string,
53+
getAddressString(registry),
5454
{ agent: this.agent, method: "HEAD", ca: this.proxy.cert },
5555
(res) => {
5656
res.destroy();
@@ -83,8 +83,8 @@ export async function checkConnections(
8383
logger: Logger,
8484
proxy: ProxyInfo,
8585
backend?: ReachabilityBackend,
86-
): Promise<Set<Registry>> {
87-
const result: Set<Registry> = new Set();
86+
): Promise<Set<ValidRegistry>> {
87+
const result: Set<ValidRegistry> = new Set();
8888

8989
// Don't do anything if there are no registries.
9090
if (proxy.registries.length === 0) return result;
@@ -96,22 +96,23 @@ export async function checkConnections(
9696
}
9797

9898
for (const registry of proxy.registries) {
99+
const address = getAddressString(registry);
99100
try {
100-
logger.debug(`Testing connection to ${registry.url}...`);
101+
logger.debug(`Testing connection to ${address}...`);
101102
const statusCode = await backend.checkConnection(registry);
102103

103104
logger.info(
104-
`Successfully tested connection to ${registry.url} (${statusCode})`,
105+
`Successfully tested connection to ${address} (${statusCode})`,
105106
);
106107
result.add(registry);
107108
} catch (e) {
108109
if (e instanceof ReachabilityError && e.statusCode !== undefined) {
109110
logger.error(
110-
`Connection test to ${registry.url} failed. (${e.statusCode})`,
111+
`Connection test to ${address} failed. (${e.statusCode})`,
111112
);
112113
} else {
113114
logger.error(
114-
`Connection test to ${registry.url} failed: ${getErrorMessage(e)}`,
115+
`Connection test to ${address} failed: ${getErrorMessage(e)}`,
115116
);
116117
}
117118
}

0 commit comments

Comments
 (0)