Skip to content

Commit 98afe3b

Browse files
authored
[VC-36032] Pass the context to Venafi clients and enable debug roundtripper (#627)
* Pass the context to the Venafi clients * Add the client-go debug round tripper to the venafi clients * Add a note about HTTP request logging to the logging flags * Add a note about setting logging-format and log-level to the Helm chart values documentation. Signed-off-by: Richard Wall <[email protected]>
1 parent 8e6110a commit 98afe3b

File tree

15 files changed

+120
-74
lines changed

15 files changed

+120
-74
lines changed

deploy/charts/venafi-kubernetes-agent/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,18 @@ Specify the command to run overriding default binary.
265265
> []
266266
> ```
267267
268-
Specify additional arguments to pass to the agent binary.
269-
Example: `["--strict", "--oneshot"]`
268+
Specify additional arguments to pass to the agent binary. For example, to enable JSON logging use `--logging-format`, or to increase the logging verbosity use `--log-level`.
269+
The log levels are: 0=Info, 1=Debug, 2=Trace.
270+
Use 6-9 for increasingly verbose HTTP request logging.
271+
The default log level is 0.
272+
273+
Example:
274+
275+
```yaml
276+
extraArgs:
277+
- --logging-format=json
278+
- --log-level=6 # To enable HTTP request logging
279+
```
270280
#### **volumes** ~ `array`
271281
> Default value:
272282
> ```yaml

deploy/charts/venafi-kubernetes-agent/values.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@
308308
},
309309
"helm-values.extraArgs": {
310310
"default": [],
311-
"description": "Specify additional arguments to pass to the agent binary.\nExample: `[\"--strict\", \"--oneshot\"]`",
311+
"description": "Specify additional arguments to pass to the agent binary. For example, to enable JSON logging use `--logging-format`, or to increase the logging verbosity use `--log-level`.\nThe log levels are: 0=Info, 1=Debug, 2=Trace.\nUse 6-9 for increasingly verbose HTTP request logging.\nThe default log level is 0.\n\nExample:\nextraArgs:\n- --logging-format=json\n- --log-level=6 # To enable HTTP request logging",
312312
"items": {},
313313
"type": "array"
314314
},

deploy/charts/venafi-kubernetes-agent/values.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,16 @@ affinity: {}
146146
command: []
147147

148148
# Specify additional arguments to pass to the agent binary.
149-
# Example: `["--strict", "--oneshot"]`
149+
# For example, to enable JSON logging use `--logging-format`, or
150+
# to increase the logging verbosity use `--log-level`.
151+
# The log levels are: 0=Info, 1=Debug, 2=Trace.
152+
# Use 6-9 for increasingly verbose HTTP request logging.
153+
# The default log level is 0.
154+
#
155+
# Example:
156+
# extraArgs:
157+
# - --logging-format=json
158+
# - --log-level=6 # To enable HTTP request logging
150159
extraArgs: []
151160

152161
# Additional volumes to add to the Venafi Kubernetes Agent container. This is

hack/e2e/values.venafi-kubernetes-agent.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ authentication:
1010

1111
extraArgs:
1212
- --logging-format=json
13-
- --log-level=2
13+
- --log-level=6

pkg/agent/config_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/spf13/cobra"
1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"
15+
"k8s.io/klog/v2"
1516
"k8s.io/klog/v2/ktesting"
1617

1718
"github.com/jetstack/preflight/pkg/client"
@@ -620,6 +621,12 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
620621
func Test_ValidateAndCombineConfig_VenafiCloudKeyPair(t *testing.T) {
621622
t.Run("server, uploader_id, and cluster name are correctly passed", func(t *testing.T) {
622623
t.Setenv("POD_NAMESPACE", "venafi")
624+
625+
ctx, cancel := context.WithCancel(context.Background())
626+
defer cancel()
627+
log := ktesting.NewLogger(t, ktesting.NewConfig(ktesting.Verbosity(10)))
628+
ctx = klog.NewContext(ctx, log)
629+
623630
srv, cert, setVenafiCloudAssert := testutil.FakeVenafiCloud(t)
624631
setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) {
625632
// Only care about /v1/tlspk/upload/clusterdata/:uploader_id?name=
@@ -648,7 +655,7 @@ func Test_ValidateAndCombineConfig_VenafiCloudKeyPair(t *testing.T) {
648655
testutil.TrustCA(t, cl, cert)
649656
assert.Equal(t, VenafiCloudKeypair, got.AuthMode)
650657

651-
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: "test cluster name"})
658+
err = cl.PostDataReadingsWithOptions(ctx, nil, client.Options{ClusterName: "test cluster name"})
652659
require.NoError(t, err)
653660
})
654661
}
@@ -724,6 +731,11 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
724731
})
725732

