Skip to content

Commit 9ffb818

Browse files
authored
feat(auht): accept standard oauth authorization header by keeping the current header
1 parent 524e4f5 commit 9ffb818

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

pkg/kubernetes/impersonate_roundtripper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type impersonateRoundTripper struct {
88

99
func (irt *impersonateRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
1010
// TODO: Solution won't work with discoveryclient which uses context.TODO() instead of the passed-in context
11-
if v, ok := req.Context().Value(AuthorizationHeader).(string); ok {
11+
if v, ok := req.Context().Value(OAuthAuthorizationHeader).(string); ok {
1212
req.Header.Set("Authorization", v)
1313
}
1414
return irt.delegate.RoundTrip(req)

pkg/kubernetes/kubernetes.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package kubernetes
22

33
import (
44
"context"
5-
"k8s.io/apimachinery/pkg/runtime"
65
"strings"
76

7+
"k8s.io/apimachinery/pkg/runtime"
8+
89
"github.com/fsnotify/fsnotify"
910

1011
"k8s.io/apimachinery/pkg/api/meta"
@@ -25,7 +26,8 @@ import (
2526
)
2627

2728
const (
28-
AuthorizationHeader = "kubernetes-authorization"
29+
CustomAuthorizationHeader = "kubernetes-authorization"
30+
OAuthAuthorizationHeader = "Authorization"
2931
)
3032

3133
type CloseWatchKubeConfig func() error
@@ -133,11 +135,11 @@ func (m *Manager) ToRESTMapper() (meta.RESTMapper, error) {
133135
}
134136

135137
func (m *Manager) Derived(ctx context.Context) *Kubernetes {
136-
authorization, ok := ctx.Value(AuthorizationHeader).(string)
138+
authorization, ok := ctx.Value(OAuthAuthorizationHeader).(string)
137139
if !ok || !strings.HasPrefix(authorization, "Bearer ") {
138140
return &Kubernetes{manager: m}
139141
}
140-
klog.V(5).Infof("%s header found (Bearer), using provided bearer token", AuthorizationHeader)
142+
klog.V(5).Infof("%s header found (Bearer), using provided bearer token", OAuthAuthorizationHeader)
141143
derivedCfg := rest.CopyConfig(m.cfg)
142144
derivedCfg.BearerToken = strings.TrimPrefix(authorization, "Bearer ")
143145
derivedCfg.BearerTokenFile = ""

pkg/mcp/mcp.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"k8s.io/utils/ptr"
1111

1212
"github.com/manusa/kubernetes-mcp-server/pkg/config"
13-
"github.com/manusa/kubernetes-mcp-server/pkg/kubernetes"
13+
internalk8s "github.com/manusa/kubernetes-mcp-server/pkg/kubernetes"
1414
"github.com/manusa/kubernetes-mcp-server/pkg/output"
1515
"github.com/manusa/kubernetes-mcp-server/pkg/version"
1616
)
@@ -41,7 +41,7 @@ func (c *Configuration) isToolApplicable(tool server.ServerTool) bool {
4141
type Server struct {
4242
configuration *Configuration
4343
server *server.MCPServer
44-
k *kubernetes.Manager
44+
k *internalk8s.Manager
4545
}
4646

4747
func NewServer(configuration Configuration) (*Server, error) {
@@ -65,7 +65,7 @@ func NewServer(configuration Configuration) (*Server, error) {
6565
}
6666

6767
func (s *Server) reloadKubernetesClient() error {
68-
k, err := kubernetes.NewManager(s.configuration.StaticConfig.KubeConfig, s.configuration.StaticConfig)
68+
k, err := internalk8s.NewManager(s.configuration.StaticConfig.KubeConfig, s.configuration.StaticConfig)
6969
if err != nil {
7070
return err
7171
}
@@ -132,5 +132,17 @@ func NewTextResult(content string, err error) *mcp.CallToolResult {
132132
}
133133

134134
func contextFunc(ctx context.Context, r *http.Request) context.Context {
135-
return context.WithValue(ctx, kubernetes.AuthorizationHeader, r.Header.Get(kubernetes.AuthorizationHeader))
135+
// Get the standard Authorization header (OAuth compliant)
136+
authHeader := r.Header.Get(internalk8s.OAuthAuthorizationHeader)
137+
if authHeader != "" {
138+
return context.WithValue(ctx, internalk8s.OAuthAuthorizationHeader, authHeader)
139+
}
140+
141+
// Fallback to custom header for backward compatibility
142+
customAuthHeader := r.Header.Get(internalk8s.CustomAuthorizationHeader)
143+
if customAuthHeader != "" {
144+
return context.WithValue(ctx, internalk8s.OAuthAuthorizationHeader, customAuthHeader)
145+
}
146+
147+
return ctx
136148
}

0 commit comments

Comments
 (0)