Skip to content

Commit

Permalink
[VC-36032] Set User-Agent header containing the agent version in all …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
wallrj authored Nov 29, 2024
1 parent 98afe3b commit 2d13638
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func Test_ValidateAndCombineConfig(t *testing.T) {

// The log line printed by pflag is not captured by the log recorder.
assert.Equal(t, testutil.Undent(`
INFO Using the Jetstack Secure OAuth auth mode since --credentials-file was specified without --venafi-cloud.
INFO Authentication mode mode="Jetstack Secure OAuth" reason="--credentials-file was specified without --venafi-cloud"
INFO Using period from config period="1h0m0s"
`), b.String())
})
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/client_api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/version"
"k8s.io/client-go/transport"
)

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

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

return c.client.Do(req)
}
3 changes: 3 additions & 0 deletions pkg/client/client_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/client-go/transport"

"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/version"
)

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

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

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

res, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/client/client_venafi_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/client-go/transport"

"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/version"
)

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

req, err := http.NewRequest(http.MethodPost, fullURL(c.baseURL, path), body)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL(c.baseURL, path), body)
if err != nil {
return nil, err
}

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

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

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

now := time.Now()
accessToken := accessTokenInformation{}
Expand Down

0 comments on commit 2d13638

Please sign in to comment.