@@ -18,23 +18,19 @@ package render
1818
1919import (
2020 "context"
21+ "encoding/base64"
2122 "fmt"
2223 "io"
2324 "net"
24- "os"
25- "path/filepath"
2625
27- "github.com/distribution/reference"
28- "github.com/docker/cli/cli/config"
29- "github.com/docker/cli/cli/config/configfile"
3026 "github.com/docker/docker/api/types/container"
3127 "github.com/docker/docker/api/types/filters"
3228 typesimage "github.com/docker/docker/api/types/image"
33- registrytypes "github.com/docker/docker/api/types/registry"
3429 "github.com/docker/docker/client"
3530 "github.com/docker/docker/errdefs"
36- "github.com/docker/docker/registry"
3731 "github.com/docker/go-connections/nat"
32+ "github.com/google/go-containerregistry/pkg/authn"
33+ "github.com/google/go-containerregistry/pkg/name"
3834
3935 "github.com/crossplane/crossplane-runtime/pkg/errors"
4036 "github.com/crossplane/crossplane-runtime/pkg/logging"
@@ -113,12 +109,12 @@ type RuntimeDocker struct {
113109 // Cleanup controls how the containers are handled after rendering.
114110 Cleanup DockerCleanup
115111
116- // ConfigFile contains information like credentials for each registry, default to ~/.docker/config.json
117- ConfigFile * configfile.ConfigFile
118-
119112 // PullPolicy controls how the runtime image is pulled.
120113 PullPolicy DockerPullPolicy
121114
115+ // Keychain to use for pulling images from private registry.
116+ Keychain authn.Keychain
117+
122118 // log is the logger for this runtime.
123119 log logging.Logger
124120}
@@ -163,31 +159,12 @@ func GetRuntimeDocker(fn pkgv1.Function, log logging.Logger) (*RuntimeDocker, er
163159 return nil , errors .Wrapf (err , "cannot get pull policy for Function %q" , fn .GetName ())
164160 }
165161
166- // Initial ConfigFile, first check environment variable XDG_RUNTIME_DIR for Podman if it exists
167- // Otherwise, use the default Docker config file
168- var configFile * configfile.ConfigFile
169- if _ , err := os .Stat (filepath .Join (os .Getenv ("XDG_RUNTIME_DIR" ), "containers/auth.json" )); err == nil {
170- // Use the auth.json file if specified XDG_RUNTIME_DIR and file exists
171- f , err := os .Open (filepath .Join (os .Getenv ("XDG_RUNTIME_DIR" ), "containers/auth.json" ))
172- if err != nil {
173- return nil , errors .Wrapf (err , "cannot open file %s" , filepath .Join (os .Getenv ("XDG_RUNTIME_DIR" ), "containers/auth.json" ))
174- }
175- defer f .Close () //nolint:errcheck // Only open for reading.
176-
177- configFile , err = config .LoadFromReader (f )
178- if err != nil {
179- return nil , errors .Wrapf (err , "cannot load config file from reader" )
180- }
181- } else {
182- configFile = config .LoadDefaultConfigFile (os .Stderr )
183- }
184-
185162 r := & RuntimeDocker {
186163 Image : fn .Spec .Package ,
187164 Name : "" ,
188165 Cleanup : cleanup ,
189- ConfigFile : configFile ,
190166 PullPolicy : pullPolicy ,
167+ Keychain : authn .DefaultKeychain ,
191168 log : log ,
192169 }
193170
@@ -318,33 +295,29 @@ func (r *RuntimeDocker) createContainer(ctx context.Context, cli *client.Client)
318295}
319296
320297func (r * RuntimeDocker ) getPullOptions () (typesimage.PullOptions , error ) {
321- // Resolve auth token by looking into config file
322- named , err := reference . ParseNormalizedNamed (r .Image )
298+ // Resolve auth token by looking into keychain
299+ ref , err := name . ParseReference (r .Image )
323300 if err != nil {
324301 return typesimage.PullOptions {}, errors .Wrapf (err , "Image is not a valid reference %s" , r .Image )
325302 }
326303
327- repoInfo , err := registry . ParseRepositoryInfo ( named )
304+ auth , err := r . Keychain . Resolve ( ref . Context (). Registry )
328305 if err != nil {
329- return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot parse repository info: %s" , named . String ())
306+ return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot resolve auth for %s" , ref . Context (). RegistryStr ())
330307 }
331308
332- configKey := repoInfo .Index .Name
333- if repoInfo .Index .Official {
334- configKey = registry .IndexServer
335- }
336- authConfig , err := r .ConfigFile .GetAuthConfig (configKey )
309+ authConfig , err := auth .Authorization ()
337310 if err != nil {
338- return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot get auth config info with configKey: %s" , configKey )
311+ return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot get auth config for %s" , ref . Context (). RegistryStr () )
339312 }
340313
341- encodedAuth , err := registrytypes . EncodeAuthConfig ( registrytypes . AuthConfig ( authConfig ) )
314+ token , err := authConfig . MarshalJSON ( )
342315 if err != nil {
343- return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot encode auth config with configKey: %s" , configKey )
316+ return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot marshal auth config for %s" , ref . Context (). RegistryStr () )
344317 }
345318
346319 return typesimage.PullOptions {
347- RegistryAuth : encodedAuth ,
320+ RegistryAuth : base64 . URLEncoding . EncodeToString ( token ) ,
348321 }, nil
349322}
350323
0 commit comments