-
Notifications
You must be signed in to change notification settings - Fork 894
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
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b7f13fa
chore: instrument external oauth2 requests
Emyrk fd1e012
Add a unit test
Emyrk a6de1e3
remove internet based request
Emyrk 005883a
use ctx
Emyrk 2f77f99
typo
Emyrk 3377a9b
Work on instrumenting related oauth2 calls
Emyrk 07fd10d
remove print
Emyrk 117a405
typos
Emyrk 73abae6
Fixup some unit tests
Emyrk e5e190d
fixup test cases
Emyrk d4b36d3
remove oidc instrument
Emyrk 8964297
typos
Emyrk 2ba7a5c
more test coverage
Emyrk 8963aaa
left debug
Emyrk bfa427f
Add comments
Emyrk 9d1c76c
use consts for source labels
Emyrk c149f8f
Spell out config
Emyrk 30c459f
fix compile
Emyrk cd98806
Use req with context
Emyrk 85e2d91
no panic
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add a unit test
- Loading branch information
commit fd1e012a67e961594f0611bd4607e299f584e89e
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,53 @@ | ||
package promoauth_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
ptestutil "github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/v2/coderd/coderdtest/oidctest" | ||
"github.com/coder/coder/v2/coderd/promoauth" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
|
||
func TestMaintainDefault(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := testutil.Context(t, testutil.WaitShort) | ||
idp := oidctest.NewFakeIDP(t, oidctest.WithServing()) | ||
reg := prometheus.NewRegistry() | ||
count := func() int { | ||
return ptestutil.CollectAndCount(reg, "coderd_oauth2_external_requests_total") | ||
} | ||
|
||
factory := promoauth.NewFactory(reg) | ||
cfg := factory.New("test", idp.OAuthConfig()) | ||
|
||
// 0 Requests before we start | ||
require.Equal(t, count(), 0) | ||
|
||
// Exchange should trigger a request | ||
code := idp.CreateAuthCode(t, "foo") | ||
token, err := cfg.Exchange(ctx, code) | ||
require.NoError(t, err) | ||
require.Equal(t, count(), 1) | ||
|
||
// Force a refresh | ||
token.Expiry = time.Now().Add(time.Hour * -1) | ||
src := cfg.TokenSource(ctx, token) | ||
refreshed, err := src.Token() | ||
require.NoError(t, err) | ||
require.NotEqual(t, token.AccessToken, refreshed.AccessToken, "token refreshed") | ||
require.Equal(t, count(), 2) | ||
|
||
// Verify the default client was not broken. This check is added because we | ||
// extend the http.DefaultTransport. If a `.Clone()` is not done, this can be | ||
// mis-used. It is cheap to run this quick check. | ||
_, err = http.DefaultClient.Get("https://coder.com") | ||
require.NoError(t, err) | ||
require.Equal(t, count(), 2) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about testing for
under.(interface{ Clone() ... })
instead? That would cover the "supports cloning" case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish I could but the method signature is:
So the interface of
Clone() http.RoundTripper
does not match, and the default transport would not implement it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, of course. That's unfortunate :(.