Skip to content

Commit 692f12a

Browse files
committed
[gitpod-protocol] handle host:token and host:port:token for getGitpodImageAuth
1 parent e62d71d commit 692f12a

File tree

2 files changed

+150
-7
lines changed

2 files changed

+150
-7
lines changed

components/gitpod-protocol/src/protocol.spec.ts

+132-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { suite, test } from "@testdeck/mocha";
88
import * as chai from "chai";
9-
import { SSHPublicKeyValue } from ".";
9+
import { SSHPublicKeyValue, EnvVar, EnvVarWithValue } from ".";
1010

1111
const expect = chai.expect;
1212

@@ -94,4 +94,134 @@ class TestSSHPublicKeyValue {
9494
).to.throw("Key is invalid");
9595
}
9696
}
97-
module.exports = new TestSSHPublicKeyValue(); // Only to circumvent no usage warning :-/
97+
98+
@suite
99+
class TestEnvVar {
100+
@test
101+
public testGetGitpodImageAuth_empty() {
102+
const result = EnvVar.getGitpodImageAuth([]);
103+
expect(result.size).to.equal(0);
104+
}
105+
106+
@test
107+
public testGetGitpodImageAuth_noRelevantVar() {
108+
const envVars: EnvVarWithValue[] = [{ name: "OTHER_VAR", value: "some_value" }];
109+
const result = EnvVar.getGitpodImageAuth(envVars);
110+
expect(result.size).to.equal(0);
111+
}
112+
113+
@test
114+
public testGetGitpodImageAuth_singleEntryNoPort() {
115+
const envVars: EnvVarWithValue[] = [
116+
{
117+
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
118+
value: "my-registry.foo.net:Zm9vOmJhcg==",
119+
},
120+
];
121+
const result = EnvVar.getGitpodImageAuth(envVars);
122+
expect(result.size).to.equal(1);
123+
expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");
124+
}
125+
126+
@test
127+
public testGetGitpodImageAuth_singleEntryWithPort() {
128+
const envVars: EnvVarWithValue[] = [
129+
{
130+
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
131+
value: "my-registry.foo.net:5000:Zm9vOmJhcg==",
132+
},
133+
];
134+
const result = EnvVar.getGitpodImageAuth(envVars);
135+
expect(result.size).to.equal(1);
136+
expect(result.get("my-registry.foo.net:5000")).to.equal("Zm9vOmJhcg==");
137+
}
138+
139+
@test
140+
public testGetGitpodImageAuth_multipleEntries() {
141+
const envVars: EnvVarWithValue[] = [
142+
{
143+
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
144+
value: "my-registry.foo.net:Zm9vOmJhcg==,hubble2.bar.com:YWJjOmRlZg==",
145+
},
146+
];
147+
const result = EnvVar.getGitpodImageAuth(envVars);
148+
expect(result.size).to.equal(2);
149+
expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");
150+
expect(result.get("hubble2.bar.com")).to.equal("YWJjOmRlZg==");
151+
}
152+
153+
@test
154+
public testGetGitpodImageAuth_multipleEntriesWithPortAndMalformed() {
155+
const envVars: EnvVarWithValue[] = [
156+
{
157+
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
158+
value: "my-registry.foo.net:5000:Zm9vOmJhcg==,hubble2.bar.com:YWJjOmRlZg==,invalidEntry,another.host:anothercred",
159+
},
160+
];
161+
const result = EnvVar.getGitpodImageAuth(envVars);
162+
expect(result.size).to.equal(3);
163+
expect(result.get("hubble2.bar.com")).to.equal("YWJjOmRlZg==");
164+
expect(result.get("another.host")).to.equal("anothercred");
165+
expect(result.get("my-registry.foo.net:5000")).to.equal("Zm9vOmJhcg==");
166+
}
167+
168+
@test
169+
public testGetGitpodImageAuth_emptyValue() {
170+
const envVars: EnvVarWithValue[] = [
171+
{
172+
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
173+
value: "",
174+
},
175+
];
176+
const result = EnvVar.getGitpodImageAuth(envVars);
177+
expect(result.size).to.equal(0);
178+
}
179+
180+
@test
181+
public testGetGitpodImageAuth_malformedEntries() {
182+
const envVars: EnvVarWithValue[] = [
183+
{
184+
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
185+
value: "justhost,hostonly:,:credonly,host1:cred1:extrapart",
186+
},
187+
];
188+
const result = EnvVar.getGitpodImageAuth(envVars);
189+
// "justhost" -> ignored (split(':').length === 1)
190+
// "hostonly:" -> ignored (split(':').length === 2, but second part is empty, current logic keeps it as key "hostonly", value "")
191+
// ":credonly" -> ignored (split(':').length === 2, but first part is empty, current logic keeps it as key "", value "credonly")
192+
// "host1:cred1:extrapart" -> ignored (split(':').length === 3)
193+
194+
// Based on current logic:
195+
// "hostonly:" becomes map.set("hostonly", "")
196+
// ":credonly" becomes map.set("", "credonly")
197+
// Let's refine assertions based on the exact behavior of `e.length == 2`
198+
// "hostonly:" -> e = ["hostonly", ""], length is 2. map.set("hostonly", "")
199+
// ":credonly" -> e = ["", "credonly"], length is 2. map.set("", "credonly")
200+
expect(result.size).to.equal(2);
201+
expect(result.get("hostonly")).to.equal("");
202+
expect(result.get("")).to.equal("credonly");
203+
}
204+
205+
// @test
206+
// public testGetGitpodImageAuth_entriesWithSpaces() {
207+
// const envVars: EnvVarWithValue[] = [
208+
// {
209+
// name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
210+
// value: " my-registry.foo.net : Zm9vOmJhcg== , hubble2.bar.com:YWJjOmRlZg== ",
211+
// },
212+
// ];
213+
// const result = EnvVar.getGitpodImageAuth(envVars);
214+
// expect(result.size).to.equal(2);
215+
// // .trim() is applied to each comma-separated part, but not to the host/cred after splitting by colon
216+
// expect(result.get("my-registry.foo.net ")).to.equal(" Zm9vOmJhcg=="); // Note the spaces
217+
// expect(result.get(" hubble2.bar.com")).to.equal("YWJjOmRlZg=="); // Note the spaces
218+
// }
219+
}
220+
221+
// Exporting both test suites
222+
const testSSHPublicKeyValue = new TestSSHPublicKeyValue();
223+
const testEnvVar = new TestEnvVar();
224+
module.exports = {
225+
testSSHPublicKeyValue,
226+
testEnvVar,
227+
};

components/gitpod-protocol/src/protocol.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,24 @@ export namespace EnvVar {
293293
return res;
294294
}
295295

296-
(imageAuth.value || "")
297-
.split(",")
298-
.map((e) => e.trim().split(":"))
299-
.filter((e) => e.length == 2)
300-
.forEach((e) => res.set(e[0], e[1]));
296+
(imageAuth.value || "").split(",").forEach((entry) => {
297+
const parts = entry.trim().split(":");
298+
if (parts.length === 2) {
299+
// host:token
300+
const host = parts[0];
301+
const token = parts[1];
302+
if (host && token) {
303+
res.set(host, token);
304+
}
305+
} else if (parts.length === 3) {
306+
// host:port:token
307+
const hostWithPort = `${parts[0]}:${parts[1]}`;
308+
const token = parts[2];
309+
if (parts[0] && parts[1] && token) {
310+
res.set(hostWithPort, token);
311+
}
312+
}
313+
});
301314
return res;
302315
}
303316
}

0 commit comments

Comments
 (0)