|
1 | 1 | package command_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "path" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | "github.com/docker/cli/cli/command" |
@@ -80,3 +82,113 @@ func TestGetDefaultAuthConfig_HelperError(t *testing.T) { |
80 | 82 | assert.Check(t, is.DeepEqual(expectedAuthConfig, authconfig)) |
81 | 83 | assert.Check(t, is.ErrorContains(err, "docker-credential-fake-does-not-exist")) |
82 | 84 | } |
| 85 | + |
| 86 | +func TestRetrieveAuthTokenFromImage(t *testing.T) { |
| 87 | + // configFileContent contains a plain-text "username:password", as stored by |
| 88 | + // the plain-text store; |
| 89 | + // https://github.com/docker/cli/blob/v28.0.4/cli/config/configfile/file.go#L218-L229 |
| 90 | + const configFileContent = `{"auths": { |
| 91 | + "https://index.docker.io/v1/": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, |
| 92 | + "[::1]": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, |
| 93 | + "[::1]:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, |
| 94 | + "127.0.0.1": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, |
| 95 | + "127.0.0.1:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, |
| 96 | + "localhost": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, |
| 97 | + "localhost:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, |
| 98 | + "registry-1.docker.io": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, |
| 99 | + "registry.hub.docker.com": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="} |
| 100 | + } |
| 101 | +}` |
| 102 | + cfg := configfile.ConfigFile{} |
| 103 | + err := cfg.LoadFromReader(bytes.NewReader([]byte(configFileContent))) |
| 104 | + assert.NilError(t, err) |
| 105 | + |
| 106 | + remoteRefs := []string{ |
| 107 | + "ubuntu", |
| 108 | + "ubuntu:latest", |
| 109 | + "ubuntu:latest@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", |
| 110 | + "ubuntu@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", |
| 111 | + "library/ubuntu", |
| 112 | + "library/ubuntu:latest", |
| 113 | + "library/ubuntu:latest@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", |
| 114 | + "library/ubuntu@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", |
| 115 | + } |
| 116 | + |
| 117 | + tests := []struct { |
| 118 | + prefix string |
| 119 | + expectedAddress string |
| 120 | + expectedAuthCfg registry.AuthConfig |
| 121 | + }{ |
| 122 | + { |
| 123 | + prefix: "", |
| 124 | + expectedAddress: "https://index.docker.io/v1/", |
| 125 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "https://index.docker.io/v1/"}, |
| 126 | + }, |
| 127 | + { |
| 128 | + prefix: "docker.io", |
| 129 | + expectedAddress: "https://index.docker.io/v1/", |
| 130 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "https://index.docker.io/v1/"}, |
| 131 | + }, |
| 132 | + { |
| 133 | + prefix: "index.docker.io", |
| 134 | + expectedAddress: "https://index.docker.io/v1/", |
| 135 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "https://index.docker.io/v1/"}, |
| 136 | + }, |
| 137 | + { |
| 138 | + // FIXME(thaJeztah): registry-1.docker.io (the actual registry) is the odd one out, and is stored separate from other URLs used for docker hub's registry |
| 139 | + prefix: "registry-1.docker.io", |
| 140 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "registry-1.docker.io"}, |
| 141 | + }, |
| 142 | + { |
| 143 | + // FIXME(thaJeztah): registry.hub.docker.com is stored separate from other URLs used for docker hub's registry |
| 144 | + prefix: "registry.hub.docker.com", |
| 145 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "registry.hub.docker.com"}, |
| 146 | + }, |
| 147 | + { |
| 148 | + prefix: "[::1]", |
| 149 | + expectedAddress: "[::1]", |
| 150 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "[::1]"}, |
| 151 | + }, |
| 152 | + { |
| 153 | + prefix: "[::1]:5000", |
| 154 | + expectedAddress: "[::1]:5000", |
| 155 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "[::1]:5000"}, |
| 156 | + }, |
| 157 | + { |
| 158 | + prefix: "127.0.0.1", |
| 159 | + expectedAddress: "127.0.0.1", |
| 160 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "127.0.0.1"}, |
| 161 | + }, |
| 162 | + { |
| 163 | + prefix: "localhost", |
| 164 | + expectedAddress: "localhost", |
| 165 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "localhost"}, |
| 166 | + }, |
| 167 | + { |
| 168 | + prefix: "localhost:5000", |
| 169 | + expectedAddress: "localhost:5000", |
| 170 | + expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "localhost:5000"}, |
| 171 | + }, |
| 172 | + { |
| 173 | + prefix: "no-auth.example.com", |
| 174 | + expectedAuthCfg: registry.AuthConfig{}, |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + for _, tc := range tests { |
| 179 | + tcName := tc.prefix |
| 180 | + if tc.prefix == "" { |
| 181 | + tcName = "no-prefix" |
| 182 | + } |
| 183 | + t.Run(tcName, func(t *testing.T) { |
| 184 | + for _, remoteRef := range remoteRefs { |
| 185 | + imageRef := path.Join(tc.prefix, remoteRef) |
| 186 | + actual, err := command.RetrieveAuthTokenFromImage(&cfg, imageRef) |
| 187 | + assert.NilError(t, err) |
| 188 | + ac, err := registry.DecodeAuthConfig(actual) |
| 189 | + assert.NilError(t, err) |
| 190 | + assert.Check(t, is.DeepEqual(*ac, tc.expectedAuthCfg)) |
| 191 | + } |
| 192 | + }) |
| 193 | + } |
| 194 | +} |
0 commit comments