Skip to content

Commit 5b34615

Browse files
committed
Validate credentials input
1 parent 1bd7fdc commit 5b34615

File tree

3 files changed

+81
-32
lines changed

3 files changed

+81
-32
lines changed

lib/start-proxy-action.js

+37-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/start-proxy-action.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/start-proxy-action.ts

+43-18
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ const PROXY_USER = "proxy_user";
1717
const KEY_SIZE = 2048;
1818
const KEY_EXPIRY_YEARS = 2;
1919

20-
export type CertificateAuthority = {
20+
type CertificateAuthority = {
2121
cert: string;
2222
key: string;
2323
};
2424

25-
export type Credential = {
25+
type Credential = {
2626
type: string;
2727
host?: string;
2828
url?: string;
@@ -31,16 +31,12 @@ export type Credential = {
3131
token?: string;
3232
};
3333

34-
function CredentialToStr(c: Credential): string {
35-
return `Type: ${c.type}; Host: ${c.host}; Url: ${c.url} Username: ${c.username}; Password: ${c.password !== undefined}; Token: ${c.token !== undefined}`
36-
}
37-
38-
export type BasicAuthCredentials = {
34+
type BasicAuthCredentials = {
3935
username: string;
4036
password: string;
4137
};
4238

43-
export type ProxyConfig = {
39+
type ProxyConfig = {
4440
all_credentials: Credential[];
4541
ca: CertificateAuthority;
4642
proxy_auth?: BasicAuthCredentials;
@@ -103,8 +99,8 @@ async function runWrapper() {
10399
core.saveState("proxy-log-file", proxyLogFilePath);
104100

105101
// Get the configuration options
106-
const credentials = getCredentials();
107-
logger.info(`Credentials loaded for the following URLs:\n ${credentials.map(c => CredentialToStr(c)).join("\n")}`);
102+
const credentials = getCredentials(logger);
103+
logger.info(`Credentials loaded for the following registries:\n ${credentials.map(c => credentialToStr(c)).join("\n")}`);
108104

109105
const ca = generateCertificateAuthority();
110106
const proxyAuth = getProxyAuth();
@@ -173,15 +169,39 @@ async function startProxy(binPath: string, config: ProxyConfig, logFilePath: str
173169
// getCredentials returns registry credentials from action inputs.
174170
// It prefers `registries_credentials` over `registry_secrets`.
175171
// If neither is set, it returns an empty array.
176-
function getCredentials(): Credential[] {
177-
const encodedCredentials = actionsUtil.getOptionalInput("registries_credentials");
178-
if (encodedCredentials !== undefined) {
179-
const credentialsStr = Buffer.from(encodedCredentials, "base64").toString();
180-
return JSON.parse(credentialsStr) as Credential[];
172+
function getCredentials(logger: Logger): Credential[] {
173+
const registriesCredentials = actionsUtil.getOptionalInput("registries_credentials");
174+
const registrySecrets = actionsUtil.getOptionalInput("registry_secrets");
175+
176+
var credentialsStr: string;
177+
if (registriesCredentials !== undefined) {
178+
logger.info(`Using registries_credentials input.`);
179+
credentialsStr = Buffer.from(registriesCredentials, "base64").toString();
180+
} else if (registrySecrets !== undefined) {
181+
logger.info(`Using registry_secrets input.`);
182+
credentialsStr = registrySecrets;
183+
} else {
184+
logger.info(`No credentials defined.`);
185+
return [];
181186
}
182-
core.info(`Using structured credentials.`);
183-
const registrySecrets = actionsUtil.getOptionalInput("registry_secrets") || "[]";
184-
return JSON.parse(registrySecrets) as Credential[];
187+
188+
// Parse and validate the credentials
189+
const parsed = JSON.parse(credentialsStr) as Credential[];
190+
let out: Credential[] = []
191+
parsed.forEach(e => {
192+
if (e.url === undefined && e.host === undefined) {
193+
throw "Invalid credentials - must specify host or url"
194+
}
195+
out.push({
196+
type: e.type,
197+
host: e.host,
198+
url: e.url,
199+
username: e.username,
200+
password: e.password,
201+
token: e.token,
202+
})
203+
});
204+
return out;
185205
}
186206

187207
// getProxyAuth returns the authentication information for the proxy itself.
@@ -212,4 +232,9 @@ async function getProxyBinaryPath(): Promise<string> {
212232
return proxyBin;
213233
}
214234

235+
function credentialToStr(c: Credential): string {
236+
return `Type: ${c.type}; Host: ${c.host}; Url: ${c.url} Username: ${c.username}; Password: ${c.password !== undefined}; Token: ${c.token !== undefined}`
237+
}
238+
239+
215240
void runWrapper();

0 commit comments

Comments
 (0)