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 all 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,
};
23 changes: 20 additions & 3 deletions components/gitpod-protocol/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,28 @@ export namespace EnvVar {
return res;
}

const parse = (parts: string[]): { host: string; auth: string } | undefined => {
if (parts.some((e) => e === "")) {
return undefined;
}
if (parts.length === 2) {
return { host: parts[0], auth: parts[1] };
} else if (parts.length === 3) {
return { host: `${parts[0]}:${parts[1]}`, auth: parts[2] };
}
return undefined;
};

(imageAuth.value || "")
.split(",")
.map((e) => e.trim().split(":"))
.filter((e) => e.length == 2)
.forEach((e) => res.set(e[0], e[1]));
.map((e) => e.split(":").map((part) => part.trim()))
.forEach((parts) => {
const parsed = parse(parts);
if (parsed) {
res.set(parsed.host, parsed.auth);
}
});

return res;
}
}
Expand Down
32 changes: 26 additions & 6 deletions components/image-builder-bob/pkg/proxy/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,50 @@ type authConfig struct {

type MapAuthorizer map[string]authConfig

func (a MapAuthorizer) Authorize(host string) (user, pass string, err error) {
func (a MapAuthorizer) Authorize(hostHeader string) (user, pass string, err error) {
defer func() {
log.WithFields(logrus.Fields{
"host": host,
"host": hostHeader,
"user": user,
}).Info("authorizing registry access")
}()

// Strip any port from the host if present
host = strings.Split(host, ":")[0]
parseHostHeader := func(hostHeader string) (string, string) {
hostHeaderSlice := strings.Split(hostHeader, ":")
hostname := strings.TrimSpace(hostHeaderSlice[0])
var port string
if len(hostHeaderSlice) > 1 {
port = strings.TrimSpace(hostHeaderSlice[1])
}
return hostname, port
}
hostname, port := parseHostHeader(hostHeader)
// gpl: Could be port 80 as well, but we don't know if we are servinc http or https, we assume https
if port == "" {
port = "443"
}
host := hostname + ":" + port

explicitHostMatcher := func() (authConfig, bool) {
// 1. precise host match
res, ok := a[host]
if ok {
return res, ok
}

// 2. make sure we not have a hostname match
res, ok = a[hostname]
return res, ok
}
ecrHostMatcher := func() (authConfig, bool) {
if isECRRegistry(host) {
if isECRRegistry(hostname) {
res, ok := a[DummyECRRegistryDomain]
return res, ok
}
return authConfig{}, false
}
dockerHubHostMatcher := func() (authConfig, bool) {
if isDockerHubRegistry(host) {
if isDockerHubRegistry(hostname) {
res, ok := a["docker.io"]
return res, ok
}
Expand Down
22 changes: 21 additions & 1 deletion components/image-builder-bob/pkg/proxy/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestAuthorize(t *testing.T) {
},
},
{
name: "docker auth format - valid credentials - host with port",
name: "docker auth format - valid credentials - host with :443 port",
constructor: NewAuthorizerFromDockerEnvVar,
input: `{"auths": {"registry.example.com": {"auth": "dXNlcjpwYXNz"}}}`, // base64(user:pass)
testHost: "registry.example.com:443",
Expand Down Expand Up @@ -110,6 +110,26 @@ func TestAuthorize(t *testing.T) {
pass: "",
},
},
{
name: "Docker Hub",
constructor: NewAuthorizerFromEnvVar,
input: `{"docker.io": {"auth": "dXNlcjpwYXNz"}}`,
testHost: "registry-1.docker.io",
expected: expectation{
user: "user",
pass: "pass",
},
},
{
name: "docker auth format - valid credentials - host with :5000 port",
constructor: NewAuthorizerFromDockerEnvVar,
input: `{"auths": {"registry.example.com:5000": {"auth": "dXNlcjpwYXNz"}}}`, // base64(user:pass)
testHost: "registry.example.com:5000",
expected: expectation{
user: "user",
pass: "pass",
},
},
}

for _, tt := range tests {
Expand Down
8 changes: 5 additions & 3 deletions components/image-builder-mk3/pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,11 @@ func (a AllowedAuthFor) additionalAuth(domain string) *Authentication {
dec, err := base64.StdEncoding.DecodeString(ath)
if err == nil {
segs := strings.Split(string(dec), ":")
if len(segs) > 1 {
res.Username = segs[0]
res.Password = strings.Join(segs[1:], ":")
numSegs := len(segs)

if numSegs > 1 {
res.Username = strings.Join(segs[:numSegs-1], ":")
res.Password = segs[numSegs-1]
}
} else {
log.Errorf("failed getting additional auth")
Expand Down
96 changes: 96 additions & 0 deletions components/image-builder-mk3/pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package auth

import (
"encoding/base64"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -30,3 +31,98 @@ func TestIsECRRegistry(t *testing.T) {
})
}
}

func TestAdditionalAuth(t *testing.T) {
tests := []struct {
name string
domain string
additionalMap map[string]string
expectedAuth *Authentication
}{
{
name: "standard host:token",
domain: "myregistry.com",
additionalMap: map[string]string{
"myregistry.com": base64.StdEncoding.EncodeToString([]byte("myregistry.com:mytoken")),
},
expectedAuth: &Authentication{
Username: "myregistry.com",
Password: "mytoken",
Auth: base64.StdEncoding.EncodeToString([]byte("myregistry.com:mytoken")),
},
},
{
name: "buggy host:port:token",
domain: "myregistry.com:5000",
additionalMap: map[string]string{
"myregistry.com:5000": base64.StdEncoding.EncodeToString([]byte("myregistry.com:5000:mytoken")),
},
expectedAuth: &Authentication{
Username: "myregistry.com:5000",
Password: "mytoken",
Auth: base64.StdEncoding.EncodeToString([]byte("myregistry.com:5000:mytoken")),
},
},
{
name: "only username, no password/token (single segment)",
domain: "useronly.com",
additionalMap: map[string]string{
"useronly.com": base64.StdEncoding.EncodeToString([]byte("justauser")),
},
expectedAuth: &Authentication{
Auth: base64.StdEncoding.EncodeToString([]byte("justauser")),
},
},
{
name: "empty auth string",
domain: "emptyauth.com",
additionalMap: map[string]string{
"emptyauth.com": base64.StdEncoding.EncodeToString([]byte("")),
},
expectedAuth: &Authentication{
Auth: base64.StdEncoding.EncodeToString([]byte("")),
},
},
{
name: "domain not in map",
domain: "notfound.com",
additionalMap: map[string]string{"someother.com": base64.StdEncoding.EncodeToString([]byte("someauth"))},
expectedAuth: nil,
},
{
name: "invalid base64 string",
domain: "invalidbase64.com",
additionalMap: map[string]string{
"invalidbase64.com": "!!!INVALID_BASE64!!!",
},
expectedAuth: &Authentication{
Auth: "!!!INVALID_BASE64!!!",
},
},
{
name: "standard host:token where username in cred is different from domain key",
domain: "docker.io",
additionalMap: map[string]string{
"docker.io": base64.StdEncoding.EncodeToString([]byte("user1:pass1")),
},
expectedAuth: &Authentication{
Username: "user1",
Password: "pass1",
Auth: base64.StdEncoding.EncodeToString([]byte("user1:pass1")),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
aaf := AllowedAuthFor{
Additional: tt.additionalMap,
}
actualAuth := aaf.additionalAuth(tt.domain)

if diff := cmp.Diff(tt.expectedAuth, actualAuth); diff != "" {
t.Errorf("additionalAuth() mismatch (-want +got):\n%s", diff)
}
})
}
}
Loading
Loading