-
Notifications
You must be signed in to change notification settings - Fork 0
feat: introspection authentication #65
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
base: main
Are you sure you want to change the base?
Changes from 13 commits
e85d7bf
70d51de
3a773f9
d9bf6ac
2f7d417
2aa487a
2dc55d2
7b1e9c0
334608e
663bdac
1aa163a
3d2f80f
4c7dfdd
cdb0b52
2dbd031
a0c0421
9119ea2
41f6dcb
5d49287
5cec80b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| package roundtripper | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/golang-jwt/jwt/v5" | ||
| "github.com/platform-mesh/golang-commons/logger" | ||
| utilnet "k8s.io/apimachinery/pkg/util/net" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/transport" | ||
|
|
||
| "github.com/platform-mesh/kubernetes-graphql-gateway/common/config" | ||
|
|
@@ -16,15 +19,17 @@ type TokenKey struct{} | |
| type roundTripper struct { | ||
| log *logger.Logger | ||
| adminRT, unauthorizedRT http.RoundTripper | ||
| baseRT http.RoundTripper | ||
| appCfg config.Config | ||
| } | ||
|
|
||
| type unauthorizedRoundTripper struct{} | ||
|
|
||
| func New(log *logger.Logger, appCfg config.Config, adminRoundTripper, unauthorizedRT http.RoundTripper) http.RoundTripper { | ||
| func New(log *logger.Logger, appCfg config.Config, adminRoundTripper, baseRoundTripper, unauthorizedRT http.RoundTripper) http.RoundTripper { | ||
| return &roundTripper{ | ||
| log: log, | ||
| adminRT: adminRoundTripper, | ||
| baseRT: baseRoundTripper, | ||
| unauthorizedRT: unauthorizedRT, | ||
| appCfg: appCfg, | ||
| } | ||
|
|
@@ -35,6 +40,18 @@ func NewUnauthorizedRoundTripper() http.RoundTripper { | |
| return &unauthorizedRoundTripper{} | ||
| } | ||
|
|
||
| // NewBaseRoundTripper creates a base HTTP transport with only TLS configuration (no authentication) | ||
| func NewBaseRoundTripper(tlsConfig rest.TLSClientConfig) (http.RoundTripper, error) { | ||
| return rest.TransportFor(&rest.Config{ | ||
| TLSClientConfig: rest.TLSClientConfig{ | ||
| Insecure: tlsConfig.Insecure, | ||
| ServerName: tlsConfig.ServerName, | ||
| CAFile: tlsConfig.CAFile, | ||
| CAData: tlsConfig.CAData, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| rt.log.Info(). | ||
| Str("req.Host", req.Host). | ||
|
|
@@ -65,13 +82,13 @@ func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | |
| } | ||
|
|
||
| // No we are going to use token based auth only, so we are reassigning the headers | ||
| req = utilnet.CloneRequest(req) | ||
| req.Header.Del("Authorization") | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
|
|
||
| if !rt.appCfg.Gateway.ShouldImpersonate { | ||
| rt.log.Debug().Str("path", req.URL.Path).Msg("Using bearer token authentication") | ||
|
|
||
| return rt.adminRT.RoundTrip(req) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We used adminRT here and it worked with any token passed since adminRT has certificates to access cluster.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also tried to replace adminRT with BearerRT below in the impersonation section, but I got the following error: I guess we should setup fga first, let me know if this we should replace adminRT in the impersonation branch as well. |
||
| fmt.Println(token) | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return transport.NewBearerAuthRoundTripper(token, rt.baseRT).RoundTrip(req) | ||
| } | ||
|
|
||
| // Impersonation mode: extract user from token and impersonate | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.