Skip to content

Commit d75d035

Browse files
committed
feat: add user-agent to client requests
Signed-off-by: Antonio Murdaca <[email protected]>
1 parent d88147c commit d75d035

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

.packit.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ actions:
3737
- make vendor-tarball VERSION=${PACKIT_PROJECT_VERSION}
3838
create-archive:
3939
- make packit-create-archive VERSION=${PACKIT_PROJECT_VERSION}
40-
fix-spec-file:
41-
- bash -c "sed -i -r \"s/Version:(\s*)\S+/Version:\1${PACKIT_PROJECT_VERSION}%{?dist}/\" build/package/rpm/go-fdo-client.spec"
4240

4341
jobs:
4442
- &copr_fedora

Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
COMMIT := $(shell git rev-parse --short HEAD)
22
DATE := $(shell date "+%Y%m%d")
3+
VERSION := git$(DATE).$(COMMIT)
34
PROJECT := go-fdo-client
45

56
SOURCEDIR := $(CURDIR)/build/package/rpm
67
SPEC_FILE_NAME := $(PROJECT).spec
78
SPEC_FILE := $(SOURCEDIR)/$(SPEC_FILE_NAME)
89
GO_VENDOR_TOOLS_FILE := $(SOURCEDIR)/go-vendor-tools.toml
910
GO_VENDOR_TOOLS_FILE_NAME := go-vendor-tools.toml
10-
VERSION ?= $(shell git describe --always --dirty --tags 2>/dev/null || echo "undefined")
1111

1212
SOURCE_TARBALL := $(SOURCEDIR)/$(PROJECT)-$(VERSION).tar.gz
1313
VENDOR_TARBALL := $(SOURCEDIR)/$(PROJECT)-$(VERSION)-vendor.tar.gz
@@ -17,7 +17,7 @@ VENDOR_TARBALL := $(SOURCEDIR)/$(PROJECT)-$(VERSION)-vendor.tar.gz
1717
all: build test
1818

1919
build: tidy fmt vet
20-
go build -ldflags="-X github.com/fido-device-onboard/go-fdo-client/internal/version/version.VERSION=${VERSION}"
20+
go build -ldflags="-X github.com/fido-device-onboard/go-fdo-client/internal/version.VERSION=${VERSION}"
2121

2222
tidy:
2323
go mod tidy
@@ -32,7 +32,7 @@ test:
3232
go test -v ./...
3333

3434
# Packit helpers
35-
.PHONY: vendor-tarball packit-create-archive vendor-licenses render-specfile
35+
.PHONY: vendor-tarball packit-create-archive vendor-licenses
3636

3737
vendor-tarball: $(VENDOR_TARBALL)
3838

@@ -81,7 +81,6 @@ RPMBUILD_SPECFILE := $(RPMBUILD_SPECS_DIR)/$(PROJECT)-$(VERSION)
8181
RPMBUILD_TARBALL := $(RPMBUILD_SOURCES_DIR)/$(PROJECT)-$(VERSION).tar.gz
8282
RPMBUILD_VENDOR_TARBALL := $(RPMBUILD_SOURCES_DIR)/$(PROJECT)-$(VERSION)-vendor.tar.gz
8383

84-
render-specfile: $(RPMBUILD_SPECFILE)
8584
# Render a versioned spec into ./rpmbuild/specs (keeps source spec pristine)
8685
$(RPMBUILD_SPECFILE):
8786
mkdir -p $(RPMBUILD_SPECS_DIR)

build/package/rpm/go-fdo-client.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ BuildRequires: golang
3939

4040
%build
4141
%global gomodulesmode GO111MODULE=on
42-
export LDFLAGS="-X %{goipath}/internal/version/version.VERSION=%{version}"
42+
export LDFLAGS="-X %{goipath}/internal/version.VERSION=%{version}"
4343
%gobuild -o %{gobuilddir}/bin/go-fdo-client %{goipath}
4444

4545
%install

internal/tls/tls.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/fido-device-onboard/go-fdo"
13+
"github.com/fido-device-onboard/go-fdo-client/internal/version"
1314
"github.com/fido-device-onboard/go-fdo/http"
1415
)
1516

@@ -29,7 +30,7 @@ func TlsTransport(baseURL string, conf *tls.Config, insecureTLS bool) fdo.Transp
2930

3031
return &http.Transport{
3132
BaseURL: baseURL,
32-
Client: &net_http.Client{Transport: &net_http.Transport{
33+
Client: &net_http.Client{Transport: &userAgentTransport{&net_http.Transport{
3334
Proxy: net_http.ProxyFromEnvironment,
3435
DialContext: (&net.Dialer{
3536
Timeout: 30 * time.Second,
@@ -41,6 +42,22 @@ func TlsTransport(baseURL string, conf *tls.Config, insecureTLS bool) fdo.Transp
4142
TLSClientConfig: conf,
4243
TLSHandshakeTimeout: 10 * time.Second,
4344
ExpectContinueTimeout: 1 * time.Second,
44-
}},
45+
}, "go-fdo-client/" + version.VERSION}},
4546
}
4647
}
48+
49+
type userAgentTransport struct {
50+
next net_http.RoundTripper
51+
userAgent string
52+
}
53+
54+
func (t *userAgentTransport) RoundTrip(req *net_http.Request) (*net_http.Response, error) {
55+
r := req.Clone(req.Context())
56+
if r.Header == nil {
57+
r.Header = make(net_http.Header)
58+
}
59+
if r.Header.Get("User-Agent") == "" {
60+
r.Header.Set("User-Agent", t.userAgent)
61+
}
62+
return t.next.RoundTrip(r)
63+
}

internal/version/version.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package version
22

3+
// VERSION is the build version, set at build time using -ldflags.
34
var VERSION = "unknown"

0 commit comments

Comments
 (0)