|
| 1 | +package webhooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + rhtasv1alpha1 "github.com/securesign/operator/api/v1alpha1" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + "k8s.io/apimachinery/pkg/types" |
| 11 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 12 | + admission "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 13 | +) |
| 14 | + |
| 15 | +func (v *SecureSignValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 16 | + reqLog := logf.FromContext(ctx) |
| 17 | + operandCR, ok := obj.(*rhtasv1alpha1.Securesign) |
| 18 | + if !ok { |
| 19 | + return nil, fmt.Errorf("expected SecureSign CR but got %T", obj) |
| 20 | + } |
| 21 | + |
| 22 | + targetNamespace := operandCR.GetNamespace() |
| 23 | + |
| 24 | + if targetNamespace == "default" { |
| 25 | + reqLog.Info("Validation failed: Deployment blocked in 'default' namespace.") |
| 26 | + return nil, fmt.Errorf("installation into the 'default' namespace is prohibited by RHTAS policy") |
| 27 | + } |
| 28 | + |
| 29 | + ns := &corev1.Namespace{} |
| 30 | + |
| 31 | + if err := v.Client.Get(ctx, types.NamespacedName{Name: targetNamespace}, ns); err != nil { |
| 32 | + reqLog.Error(err, "Failed to retrieve target namespace object for validation.") |
| 33 | + return nil, fmt.Errorf("failed to retrieve target namespace %s: %w", targetNamespace, err) |
| 34 | + } |
| 35 | + |
| 36 | + runLevel, found := ns.Labels["openshift.io/run-level"] |
| 37 | + if found && reservedRunLevels[runLevel] { |
| 38 | + reqLog.Info("Validation failed: Deployment blocked in reserved namespace.", |
| 39 | + "namespace", targetNamespace, "run-level", runLevel) |
| 40 | + return nil, fmt.Errorf("installation into reserved OpenShift namespace '%s' (run-level %s) is prohibited by RHTAS policy", targetNamespace, runLevel) |
| 41 | + } |
| 42 | + |
| 43 | + return nil, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (v *SecureSignValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { |
| 47 | + return nil, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (v *SecureSignValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 51 | + return nil, nil |
| 52 | +} |
0 commit comments