@@ -2,6 +2,8 @@ package commands
22
33import (
44 "bytes"
5+ "context"
6+ "encoding/json"
57 "fmt"
68 "io"
79 "io/ioutil"
@@ -23,8 +25,10 @@ import (
2325 "github.com/docker/cli/cli/context/docker"
2426 "github.com/docker/cli/cli/context/store"
2527 "github.com/docker/distribution/reference"
28+ "github.com/docker/docker/api/types"
2629 "github.com/docker/docker/api/types/container"
2730 "github.com/docker/docker/api/types/mount"
31+ "github.com/docker/docker/registry"
2832 "github.com/pkg/errors"
2933)
3034
@@ -35,41 +39,91 @@ type bindMount struct {
3539
3640const defaultSocketPath string = "/var/run/docker.sock"
3741
38- func prepareCredentialSet (contextName string , contextStore store.Store , b * bundle.Bundle , namedCredentialsets []string ) (map [string ]string , error ) {
42+ type credentialSetOpt func (b * bundle.Bundle , creds credentials.Set ) error
43+
44+ func addNamedCredentialSets (namedCredentialsets []string ) credentialSetOpt {
45+ return func (_ * bundle.Bundle , creds credentials.Set ) error {
46+ for _ , file := range namedCredentialsets {
47+ if _ , err := os .Stat (file ); err != nil {
48+ file = filepath .Join (duffleHome ().Credentials (), file + ".yaml" )
49+ }
50+ c , err := credentials .Load (file )
51+ if err != nil {
52+ return err
53+ }
54+ values , err := c .Resolve ()
55+ if err != nil {
56+ return err
57+ }
58+ if err := creds .Merge (values ); err != nil {
59+ return err
60+ }
61+ }
62+ return nil
63+ }
64+ }
65+
66+ func addDockerCredentials (contextName string , contextStore store.Store ) credentialSetOpt {
3967 // docker desktop contexts require some rewriting for being used within a container
4068 contextStore = dockerDesktopAwareStore {Store : contextStore }
41- creds := map [string ]string {}
42- for _ , file := range namedCredentialsets {
43- if _ , err := os .Stat (file ); err != nil {
44- file = filepath .Join (duffleHome ().Credentials (), file + ".yaml" )
45- }
46- c , err := credentials .Load (file )
47- if err != nil {
48- return nil , err
69+ return func (_ * bundle.Bundle , creds credentials.Set ) error {
70+ if contextName != "" {
71+ data , err := ioutil .ReadAll (store .Export (contextName , contextStore ))
72+ if err != nil {
73+ return err
74+ }
75+ creds [internal .CredentialDockerContextName ] = string (data )
4976 }
50- values , err := c .Resolve ()
51- if err != nil {
52- return nil , err
77+ return nil
78+ }
79+ }
80+
81+ func addRegistryCredentials (shouldPopulate bool , dockerCli command.Cli ) credentialSetOpt {
82+ return func (b * bundle.Bundle , creds credentials.Set ) error {
83+ if _ , ok := b .Credentials [internal .CredentialRegistryName ]; ! ok {
84+ return nil
5385 }
54- for k , v := range values {
55- if _ , ok := creds [k ]; ok {
56- return nil , fmt .Errorf ("ambiguous credential resolution: %q is present in multiple credential sets" , k )
86+
87+ registryCreds := map [string ]types.AuthConfig {}
88+ if shouldPopulate {
89+ for _ , img := range b .Images {
90+ named , err := reference .ParseNormalizedNamed (img .Image )
91+ if err != nil {
92+ return err
93+ }
94+ info , err := registry .ParseRepositoryInfo (named )
95+ if err != nil {
96+ return err
97+ }
98+ key := registry .GetAuthConfigKey (info .Index )
99+ if _ , ok := registryCreds [key ]; ! ok {
100+ registryCreds [key ] = command .ResolveAuthConfig (context .Background (), dockerCli , info .Index )
101+ }
57102 }
58- creds [k ] = v
59103 }
60- }
61- if contextName != "" {
62- data , err := ioutil .ReadAll (store .Export (contextName , contextStore ))
104+ registryCredsJSON , err := json .Marshal (registryCreds )
63105 if err != nil {
106+ return err
107+ }
108+ creds [internal .CredentialRegistryName ] = string (registryCredsJSON )
109+ return nil
110+ }
111+ }
112+
113+ func prepareCredentialSet (b * bundle.Bundle , opts ... credentialSetOpt ) (map [string ]string , error ) {
114+ creds := map [string ]string {}
115+ for _ , op := range opts {
116+ if err := op (b , creds ); err != nil {
64117 return nil , err
65118 }
66- creds ["docker.context" ] = string (data )
67119 }
68- _ , requiresDockerContext := b .Credentials ["docker.context" ]
69- _ , hasDockerContext := creds ["docker.context" ]
120+
121+ _ , requiresDockerContext := b .Credentials [internal .CredentialDockerContextName ]
122+ _ , hasDockerContext := creds [internal .CredentialDockerContextName ]
70123 if requiresDockerContext && ! hasDockerContext {
71124 return nil , errors .New ("no target context specified. Use --target-context= or DOCKER_TARGET_CONTEXT= to define it" )
72125 }
126+
73127 return creds , nil
74128}
75129
@@ -199,7 +253,7 @@ func resolveBundle(dockerCli command.Cli, name string, pullRef bool, insecureReg
199253
200254func requiredClaimBindMount (c claim.Claim , targetContextName string , dockerCli command.Cli ) (bindMount , error ) {
201255 var specifiedOrchestrator string
202- if rawOrchestrator , ok := c .Parameters ["docker.orchestrator" ]; ok {
256+ if rawOrchestrator , ok := c .Parameters [internal . ParameterOrchestratorName ]; ok {
203257 specifiedOrchestrator = rawOrchestrator .(string )
204258 }
205259
@@ -253,7 +307,7 @@ func isDockerHostLocal(host string) bool {
253307func prepareCustomAction (actionName string , dockerCli command.Cli , appname string , stdout io.Writer ,
254308 registryOpts registryOptions , pullOpts pullOptions , paramsOpts parametersOptions ) (* action.RunCustom , * claim.Claim , * bytes.Buffer , error ) {
255309
256- c , err := claim .New (actionName )
310+ c , err := claim .New ("custom-action" )
257311 if err != nil {
258312 return nil , nil , nil , err
259313 }
@@ -267,17 +321,15 @@ func prepareCustomAction(actionName string, dockerCli command.Cli, appname strin
267321 }
268322 c .Bundle = bundle
269323
270- parameters , err := mergeBundleParameters (c . Bundle ,
324+ if err := mergeBundleParameters (c ,
271325 withFileParameters (paramsOpts .parametersFiles ),
272326 withCommandLineParameters (paramsOpts .overrides ),
273- )
274- if err != nil {
327+ ); err != nil {
275328 return nil , nil , nil , err
276329 }
277- c .Parameters = parameters
278330
279331 a := & action.RunCustom {
280- Action : internal . Namespace + actionName ,
332+ Action : actionName ,
281333 Driver : driverImpl ,
282334 }
283335 return a , c , errBuf , nil
0 commit comments