-
Notifications
You must be signed in to change notification settings - Fork 36
refactor: centralize outlier functions and deduplicate shared scaffolding #9654
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3135ce3
Initial plan
Copilot 168136f
refactor: semantic function clustering — move GenerateRandomAgentID, …
Copilot bf8e4fb
Fix golangci-lint QF1008 in observed_url_domains_logger_test
lpcox 3ffc166
Address review feedback: relocate ID tests, drop duplicate parser test
lpcox 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
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,27 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/github/gh-aw-mcpg/internal/logger" | ||
| "github.com/github/gh-aw-mcpg/internal/util" | ||
| ) | ||
|
|
||
| // logAPIKey is the debug logger for API-key / agent-ID generation. | ||
| // It uses the custom namespace "auth:apikey" so callers can filter these | ||
| // debug logs independently with DEBUG=auth:apikey. | ||
| var logAPIKey = logger.New("auth:apikey") | ||
|
|
||
| // GenerateRandomAgentID generates a cryptographically random agent ID. | ||
| // Per spec §7.3, the gateway SHOULD generate a random agent ID on startup | ||
| // if none is provided. Returns a 32-byte hex-encoded string (64 chars). | ||
| func GenerateRandomAgentID() (string, error) { | ||
| logAPIKey.Print("Generating random agent ID") | ||
| key, err := util.RandomHex(32) | ||
| if err != nil { | ||
| logAPIKey.Printf("Random agent ID generation failed: %v", err) | ||
| return "", fmt.Errorf("failed to generate random agent ID: %w", err) | ||
| } | ||
| logAPIKey.Print("Random agent ID generated successfully") | ||
| return key, nil | ||
| } | ||
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,57 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // errorReader is a test helper io.Reader that always returns the configured error. | ||
| type errorReader struct { | ||
| err error | ||
| } | ||
|
|
||
| func (r *errorReader) Read(_ []byte) (int, error) { | ||
| return 0, r.err | ||
| } | ||
|
|
||
| // TestGenerateRandomAgentID_RandomFailure verifies that GenerateRandomAgentID | ||
| // correctly wraps and propagates errors from the underlying random source. | ||
| // This test must NOT run in parallel because it temporarily replaces the | ||
| // global crypto/rand.Reader. | ||
| func TestGenerateRandomAgentID_RandomFailure(t *testing.T) { | ||
| syntheticErr := errors.New("synthetic entropy failure") | ||
|
|
||
| origReader := rand.Reader | ||
| rand.Reader = &errorReader{err: syntheticErr} | ||
| defer func() { rand.Reader = origReader }() | ||
|
|
||
| key, err := GenerateRandomAgentID() | ||
|
|
||
| assert.Empty(t, key, "key should be empty when random generation fails") | ||
| require.Error(t, err, "should return an error when the random source fails") | ||
| assert.ErrorIs(t, err, syntheticErr, "error should wrap the underlying source error") | ||
| assert.Contains(t, err.Error(), "failed to generate random agent ID", | ||
| "error message should describe the failure context") | ||
| } | ||
|
|
||
| // TestGenerateRandomAgentID_RecoveryAfterFailure verifies that | ||
| // GenerateRandomAgentID works correctly after the random source is restored, | ||
| // confirming that no state is leaked between calls. | ||
| // This test must NOT run in parallel because it temporarily replaces the | ||
| // global crypto/rand.Reader. | ||
| func TestGenerateRandomAgentID_RecoveryAfterFailure(t *testing.T) { | ||
| origReader := rand.Reader | ||
| rand.Reader = &errorReader{err: errors.New("transient failure")} | ||
| _, err := GenerateRandomAgentID() | ||
| require.Error(t, err, "should fail with broken reader") | ||
|
|
||
| // Restore and verify subsequent call succeeds. | ||
| rand.Reader = origReader | ||
| key, err := GenerateRandomAgentID() | ||
| require.NoError(t, err, "should succeed after reader is restored") | ||
| assert.Len(t, key, 64, "restored call should return 64-char hex key") | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Done in 3ffc166 — moved the ID-generation failure/recovery tests and the
errorReaderhelper into a newinternal/auth/id_test.go, and dropped the now-unusedcrypto/randanderrorsimports fromheader_test.go.