Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 118 additions & 2 deletions components/gitpod-protocol/src/protocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { suite, test } from "@testdeck/mocha";
import * as chai from "chai";
import { SSHPublicKeyValue } from ".";
import { SSHPublicKeyValue, EnvVar, EnvVarWithValue } from ".";

const expect = chai.expect;

Expand Down Expand Up @@ -94,4 +94,120 @@ class TestSSHPublicKeyValue {
).to.throw("Key is invalid");
}
}
module.exports = new TestSSHPublicKeyValue(); // Only to circumvent no usage warning :-/

@suite
class TestEnvVar {
@test
public testGetGitpodImageAuth_empty() {
const result = EnvVar.getGitpodImageAuth([]);
expect(result.size).to.equal(0);
}

@test
public testGetGitpodImageAuth_noRelevantVar() {
const envVars: EnvVarWithValue[] = [{ name: "OTHER_VAR", value: "some_value" }];
const result = EnvVar.getGitpodImageAuth(envVars);
expect(result.size).to.equal(0);
}

@test
public testGetGitpodImageAuth_singleEntryNoPort() {
const envVars: EnvVarWithValue[] = [
{
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
value: "my-registry.foo.net:Zm9vOmJhcg==",
},
];
const result = EnvVar.getGitpodImageAuth(envVars);
expect(result.size).to.equal(1);
expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");
}

@test
public testGetGitpodImageAuth_singleEntryWithPort() {
const envVars: EnvVarWithValue[] = [
{
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
value: "my-registry.foo.net:5000:Zm9vOmJhcg==",
},
];
const result = EnvVar.getGitpodImageAuth(envVars);
expect(result.size).to.equal(1);
expect(result.get("my-registry.foo.net:5000")).to.equal("Zm9vOmJhcg==");
}

@test
public testGetGitpodImageAuth_multipleEntries() {
const envVars: EnvVarWithValue[] = [
{
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
value: "my-registry.foo.net:Zm9vOmJhcg==,my-registry2.bar.com:YWJjOmRlZg==",
},
];
const result = EnvVar.getGitpodImageAuth(envVars);
expect(result.size).to.equal(2);
expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");
expect(result.get("my-registry2.bar.com")).to.equal("YWJjOmRlZg==");
}

@test
public testGetGitpodImageAuth_multipleEntriesWithPortAndMalformed() {
const envVars: EnvVarWithValue[] = [
{
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
value: "my-registry.foo.net:5000:Zm9vOmJhcg==,my-registry2.bar.com:YWJjOmRlZg==,invalidEntry,another.host:anothercred",
},
];
const result = EnvVar.getGitpodImageAuth(envVars);
expect(result.size).to.equal(3);
expect(result.get("my-registry2.bar.com")).to.equal("YWJjOmRlZg==");
expect(result.get("another.host")).to.equal("anothercred");
expect(result.get("my-registry.foo.net:5000")).to.equal("Zm9vOmJhcg==");
}

@test
public testGetGitpodImageAuth_emptyValue() {
const envVars: EnvVarWithValue[] = [
{
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
value: "",
},
];
const result = EnvVar.getGitpodImageAuth(envVars);
expect(result.size).to.equal(0);
}

@test
public testGetGitpodImageAuth_malformedEntries() {
const envVars: EnvVarWithValue[] = [
{
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
value: "justhost,hostonly:,:credonly,:::,",
},
];
const result = EnvVar.getGitpodImageAuth(envVars);
expect(result.size).to.equal(0);
}

@test
public testGetGitpodImageAuth_entriesWithSpaces() {
const envVars: EnvVarWithValue[] = [
{
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
value: " my-registry.foo.net : Zm9vOmJhcg== , my-registry2.bar.com:YWJjOmRlZg== ",
},
];
const result = EnvVar.getGitpodImageAuth(envVars);
expect(result.size).to.equal(2);
expect(result.get("my-registry.foo.net ")).to.equal(" Zm9vOmJhcg==");
expect(result.get("my-registry2.bar.com")).to.equal("YWJjOmRlZg==");
}
}

// Exporting both test suites
const testSSHPublicKeyValue = new TestSSHPublicKeyValue();
const testEnvVar = new TestEnvVar();
module.exports = {
testSSHPublicKeyValue,
testEnvVar,
};
24 changes: 19 additions & 5 deletions components/gitpod-protocol/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,25 @@ export namespace EnvVar {
return res;
}

(imageAuth.value || "")
.split(",")
.map((e) => e.trim().split(":"))
.filter((e) => e.length == 2)
.forEach((e) => res.set(e[0], e[1]));
(imageAuth.value || "").split(",").forEach((entry) => {
const parts = entry.trim().split(":");
if (parts.length === 2) {
// host:token
const host = parts[0];
const token = parts[1];
if (host && token && host.length > 0 && token.length > 0) {
res.set(host, token);
}
} else if (parts.length === 3) {
// host:port:token
const hostWithPort = `${parts[0]}:${parts[1]}`;
const token = parts[2];
if (hostWithPort && token && hostWithPort.length > 0 && token.length > 0) {
res.set(hostWithPort, token);
}
}
});

return res;
}
}
Expand Down
Loading