1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-15 17:51:01 +00:00
external-secrets/apis/externalsecrets/v1beta1/externalsecret_validator.go
Amirhossein Akhlaghpour 1bd07fd90e
Validator (#3003)
* feat: add validator for duplicates keys

Signed-off-by: Mehrbod Akhlaghpour <m9.akhlaghpoor@gmail.com>

* feat: retain mode on duplicate keys

Signed-off-by: Mehrbod Akhlaghpour <m9.akhlaghpoor@gmail.com>

* feat: add new test

Signed-off-by: Mehrbod Akhlaghpour <m9.akhlaghpoor@gmail.com>

* chore: rebase the test

Signed-off-by: Mehrbod Akhlaghpour <m9.akhlaghpoor@gmail.com>

* fix: test cases for duplicate keys

Signed-off-by: Mehrbod Akhlaghpour <m9.akhlaghpoor@gmail.com>

---------

Signed-off-by: Mehrbod Akhlaghpour <m9.akhlaghpoor@gmail.com>
2024-01-14 18:08:59 +01:00

82 lines
2.9 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1beta1
import (
"context"
"errors"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
type ExternalSecretValidator struct{}
func (esv *ExternalSecretValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
return validateExternalSecret(obj)
}
func (esv *ExternalSecretValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
return validateExternalSecret(newObj)
}
func (esv *ExternalSecretValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return nil, nil
}
func validateExternalSecret(obj runtime.Object) (admission.Warnings, error) {
es, ok := obj.(*ExternalSecret)
if !ok {
return nil, fmt.Errorf("unexpected type")
}
var errs error
if (es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyMerge) ||
(es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyNone) {
errs = errors.Join(errs, fmt.Errorf("deletionPolicy=Delete must not be used when the controller doesn't own the secret. Please set creationPolicy=Owner"))
}
if es.Spec.Target.DeletionPolicy == DeletionPolicyMerge && es.Spec.Target.CreationPolicy == CreatePolicyNone {
errs = errors.Join(errs, fmt.Errorf("deletionPolicy=Merge must not be used with creationPolicy=None. There is no Secret to merge with"))
}
if len(es.Spec.Data) == 0 && len(es.Spec.DataFrom) == 0 {
errs = errors.Join(errs, fmt.Errorf("either data or dataFrom should be specified"))
}
for _, ref := range es.Spec.DataFrom {
findOrExtract := ref.Find != nil || ref.Extract != nil
if findOrExtract && ref.SourceRef != nil && ref.SourceRef.GeneratorRef != nil {
errs = errors.Join(errs, fmt.Errorf("generator can not be used with find or extract"))
}
}
errs = validateDuplicateKeys(es, errs)
return nil, errs
}
func validateDuplicateKeys(es *ExternalSecret, errs error) error {
if es.Spec.Target.DeletionPolicy == DeletionPolicyRetain {
seenKeys := make(map[string]struct{})
for _, data := range es.Spec.Data {
secretKey := data.SecretKey
if _, exists := seenKeys[secretKey]; exists {
errs = errors.Join(errs, fmt.Errorf("duplicate secretKey found: %s", secretKey))
}
seenKeys[secretKey] = struct{}{}
}
}
return errs
}