Skip to content

Commit

Permalink
Don't use Trivy auth helpers when using Azure
Browse files Browse the repository at this point in the history
and user has provider username and pass in pull secret
  • Loading branch information
Anton Sankov authored and Anton Sankov committed Apr 26, 2024
1 parent 4a32152 commit 1b63241
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion image/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ func NewFromRemote(ctx context.Context, imageName string, option types.ImageOpti
return img, nil
}

func getToken(ctx context.Context, domain string, opt types.RegistryOptions) (auth authn.Basic) {
// if the registry is Azure AND the user configured a pull secret with username and password
// just return them as basic auth and don't user trivy's GetToken function
// which does not honor the provided username and password and tries to authenticate with the registry
// via workload identity
if strings.HasSuffix(domain, "azurecr.io") {
if len(opt.Credentials) > 0 {
return authn.Basic{
Username: opt.Credentials[0].Username,
Password: opt.Credentials[0].Password,
}
}
}

return registry.GetToken(ctx, domain, opt)
}

func tryRemote(ctx context.Context, imageName string, ref name.Reference, option types.ImageOptions) (ImageWithIndex, error) {
var remoteOpts []remote.Option
if option.RegistryOptions.Insecure {
Expand All @@ -53,7 +70,7 @@ func tryRemote(ctx context.Context, imageName string, ref name.Reference, option
remoteOpts = append(remoteOpts, remote.WithContext(ctx))

domain := ref.Context().RegistryStr()
auth := registry.GetToken(ctx, domain, option.RegistryOptions)
auth := getToken(ctx, domain, option.RegistryOptions)
if auth.Username != "" && auth.Password != "" {
remoteOpts = append(remoteOpts, remote.WithAuth(&auth))
} else if option.RegistryOptions.RegistryToken != "" {
Expand Down

0 comments on commit 1b63241

Please sign in to comment.