-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: sal rashid <[email protected]>
- Loading branch information
1 parent
9f404ca
commit 0c2e913
Showing
6 changed files
with
158 additions
and
70 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module mtlstokensource | ||
|
||
go 1.22.4 | ||
|
||
require golang.org/x/oauth2 v0.22.0 |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA= | ||
golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= |
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package mtlstokensource | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/json" | ||
"io" | ||
"net" | ||
"net/http" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"golang.org/x/oauth2" | ||
) | ||
|
||
type MtlsTokenConfig struct { | ||
RootCA x509.CertPool | ||
TLSCertificate tls.Certificate | ||
} | ||
|
||
type gceMetadataTransport struct { | ||
rtp http.RoundTripper | ||
tlsConfig *tls.Config | ||
} | ||
|
||
func GCEMetadataTLSTransport(tlsconfig *tls.Config) *gceMetadataTransport { | ||
tr := &gceMetadataTransport{ | ||
tlsConfig: tlsconfig, | ||
} | ||
|
||
myDialer := &net.Dialer{ | ||
Timeout: 500 * time.Millisecond, | ||
} | ||
dc := func(ctx context.Context, network, address string) (net.Conn, error) { | ||
overrideAddress := os.Getenv("GCE_METADATA_HOST") | ||
if overrideAddress == "" { | ||
overrideAddress = "metadata.google.internal:443" | ||
} | ||
return myDialer.DialContext(ctx, network, overrideAddress) | ||
} | ||
|
||
tr.tlsConfig.ServerName = "metadata.google.internal" | ||
tr.rtp = &http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: dc, | ||
TLSHandshakeTimeout: 400 * time.Millisecond, | ||
TLSClientConfig: tr.tlsConfig, | ||
} | ||
return tr | ||
} | ||
|
||
func (tr *gceMetadataTransport) RoundTrip(r *http.Request) (*http.Response, error) { | ||
r.URL.Scheme = "https" | ||
r.Header.Add("Metadata-Flavor", "Google") | ||
return tr.rtp.RoundTrip(r) | ||
} | ||
|
||
const () | ||
|
||
func MtlsTokenSource(tokenConfig *MtlsTokenConfig) (oauth2.TokenSource, error) { | ||
|
||
tlsConfig := &tls.Config{ | ||
RootCAs: &tokenConfig.RootCA, | ||
Certificates: []tls.Certificate{tokenConfig.TLSCertificate}, | ||
} | ||
|
||
return &mtlsTokenSource{ | ||
refreshMutex: &sync.Mutex{}, | ||
mtlsToken: oauth2.Token{}, | ||
tlsConfig: tlsConfig, | ||
}, nil | ||
} | ||
|
||
type mtlsTokenSource struct { | ||
refreshMutex *sync.Mutex | ||
mtlsToken oauth2.Token | ||
tlsConfig *tls.Config | ||
} | ||
|
||
func (ts *mtlsTokenSource) Token() (*oauth2.Token, error) { | ||
|
||
ts.refreshMutex.Lock() | ||
defer ts.refreshMutex.Unlock() | ||
|
||
if ts.mtlsToken.Valid() { | ||
return &ts.mtlsToken, nil | ||
} | ||
|
||
client := &http.Client{ | ||
Transport: GCEMetadataTLSTransport(ts.tlsConfig), | ||
Timeout: time.Duration(100) * time.Millisecond, | ||
} | ||
|
||
accessTokenResp, err := client.Get("https://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
accessTokenBytes, err := io.ReadAll(accessTokenResp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer accessTokenResp.Body.Close() | ||
|
||
tok := &oauth2.Token{} | ||
err = json.Unmarshal(accessTokenBytes, tok) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return tok, nil | ||
|
||
} |