git.iko.soy/typeable/oauth2-proxy
on master help sign in
feat: Replace default Go user-agent with oauth2-proxy and version (#2570)

* feat: Replace default Go user-agent with oauth2-proxy and version * Add to CHANGELOG * Make userAgentTransport configurable and composable * Use correct naming convention for DefaultHTTPClient * Move version to own package and use named arguments * Update version path in Makefile * Fix import path in Makefile * Change importpath in dist.sh * Minor style issues

commit3045392c1700d66345426fcb8aaf4311bae03eff authorJacob Middag <middagj@users.noreply.github.com> dateSun 14 Jul 2024, 22:09 (2 years ago) parent45ec12b
11 files changed+54−9
CHANGELOG.md+1 Makefile+1−1 dist.sh+4−2 main.go+2−1 oauthproxy.go+2−1 pkg/providers/oidc/provider_verifier.go+2 pkg/requests/builder.go+2−2 pkg/requests/builder_test.go+3−1 pkg/requests/http.go+29 version.go → pkg/version/version.go+1−1 providers/oidc.go+7
CHANGELOG.md+1
@@ -10,6 +10,7 @@
1010
1111- [#2539](https://github.com/oauth2-proxy/oauth2-proxy/pull/2539) pkg/http: Fix leaky test (@isodude)
1212- [#4917](https://github.com/oauth2-proxy/oauth2-proxy/pull/4917) Upgraded all modules to the latest version (@pierluigilenoci)
13+- [#2570](https://github.com/oauth2-proxy/oauth2-proxy/pull/2570) Set default user agent to oauth2-proxy/$version (from default Golang one)
1314
1415# V7.6.0
1516
Makefile+1−1
@@ -40,7 +40,7 @@ lint: validate-go-version
4040build: validate-go-version clean $(BINARY)
4141
4242$(BINARY):
43− CGO_ENABLED=0 $(GO) build -a -installsuffix cgo -ldflags="-X main.VERSION=${VERSION}" -o $@ github.com/oauth2-proxy/oauth2-proxy/v7
43+ CGO_ENABLED=0 $(GO) build -a -installsuffix cgo -ldflags="-X github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version.VERSION=${VERSION}" -o $@ github.com/oauth2-proxy/oauth2-proxy/v7
4444
4545DOCKER_BUILD_PLATFORM ?= linux/amd64,linux/arm64,linux/ppc64le,linux/arm/v7
4646DOCKER_BUILD_RUNTIME_IMAGE ?= gcr.io/distroless/static:nonroot
dist.sh+4−2
@@ -32,10 +32,12 @@ for ARCH in "${ARCHS[@]}"; do
3232 # Create architecture specific binaries
3333 if [[ ${GO_ARCH} == armv* ]]; then
3434 GO_ARM=$(echo $GO_ARCH | awk -Fv '{print $2}')
35− GO111MODULE=on GOOS=${GO_OS} GOARCH=arm GOARM=${GO_ARM} CGO_ENABLED=0 go build -ldflags="-X main.VERSION=${VERSION}" \
35+ GO111MODULE=on GOOS=${GO_OS} GOARCH=arm GOARM=${GO_ARM} CGO_ENABLED=0 go build \
36+ -ldflags="-X github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version.VERSION=${VERSION}" \
3637 -o release/${BINARY}-${VERSION}.${ARCH}/${BINARY} .
3738 else
38− GO111MODULE=on GOOS=${GO_OS} GOARCH=${GO_ARCH} CGO_ENABLED=0 go build -ldflags="-X main.VERSION=${VERSION}" \
39+ GO111MODULE=on GOOS=${GO_OS} GOARCH=${GO_ARCH} CGO_ENABLED=0 go build \
40+ -ldflags="-X github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version.VERSION=${VERSION}" \
3941 -o release/${BINARY}-${VERSION}.${ARCH}/${BINARY} .
4042 fi
4143
main.go+2−1
@@ -9,6 +9,7 @@ import (
99 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
1010 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
1111 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation"
12+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version"
1213 "github.com/spf13/pflag"
1314)
1415
@@ -28,7 +29,7 @@ func main() {
2829 configFlagSet.Parse(os.Args[1:])
2930
3031 if *showVersion {
31− fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
32+ fmt.Printf("oauth2-proxy %s (built with %s)\n", version.VERSION, runtime.Version())
3233 return
3334 }
3435
oauthproxy.go+2−1
@@ -30,6 +30,7 @@ import (
3030 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
3131 proxyhttp "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/http"
3232 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/util"
33+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version"
3334
3435 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/ip"
3536 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
@@ -142,7 +143,7 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
142143 CustomLogo: opts.Templates.CustomLogo,
143144 ProxyPrefix: opts.ProxyPrefix,
144145 Footer: opts.Templates.Footer,
145− Version: VERSION,
146+ Version: version.VERSION,
146147 Debug: opts.Templates.Debug,
147148 ProviderName: buildProviderName(provider, opts.Providers[0].Name),
148149 SignInMessage: buildSignInMessage(opts),
pkg/providers/oidc/provider_verifier.go+2
@@ -6,6 +6,7 @@ import (
66 "fmt"
77
88 "github.com/coreos/go-oidc/v3/oidc"
9+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
910 k8serrors "k8s.io/apimachinery/pkg/util/errors"
1011)
1112
@@ -130,6 +131,7 @@ func getVerifierBuilder(ctx context.Context, opts ProviderVerifierOptions) (veri
130131
131132// newVerifierBuilder returns a function to create a IDToken verifier from an OIDC config.
132133func newVerifierBuilder(ctx context.Context, issuerURL, jwksURL string, supportedSigningAlgs []string) verifierBuilder {
134+ ctx = oidc.ClientContext(ctx, requests.DefaultHTTPClient)
133135 keySet := oidc.NewRemoteKeySet(ctx, jwksURL)
134136 return func(oidcConfig *oidc.Config) *oidc.IDTokenVerifier {
135137 if len(supportedSigningAlgs) > 0 {
pkg/requests/builder.go+2−2
@@ -58,7 +58,7 @@ func (r *builder) WithMethod(method string) Builder {
5858
5959// WithHeaders replaces the request header map with the given header map.
6060func (r *builder) WithHeaders(header http.Header) Builder {
61− r.header = header
61+ r.header = header.Clone()
6262 return r
6363}
6464
@@ -99,7 +99,7 @@ func (r *builder) do() Result {
9999 }
100100 req.Header = r.header
101101
102− resp, err := http.DefaultClient.Do(req)
102+ resp, err := DefaultHTTPClient.Do(req)
103103 if err != nil {
104104 r.result = &result{err: fmt.Errorf("error performing request: %v", err)}
105105 return r.result
pkg/requests/builder_test.go+3−1
@@ -8,6 +8,8 @@ import (
88 "fmt"
99 "net/http"
1010
11+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version"
12+
1113 "github.com/bitly/go-simplejson"
1214 . "github.com/onsi/ginkgo"
1315 . "github.com/onsi/gomega"
@@ -19,7 +21,7 @@ var _ = Describe("Builder suite", func() {
1921
2022 baseHeaders := http.Header{
2123 "Accept-Encoding": []string{"gzip"},
22− "User-Agent": []string{"Go-http-client/1.1"},
24+ "User-Agent": []string{"oauth2-proxy/" + version.VERSION},
2325 }
2426
2527 BeforeEach(func() {
pkg/requests/http.gonew file+29
@@ -0,0 +1,29 @@
1+package requests
2+
3+import (
4+ "net/http"
5+
6+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version"
7+)
8+
9+type userAgentTransport struct {
10+ next http.RoundTripper
11+ userAgent string
12+}
13+
14+func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
15+ r := req.Clone(req.Context())
16+ setDefaultUserAgent(r.Header, t.userAgent)
17+ return t.next.RoundTrip(r)
18+}
19+
20+var DefaultHTTPClient = &http.Client{Transport: &userAgentTransport{
21+ next: http.DefaultTransport,
22+ userAgent: "oauth2-proxy/" + version.VERSION,
23+}}
24+
25+func setDefaultUserAgent(header http.Header, userAgent string) {
26+ if header != nil && len(header.Values("User-Agent")) == 0 {
27+ header.Set("User-Agent", userAgent)
28+ }
29+}
version.go → pkg/version/version.gorenamed+1−1
@@ -1,4 +1,4 @@
1−package main
1+package version
22
33// VERSION contains version information
44var VERSION = "undefined"
providers/oidc.go+7
@@ -7,9 +7,11 @@ import (
77 "net/url"
88 "time"
99
10+ "github.com/coreos/go-oidc/v3/oidc"
1011 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
1112 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
1213 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
14+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
1315 "golang.org/x/oauth2"
1416)
1517
@@ -83,6 +85,8 @@ func (p *OIDCProvider) Redeem(ctx context.Context, redirectURL, code, codeVerifi
8385 },
8486 RedirectURL: redirectURL,
8587 }
88+
89+ ctx = oidc.ClientContext(ctx, requests.DefaultHTTPClient)
8690 token, err := c.Exchange(ctx, code, opts...)
8791 if err != nil {
8892 return nil, fmt.Errorf("token exchange failed: %v", err)
@@ -103,6 +107,7 @@ func (p *OIDCProvider) EnrichSession(_ context.Context, s *sessions.SessionState
103107
104108// ValidateSession checks that the session's IDToken is still valid
105109func (p *OIDCProvider) ValidateSession(ctx context.Context, s *sessions.SessionState) bool {
110+ ctx = oidc.ClientContext(ctx, requests.DefaultHTTPClient)
106111 _, err := p.Verifier.Verify(ctx, s.IDToken)
107112 if err != nil {
108113 logger.Errorf("id_token verification failed: %v", err)
@@ -127,6 +132,7 @@ func (p *OIDCProvider) RefreshSession(ctx context.Context, s *sessions.SessionSt
127132 return false, nil
128133 }
129134
135+ ctx = oidc.ClientContext(ctx, requests.DefaultHTTPClient)
130136 err := p.redeemRefreshToken(ctx, s)
131137 if err != nil {
132138 return false, fmt.Errorf("unable to redeem refresh token: %v", err)
@@ -185,6 +191,7 @@ func (p *OIDCProvider) redeemRefreshToken(ctx context.Context, s *sessions.Sessi
185191
186192// CreateSessionFromToken converts Bearer IDTokens into sessions
187193func (p *OIDCProvider) CreateSessionFromToken(ctx context.Context, token string) (*sessions.SessionState, error) {
194+ ctx = oidc.ClientContext(ctx, requests.DefaultHTTPClient)
188195 idToken, err := p.Verifier.Verify(ctx, token)
189196 if err != nil {
190197 return nil, err