Skip to content

chore: instrument external oauth2 requests #11519

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

Merged
merged 20 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
typos
  • Loading branch information
Emyrk committed Jan 10, 2024
commit 117a40542aa43ec270e192303f68ebf28a6e99c9
30 changes: 15 additions & 15 deletions coderd/externalauth/externalauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

// Config is used for authentication for Git operations.
type Config struct {
promoauth.InstrumentedeOAuth2Config
promoauth.InstrumentedOAuth2Config
// ID is a unique identifier for the authenticator.
ID string
// Type is the type of provider.
Expand Down Expand Up @@ -188,7 +188,7 @@ func (c *Config) ValidateToken(ctx context.Context, token string) (bool, *coders
}

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
res, err := c.InstrumentedeOAuth2Config.Do(ctx, "ValidateToken", req)
res, err := c.InstrumentedOAuth2Config.Do(ctx, "ValidateToken", req)
if err != nil {
return false, nil, err
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func (c *Config) AppInstallations(ctx context.Context, token string) ([]codersdk
return nil, false, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
res, err := c.InstrumentedeOAuth2Config.Do(ctx, "AppInstallations", req)
res, err := c.InstrumentedOAuth2Config.Do(ctx, "AppInstallations", req)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func (c *Config) AppInstallations(ctx context.Context, token string) ([]codersdk

type DeviceAuth struct {
// Cfg is provided for the http client method.
Cfg promoauth.InstrumentedeOAuth2Config
Cfg promoauth.InstrumentedOAuth2Config
ClientID string
TokenURL string
Scopes []string
Expand Down Expand Up @@ -456,17 +456,17 @@ func ConvertConfig(instrument *promoauth.Factory, entries []codersdk.ExternalAut
}

cfg := &Config{
InstrumentedeOAuth2Config: instrument.New(entry.ID, oauthConfig),
ID: entry.ID,
Regex: regex,
Type: entry.Type,
NoRefresh: entry.NoRefresh,
ValidateURL: entry.ValidateURL,
AppInstallationsURL: entry.AppInstallationsURL,
AppInstallURL: entry.AppInstallURL,
DisplayName: entry.DisplayName,
DisplayIcon: entry.DisplayIcon,
ExtraTokenKeys: entry.ExtraTokenKeys,
InstrumentedOAuth2Config: instrument.New(entry.ID, oauthConfig),
ID: entry.ID,
Regex: regex,
Type: entry.Type,
NoRefresh: entry.NoRefresh,
ValidateURL: entry.ValidateURL,
AppInstallationsURL: entry.AppInstallationsURL,
AppInstallURL: entry.AppInstallURL,
DisplayName: entry.DisplayName,
DisplayIcon: entry.DisplayIcon,
ExtraTokenKeys: entry.ExtraTokenKeys,
}

if entry.DeviceFlow {
Expand Down
13 changes: 4 additions & 9 deletions coderd/promoauth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ type OAuth2Config interface {
TokenSource(context.Context, *oauth2.Token) oauth2.TokenSource
}

type InstrumentedeOAuth2Config interface {
// InstrumentedOAuth2Config extends OAuth2Config with a `Do` method that allows
// external oauth related calls to be instrumented. This is to support
// "ValidateToken" which is not an oauth2 specified method.
type InstrumentedOAuth2Config interface {
OAuth2Config

// Do is provided as a convience method to make a request with the oauth2 client.
Expand All @@ -28,14 +31,6 @@ type InstrumentedeOAuth2Config interface {
Do(ctx context.Context, source string, req *http.Request) (*http.Response, error)
}

type HTTPDo interface {
// Do is provided as a convience method to make a request with the oauth2 client.
// It mirrors `http.Client.Do`.
// We need this because Coder adds some extra functionality to
// oauth clients such as the `ValidateToken()` method.
Do(ctx context.Context, source string, req *http.Request) (*http.Response, error)
}

var _ OAuth2Config = (*Config)(nil)

type Factory struct {
Expand Down