1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-14 11:57:59 +00:00
external-secrets/apis/externalsecrets/v1beta1/secretstore_validator.go
Gergely Brautigam a5ddd97c21
chore: update go version of the project to 1.23 (#3829)
* chore: update go version of the project to 1.23

Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com>

* fixed an absurd amount of linter issues

Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com>

---------

Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com>
2024-08-26 11:10:58 +02:00

82 lines
2.3 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"
"regexp"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
var _ admission.CustomValidator = &GenericStoreValidator{}
const (
errInvalidStore = "invalid store"
)
type GenericStoreValidator struct{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *GenericStoreValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
st, ok := obj.(GenericStore)
if !ok {
return nil, errors.New(errInvalidStore)
}
return validateStore(st)
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *GenericStoreValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
st, ok := newObj.(GenericStore)
if !ok {
return nil, errors.New(errInvalidStore)
}
return validateStore(st)
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *GenericStoreValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return nil, nil
}
func validateStore(store GenericStore) (admission.Warnings, error) {
if err := validateConditions(store); err != nil {
return nil, err
}
provider, err := GetProvider(store)
if err != nil {
return nil, err
}
return provider.ValidateStore(store)
}
func validateConditions(store GenericStore) error {
var errs error
for ci, condition := range store.GetSpec().Conditions {
for ri, r := range condition.NamespaceRegexes {
if _, err := regexp.Compile(r); err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to compile %dth namespace regex in %dth condition: %w", ri, ci, err))
}
}
}
return errs
}