-
Notifications
You must be signed in to change notification settings - Fork 3
Add workload federation auth #134
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" | ||
| "golang.org/x/oauth2" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| "gopkg.in/square/go-jose.v2/json" | ||
|
|
||
| "github.com/conductorone/cone/pkg/uhttp" | ||
| ) | ||
|
|
||
| const ( | ||
| grantTypeTokenExchange = "urn:ietf:params:oauth:grant-type:token-exchange" //nolint:gosec // OAuth2 grant type URI, not a credential | ||
| subjectTokenTypeJWT = "urn:ietf:params:oauth:token-type:jwt" //nolint:gosec // OAuth2 token type URI, not a credential | ||
| ) | ||
pquerna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // tokenExchangeSource implements oauth2.TokenSource by exchanging an external | ||
| // OIDC JWT for a ConductorOne access token via RFC 8693 token exchange. | ||
| type tokenExchangeSource struct { | ||
| oidcToken string | ||
| clientID string | ||
| tokenHost string | ||
| httpClient *http.Client | ||
| } | ||
|
|
||
| func (t *tokenExchangeSource) Token() (*oauth2.Token, error) { | ||
| body := url.Values{ | ||
| "grant_type": []string{grantTypeTokenExchange}, | ||
| "subject_token": []string{t.oidcToken}, | ||
| "subject_token_type": []string{subjectTokenTypeJWT}, | ||
| "client_id": []string{t.clientID}, | ||
| } | ||
|
|
||
| tokenURL := url.URL{ | ||
| Scheme: "https", | ||
| Host: t.tokenHost, | ||
| Path: "auth/v1/token", | ||
| } | ||
|
|
||
| req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, tokenURL.String(), strings.NewReader(body.Encode())) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
|
||
| resp, err := t.httpClient.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
|
Comment on lines
+33
to
+57
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. 🧩 Analysis chain🏁 Script executed: rg -n "NewClient|NewTransport|Timeout|ResponseHeaderTimeout|IdleConnTimeout" pkg/uhttp -C 3Repository: ConductorOne/cone Length of output: 1939 🏁 Script executed: cat -n pkg/uhttp/client.go | head -130Repository: ConductorOne/cone Length of output: 3153 Propagate caller context and ensure timeout for token exchange requests.
🛠️ Proposed fix type tokenExchangeSource struct {
+ ctx context.Context
oidcToken string
clientID string
tokenHost string
httpClient *http.Client
}
func (t *tokenExchangeSource) Token() (*oauth2.Token, error) {
+ ctx := t.ctx
+ if ctx == nil {
+ ctx = context.Background()
+ }
body := url.Values{
"grant_type": []string{grantTypeTokenExchange},
"subject_token": []string{t.oidcToken},
"subject_token_type": []string{subjectTokenTypeJWT},
"client_id": []string{t.clientID},
}
tokenURL := url.URL{
Scheme: "https",
Host: t.tokenHost,
Path: "auth/v1/token",
}
- req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, tokenURL.String(), strings.NewReader(body.Encode()))
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL.String(), strings.NewReader(body.Encode()))
if err != nil {
return nil, err
} func NewTokenExchangeSource(ctx context.Context, oidcToken, clientID, tokenHost string, debug bool) (oauth2.TokenSource, error) {
+ if ctx == nil {
+ ctx = context.Background()
+ }
httpClient, err := uhttp.NewClient(ctx,
uhttp.WithLogger(true, ctxzap.Extract(ctx)),
uhttp.WithUserAgent("cone-wfe-credential-provider"),
uhttp.WithDebug(debug),
)
if err != nil {
return nil, err
}
+ httpClient.Timeout = 30 * time.Second
return oauth2.ReuseTokenSource(nil, &tokenExchangeSource{
+ ctx: ctx,
oidcToken: oidcToken,
clientID: clientID,
tokenHost: tokenHost,
httpClient: httpClient,
}), nil
}🤖 Prompt for AI Agents |
||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, status.Errorf(codes.Unauthenticated, "token exchange failed: %s", resp.Status) | ||
| } | ||
|
|
||
| c1t := &c1Token{} | ||
| err = json.NewDecoder(resp.Body).Decode(c1t) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if c1t.AccessToken == "" { | ||
| return nil, status.Errorf(codes.Unauthenticated, "token exchange failed: empty access token") | ||
| } | ||
|
|
||
| return &oauth2.Token{ | ||
| AccessToken: c1t.AccessToken, | ||
| TokenType: c1t.TokenType, | ||
| Expiry: time.Now().Add(time.Duration(c1t.Expiry) * time.Second), | ||
| }, nil | ||
| } | ||
|
|
||
| // NewTokenExchangeSource creates an oauth2.TokenSource that exchanges an external | ||
| // OIDC token for a ConductorOne access token via RFC 8693 token exchange. | ||
| func NewTokenExchangeSource(ctx context.Context, oidcToken, clientID, tokenHost string, debug bool) (oauth2.TokenSource, error) { | ||
| httpClient, err := uhttp.NewClient(ctx, | ||
| uhttp.WithLogger(true, ctxzap.Extract(ctx)), | ||
| uhttp.WithUserAgent("cone-wfe-credential-provider"), | ||
| uhttp.WithDebug(debug), | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return oauth2.ReuseTokenSource(nil, &tokenExchangeSource{ | ||
| oidcToken: oidcToken, | ||
| clientID: clientID, | ||
| tokenHost: tokenHost, | ||
| httpClient: httpClient, | ||
| }), nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.