|
| 1 | +package v1alpha1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "k8s.io/apimachinery/pkg/runtime" |
| 8 | + ctrl "sigs.k8s.io/controller-runtime" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 11 | +) |
| 12 | + |
| 13 | +// SetupWebhookWithManager sets up the webhook with the Manager |
| 14 | +func (r *MCPExternalAuthConfig) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 15 | + return ctrl.NewWebhookManagedBy(mgr). |
| 16 | + For(r). |
| 17 | + Complete() |
| 18 | +} |
| 19 | + |
| 20 | +//nolint:lll // kubebuilder webhook marker cannot be split |
| 21 | +// +kubebuilder:webhook:path=/validate-toolhive-stacklok-com-v1alpha1-mcpexternalauthconfig,mutating=false,failurePolicy=fail,sideEffects=None,groups=toolhive.stacklok.com,resources=mcpexternalauthconfigs,verbs=create;update,versions=v1alpha1,name=vmcpexternalauthconfig.kb.io,admissionReviewVersions=v1 |
| 22 | + |
| 23 | +var _ webhook.CustomValidator = &MCPExternalAuthConfig{} |
| 24 | + |
| 25 | +// ValidateCreate implements webhook.CustomValidator |
| 26 | +func (r *MCPExternalAuthConfig) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) { |
| 27 | + var warnings admission.Warnings |
| 28 | + if r.Spec.Type == ExternalAuthTypeUnauthenticated { |
| 29 | + warnings = append(warnings, |
| 30 | + "'unauthenticated' type disables authentication to the backend. "+ |
| 31 | + "Only use for backends on trusted networks or when authentication is handled by network-level security.") |
| 32 | + } |
| 33 | + return warnings, r.validate() |
| 34 | +} |
| 35 | + |
| 36 | +// ValidateUpdate implements webhook.CustomValidator |
| 37 | +func (r *MCPExternalAuthConfig) ValidateUpdate( |
| 38 | + _ context.Context, _ runtime.Object, _ runtime.Object, |
| 39 | +) (admission.Warnings, error) { |
| 40 | + var warnings admission.Warnings |
| 41 | + if r.Spec.Type == ExternalAuthTypeUnauthenticated { |
| 42 | + warnings = append(warnings, |
| 43 | + "'unauthenticated' type disables authentication to the backend. "+ |
| 44 | + "Only use for backends on trusted networks or when authentication is handled by network-level security.") |
| 45 | + } |
| 46 | + return warnings, r.validate() |
| 47 | +} |
| 48 | + |
| 49 | +// ValidateDelete implements webhook.CustomValidator |
| 50 | +func (*MCPExternalAuthConfig) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { |
| 51 | + // No validation needed for deletion |
| 52 | + return nil, nil |
| 53 | +} |
| 54 | + |
| 55 | +// validate performs validation on the MCPExternalAuthConfig spec |
| 56 | +func (r *MCPExternalAuthConfig) validate() error { |
| 57 | + switch r.Spec.Type { |
| 58 | + case ExternalAuthTypeTokenExchange: |
| 59 | + if r.Spec.TokenExchange == nil { |
| 60 | + return fmt.Errorf("tokenExchange configuration is required when type is 'tokenExchange'") |
| 61 | + } |
| 62 | + if r.Spec.HeaderInjection != nil { |
| 63 | + return fmt.Errorf("headerInjection must not be set when type is 'tokenExchange'") |
| 64 | + } |
| 65 | + |
| 66 | + case ExternalAuthTypeHeaderInjection: |
| 67 | + if r.Spec.HeaderInjection == nil { |
| 68 | + return fmt.Errorf("headerInjection configuration is required when type is 'headerInjection'") |
| 69 | + } |
| 70 | + if r.Spec.TokenExchange != nil { |
| 71 | + return fmt.Errorf("tokenExchange must not be set when type is 'headerInjection'") |
| 72 | + } |
| 73 | + |
| 74 | + case ExternalAuthTypeUnauthenticated: |
| 75 | + if r.Spec.TokenExchange != nil { |
| 76 | + return fmt.Errorf("tokenExchange must not be set when type is 'unauthenticated'") |
| 77 | + } |
| 78 | + if r.Spec.HeaderInjection != nil { |
| 79 | + return fmt.Errorf("headerInjection must not be set when type is 'unauthenticated'") |
| 80 | + } |
| 81 | + |
| 82 | + default: |
| 83 | + return fmt.Errorf("unsupported auth type: %s", r.Spec.Type) |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
0 commit comments