Skip to content

Commit 2d13638

Browse files
authored
[VC-36032] Set User-Agent header containing the agent version in all HTTP requests (#631)
* Allow cancellation of the data upload request when using Venafi Cloud with private key authentication * Set User-Agent header containing the agent version in all HTTP requests This will make it easier to diagnose problems by allowing platform teams to parse HTTP server logs or intermediate HTTP proxy logs and know which version of the agent has made the request. * Fix the config tests Signed-off-by: Richard Wall <richard.wall@venafi.com>
1 parent 98afe3b commit 2d13638

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

pkg/agent/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
178178

179179
// The log line printed by pflag is not captured by the log recorder.
180180
assert.Equal(t, testutil.Undent(`
181-
INFO Using the Jetstack Secure OAuth auth mode since --credentials-file was specified without --venafi-cloud.
181+
INFO Authentication mode mode="Jetstack Secure OAuth" reason="--credentials-file was specified without --venafi-cloud"
182182
INFO Using period from config period="1h0m0s"
183183
`), b.String())
184184
})

pkg/client/client_api_token.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/jetstack/preflight/api"
14+
"github.com/jetstack/preflight/pkg/version"
1415
"k8s.io/client-go/transport"
1516
)
1617

@@ -90,6 +91,7 @@ func (c *APITokenClient) Post(ctx context.Context, path string, body io.Reader)
9091

9192
req.Header.Set("Content-Type", "application/json")
9293
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiToken))
94+
req.Header.Set("User-Agent", fmt.Sprintf("venafi-kubernetes-agent/%s", version.PreflightVersion))
9395

9496
return c.client.Do(req)
9597
}

pkg/client/client_oauth.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/client-go/transport"
1818

1919
"github.com/jetstack/preflight/api"
20+
"github.com/jetstack/preflight/pkg/version"
2021
)
2122

2223
type (
@@ -151,6 +152,7 @@ func (c *OAuthClient) Post(ctx context.Context, path string, body io.Reader) (*h
151152
}
152153

153154
req.Header.Set("Content-Type", "application/json")
155+
req.Header.Set("User-Agent", fmt.Sprintf("venafi-kubernetes-agent/%s", version.PreflightVersion))
154156

155157
if len(token.bearer) > 0 {
156158
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.bearer))
@@ -188,6 +190,7 @@ func (c *OAuthClient) renewAccessToken(ctx context.Context) error {
188190
return errors.WithStack(err)
189191
}
190192
req.Header.Add("content-type", "application/x-www-form-urlencoded")
193+
req.Header.Set("User-Agent", fmt.Sprintf("venafi-kubernetes-agent/%s", version.PreflightVersion))
191194

192195
res, err := http.DefaultClient.Do(req)
193196
if err != nil {

pkg/client/client_venafi_cloud.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/client-go/transport"
3131

3232
"github.com/jetstack/preflight/api"
33+
"github.com/jetstack/preflight/pkg/version"
3334
)
3435

3536
type (
@@ -265,13 +266,14 @@ func (c *VenafiCloudClient) Post(ctx context.Context, path string, body io.Reade
265266
return nil, err
266267
}
267268

268-
req, err := http.NewRequest(http.MethodPost, fullURL(c.baseURL, path), body)
269+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL(c.baseURL, path), body)
269270
if err != nil {
270271
return nil, err
271272
}
272273

273274
req.Header.Set("Accept", "application/json")
274275
req.Header.Set("Content-Type", "application/json")
276+
req.Header.Set("User-Agent", fmt.Sprintf("venafi-kubernetes-agent/%s", version.PreflightVersion))
275277

276278
if len(token.accessToken) > 0 {
277279
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.accessToken))
@@ -314,6 +316,7 @@ func (c *VenafiCloudClient) updateAccessToken(ctx context.Context) error {
314316

315317
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
316318
request.Header.Add("Content-Length", strconv.Itoa(len(encoded)))
319+
request.Header.Set("User-Agent", fmt.Sprintf("venafi-kubernetes-agent/%s", version.PreflightVersion))
317320

318321
now := time.Now()
319322
accessToken := accessTokenInformation{}

0 commit comments

Comments
 (0)