Skip to content

Commit dc92ab6

Browse files
committed
Consider registries_credentials as input
1 parent 16639b4 commit dc92ab6

File tree

4 files changed

+113
-49
lines changed

4 files changed

+113
-49
lines changed

lib/start-proxy-action.js

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

lib/start-proxy-action.js.map

Lines changed: 1 addition & 1 deletion
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: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,46 +89,37 @@ function generateCertificateAuthority(): CertificateAuthority {
8989
}
9090

9191
async function runWrapper() {
92+
// Setup logging
9293
const tempDir = actionsUtil.getTemporaryDirectory();
9394
const logFilePath = path.resolve(tempDir, "proxy.log");
94-
const input = actionsUtil.getOptionalInput("registry_secrets") || "[]";
95-
const credentials = JSON.parse(input) as Credential[];
96-
const ca = generateCertificateAuthority();
97-
const proxy_password = actionsUtil.getOptionalInput("proxy_password");
9895
core.saveState("proxy-log-file", logFilePath);
9996

100-
let proxy_auth: BasicAuthCredentials | undefined = undefined;
101-
if (proxy_password) {
102-
proxy_auth = {
103-
username: PROXY_USER,
104-
password: proxy_password,
105-
};
106-
}
97+
// Get the configuration options
98+
const credentials = getCredentials();
99+
const ca = generateCertificateAuthority();
100+
const proxyAuth = getProxyAuth();
101+
107102
const proxyConfig: ProxyConfig = {
108103
all_credentials: credentials,
109104
ca,
110-
proxy_auth,
105+
proxy_auth: proxyAuth,
111106
};
107+
108+
// Start the Proxy
109+
const proxyBin = await getProxyBinaryPath();
110+
await startProxy(proxyBin, proxyConfig, logFilePath);
111+
}
112+
113+
async function startProxy(binPath: string, config: ProxyConfig, logFilePath: string) {
112114
const host = "127.0.0.1";
113-
let proxyBin = toolcache.find(UPDATEJOB_PROXY, UPDATEJOB_PROXY_VERSION);
114-
if (!proxyBin) {
115-
const temp = await toolcache.downloadTool(UPDATEJOB_PROXY_URL);
116-
const extracted = await toolcache.extractTar(temp);
117-
proxyBin = await toolcache.cacheDir(
118-
extracted,
119-
UPDATEJOB_PROXY,
120-
UPDATEJOB_PROXY_VERSION,
121-
);
122-
}
123-
proxyBin = path.join(proxyBin, UPDATEJOB_PROXY);
124115
let port = 49152;
125116
try {
126117
let subprocess: ChildProcess | undefined = undefined;
127118
let tries = 5;
128119
let subprocessError: Error | undefined = undefined;
129120
while (tries-- > 0 && !subprocess && !subprocessError) {
130121
subprocess = spawn(
131-
proxyBin,
122+
binPath,
132123
["-addr", `${host}:${port}`, "-config", "-", "-logfile", logFilePath],
133124
{
134125
detached: true,
@@ -149,7 +140,7 @@ async function runWrapper() {
149140
subprocess = undefined;
150141
}
151142
});
152-
subprocess.stdin?.write(JSON.stringify(proxyConfig));
143+
subprocess.stdin?.write(JSON.stringify(config));
153144
subprocess.stdin?.end();
154145
// Wait a little to allow the proxy to start
155146
await util.delay(1000);
@@ -160,12 +151,55 @@ async function runWrapper() {
160151
core.info(`Proxy started on ${host}:${port}`);
161152
core.setOutput("proxy_host", host);
162153
core.setOutput("proxy_port", port.toString());
163-
core.setOutput("proxy_ca_certificate", ca.cert);
154+
core.setOutput("proxy_ca_certificate", config.ca.cert);
164155
} catch (error) {
165156
core.setFailed(
166157
`start-proxy action failed: ${util.wrapError(error).message}`,
167158
);
168159
}
169160
}
170161

162+
// getCredentials returns registry credentials from action inputs.
163+
// It prefers `registries_credentials` over `registry_secrets`.
164+
// If neither is set, it returns an empty array.
165+
function getCredentials(): Credential[] {
166+
const encodedCredentials = actionsUtil.getOptionalInput("registries_credentials");
167+
if (encodedCredentials !== undefined) {
168+
core.info(`Using encoded credentials.`);
169+
const credentialsStr = Buffer.from(encodedCredentials, "base64").toString();
170+
return JSON.parse(credentialsStr) as Credential[];
171+
}
172+
core.info(`Using structured credentials.`);
173+
const registrySecrets = actionsUtil.getOptionalInput("registry_secrets") || "[]";
174+
return JSON.parse(registrySecrets) as Credential[];
175+
}
176+
177+
// getProxyAuth returns the authentication information for the proxy itself.
178+
function getProxyAuth(): BasicAuthCredentials | undefined{
179+
const proxy_password = actionsUtil.getOptionalInput("proxy_password");
180+
if (proxy_password) {
181+
return {
182+
username: PROXY_USER,
183+
password: proxy_password,
184+
};
185+
}
186+
return ;
187+
}
188+
189+
190+
async function getProxyBinaryPath(): Promise<string> {
191+
let proxyBin = toolcache.find(UPDATEJOB_PROXY, UPDATEJOB_PROXY_VERSION);
192+
if (!proxyBin) {
193+
const temp = await toolcache.downloadTool(UPDATEJOB_PROXY_URL);
194+
const extracted = await toolcache.extractTar(temp);
195+
proxyBin = await toolcache.cacheDir(
196+
extracted,
197+
UPDATEJOB_PROXY,
198+
UPDATEJOB_PROXY_VERSION,
199+
);
200+
}
201+
proxyBin = path.join(proxyBin, UPDATEJOB_PROXY);
202+
return proxyBin;
203+
}
204+
171205
void runWrapper();

start-proxy/action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ inputs:
66
description: The URLs and credentials of package registries
77
required: false
88
default: "[]"
9+
registries_credentials:
10+
description: Base64 encoded JSON configuration for the URLs and credentials of the package registries
11+
required: false
912
proxy_password:
1013
required: false
1114
description: The password of the proxy

0 commit comments

Comments
 (0)