- Notifications
You must be signed in to change notification settings - Fork1k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
b7f13fa
fd1e012
a6de1e3
005883a
2f77f99
3377a9b
07fd10d
117a405
73abae6
e5e190d
d4b36d3
8964297
2ba7a5c
8963aaa
bfa427f
9d1c76c
c149f8f
30c459f
cd98806
85e2d91
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -22,19 +22,14 @@ import ( | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbtime" | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/coderd/promoauth" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/retry" | ||
) | ||
// Config is used for authentication for Git operations. | ||
type Config struct { | ||
promoauth.InstrumentedOAuth2Config | ||
// ID is a unique identifier for the authenticator. | ||
ID string | ||
// Type is the type of provider. | ||
@@ -192,12 +187,8 @@ func (c *Config) ValidateToken(ctx context.Context, token string) (bool, *coders | ||
return false, nil, err | ||
} | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
res, err :=c.InstrumentedOAuth2Config.Do(ctx, promoauth.SourceValidateToken,req) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
@@ -247,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.InstrumentedOAuth2Config.Do(ctx, promoauth.SourceAppInstallations,req) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
@@ -287,6 +278,8 @@ func (c *Config) AppInstallations(ctx context.Context, token string) ([]codersdk | ||
} | ||
type DeviceAuth struct { | ||
// Config is provided for the http client method. | ||
Config promoauth.InstrumentedOAuth2Config | ||
ClientID string | ||
TokenURL string | ||
Scopes []string | ||
@@ -307,8 +300,17 @@ func (c *DeviceAuth) AuthorizeDevice(ctx context.Context) (*codersdk.ExternalAut | ||
if err != nil { | ||
return nil, err | ||
} | ||
do := http.DefaultClient.Do | ||
if c.Config != nil { | ||
// The cfg can be nil in unit tests. | ||
do = func(req *http.Request) (*http.Response, error) { | ||
return c.Config.Do(ctx, promoauth.SourceAuthorizeDevice, req) | ||
} | ||
} | ||
resp, err := do(req) | ||
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. Noticed we never check HTTP status code for 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 am not sure, I did not write this part. Have not checked the payloads myself here. | ||
req.Header.Set("Accept", "application/json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
@@ -401,7 +403,7 @@ func (c *DeviceAuth) formatDeviceCodeURL() (string, error) { | ||
// ConvertConfig converts the SDK configuration entry format | ||
// to the parsed and ready-to-consume in coderd provider type. | ||
func ConvertConfig(instrument *promoauth.Factory,entries []codersdk.ExternalAuthConfig, accessURL *url.URL) ([]*Config, error) { | ||
ids := map[string]struct{}{} | ||
configs := []*Config{} | ||
for _, entry := range entries { | ||
@@ -453,7 +455,7 @@ func ConvertConfig(entries []codersdk.ExternalAuthConfig, accessURL *url.URL) ([ | ||
Scopes: entry.Scopes, | ||
} | ||
var oauthConfigpromoauth.OAuth2Config = oc | ||
// Azure DevOps uses JWT token authentication! | ||
if entry.Type == string(codersdk.EnhancedExternalAuthProviderAzureDevops) { | ||
oauthConfig = &jwtConfig{oc} | ||
@@ -463,24 +465,25 @@ func ConvertConfig(entries []codersdk.ExternalAuthConfig, accessURL *url.URL) ([ | ||
} | ||
cfg := &Config{ | ||
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 { | ||
if entry.DeviceCodeURL == "" { | ||
return nil, xerrors.Errorf("external auth provider %q: device auth url must be provided", entry.ID) | ||
} | ||
cfg.DeviceAuth = &DeviceAuth{ | ||
Config: cfg, | ||
ClientID: entry.ClientID, | ||
TokenURL: oc.Endpoint.TokenURL, | ||
Scopes: entry.Scopes, | ||
Uh oh!
There was an error while loading.Please reload this page.