|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/docker/cli/cli/config/configfile" |
| 9 | + "github.com/docker/cli/cli/config/types" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/replicate/cog/pkg/global" |
| 14 | +) |
| 15 | + |
| 16 | +func TestLoadRegistryAuths_Fallback(t *testing.T) { |
| 17 | + ctx := context.Background() |
| 18 | + |
| 19 | + t.Run("uses credentials for requested host when available", func(t *testing.T) { |
| 20 | + // Create a mock config with credentials for the requested host |
| 21 | + conf := &configfile.ConfigFile{ |
| 22 | + AuthConfigs: map[string]types.AuthConfig{ |
| 23 | + "registry.example.com": { |
| 24 | + Username: "user1", |
| 25 | + Password: "pass1", |
| 26 | + }, |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + auth, err := tryLoadAuthForHost(ctx, conf, "registry.example.com") |
| 31 | + require.NoError(t, err) |
| 32 | + require.NotNil(t, auth) |
| 33 | + assert.Equal(t, "user1", auth.Username) |
| 34 | + assert.Equal(t, "pass1", auth.Password) |
| 35 | + assert.Equal(t, "registry.example.com", auth.ServerAddress) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("falls back to default registry credentials when alternate registry has no credentials", func(t *testing.T) { |
| 39 | + // Set up a temporary docker config file |
| 40 | + tmpDir := t.TempDir() |
| 41 | + dockerConfigPath := filepath.Join(tmpDir, "config.json") |
| 42 | + |
| 43 | + // Create a config file with credentials only for the default registry |
| 44 | + conf := &configfile.ConfigFile{ |
| 45 | + Filename: dockerConfigPath, |
| 46 | + AuthConfigs: map[string]types.AuthConfig{ |
| 47 | + global.DefaultReplicateRegistryHost: { |
| 48 | + Username: "defaultuser", |
| 49 | + Password: "defaultpass", |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | + require.NoError(t, conf.Save()) |
| 54 | + |
| 55 | + // Point Docker to our test config |
| 56 | + t.Setenv("DOCKER_CONFIG", tmpDir) |
| 57 | + |
| 58 | + // Try loading auth for an alternate registry that doesn't have credentials |
| 59 | + auths, err := loadRegistryAuths(ctx, "registry.example.com") |
| 60 | + require.NoError(t, err) |
| 61 | + require.NotNil(t, auths) |
| 62 | + |
| 63 | + // Should have fallen back to default registry credentials |
| 64 | + auth, ok := auths["registry.example.com"] |
| 65 | + require.True(t, ok, "should have auth for registry.example.com") |
| 66 | + assert.Equal(t, "defaultuser", auth.Username) |
| 67 | + assert.Equal(t, "defaultpass", auth.Password) |
| 68 | + assert.Equal(t, "registry.example.com", auth.ServerAddress, "server address should be updated to the requested host") |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("does not fallback when requesting default registry", func(t *testing.T) { |
| 72 | + // This test uses tryLoadAuthForHost directly to avoid credential store issues |
| 73 | + conf := &configfile.ConfigFile{ |
| 74 | + AuthConfigs: map[string]types.AuthConfig{}, |
| 75 | + } |
| 76 | + |
| 77 | + // Try loading auth for the default registry |
| 78 | + auth, err := tryLoadAuthForHost(ctx, conf, global.DefaultReplicateRegistryHost) |
| 79 | + require.Error(t, err, "should error when no credentials found") |
| 80 | + assert.Nil(t, auth) |
| 81 | + assert.Contains(t, err.Error(), "no credentials found") |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("prefers direct credentials over fallback", func(t *testing.T) { |
| 85 | + // Create a mock config with credentials for both registries |
| 86 | + conf := &configfile.ConfigFile{ |
| 87 | + AuthConfigs: map[string]types.AuthConfig{ |
| 88 | + global.DefaultReplicateRegistryHost: { |
| 89 | + Username: "defaultuser", |
| 90 | + Password: "defaultpass", |
| 91 | + }, |
| 92 | + "registry.example.com": { |
| 93 | + Username: "directuser", |
| 94 | + Password: "directpass", |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + // Try loading auth for the alternate registry |
| 100 | + auth, err := tryLoadAuthForHost(ctx, conf, "registry.example.com") |
| 101 | + require.NoError(t, err) |
| 102 | + require.NotNil(t, auth) |
| 103 | + |
| 104 | + // Should use direct credentials, not fallback |
| 105 | + assert.Equal(t, "directuser", auth.Username) |
| 106 | + assert.Equal(t, "directpass", auth.Password) |
| 107 | + assert.Equal(t, "registry.example.com", auth.ServerAddress) |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("returns empty map when no credentials available", func(t *testing.T) { |
| 111 | + // This test uses tryLoadAuthForHost to avoid credential store issues |
| 112 | + // The loadRegistryAuths function doesn't error when no credentials are found, |
| 113 | + // it just returns an empty map |
| 114 | + conf := &configfile.ConfigFile{ |
| 115 | + AuthConfigs: map[string]types.AuthConfig{}, |
| 116 | + } |
| 117 | + |
| 118 | + // Try loading auth for an alternate registry (will fail) |
| 119 | + auth1, err := tryLoadAuthForHost(ctx, conf, "registry.example.com") |
| 120 | + require.Error(t, err) |
| 121 | + assert.Nil(t, auth1) |
| 122 | + |
| 123 | + // Try loading auth for default registry (will also fail) |
| 124 | + auth2, err := tryLoadAuthForHost(ctx, conf, global.DefaultReplicateRegistryHost) |
| 125 | + require.Error(t, err) |
| 126 | + assert.Nil(t, auth2) |
| 127 | + |
| 128 | + // Since both fail, loadRegistryAuths would return an empty map |
| 129 | + // (it doesn't error, just silently skips hosts without credentials) |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +func TestTryLoadAuthForHost(t *testing.T) { |
| 134 | + ctx := context.Background() |
| 135 | + |
| 136 | + t.Run("loads auth from config file", func(t *testing.T) { |
| 137 | + conf := &configfile.ConfigFile{ |
| 138 | + AuthConfigs: map[string]types.AuthConfig{ |
| 139 | + "registry.example.com": { |
| 140 | + Username: "testuser", |
| 141 | + Password: "testpass", |
| 142 | + Auth: "dGVzdHVzZXI6dGVzdHBhc3M=", |
| 143 | + |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + auth, err := tryLoadAuthForHost(ctx, conf, "registry.example.com") |
| 149 | + require.NoError(t, err) |
| 150 | + require.NotNil(t, auth) |
| 151 | + assert.Equal(t, "testuser", auth.Username) |
| 152 | + assert.Equal(t, "testpass", auth.Password) |
| 153 | + assert.Equal(t, "dGVzdHVzZXI6dGVzdHBhc3M=", auth.Auth) |
| 154 | + assert. Equal( t, "[email protected]", auth. Email) |
| 155 | + assert.Equal(t, "registry.example.com", auth.ServerAddress) |
| 156 | + }) |
| 157 | + |
| 158 | + t.Run("returns error when no auth found", func(t *testing.T) { |
| 159 | + conf := &configfile.ConfigFile{ |
| 160 | + AuthConfigs: map[string]types.AuthConfig{}, |
| 161 | + } |
| 162 | + |
| 163 | + auth, err := tryLoadAuthForHost(ctx, conf, "registry.example.com") |
| 164 | + require.Error(t, err) |
| 165 | + assert.Nil(t, auth) |
| 166 | + assert.Contains(t, err.Error(), "no credentials found") |
| 167 | + }) |
| 168 | +} |
0 commit comments