Skip to content

[gitpod-protocol] handle host:port:token for getGitpodImageAuth #20806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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,
};
16 changes: 14 additions & 2 deletions components/gitpod-protocol/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,23 @@ export namespace EnvVar {
return res;
}

// Returns a host value, which can include port, if token also has length.
const getHost = (parts: string[]): string => {
if (parts.length === 2 && parts[0] && parts[1]) {
return parts[0];
} else if (parts.length === 3 && parts[0] && parts[1] && parts[2]) {
return `${parts[0]}:${parts[1]}`;
}
return "";
};

(imageAuth.value || "")
.split(",")
.map((e) => e.trim().split(":"))
.filter((e) => e.length == 2)
.forEach((e) => res.set(e[0], e[1]));
.forEach((parts) => {
getHost(parts) !== "" ? res.set(getHost(parts), parts[parts.length - 1]) : null;
});

return res;
}
}
Expand Down
Loading