Skip to content

feat: support multiple auths in a single object when checking upstream #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions internal/webhook/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (t *ruleTransformer) CheckUpstream(ctx context.Context, imageRef string) (b

options := make([]crane.Option, 0)
if t.rule.AuthSecretName != "" {
auth, err := t.auth(ctx)
auth, err := t.auth(ctx, imageRef)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func (t *ruleTransformer) CheckUpstream(ctx context.Context, imageRef string) (b
}
}

func (t *ruleTransformer) auth(ctx context.Context) (authn.Authenticator, error) {
func (t *ruleTransformer) auth(ctx context.Context, imageRef string) (authn.Authenticator, error) {
var secret corev1.Secret
logger.Info("token key: ", "key", client.ObjectKey{Namespace: t.rule.Namespace, Name: t.rule.AuthSecretName})
if err := t.client.Get(ctx, client.ObjectKey{Namespace: t.rule.Namespace, Name: t.rule.AuthSecretName}, &secret); err != nil {
Expand All @@ -205,19 +205,18 @@ func (t *ruleTransformer) auth(ctx context.Context) (authn.Authenticator, error)
if err := json.Unmarshal(dockerConfigJSONBytes, &dockerConfigJSON); err != nil {
return nil, err
}
// TODO: full keyring support?
if len(dockerConfigJSON.Auths) != 1 {
return nil, fmt.Errorf("only .dockerconfigjson with one auth method is supported, found %d", len(dockerConfigJSON.Auths))
}
for _, method := range dockerConfigJSON.Auths {
if method.Auth != "" {
user, pass, err := decodeDockerConfigFieldAuth(method.Auth)
if err != nil {
return nil, fmt.Errorf("failed to parse auth docker config auth field in secret %q", t.rule.AuthSecretName)
for key, method := range dockerConfigJSON.Auths {
keyRegex := regexp.MustCompile(key)
if keyRegex.Find([]byte(imageRef)) != nil {
if method.Auth != "" {
user, pass, err := decodeDockerConfigFieldAuth(method.Auth)
if err != nil {
return nil, fmt.Errorf("failed to parse auth docker config auth field in secret %q", t.rule.AuthSecretName)
}
return &authn.Basic{Username: user, Password: pass}, nil
}
return &authn.Basic{Username: user, Password: pass}, nil
return &authn.Basic{Username: method.Username, Password: method.Password}, nil
}
return &authn.Basic{Username: method.Username, Password: method.Password}, nil
}
}

Expand Down