@@ -18,15 +18,15 @@ package docker
18
18
19
19
import (
20
20
"context"
21
- "encoding/base64"
22
21
"encoding/json"
23
22
"fmt"
24
23
"os"
25
24
"path/filepath"
26
25
26
+ "github.com/distribution/reference"
27
27
"github.com/docker/cli/cli/config"
28
28
"github.com/docker/cli/cli/config/configfile"
29
- "github.com/docker/distribution/reference "
29
+ clitypes "github.com/docker/cli/cli/config/types "
30
30
types "github.com/docker/docker/api/types/registry"
31
31
"github.com/docker/docker/pkg/homedir"
32
32
"github.com/docker/docker/registry"
@@ -79,14 +79,67 @@ func (h credsHelper) GetAuthConfig(registry string) (types.AuthConfig, error) {
79
79
return types.AuthConfig {}, err
80
80
}
81
81
82
+ return h .loadCredentials (cf , registry )
83
+ }
84
+
85
+ func (h credsHelper ) loadCredentials (cf * configfile.ConfigFile , registry string ) (types.AuthConfig , error ) {
86
+ if helper := cf .CredentialHelpers [registry ]; helper == "gcloud" {
87
+ authCfg , err := h .getGoogleAuthConfig (registry )
88
+ if err == nil {
89
+ return authCfg , nil
90
+ }
91
+ log .Entry (context .TODO ()).Debugf ("error getting google authenticator, falling back to docker auth: %v" , err )
92
+ }
93
+
94
+ var anonymous clitypes.AuthConfig
82
95
auth , err := cf .GetAuthConfig (registry )
83
96
if err != nil {
84
97
return types.AuthConfig {}, err
85
98
}
86
99
100
+ // From go-containerrergistry logic, the ServerAddress is not considered when determining if returned auth is anonymous.
101
+ anonymous .ServerAddress = auth .ServerAddress
102
+ if auth != anonymous {
103
+ return types .AuthConfig (auth ), nil
104
+ }
105
+
106
+ if isGoogleRegistry (registry ) {
107
+ authCfg , err := h .getGoogleAuthConfig (registry )
108
+ if err == nil {
109
+ return authCfg , nil
110
+ }
111
+ }
112
+
87
113
return types .AuthConfig (auth ), nil
88
114
}
89
115
116
+ func (h credsHelper ) getGoogleAuthConfig (registry string ) (types.AuthConfig , error ) {
117
+ auth := getGoogleAuthenticator ()
118
+ if auth == nil {
119
+ return types.AuthConfig {}, fmt .Errorf ("error getting google authenticator" )
120
+ }
121
+ cfg , err := auth .Authorization ()
122
+ if err != nil {
123
+ return types.AuthConfig {}, err
124
+ }
125
+
126
+ bCfg , err := cfg .MarshalJSON ()
127
+ if err != nil {
128
+ return types.AuthConfig {}, err
129
+ }
130
+
131
+ var authCfg types.AuthConfig
132
+ err = json .Unmarshal (bCfg , & authCfg )
133
+ if err != nil {
134
+ return types.AuthConfig {}, err
135
+ }
136
+
137
+ // The docker library does the same when we request the credentials
138
+ authCfg .ServerAddress = registry
139
+
140
+ return authCfg , nil
141
+ }
142
+
90
143
// GetAllAuthConfigs retrieves all the auth configs.
91
144
// Because this can take a long time, we make sure it can be interrupted by the user.
92
145
func (h credsHelper ) GetAllAuthConfigs (ctx context.Context ) (map [string ]types.AuthConfig , error ) {
@@ -111,22 +164,31 @@ func (h credsHelper) GetAllAuthConfigs(ctx context.Context) (map[string]types.Au
111
164
}
112
165
113
166
func (h credsHelper ) doGetAllAuthConfigs () (map [string ]types.AuthConfig , error ) {
167
+ credentials := make (map [string ]types.AuthConfig )
114
168
cf , err := loadDockerConfig ()
115
169
if err != nil {
116
170
return nil , err
117
171
}
118
172
119
- credentials , err := cf .GetAllCredentials ()
173
+ defaultCreds , err := cf .GetCredentialsStore ( "" ). GetAll ()
120
174
if err != nil {
121
175
return nil , err
122
176
}
123
177
124
- authConfigs := make (map [string ]types.AuthConfig , len (credentials ))
125
- for k , auth := range credentials {
126
- authConfigs [k ] = types .AuthConfig (auth )
178
+ for registry , cred := range defaultCreds {
179
+ credentials [registry ] = types .AuthConfig (cred )
180
+ }
181
+
182
+ for registry := range cf .CredentialHelpers {
183
+ authCfg , err := h .loadCredentials (cf , registry )
184
+ if err != nil {
185
+ log .Entry (context .TODO ()).Debugf ("failed to get credentials for registry %v: %v" , registry , err )
186
+ continue
187
+ }
188
+ credentials [registry ] = authCfg
127
189
}
128
190
129
- return authConfigs , nil
191
+ return credentials , nil
130
192
}
131
193
132
194
func (l * localDaemon ) encodedRegistryAuth (ctx context.Context , a AuthConfigHelper , image string ) (string , error ) {
@@ -150,12 +212,7 @@ func (l *localDaemon) encodedRegistryAuth(ctx context.Context, a AuthConfigHelpe
150
212
return "" , fmt .Errorf ("getting auth config: %w" , err )
151
213
}
152
214
153
- buf , err := json .Marshal (ac )
154
- if err != nil {
155
- return "" , err
156
- }
157
-
158
- return base64 .URLEncoding .EncodeToString (buf ), nil
215
+ return types .EncodeAuthConfig (ac )
159
216
}
160
217
161
218
func (l * localDaemon ) officialRegistry (ctx context.Context ) string {
0 commit comments