Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VC-36032] Set User-Agent header containing the agent version in all HTTP requests #631

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another drive-by fix. This test was introduced in #628 but #625 which altered the logging format (and which was merged afterwards) had not been rebased and did not alter the expected log output in this test.

Copy link
Member Author

@wallrj wallrj Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are not being run on the master branch and the green badge on the README file does not reflect the status of the current branch:

Opened an issue about that: #632

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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a drive-by fix. I missed this in #627

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