Skip to content

Commit 8be0772

Browse files
committed
use consts for source labels
1 parent 5222f66 commit 8be0772

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

coderd/externalauth/externalauth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (c *Config) ValidateToken(ctx context.Context, token string) (bool, *coders
188188
}
189189

190190
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
191-
res, err := c.InstrumentedOAuth2Config.Do(ctx, "ValidateToken", req)
191+
res, err := c.InstrumentedOAuth2Config.Do(ctx, promoauth.SourceValidateToken, req)
192192
if err != nil {
193193
return false, nil, err
194194
}
@@ -238,7 +238,7 @@ func (c *Config) AppInstallations(ctx context.Context, token string) ([]codersdk
238238
return nil, false, err
239239
}
240240
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
241-
res, err := c.InstrumentedOAuth2Config.Do(ctx, "AppInstallations", req)
241+
res, err := c.InstrumentedOAuth2Config.Do(ctx, promoauth.SourceAppInstallations, req)
242242
if err != nil {
243243
return nil, false, err
244244
}
@@ -305,7 +305,7 @@ func (c *DeviceAuth) AuthorizeDevice(ctx context.Context) (*codersdk.ExternalAut
305305
if c.Cfg != nil {
306306
// The cfg can be nil in unit tests.
307307
do = func(req *http.Request) (*http.Response, error) {
308-
return c.Cfg.Do(ctx, "AuthorizeDevice", req)
308+
return c.Cfg.Do(ctx, promoauth.SourceAuthorizeDevice, req)
309309
}
310310
}
311311

coderd/promoauth/oauth2.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import (
1010
"golang.org/x/oauth2"
1111
)
1212

13+
type Oauth2Source string
14+
15+
const (
16+
SourceValidateToken Oauth2Source = "ValidateToken"
17+
SourceExchange Oauth2Source = "Exchange"
18+
SourceTokenSource Oauth2Source = "TokenSource"
19+
SourceAppInstallations Oauth2Source = "AppInstallations"
20+
SourceAuthorizeDevice Oauth2Source = "AuthorizeDevice"
21+
)
22+
1323
// OAuth2Config exposes a subset of *oauth2.Config functions for easier testing.
1424
// *oauth2.Config should be used instead of implementing this in production.
1525
type OAuth2Config interface {
@@ -27,7 +37,7 @@ type InstrumentedOAuth2Config interface {
2737

2838
// Do is provided as a convenience method to make a request with the oauth2 client.
2939
// It mirrors `http.Client.Do`.
30-
Do(ctx context.Context, source string, req *http.Request) (*http.Response, error)
40+
Do(ctx context.Context, source Oauth2Source, req *http.Request) (*http.Response, error)
3141
}
3242

3343
var _ OAuth2Config = (*Config)(nil)
@@ -79,7 +89,7 @@ type Config struct {
7989
metrics *metrics
8090
}
8191

82-
func (c *Config) Do(ctx context.Context, source string, req *http.Request) (*http.Response, error) {
92+
func (c *Config) Do(ctx context.Context, source Oauth2Source, req *http.Request) (*http.Response, error) {
8393
cli := c.oauthHTTPClient(ctx, source)
8494
return cli.Do(req)
8595
}
@@ -90,11 +100,11 @@ func (c *Config) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string
90100
}
91101

92102
func (c *Config) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) {
93-
return c.underlying.Exchange(c.wrapClient(ctx, "Exchange"), code, opts...)
103+
return c.underlying.Exchange(c.wrapClient(ctx, SourceExchange), code, opts...)
94104
}
95105

96106
func (c *Config) TokenSource(ctx context.Context, token *oauth2.Token) oauth2.TokenSource {
97-
return c.underlying.TokenSource(c.wrapClient(ctx, "TokenSource"), token)
107+
return c.underlying.TokenSource(c.wrapClient(ctx, SourceTokenSource), token)
98108
}
99109

100110
// wrapClient is the only way we can accurately instrument the oauth2 client.
@@ -104,12 +114,12 @@ func (c *Config) TokenSource(ctx context.Context, token *oauth2.Token) oauth2.To
104114
// For example, the 'TokenSource' method will return a token
105115
// source that will make a network request when the 'Token' method is called on
106116
// it if the token is expired.
107-
func (c *Config) wrapClient(ctx context.Context, source string) context.Context {
117+
func (c *Config) wrapClient(ctx context.Context, source Oauth2Source) context.Context {
108118
return context.WithValue(ctx, oauth2.HTTPClient, c.oauthHTTPClient(ctx, source))
109119
}
110120

111121
// oauthHTTPClient returns an http client that will instrument every request made.
112-
func (c *Config) oauthHTTPClient(ctx context.Context, source string) *http.Client {
122+
func (c *Config) oauthHTTPClient(ctx context.Context, source Oauth2Source) *http.Client {
113123
cli := &http.Client{}
114124

115125
// Check if the context has a http client already.
@@ -124,13 +134,13 @@ func (c *Config) oauthHTTPClient(ctx context.Context, source string) *http.Clien
124134

125135
type instrumentedTripper struct {
126136
c *Config
127-
source string
137+
source Oauth2Source
128138
underlying http.RoundTripper
129139
}
130140

131141
// newInstrumentedTripper intercepts a http request, and increments the
132142
// externalRequestCount metric.
133-
func newInstrumentedTripper(c *Config, source string, under http.RoundTripper) *instrumentedTripper {
143+
func newInstrumentedTripper(c *Config, source Oauth2Source, under http.RoundTripper) *instrumentedTripper {
134144
if under == nil {
135145
under = http.DefaultTransport
136146
}
@@ -156,7 +166,7 @@ func (i *instrumentedTripper) RoundTrip(r *http.Request) (*http.Response, error)
156166
}
157167
i.c.metrics.externalRequestCount.With(prometheus.Labels{
158168
"name": i.c.name,
159-
"source": i.source,
169+
"source": string(i.source),
160170
"status_code": fmt.Sprintf("%d", statusCode),
161171
}).Inc()
162172
return resp, err

0 commit comments

Comments
 (0)