chore: identify the gateway with a User-Agent header on requests to Twingate - #459
chore: identify the gateway with a User-Agent header on requests to Twingate#459minhtule wants to merge 2 commits into
Conversation
7a286a4 to
3c09309
Compare
3c09309 to
90f962f
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces a shared mechanism for identifying the gateway via an outbound User-Agent: Twingate-Gateway/<version> header, and wires it into the two places the gateway makes HTTP requests to Twingate (hostname resolution and JWKS fetch for JWT verification).
Changes:
- Added
internal/util/useragenthelper (string +http.RoundTripperwrapper) to stamp the gateway User-Agent onto outbound requests. - Updated Twingate hostname resolution HTTP client to use the User-Agent transport.
- Updated JWKS retrieval (keyfunc) to use a custom HTTP client transport so the User-Agent header is applied, and added tests to verify the header is sent.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/util/useragent/useragent.go | New helper for generating the gateway User-Agent and applying it via a transport wrapper. |
| internal/util/useragent/useragent_test.go | Unit test validating the User-Agent string formatting. |
| internal/token/parser.go | JWKS client customization so keyfunc JWKS fetch includes the gateway User-Agent. |
| internal/token/parser_test.go | Test verifying JWKS endpoint receives the expected User-Agent. |
| internal/config/config.go | Hostname resolution HTTP client updated to use the User-Agent transport. |
| internal/config/config_test.go | Test verifying hostname resolution sends the expected User-Agent. |
Suppressed comments (1)
internal/token/parser.go:40
- The override
http.Clientused for JWKS fetching should set a timeout to avoid hanging on network stalls. Without it,NewParsercan block indefinitely while waiting for the initial JWKS fetch.
jwks, err := keyfunc.NewDefaultOverrideCtx(context.Background(), []string{config.JWKSURL}, keyfunc.Override{
Client: &http.Client{Transport: useragent.Transport{}},
})
if err != nil {
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Suppressed comments (1)
internal/config/config_test.go:127
- This
selectuses adefaultbranch, so the assertion can be skipped if the request arrives slightly after the check. Waiting up to a short timeout makes the test deterministic while still failing quickly when no request is made.
select {
case userAgent := <-userAgents:
assert.Equal(t, "Twingate-Gateway/dev", userAgent)
default:
t.Fatal("hostname resolution endpoint was not requested")
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #459 +/- ##
==========================================
+ Coverage 90.63% 90.66% +0.03%
==========================================
Files 41 42 +1
Lines 2903 2915 +12
==========================================
+ Hits 2631 2643 +12
Misses 177 177
Partials 95 95
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Related Tickets & Documents
Changes
User-Agent: Twingate-Gateway/<version>on requests to Twingate (Twingate hostname resolution and the JWKS fetch).Notes
version.Versionisdevin local builds and set via ldflags by goreleaser, so released builds report the real version.