@@ -14,7 +14,6 @@ import (
1414 v1 "github.com/google/go-containerregistry/pkg/v1"
1515 "github.com/google/go-containerregistry/pkg/v1/layout"
1616 "github.com/google/go-containerregistry/pkg/v1/remote"
17- "github.com/pkg/errors"
1817 progress "github.com/schollz/progressbar/v3"
1918
2019 fn "knative.dev/func/pkg/functions"
@@ -23,6 +22,15 @@ import (
2322type Credentials struct {
2423 Username string
2524 Password string
25+ Token string
26+ }
27+
28+ func (c Credentials ) Authorization () (* authn.AuthConfig , error ) {
29+ return & authn.AuthConfig {
30+ Username : c .Username ,
31+ Password : c .Password ,
32+ IdentityToken : c .Token ,
33+ }, nil
2634}
2735
2836type CredentialsProvider func (ctx context.Context , image string ) (Credentials , error )
@@ -35,8 +43,6 @@ type Pusher struct {
3543 credentialsProvider CredentialsProvider
3644
3745 Insecure bool
38- Token string
39- Username string
4046 Verbose bool
4147
4248 updates chan v1.Update
@@ -169,45 +175,8 @@ func (p *Pusher) writeIndex(ctx context.Context, ref name.Reference, ii v1.Image
169175 }
170176
171177 if ! p .Anonymous {
172- a , err := p .authOption (ctx , creds )
173- if err != nil {
174- return err
175- }
176- oo = append (oo , a )
178+ oo = append (oo , remote .WithAuth (creds ))
177179 }
178180
179181 return remote .WriteIndex (ref , ii , oo ... )
180182}
181-
182- // authOption selects an appropriate authentication option.
183- // If user provided = basic auth (secret is password)
184- // If only secret provided = bearer token auth
185- // If neither are provided = creds from credentials provider
186- // which performs the following in order:
187- // - Default Keychain (docker and podman config files)
188- // - Google Keychain
189- // - TODO: ECR Amazon
190- // - TODO: ACR Azure
191- // - interactive prompt for username and password
192- func (p * Pusher ) authOption (ctx context.Context , creds Credentials ) (remote.Option , error ) {
193-
194- // Basic Auth if provided
195- username , _ := ctx .Value (fn.PushUsernameKey {}).(string )
196- password , _ := ctx .Value (fn.PushPasswordKey {}).(string )
197- token , _ := ctx .Value (fn.PushTokenKey {}).(string )
198- if username != "" && token != "" {
199- return nil , errors .New ("only one of username/password or token authentication allowed. Received both a token and username" )
200- } else if token != "" {
201- return remote .WithAuth (& authn.Bearer {Token : token }), nil
202- } else if username != "" {
203- return remote .WithAuth (& authn.Basic {Username : username , Password : password }), nil
204- }
205-
206- // Use provided credentials if available or prompt for them
207- if creds .Username != "" && creds .Password != "" {
208- return remote .WithAuth (& authn.Basic {Username : creds .Username , Password : creds .Password }), nil
209- }
210-
211- // Return anonymous auth when no credentials are provided (e.g., for localhost registries)
212- return remote .WithAuth (authn .Anonymous ), nil
213- }
0 commit comments