Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.

Commit b762772

Browse files
committed
fix: make per user request work
1 parent 3d2aaf4 commit b762772

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

internal/manager/manager.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ func NewManager(log *logger.Logger, cfg *rest.Config, appCfg appConfig.Config) (
6464
}
6565
cfg.Host = fmt.Sprintf("%s://%s", u.Scheme, u.Host)
6666

67-
cfg.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
67+
cfg.Wrap(func(rt http.RoundTripper) http.RoundTripper {
6868
return NewRoundTripper(log, rt)
69-
}
69+
})
7070

7171
runtimeClient, err := kcp.NewClusterAwareClientWithWatch(cfg, client.Options{})
7272
if err != nil {
@@ -215,11 +215,15 @@ func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
215215
return
216216
}
217217

218-
// let's store the token in the context for further use in the roundTripper
219-
r = r.WithContext(context.WithValue(r.Context(), TokenKey{}, token))
220-
// let's store the workspace in the context for cluster aware client
221218
r = r.WithContext(kontext.WithCluster(r.Context(), logicalcluster.Name(workspace)))
222219

220+
split := strings.Split(token, " ")
221+
if len(split) == 1 {
222+
r = r.WithContext(context.WithValue(r.Context(), TokenKey{}, token))
223+
} else {
224+
r = r.WithContext(context.WithValue(r.Context(), TokenKey{}, split[1]))
225+
}
226+
223227
if r.Header.Get("Accept") == "text/event-stream" {
224228
s.handleSubscription(w, r, h.schema)
225229
} else {

internal/manager/roundtripper.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package manager
22

33
import (
4-
"github.com/openmfp/golang-commons/logger"
5-
utilnet "k8s.io/apimachinery/pkg/util/net"
64
"net/http"
5+
6+
"github.com/golang-jwt/jwt/v5"
7+
"github.com/openmfp/golang-commons/logger"
8+
"k8s.io/client-go/transport"
79
)
810

911
type TokenKey struct{}
@@ -23,14 +25,20 @@ func NewRoundTripper(log *logger.Logger, base http.RoundTripper) http.RoundTripp
2325
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
2426
token, ok := req.Context().Value(TokenKey{}).(string)
2527
if !ok {
26-
rt.log.Debug().Str("requestURI", req.RequestURI).Msg("No token found in context")
28+
rt.log.Debug().Msg("No token found in context")
2729
return rt.base.RoundTrip(req)
2830
}
2931

30-
rt.log.Debug().Str("requestURI", req.RequestURI).Msg("Adding token to request")
32+
claims := jwt.MapClaims{}
33+
_, _, err := jwt.NewParser().ParseUnverified(token, claims)
34+
if err != nil {
35+
rt.log.Error().Err(err).Msg("Failed to parse token")
36+
return rt.base.RoundTrip(req)
37+
}
3138

32-
req = utilnet.CloneRequest(req)
33-
req.Header.Set("Authorization", "Bearer "+token)
39+
t := transport.NewImpersonatingRoundTripper(transport.ImpersonationConfig{
40+
UserName: claims["email"].(string),
41+
}, rt.base)
3442

35-
return rt.base.RoundTrip(req)
43+
return t.RoundTrip(req)
3644
}

0 commit comments

Comments
 (0)