Skip to content

Commit

Permalink
fix: Minor fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Bence Csati <[email protected]>
  • Loading branch information
csatib02 committed Mar 27, 2024
1 parent 5901fb2 commit 916a2b8
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/common/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Cisco
// Copyright © 2024 Cisco
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Cisco
// Copyright © 2024 Cisco
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/bao/client_logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Bank-Vaults Maintainers
// Copyright © 2024 Cisco
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/bao/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2021 Banzai Cloud
// Copyright © 2024 Cisco
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import (
"github.com/bank-vaults/secrets-webhook/pkg/provider/vault"
)

func (mw *MutatingWebhook) MutateConfigMap(configMap *corev1.ConfigMap, configs []interface{}) error {
func (mw *MutatingWebhook) MutateConfigMap(configMap *corev1.ConfigMap) error {
// do an early exit if no mutation is needed
if !configMapNeedsMutation(configMap) {
return nil
}

for _, config := range configs {
for _, config := range mw.providerConfigs {
switch providerConfig := config.(type) {
case vault.Config:
currentlyUsedProvider = vault.ProviderName
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func sliceIterator(s []interface{}) iterator {
return c
}

func (mw *MutatingWebhook) MutateObject(object *unstructured.Unstructured, configs []interface{}) error {
func (mw *MutatingWebhook) MutateObject(object *unstructured.Unstructured) error {
mw.logger.Debug(fmt.Sprintf("mutating object: %s.%s", object.GetNamespace(), object.GetName()))

for _, config := range configs {
for _, config := range mw.providerConfigs {
switch providerConfig := config.(type) {
case vault.Config:
currentlyUsedProvider = vault.ProviderName
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ import (

const SecretInitVolumeName = "secret-init"

func (mw *MutatingWebhook) MutatePod(ctx context.Context, pod *corev1.Pod, webhookConfig common.Config, secretInitConfig common.SecretInitConfig, dryRun bool, configs []interface{}) error {
func (mw *MutatingWebhook) MutatePod(ctx context.Context, pod *corev1.Pod, webhookConfig common.Config, secretInitConfig common.SecretInitConfig, dryRun bool) error {
mw.logger.Debug("Successfully connected to the API")

for _, config := range configs {
for _, config := range mw.providerConfigs {
switch providerConfig := config.(type) {
case vault.Config:
currentlyUsedProvider = vault.ProviderName
Expand Down
3 changes: 2 additions & 1 deletion pkg/webhook/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1800,8 +1800,9 @@ func Test_mutatingWebhook_mutatePod(t *testing.T) {
t.Errorf("parseProviderConfigs() error = %v, wantErr %v", err, ttp.wantErr)
return
}
mw.providerConfigs = providerConfigs

err = mw.MutatePod(context.Background(), ttp.args.pod, ttp.args.webhookConfig, ttp.args.secretInitConfig, false, providerConfigs)
err = mw.MutatePod(context.Background(), ttp.args.pod, ttp.args.webhookConfig, ttp.args.secretInitConfig, false)
if (err != nil) != ttp.wantErr {
t.Errorf("MutatingWebhook.MutatePod() error = %v, wantErr %v", err, ttp.wantErr)
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type dockerAuthConfig struct {
RegistryToken string `json:"registrytoken,omitempty"`
}

func (mw *MutatingWebhook) MutateSecret(secret *corev1.Secret, configs []interface{}) error {
for _, config := range configs {
func (mw *MutatingWebhook) MutateSecret(secret *corev1.Secret) error {
for _, config := range mw.providerConfigs {
switch providerConfig := config.(type) {
case vault.Config:
currentlyUsedProvider = vault.ProviderName
Expand Down
29 changes: 19 additions & 10 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ import (
var currentlyUsedProvider string

type MutatingWebhook struct {
k8sClient kubernetes.Interface
namespace string
registry ImageRegistry
logger *slog.Logger
k8sClient kubernetes.Interface
namespace string
registry ImageRegistry
logger *slog.Logger
providerConfigs []interface{}
}

func (mw *MutatingWebhook) SecretsMutator(ctx context.Context, ar *model.AdmissionReview, obj metav1.Object) (*mutating.MutatorResult, error) {
Expand All @@ -69,19 +70,20 @@ func (mw *MutatingWebhook) SecretsMutator(ctx context.Context, ar *model.Admissi
if err != nil {
return nil, fmt.Errorf("failed to parse provider configs: %w", err)
}
mw.providerConfigs = configs

switch v := obj.(type) {
case *corev1.Pod:
return &mutating.MutatorResult{MutatedObject: v}, mw.MutatePod(ctx, v, webhookConfig, secretInitConfig, ar.DryRun, configs)
return &mutating.MutatorResult{MutatedObject: v}, mw.MutatePod(ctx, v, webhookConfig, secretInitConfig, ar.DryRun)

case *corev1.Secret:
return &mutating.MutatorResult{MutatedObject: v}, mw.MutateSecret(v, configs)
return &mutating.MutatorResult{MutatedObject: v}, mw.MutateSecret(v)

case *corev1.ConfigMap:
return &mutating.MutatorResult{MutatedObject: v}, mw.MutateConfigMap(v, configs)
return &mutating.MutatorResult{MutatedObject: v}, mw.MutateConfigMap(v)

case *unstructured.Unstructured:
return &mutating.MutatorResult{MutatedObject: v}, mw.MutateObject(v, configs)
return &mutating.MutatorResult{MutatedObject: v}, mw.MutateObject(v)

default:
return &mutating.MutatorResult{}, nil
Expand Down Expand Up @@ -248,11 +250,18 @@ func parseProviderConfigs(obj metav1.Object, ar *model.AdmissionReview, provider
for _, providerName := range providers {
switch providerName {
case vaultprov.ProviderName:
vaultConfig, err := vaultprov.ParseConfig(obj, ar)
config, err := vaultprov.ParseConfig(obj, ar)
if err != nil {
return nil, errors.Wrap(err, "failed to parse vault config")
}
configs = append(configs, vaultConfig)
configs = append(configs, config)

case baoprov.ProviderName:
config, err := baoprov.ParseConfig(obj, ar)
if err != nil {
return nil, errors.Wrap(err, "failed to parse bao config")
}
configs = append(configs, config)

default:
return nil, errors.Errorf("unknown provider: %s", providerName)
Expand Down

0 comments on commit 916a2b8

Please sign in to comment.