-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: parse image auth config correctly (#103)
- Loading branch information
Showing
6 changed files
with
270 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,98 @@ func TestDocker(t *testing.T) { | |
execer.AssertCommandsCalled(t) | ||
}) | ||
|
||
t.Run("Images", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testcase struct { | ||
name string | ||
image string | ||
success bool | ||
} | ||
|
||
testcases := []testcase{ | ||
{ | ||
name: "Repository", | ||
image: "ubuntu", | ||
success: true, | ||
}, | ||
{ | ||
name: "RepositoryPath", | ||
image: "ubuntu/ubuntu", | ||
success: true, | ||
}, | ||
|
||
{ | ||
name: "RepositoryLatest", | ||
image: "ubuntu:latest", | ||
success: true, | ||
}, | ||
{ | ||
name: "RepositoryTag", | ||
image: "ubuntu:24.04", | ||
success: true, | ||
}, | ||
{ | ||
name: "RepositoryPathTag", | ||
image: "ubuntu/ubuntu:18.04", | ||
success: true, | ||
}, | ||
{ | ||
name: "RegistryRepository", | ||
image: "gcr.io/ubuntu", | ||
success: true, | ||
}, | ||
{ | ||
name: "RegistryRepositoryTag", | ||
image: "gcr.io/ubuntu:24.04", | ||
success: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx, cmd := clitest.New(t, "docker", | ||
"--image="+tc.image, | ||
"--username=root", | ||
"--agent-token=hi", | ||
) | ||
|
||
called := make(chan struct{}) | ||
execer := clitest.Execer(ctx) | ||
client := clitest.DockerClient(t, ctx) | ||
execer.AddCommands(&xunixfake.FakeCmd{ | ||
FakeCmd: &testingexec.FakeCmd{ | ||
Argv: []string{ | ||
"sysbox-mgr", | ||
}, | ||
}, | ||
WaitFn: func() error { close(called); select {} }, //nolint:revive | ||
}) | ||
|
||
var created bool | ||
client.ContainerCreateFn = func(_ context.Context, conf *container.Config, _ *container.HostConfig, _ *network.NetworkingConfig, _ *v1.Platform, _ string) (container.CreateResponse, error) { | ||
created = true | ||
require.Equal(t, tc.image, conf.Image) | ||
return container.CreateResponse{}, nil | ||
} | ||
|
||
err := cmd.ExecuteContext(ctx) | ||
if !tc.success { | ||
require.Error(t, err) | ||
return | ||
} | ||
|
||
<-called | ||
require.NoError(t, err) | ||
require.True(t, created, "container create fn not called") | ||
execer.AssertCommandsCalled(t) | ||
}) | ||
} | ||
}) | ||
|
||
// Test that dockerd is configured correctly. | ||
t.Run("DockerdConfigured", func(t *testing.T) { | ||
t.Parallel() | ||
|
@@ -384,13 +476,13 @@ func TestDocker(t *testing.T) { | |
t.Parallel() | ||
|
||
ctx, cmd := clitest.New(t, "docker", | ||
"--image=ubuntu", | ||
"--image=us.gcr.io/ubuntu", | ||
"--username=root", | ||
"--agent-token=hi", | ||
fmt.Sprintf("--image-secret=%s", rawDockerAuth), | ||
) | ||
|
||
raw := []byte(`{"username":"_json_key","password":"{\"type\": \"service_account\", \"project_id\": \"some-test\", \"private_key_id\": \"blahblah\", \"private_key\": \"-----BEGIN PRIVATE KEY-----mykey-----END PRIVATE KEY-----\", \"client_email\": \"[email protected]\", \"client_id\": \"123\", \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\", \"token_uri\": \"https://oauth2.googleapis.com/token\", \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\", \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/test.iam.gserviceaccount.com\" }","auth":"X2pzb25fa2V5OnsKCgkidHlwZSI6ICJzZXJ2aWNlX2FjY291bnQiLAoJInByb2plY3RfaWQiOiAic29tZS10ZXN0IiwKCSJwcml2YXRlX2tleV9pZCI6ICJibGFoYmxhaCIsCgkicHJpdmF0ZV9rZXkiOiAiLS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCm15a2V5LS0tLS1FTkQgUFJJVkFURSBLRVktLS0tLQoiLAoJImNsaWVudF9lbWFpbCI6ICJ0ZXN0QHRlc3QuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20iLAoJImNsaWVudF9pZCI6ICIxMjMiLAoJImF1dGhfdXJpIjogImh0dHBzOi8vYWNjb3VudHMuZ29vZ2xlLmNvbS9vL29hdXRoMi9hdXRoIiwKCSJ0b2tlbl91cmkiOiAiaHR0cHM6Ly9vYXV0aDIuZ29vZ2xlYXBpcy5jb20vdG9rZW4iLAoJImF1dGhfcHJvdmlkZXJfeDUwOV9jZXJ0X3VybCI6ICJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9vYXV0aDIvdjEvY2VydHMiLAoJImNsaWVudF94NTA5X2NlcnRfdXJsIjogImh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL3JvYm90L3YxL21ldGFkYXRhL3g1MDkvdGVzdC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIKfQo=","email":"[email protected]"}`) | ||
raw := []byte(`{"username":"_json_key","password":"{\"type\": \"service_account\", \"project_id\": \"some-test\", \"private_key_id\": \"blahblah\", \"private_key\": \"-----BEGIN PRIVATE KEY-----mykey-----END PRIVATE KEY-----\", \"client_email\": \"[email protected]\", \"client_id\": \"123\", \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\", \"token_uri\": \"https://oauth2.googleapis.com/token\", \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\", \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/test.iam.gserviceaccount.com\" }"}`) | ||
authB64 := base64.URLEncoding.EncodeToString(raw) | ||
|
||
client := clitest.DockerClient(t, ctx) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package dockerutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/envbox/dockerutil" | ||
) | ||
|
||
func TestAuthConfigFromString(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("Auth", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
//nolint:gosec // this is a test | ||
creds := `{ "auths": { "docker.registry.test": { "auth": "Zm9vQGJhci5jb206YWJjMTIz" } } }` | ||
expectedUsername := "[email protected]" | ||
expectedPassword := "abc123" | ||
|
||
cfg, err := dockerutil.AuthConfigFromString(creds, "docker.registry.test") | ||
require.NoError(t, err) | ||
require.Equal(t, expectedUsername, cfg.Username) | ||
require.Equal(t, expectedPassword, cfg.Password) | ||
}) | ||
|
||
t.Run("UsernamePassword", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
//nolint:gosec // this is a test | ||
creds := `{ "auths": { "docker.registry.test": { "username": "foobarbaz", "password": "123abc" } } }` | ||
expectedUsername := "foobarbaz" | ||
expectedPassword := "123abc" | ||
|
||
cfg, err := dockerutil.AuthConfigFromString(creds, "docker.registry.test") | ||
require.NoError(t, err) | ||
require.Equal(t, expectedUsername, cfg.Username) | ||
require.Equal(t, expectedPassword, cfg.Password) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.