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
This commit is contained in:
Jacob Middag
2024-07-14 21:09:17 +01:00
committed by GitHub
parent 45ec12bcae
commit 3045392c17
11 changed files with 54 additions and 9 deletions
+2
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
k8serrors "k8s.io/apimachinery/pkg/util/errors"
)
@@ -130,6 +131,7 @@ func getVerifierBuilder(ctx context.Context, opts ProviderVerifierOptions) (veri
// newVerifierBuilder returns a function to create a IDToken verifier from an OIDC config.
func newVerifierBuilder(ctx context.Context, issuerURL, jwksURL string, supportedSigningAlgs []string) verifierBuilder {
ctx = oidc.ClientContext(ctx, requests.DefaultHTTPClient)
keySet := oidc.NewRemoteKeySet(ctx, jwksURL)
return func(oidcConfig *oidc.Config) *oidc.IDTokenVerifier {
if len(supportedSigningAlgs) > 0 {
+2 -2
View File
@@ -58,7 +58,7 @@ func (r *builder) WithMethod(method string) Builder {
// WithHeaders replaces the request header map with the given header map.
func (r *builder) WithHeaders(header http.Header) Builder {
r.header = header
r.header = header.Clone()
return r
}
@@ -99,7 +99,7 @@ func (r *builder) do() Result {
}
req.Header = r.header
resp, err := http.DefaultClient.Do(req)
resp, err := DefaultHTTPClient.Do(req)
if err != nil {
r.result = &result{err: fmt.Errorf("error performing request: %v", err)}
return r.result
+3 -1
View File
@@ -8,6 +8,8 @@ import (
"fmt"
"net/http"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version"
"github.com/bitly/go-simplejson"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@@ -19,7 +21,7 @@ var _ = Describe("Builder suite", func() {
baseHeaders := http.Header{
"Accept-Encoding": []string{"gzip"},
"User-Agent": []string{"Go-http-client/1.1"},
"User-Agent": []string{"oauth2-proxy/" + version.VERSION},
}
BeforeEach(func() {
+29
View File
@@ -0,0 +1,29 @@
package requests
import (
"net/http"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version"
)
type userAgentTransport struct {
next http.RoundTripper
userAgent string
}
func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
r := req.Clone(req.Context())
setDefaultUserAgent(r.Header, t.userAgent)
return t.next.RoundTrip(r)
}
var DefaultHTTPClient = &http.Client{Transport: &userAgentTransport{
next: http.DefaultTransport,
userAgent: "oauth2-proxy/" + version.VERSION,
}}
func setDefaultUserAgent(header http.Header, userAgent string) {
if header != nil && len(header.Values("User-Agent")) == 0 {
header.Set("User-Agent", userAgent)
}
}
+4
View File
@@ -0,0 +1,4 @@
package version
// VERSION contains version information
var VERSION = "undefined"