Skip to content

Commit d8b6ea0

Browse files
User agent (#335)
* chore(deps): bump github.com/docker/distribution Bumps [github.com/docker/distribution](https://github.com/docker/distribution) from 2.8.1+incompatible to 2.8.2+incompatible. - [Release notes](https://github.com/docker/distribution/releases) - [Commits](distribution/distribution@v2.8.1...v2.8.2) --- updated-dependencies: - dependency-name: github.com/docker/distribution dependency-type: indirect ... Signed-off-by: dependabot[bot] <[email protected]> * feat(client): add user agent --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 68f8259 commit d8b6ea0

File tree

6 files changed

+44
-11
lines changed

6 files changed

+44
-11
lines changed

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ builds:
1414
flags:
1515
- -trimpath
1616
ldflags:
17-
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}'
17+
- '-s -w -X github.com/draios/terraform-provider-sysdig/buildinfo.Version={{.Version}} -X main.commit={{.Commit}}'
1818
goos:
1919
- freebsd
2020
- windows

buildinfo/info.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package buildinfo
22

33
var (
4+
Version = "dev"
45
SysdigMonitor bool
56
SysdigSecure bool
67
IBMMonitor bool

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/containerd/stargz-snapshotter/estargz v0.10.1 // indirect
2222
github.com/davecgh/go-spew v1.1.1 // indirect
2323
github.com/docker/cli v20.10.12+incompatible // indirect
24-
github.com/docker/distribution v2.8.1+incompatible // indirect
24+
github.com/docker/distribution v2.8.2+incompatible // indirect
2525
github.com/docker/docker v20.10.24+incompatible // indirect
2626
github.com/docker/docker-credential-helpers v0.6.4 // indirect
2727
github.com/falcosecurity/kilt/pkg v0.0.0-20211102134108-7c97c711c0d2 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ github.com/docker/cli v20.10.12+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hH
295295
github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY=
296296
github.com/docker/distribution v2.7.1-0.20190205005809-0d3efadf0154+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
297297
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
298-
github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68=
299-
github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
298+
github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=
299+
github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
300300
github.com/docker/docker v1.4.2-0.20190924003213-a8608b5b67c7/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
301301
github.com/docker/docker v20.10.12+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
302302
github.com/docker/docker v20.10.24+incompatible h1:Ugvxm7a8+Gz6vqQYQQ2W7GYq5EUPaAiuPgIfVyI3dYE=

sysdig/internal/client/v2/client.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10+
"github.com/draios/terraform-provider-sysdig/buildinfo"
1011
"github.com/hashicorp/go-retryablehttp"
1112
"github.com/jmespath/go-jmespath"
1213
"github.com/spf13/cast"
@@ -18,13 +19,15 @@ import (
1819
)
1920

2021
const (
21-
GetMePath = "/api/users/me"
22-
AuthorizationHeader = "Authorization"
23-
ContentTypeHeader = "Content-Type"
24-
SysdigProviderHeader = "Sysdig-Provider"
25-
SysdigProviderHeaderValue = "Terraform"
26-
ContentTypeJSON = "application/json"
27-
ContentTypeFormURLEncoded = "x-www-form-urlencoded"
22+
GetMePath = "/api/users/me"
23+
UserAgentHeader = "User-Agent"
24+
AuthorizationHeader = "Authorization"
25+
ContentTypeHeader = "Content-Type"
26+
SysdigProviderHeader = "Sysdig-Provider"
27+
SysdigProviderHeaderValue = "Terraform"
28+
SysdigUserAgentHeaderValue = "SysdigTerraform"
29+
ContentTypeJSON = "application/json"
30+
ContentTypeFormURLEncoded = "x-www-form-urlencoded"
2831
)
2932

3033
var (
@@ -92,6 +95,8 @@ func Marshal[T any](data T) (io.Reader, error) {
9295
}
9396

9497
func request(httpClient *http.Client, cfg *config, request *http.Request) (*http.Response, error) {
98+
request.Header.Set(UserAgentHeader, fmt.Sprintf("%s/%s", SysdigUserAgentHeaderValue, buildinfo.Version))
99+
95100
if cfg.extraHeaders != nil {
96101
for key, value := range cfg.extraHeaders {
97102
request.Header.Set(key, value)

sysdig/internal/client/v2/client_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ package v2
44

55
import (
66
"encoding/json"
7+
"fmt"
78
"io"
89
"net/http"
10+
"net/http/httptest"
911
"strings"
1012
"testing"
1113
)
@@ -91,3 +93,28 @@ func TestClient_ErrorFromResponse(t *testing.T) {
9193
t.Errorf("expected err %v, got %v", expected, err)
9294
}
9395
}
96+
97+
func TestRequest(t *testing.T) {
98+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
99+
agent := r.Header.Get(UserAgentHeader)
100+
agentParts := strings.Split(agent, "/")
101+
if len(agentParts) != 2 || agentParts[0] != SysdigUserAgentHeaderValue || agentParts[1] == "" {
102+
t.Errorf("invalid user agent: %v", agent)
103+
}
104+
}))
105+
106+
cfg := &config{
107+
url: server.URL,
108+
}
109+
client := newHTTPClient(cfg)
110+
111+
r, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/", cfg.url), nil)
112+
if err != nil {
113+
t.Errorf("failed to create request, %v", err)
114+
}
115+
116+
_, err = request(client, cfg, r)
117+
if err != nil {
118+
t.Errorf("failed to send request, %v", err)
119+
}
120+
}

0 commit comments

Comments
 (0)