-
Notifications
You must be signed in to change notification settings - Fork 153
Add unauthenticated and headerinjection auth strategy to MCPExternalAuthConfig #2915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yrobla
wants to merge
3
commits into
main
Choose a base branch
from
feat/vmcp_auth_strategies_incompatible
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
cmd/thv-operator/api/v1alpha1/mcpexternalauthconfig_webhook.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "k8s.io/apimachinery/pkg/runtime" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/webhook" | ||
| "sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
| ) | ||
|
|
||
| // SetupWebhookWithManager sets up the webhook with the Manager | ||
| func (r *MCPExternalAuthConfig) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
| return ctrl.NewWebhookManagedBy(mgr). | ||
| For(r). | ||
| Complete() | ||
| } | ||
|
|
||
| //nolint:lll // kubebuilder webhook marker cannot be split | ||
| // +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 | ||
|
|
||
| var _ webhook.CustomValidator = &MCPExternalAuthConfig{} | ||
|
|
||
| // ValidateCreate implements webhook.CustomValidator | ||
| func (r *MCPExternalAuthConfig) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) { | ||
| return nil, r.validate() | ||
| } | ||
|
|
||
| // ValidateUpdate implements webhook.CustomValidator | ||
| func (r *MCPExternalAuthConfig) ValidateUpdate( | ||
| _ context.Context, _ runtime.Object, _ runtime.Object, | ||
| ) (admission.Warnings, error) { | ||
| return nil, r.validate() | ||
| } | ||
|
|
||
| // ValidateDelete implements webhook.CustomValidator | ||
| func (*MCPExternalAuthConfig) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { | ||
| // No validation needed for deletion | ||
| return nil, nil | ||
| } | ||
|
|
||
| // validate performs validation on the MCPExternalAuthConfig spec | ||
| func (r *MCPExternalAuthConfig) validate() error { | ||
| switch r.Spec.Type { | ||
| case ExternalAuthTypeTokenExchange: | ||
| if r.Spec.TokenExchange == nil { | ||
| return fmt.Errorf("tokenExchange configuration is required when type is 'tokenExchange'") | ||
| } | ||
| if r.Spec.HeaderInjection != nil { | ||
| return fmt.Errorf("headerInjection must not be set when type is 'tokenExchange'") | ||
| } | ||
|
|
||
| case ExternalAuthTypeHeaderInjection: | ||
| if r.Spec.HeaderInjection == nil { | ||
| return fmt.Errorf("headerInjection configuration is required when type is 'headerInjection'") | ||
| } | ||
| if r.Spec.TokenExchange != nil { | ||
| return fmt.Errorf("tokenExchange must not be set when type is 'headerInjection'") | ||
| } | ||
|
|
||
| case ExternalAuthTypeUnauthenticated: | ||
| if r.Spec.TokenExchange != nil { | ||
| return fmt.Errorf("tokenExchange must not be set when type is 'unauthenticated'") | ||
| } | ||
| if r.Spec.HeaderInjection != nil { | ||
| return fmt.Errorf("headerInjection must not be set when type is 'unauthenticated'") | ||
| } | ||
|
|
||
| default: | ||
| return fmt.Errorf("unsupported auth type: %s", r.Spec.Type) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
262 changes: 262 additions & 0 deletions
262
cmd/thv-operator/api/v1alpha1/mcpexternalauthconfig_webhook_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| func TestMCPExternalAuthConfig_ValidateCreate(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| config *MCPExternalAuthConfig | ||
| expectError bool | ||
| errorMsg string | ||
| }{ | ||
| { | ||
| name: "valid unauthenticated", | ||
| config: &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-unauthenticated", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeUnauthenticated, | ||
| }, | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "unauthenticated with tokenExchange should fail", | ||
| config: &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-invalid", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeUnauthenticated, | ||
| TokenExchange: &TokenExchangeConfig{ | ||
| TokenURL: "https://oauth.example.com/token", | ||
| }, | ||
| }, | ||
| }, | ||
| expectError: true, | ||
| errorMsg: "tokenExchange must not be set when type is 'unauthenticated'", | ||
| }, | ||
| { | ||
| name: "unauthenticated with headerInjection should fail", | ||
| config: &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-invalid", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeUnauthenticated, | ||
| HeaderInjection: &HeaderInjectionConfig{ | ||
| HeaderName: "Authorization", | ||
| ValueSecretRef: &SecretKeyRef{ | ||
| Name: "secret", | ||
| Key: "key", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectError: true, | ||
| errorMsg: "headerInjection must not be set when type is 'unauthenticated'", | ||
| }, | ||
| { | ||
| name: "valid tokenExchange", | ||
| config: &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-tokenexchange", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeTokenExchange, | ||
| TokenExchange: &TokenExchangeConfig{ | ||
| TokenURL: "https://oauth.example.com/token", | ||
| Audience: "backend-service", | ||
| }, | ||
| }, | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "tokenExchange without config should fail", | ||
| config: &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-invalid", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeTokenExchange, | ||
| }, | ||
| }, | ||
| expectError: true, | ||
| errorMsg: "tokenExchange configuration is required when type is 'tokenExchange'", | ||
| }, | ||
| { | ||
| name: "tokenExchange with headerInjection should fail", | ||
| config: &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-invalid", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeTokenExchange, | ||
| TokenExchange: &TokenExchangeConfig{ | ||
| TokenURL: "https://oauth.example.com/token", | ||
| Audience: "backend-service", | ||
| }, | ||
| HeaderInjection: &HeaderInjectionConfig{ | ||
| HeaderName: "Authorization", | ||
| ValueSecretRef: &SecretKeyRef{ | ||
| Name: "secret", | ||
| Key: "key", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectError: true, | ||
| errorMsg: "headerInjection must not be set when type is 'tokenExchange'", | ||
| }, | ||
| { | ||
| name: "valid headerInjection", | ||
| config: &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-headerinjection", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeHeaderInjection, | ||
| HeaderInjection: &HeaderInjectionConfig{ | ||
| HeaderName: "X-API-Key", | ||
| ValueSecretRef: &SecretKeyRef{ | ||
| Name: "api-key-secret", | ||
| Key: "api-key", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "headerInjection without config should fail", | ||
| config: &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-invalid", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeHeaderInjection, | ||
| }, | ||
| }, | ||
| expectError: true, | ||
| errorMsg: "headerInjection configuration is required when type is 'headerInjection'", | ||
| }, | ||
| { | ||
| name: "headerInjection with tokenExchange should fail", | ||
| config: &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-invalid", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeHeaderInjection, | ||
| HeaderInjection: &HeaderInjectionConfig{ | ||
| HeaderName: "X-API-Key", | ||
| ValueSecretRef: &SecretKeyRef{ | ||
| Name: "secret", | ||
| Key: "key", | ||
| }, | ||
| }, | ||
| TokenExchange: &TokenExchangeConfig{ | ||
| TokenURL: "https://oauth.example.com/token", | ||
| }, | ||
| }, | ||
| }, | ||
| expectError: true, | ||
| errorMsg: "tokenExchange must not be set when type is 'headerInjection'", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| warnings, err := tt.config.ValidateCreate(context.Background(), tt.config) | ||
|
|
||
| if tt.expectError { | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), tt.errorMsg) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| // Warnings should always be nil for now | ||
| assert.Nil(t, warnings) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestMCPExternalAuthConfig_ValidateUpdate(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| config := &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-unauthenticated", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeUnauthenticated, | ||
| }, | ||
| } | ||
|
|
||
| // ValidateUpdate should use the same logic as ValidateCreate | ||
| warnings, err := config.ValidateUpdate(context.Background(), nil, config) | ||
| require.NoError(t, err) | ||
| assert.Nil(t, warnings) | ||
|
|
||
| // Test invalid update | ||
| invalidConfig := &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-invalid", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeUnauthenticated, | ||
| TokenExchange: &TokenExchangeConfig{ | ||
| TokenURL: "https://oauth.example.com/token", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| warnings, err = invalidConfig.ValidateUpdate(context.Background(), nil, invalidConfig) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "tokenExchange must not be set when type is 'unauthenticated'") | ||
| assert.Nil(t, warnings) | ||
| } | ||
|
|
||
| func TestMCPExternalAuthConfig_ValidateDelete(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| config := &MCPExternalAuthConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-unauthenticated", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: MCPExternalAuthConfigSpec{ | ||
| Type: ExternalAuthTypeUnauthenticated, | ||
| }, | ||
| } | ||
|
|
||
| // ValidateDelete should always succeed | ||
| warnings, err := config.ValidateDelete(context.Background(), config) | ||
| require.NoError(t, err) | ||
| assert.Nil(t, warnings) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.