This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Feat: Enable proxy-authorization in admin client #437
Closed
Closed
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9b80f4d
Add proxyCommand to client config
d6a2ac8
Add proxy auth unary interceptor
410e3bd
Use proxy auth in http client for oauth
e435a3a
Cache tokens obtained from external commands
cd5cc57
Make tests pass
27b8e75
Add tests for proxy auth interceptor
ea9090e
Make work without 2nd token cache but instead with 2nd credentials fu…
7bce3d9
Adapt existing tests to not using a 2nd token cache but a 2nd credent…
132dbb9
Adapt new tests to not using a 2nd token cache but a 2nd credentials …
66e6f65
Fix number of opts in NewAdminConnection
f8caf4f
Don't overwrite original error in NewProxyAuthInterceptor
b947012
Improve error message when failing to create http client for oauth
19d069e
Actually don't return any error from setHTTPClientContext at all as b…
6a56775
Don't require github.com/golang-jwt/jwt anymore
fd10310
Make tests pass again
89ab23c
Lint
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 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package admin | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
|
@@ -16,10 +17,12 @@ import ( | |
"google.golang.org/grpc" | ||
) | ||
|
||
const ProxyAuthorizationHeader = "proxy-authorization" | ||
|
||
// MaterializeCredentials will attempt to build a TokenSource given the anonymously available information exposed by the server. | ||
// Once established, it'll invoke PerRPCCredentialsFuture.Store() on perRPCCredentials to populate it with the appropriate values. | ||
func MaterializeCredentials(ctx context.Context, cfg *Config, tokenCache cache.TokenCache, perRPCCredentials *PerRPCCredentialsFuture) error { | ||
authMetadataClient, err := InitializeAuthMetadataClient(ctx, cfg) | ||
func MaterializeCredentials(ctx context.Context, cfg *Config, tokenCache cache.TokenCache, perRPCCredentials *PerRPCCredentialsFuture, proxyCredentialsFuture *PerRPCCredentialsFuture) error { | ||
authMetadataClient, err := InitializeAuthMetadataClient(ctx, cfg, proxyCredentialsFuture) | ||
if err != nil { | ||
return fmt.Errorf("failed to initialized Auth Metadata Client. Error: %w", err) | ||
} | ||
|
@@ -48,19 +51,70 @@ func MaterializeCredentials(ctx context.Context, cfg *Config, tokenCache cache.T | |
return nil | ||
} | ||
|
||
func GetProxyTokenSource(ctx context.Context, cfg *Config) (oauth2.TokenSource, error) { | ||
tokenSourceProvider, err := NewExternalTokenSourceProvider(cfg.ProxyCommand) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to initialized proxy authorization token source provider. Err: %w", err) | ||
} | ||
proxyTokenSource, err := tokenSourceProvider.GetTokenSource(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return proxyTokenSource, nil | ||
} | ||
|
||
func MaterializeProxyAuthCredentials(ctx context.Context, cfg *Config, proxyCredentialsFuture *PerRPCCredentialsFuture) error { | ||
proxyTokenSource, err := GetProxyTokenSource(ctx, cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
wrappedTokenSource := NewCustomHeaderTokenSource(proxyTokenSource, cfg.UseInsecureConnection, ProxyAuthorizationHeader) | ||
proxyCredentialsFuture.Store(wrappedTokenSource) | ||
|
||
return nil | ||
} | ||
|
||
func shouldAttemptToAuthenticate(errorCode codes.Code) bool { | ||
return errorCode == codes.Unauthenticated | ||
} | ||
|
||
type proxyAuthTransport struct { | ||
transport http.RoundTripper | ||
proxyCredentialsFuture *PerRPCCredentialsFuture | ||
} | ||
|
||
func (c *proxyAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
// check if the proxy credentials future is initialized | ||
if !c.proxyCredentialsFuture.IsInitialized() { | ||
return nil, errors.New("proxy credentials future is not initialized") | ||
} | ||
|
||
metadata, err := c.proxyCredentialsFuture.GetRequestMetadata(context.Background(), "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
token := metadata[ProxyAuthorizationHeader] | ||
req.Header.Add(ProxyAuthorizationHeader, token) | ||
return c.transport.RoundTrip(req) | ||
} | ||
|
||
// Set up http client used in oauth2 | ||
func setHTTPClientContext(ctx context.Context, cfg *Config) context.Context { | ||
func setHTTPClientContext(ctx context.Context, cfg *Config, proxyCredentialsFuture *PerRPCCredentialsFuture) context.Context { | ||
httpClient := &http.Client{} | ||
transport := &http.Transport{} | ||
|
||
if len(cfg.HTTPProxyURL.String()) > 0 { | ||
// create a transport that uses the proxy | ||
transport := &http.Transport{ | ||
Proxy: http.ProxyURL(&cfg.HTTPProxyURL.URL), | ||
transport.Proxy = http.ProxyURL(&cfg.HTTPProxyURL.URL) | ||
} | ||
|
||
if cfg.ProxyCommand != nil { | ||
httpClient.Transport = &proxyAuthTransport{ | ||
transport: transport, | ||
proxyCredentialsFuture: proxyCredentialsFuture, | ||
} | ||
} else { | ||
httpClient.Transport = transport | ||
} | ||
|
||
|
@@ -77,9 +131,9 @@ func setHTTPClientContext(ctx context.Context, cfg *Config) context.Context { | |
// more. It'll fail hard if it couldn't do so (i.e. it will no longer attempt to send an unauthenticated request). Once | ||
// a token source has been created, it'll invoke the grpc pipeline again, this time the grpc.PerRPCCredentials should | ||
// be able to find and acquire a valid AccessToken to annotate the request with. | ||
func NewAuthInterceptor(cfg *Config, tokenCache cache.TokenCache, credentialsFuture *PerRPCCredentialsFuture) grpc.UnaryClientInterceptor { | ||
func NewAuthInterceptor(cfg *Config, tokenCache cache.TokenCache, credentialsFuture *PerRPCCredentialsFuture, proxyCredentialsFuture *PerRPCCredentialsFuture) grpc.UnaryClientInterceptor { | ||
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
ctx = setHTTPClientContext(ctx, cfg) | ||
ctx = setHTTPClientContext(ctx, cfg, proxyCredentialsFuture) | ||
|
||
err := invoker(ctx, method, req, reply, cc, opts...) | ||
if err != nil { | ||
|
@@ -89,7 +143,7 @@ func NewAuthInterceptor(cfg *Config, tokenCache cache.TokenCache, credentialsFut | |
// If the error we receive from executing the request expects | ||
if shouldAttemptToAuthenticate(st.Code()) { | ||
logger.Debugf(ctx, "Request failed due to [%v]. Attempting to establish an authenticated connection and trying again.", st.Code()) | ||
newErr := MaterializeCredentials(ctx, cfg, tokenCache, credentialsFuture) | ||
newErr := MaterializeCredentials(ctx, cfg, tokenCache, credentialsFuture, proxyCredentialsFuture) | ||
if newErr != nil { | ||
return fmt.Errorf("authentication error! Original Error: %v, Auth Error: %w", err, newErr) | ||
} | ||
|
@@ -102,3 +156,18 @@ func NewAuthInterceptor(cfg *Config, tokenCache cache.TokenCache, credentialsFut | |
return err | ||
} | ||
} | ||
|
||
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. Adapted from |
||
func NewProxyAuthInterceptor(cfg *Config, proxyCredentialsFuture *PerRPCCredentialsFuture) grpc.UnaryClientInterceptor { | ||
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
|
||
err := invoker(ctx, method, req, reply, cc, opts...) | ||
if err != nil { | ||
newErr := MaterializeProxyAuthCredentials(ctx, cfg, proxyCredentialsFuture) | ||
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. The external command is called a single time per call of |
||
if newErr != nil { | ||
return fmt.Errorf("proxy authorization error! Original Error: %v, Proxy Auth Error: %w", err, newErr) | ||
} | ||
return invoker(ctx, method, req, reply, cc, opts...) | ||
} | ||
return err | ||
} | ||
} |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adapted from
MaterializeAuthCredentials
.