726733
t.Run("the server field is ignored when VenafiConnection is used", func(t *testing.T) {
734+
ctx, cancel := context.WithCancel(context.Background())
735+
defer cancel()
736+
log := ktesting.NewLogger(t, ktesting.NewConfig(ktesting.Verbosity(10)))
737+
ctx = klog.NewContext(ctx, log)
738+
727739
expected := srv.URL
728740
setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) {
729741
assert.Equal(t, expected, "https://"+gotReq.Host)
@@ -738,13 +750,13 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
738750
withCmdLineFlags("--venafi-connection", "venafi-components", "--install-namespace", "venafi"))
739751
require.NoError(t, err)
740752

741-
testutil.VenConnStartWatching(t, cl)
753+
testutil.VenConnStartWatching(ctx, t, cl)
742754
testutil.TrustCA(t, cl, cert)
743755

744756
// TODO(mael): the client should keep track of the cluster ID, we
745757
// shouldn't need to pass it as an option to
746758
// PostDataReadingsWithOptions.
747-
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: cfg.ClusterID})
759+
err = cl.PostDataReadingsWithOptions(ctx, nil, client.Options{ClusterName: cfg.ClusterID})
748760
require.NoError(t, err)
749761
})
750762
}

pkg/agent/run.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
231231
// If any of the go routines exit (with nil or error) the main context will
232232
// be cancelled, which will cause this blocking loop to exit
233233
// instead of waiting for the time period.
234-
// TODO(wallrj): Pass a context to gatherAndOutputData, so that we don't
235-
// have to wait for it to finish before exiting the process.
236234
for {
237235
if err := gatherAndOutputData(klog.NewContext(ctx, log), eventf, config, preflightClient, dataGatherers); err != nil {
238236
return err
@@ -397,9 +395,7 @@ func postData(ctx context.Context, config CombinedConfig, preflightClient client
397395

398396
if config.AuthMode == VenafiCloudKeypair || config.AuthMode == VenafiCloudVenafiConnection {
399397
// orgID and clusterID are not required for Venafi Cloud auth
400-
// TODO(wallrj): Pass the context to PostDataReadingsWithOptions, so
401-
// that its network operations can be cancelled.
402-
err := preflightClient.PostDataReadingsWithOptions(readings, client.Options{
398+
err := preflightClient.PostDataReadingsWithOptions(ctx, readings, client.Options{
403399
ClusterName: config.ClusterID,
404400
ClusterDescription: config.ClusterDescription,
405401
})
@@ -427,9 +423,7 @@ func postData(ctx context.Context, config CombinedConfig, preflightClient client
427423
if path == "" {
428424
path = "/api/v1/datareadings"
429425
}
430-
// TODO(wallrj): Pass the context to Post, so that its network
431-
// operations can be cancelled.
432-
res, err := preflightClient.Post(path, bytes.NewBuffer(data))
426+
res, err := preflightClient.Post(ctx, path, bytes.NewBuffer(data))
433427

434428
if err != nil {
435429
return fmt.Errorf("failed to post data: %+v", err)
@@ -453,9 +447,7 @@ func postData(ctx context.Context, config CombinedConfig, preflightClient client
453447
return fmt.Errorf("post to server failed: missing clusterID from agent configuration")
454448
}
455449

456-
// TODO(wallrj): Pass the context to PostDataReadings, so
457-
// that its network operations can be cancelled.
458-
err := preflightClient.PostDataReadings(config.OrganizationID, config.ClusterID, readings)
450+
err := preflightClient.PostDataReadings(ctx, config.OrganizationID, config.ClusterID, readings)
459451
if err != nil {
460452
return fmt.Errorf("post to server failed: %+v", err)
461453
}

pkg/client/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package client
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"net/http"
@@ -29,9 +30,9 @@ type (
2930

3031
// The Client interface describes types that perform requests against the Jetstack Secure backend.
3132
Client interface {
32-
PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error
33-
PostDataReadingsWithOptions(readings []*api.DataReading, options Options) error
34-
Post(path string, body io.Reader) (*http.Response, error)
33+
PostDataReadings(ctx context.Context, orgID, clusterID string, readings []*api.DataReading) error
34+
PostDataReadingsWithOptions(ctx context.Context, readings []*api.DataReading, options Options) error
35+
Post(ctx context.Context, path string, body io.Reader) (*http.Response, error)
3536
}
3637

3738
// The Credentials interface describes methods for credential types to implement for verification.

pkg/client/client_api_token.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -10,6 +11,7 @@ import (
1011
"time"
1112

1213
"github.com/jetstack/preflight/api"
14+
"k8s.io/client-go/transport"
1315
)
1416

1517
type (
@@ -34,19 +36,22 @@ func NewAPITokenClient(agentMetadata *api.AgentMetadata, apiToken, baseURL strin
3436
apiToken: apiToken,
3537
agentMetadata: agentMetadata,
3638
baseURL: baseURL,
37-
client: &http.Client{Timeout: time.Minute},
39+
client: &http.Client{
40+
Timeout: time.Minute,
41+
Transport: transport.DebugWrappers(http.DefaultTransport),
42+
},
3843
}, nil
3944
}
4045

4146
// PostDataReadingsWithOptions uploads the slice of api.DataReading to the Jetstack Secure backend to be processed for later
4247
// viewing in the user-interface.
43-
func (c *APITokenClient) PostDataReadingsWithOptions(readings []*api.DataReading, opts Options) error {
44-
return c.PostDataReadings(opts.OrgID, opts.ClusterID, readings)
48+
func (c *APITokenClient) PostDataReadingsWithOptions(ctx context.Context, readings []*api.DataReading, opts Options) error {
49+
return c.PostDataReadings(ctx, opts.OrgID, opts.ClusterID, readings)
4550
}
4651

4752
// PostDataReadings uploads the slice of api.DataReading to the Jetstack Secure backend to be processed for later
4853
// viewing in the user-interface.
49-
func (c *APITokenClient) PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error {
54+
func (c *APITokenClient) PostDataReadings(ctx context.Context, orgID, clusterID string, readings []*api.DataReading) error {
5055
payload := api.DataReadingsPost{
5156
AgentMetadata: c.agentMetadata,
5257
DataGatherTime: time.Now().UTC(),
@@ -57,7 +62,7 @@ func (c *APITokenClient) PostDataReadings(orgID, clusterID string, readings []*a
5762
return err
5863
}
5964

60-
res, err := c.Post(filepath.Join("/api/v1/org", orgID, "datareadings", clusterID), bytes.NewBuffer(data))
65+
res, err := c.Post(ctx, filepath.Join("/api/v1/org", orgID, "datareadings", clusterID), bytes.NewBuffer(data))
6166
if err != nil {
6267
return err
6368
}
@@ -77,8 +82,8 @@ func (c *APITokenClient) PostDataReadings(orgID, clusterID string, readings []*a
7782
}
7883

7984
// Post performs an HTTP POST request.
80-
func (c *APITokenClient) Post(path string, body io.Reader) (*http.Response, error) {
81-
req, err := http.NewRequest(http.MethodPost, fullURL(c.baseURL, path), body)
85+
func (c *APITokenClient) Post(ctx context.Context, path string, body io.Reader) (*http.Response, error) {
86+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL(c.baseURL, path), body)
8287
if err != nil {
8388
return nil, err
8489
}

pkg/client/client_oauth.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -13,6 +14,7 @@ import (
1314

1415
"github.com/hashicorp/go-multierror"
1516
"github.com/pkg/errors"
17+
"k8s.io/client-go/transport"
1618

1719
"github.com/jetstack/preflight/api"
1820
)
@@ -93,17 +95,20 @@ func NewOAuthClient(agentMetadata *api.AgentMetadata, credentials *OAuthCredenti
9395
credentials: credentials,
9496
baseURL: baseURL,
9597
accessToken: &accessToken{},
96-
client: &http.Client{Timeout: time.Minute},
98+
client: &http.Client{
99+
Timeout: time.Minute,
100+
Transport: transport.DebugWrappers(http.DefaultTransport),
101+
},
97102
}, nil
98103
}
99104

100-
func (c *OAuthClient) PostDataReadingsWithOptions(readings []*api.DataReading, opts Options) error {
101-
return c.PostDataReadings(opts.OrgID, opts.ClusterID, readings)
105+
func (c *OAuthClient) PostDataReadingsWithOptions(ctx context.Context, readings []*api.DataReading, opts Options) error {
106+
return c.PostDataReadings(ctx, opts.OrgID, opts.ClusterID, readings)
102107
}
103108

104109
// PostDataReadings uploads the slice of api.DataReading to the Jetstack Secure backend to be processed for later
105110
// viewing in the user-interface.
106-
func (c *OAuthClient) PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error {
111+
func (c *OAuthClient) PostDataReadings(ctx context.Context, orgID, clusterID string, readings []*api.DataReading) error {
107112
payload := api.DataReadingsPost{
108113
AgentMetadata: c.agentMetadata,
109114
DataGatherTime: time.Now().UTC(),
@@ -114,7 +119,7 @@ func (c *OAuthClient) PostDataReadings(orgID, clusterID string, readings []*api.
114119
return err
115120
}
116121

117-
res, err := c.Post(filepath.Join("/api/v1/org", orgID, "datareadings", clusterID), bytes.NewBuffer(data))
122+
res, err := c.Post(ctx, filepath.Join("/api/v1/org", orgID, "datareadings", clusterID), bytes.NewBuffer(data))
118123
if err != nil {
119124
return err
120125
}
@@ -134,13 +139,13 @@ func (c *OAuthClient) PostDataReadings(orgID, clusterID string, readings []*api.
134139
}
135140

136141
// Post performs an HTTP POST request.
137-
func (c *OAuthClient) Post(path string, body io.Reader) (*http.Response, error) {
138-
token, err := c.getValidAccessToken()
142+
func (c *OAuthClient) Post(ctx context.Context, path string, body io.Reader) (*http.Response, error) {
143+
token, err := c.getValidAccessToken(ctx)
139144
if err != nil {
140145
return nil, err
141146
}
142147

143-
req, err := http.NewRequest(http.MethodPost, fullURL(c.baseURL, path), body)
148+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL(c.baseURL, path), body)
144149
if err != nil {
145150
return nil, err
146151
}
@@ -157,9 +162,9 @@ func (c *OAuthClient) Post(path string, body io.Reader) (*http.Response, error)
157162
// getValidAccessToken returns a valid access token. It will fetch a new access
158163
// token from the auth server in case the current access token does not exist
159164
// or it is expired.
160-
func (c *OAuthClient) getValidAccessToken() (*accessToken, error) {
165+
func (c *OAuthClient) getValidAccessToken(ctx context.Context) (*accessToken, error) {
161166
if c.accessToken.needsRenew() {
162-
err := c.renewAccessToken()
167+
err := c.renewAccessToken(ctx)
163168
if err != nil {
164169
return nil, err
165170
}
@@ -168,7 +173,7 @@ func (c *OAuthClient) getValidAccessToken() (*accessToken, error) {
168173
return c.accessToken, nil
169174
}
170175

171-
func (c *OAuthClient) renewAccessToken() error {
176+
func (c *OAuthClient) renewAccessToken(ctx context.Context) error {
172177
tokenURL := fmt.Sprintf("https://%s/oauth/token", c.credentials.AuthServerDomain)
173178
audience := "https://preflight.jetstack.io/api/v1"
174179
payload := url.Values{}
@@ -178,7 +183,7 @@ func (c *OAuthClient) renewAccessToken() error {
178183
payload.Set("audience", audience)
179184
payload.Set("username", c.credentials.UserID)
180185
payload.Set("password", c.credentials.UserSecret)
181-
req, err := http.NewRequest("POST", tokenURL, strings.NewReader(payload.Encode()))
186+
req, err := http.NewRequestWithContext(ctx, "POST", tokenURL, strings.NewReader(payload.Encode()))
182187
if err != nil {
183188
return errors.WithStack(err)
184189
}

0 commit comments

Comments
 (0)