|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package v1beta1 |
| 5 | + |
| 6 | +import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 7 | + |
| 8 | +// WebhookFailurePolicy defines how webhook errors are handled. |
| 9 | +type WebhookFailurePolicy string |
| 10 | + |
| 11 | +const ( |
| 12 | + // WebhookFailurePolicyFail denies the request on webhook error. |
| 13 | + WebhookFailurePolicyFail WebhookFailurePolicy = "fail" |
| 14 | + // WebhookFailurePolicyIgnore allows the request on webhook error. |
| 15 | + WebhookFailurePolicyIgnore WebhookFailurePolicy = "ignore" |
| 16 | +) |
| 17 | + |
| 18 | +// WebhookTLSConfig contains TLS configuration for secure webhook connections |
| 19 | +type WebhookTLSConfig struct { |
| 20 | + // CASecretRef references a Secret containing the CA certificate bundle used to verify the webhook server's certificate. |
| 21 | + // Contains a bundle of PEM-encoded X.509 certificates. |
| 22 | + // +optional |
| 23 | + CASecretRef *SecretKeyRef `json:"caSecretRef,omitempty"` |
| 24 | + |
| 25 | + // ClientCertSecretRef references a Secret containing the client certificate for mTLS authentication. |
| 26 | + // The referenced key must contain a PEM-encoded client certificate. |
| 27 | + // Use ClientKeySecretRef to provide the corresponding private key. |
| 28 | + // +optional |
| 29 | + ClientCertSecretRef *SecretKeyRef `json:"clientCertSecretRef,omitempty"` |
| 30 | + |
| 31 | + // ClientKeySecretRef references a Secret containing the private key for the client certificate. |
| 32 | + // Required when ClientCertSecretRef is set to enable mTLS. |
| 33 | + // +optional |
| 34 | + ClientKeySecretRef *SecretKeyRef `json:"clientKeySecretRef,omitempty"` |
| 35 | + |
| 36 | + // InsecureSkipVerify disables server certificate verification. |
| 37 | + // WARNING: This should only be used for development/testing and not in production environments. |
| 38 | + // +optional |
| 39 | + InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +// WebhookSpec defines the configuration for a single webhook middleware |
| 43 | +type WebhookSpec struct { |
| 44 | + // Name is a unique identifier for this webhook |
| 45 | + // +kubebuilder:validation:MinLength=1 |
| 46 | + // +kubebuilder:validation:MaxLength=63 |
| 47 | + Name string `json:"name"` |
| 48 | + |
| 49 | + // URL is the endpoint to call for this webhook. Must be an HTTP/HTTPS URL. |
| 50 | + // +kubebuilder:validation:Format=uri |
| 51 | + URL string `json:"url"` |
| 52 | + |
| 53 | + // Timeout configures the maximum time to wait for the webhook to respond. |
| 54 | + // Defaults to 10s if not specified. Maximum is 30s. |
| 55 | + // +kubebuilder:validation:Type=string |
| 56 | + // +kubebuilder:validation:Format=duration |
| 57 | + // +optional |
| 58 | + Timeout *metav1.Duration `json:"timeout,omitempty"` |
| 59 | + |
| 60 | + // FailurePolicy defines how to handle errors when communicating with the webhook. |
| 61 | + // Supported values: "fail", "ignore". Defaults to "fail". |
| 62 | + // +kubebuilder:validation:Enum=fail;ignore |
| 63 | + // +kubebuilder:default=fail |
| 64 | + // +optional |
| 65 | + FailurePolicy WebhookFailurePolicy `json:"failurePolicy,omitempty"` |
| 66 | + |
| 67 | + // TLSConfig contains optional TLS configuration for the webhook connection. |
| 68 | + // +optional |
| 69 | + TLSConfig *WebhookTLSConfig `json:"tlsConfig,omitempty"` |
| 70 | + |
| 71 | + // HMACSecretRef references a Kubernetes Secret containing the HMAC signing key |
| 72 | + // used to sign the webhook payload. If set, the X-Toolhive-Signature header will be injected. |
| 73 | + // +optional |
| 74 | + HMACSecretRef *SecretKeyRef `json:"hmacSecretRef,omitempty"` |
| 75 | +} |
| 76 | + |
| 77 | +// MCPWebhookConfigSpec defines the desired state of MCPWebhookConfig |
| 78 | +// +kubebuilder:validation:XValidation:rule="(has(self.validating) ? size(self.validating) : 0) + (has(self.mutating) ? size(self.mutating) : 0) > 0",message="at least one validating or mutating webhook must be defined" |
| 79 | +// |
| 80 | +//nolint:lll // CEL validation rules exceed line length limit |
| 81 | +type MCPWebhookConfigSpec struct { |
| 82 | + // Validating webhooks are called to approve or deny MCP requests. |
| 83 | + // +optional |
| 84 | + Validating []WebhookSpec `json:"validating,omitempty"` |
| 85 | + |
| 86 | + // Mutating webhooks are called to transform MCP requests before processing. |
| 87 | + // +optional |
| 88 | + Mutating []WebhookSpec `json:"mutating,omitempty"` |
| 89 | +} |
| 90 | + |
| 91 | +// MCPWebhookConfigStatus defines the observed state of MCPWebhookConfig |
| 92 | +type MCPWebhookConfigStatus struct { |
| 93 | + // Conditions represent the latest available observations |
| 94 | + // +listType=map |
| 95 | + // +listMapKey=type |
| 96 | + // +optional |
| 97 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 98 | + |
| 99 | + // ObservedGeneration is the last observed generation corresponding to the current status |
| 100 | + // +optional |
| 101 | + ObservedGeneration int64 `json:"observedGeneration,omitempty"` |
| 102 | + |
| 103 | + // ConfigHash is a hash of the spec, used for detecting changes |
| 104 | + // +optional |
| 105 | + ConfigHash string `json:"configHash,omitempty"` |
| 106 | + |
| 107 | + // ReferencingWorkloads is a list of workload resources that reference this MCPWebhookConfig. |
| 108 | + // Each entry identifies the workload by kind and name. |
| 109 | + // +listType=map |
| 110 | + // +listMapKey=name |
| 111 | + // +optional |
| 112 | + ReferencingWorkloads []WorkloadReference `json:"referencingWorkloads,omitempty"` |
| 113 | +} |
0 commit comments