@@ -2,6 +2,7 @@ package client
22
33import (
44 "bytes"
5+ "context"
56 "crypto"
67 "crypto/ecdsa"
78 "crypto/ed25519"
@@ -26,6 +27,7 @@ import (
2627 "github.com/google/uuid"
2728 "github.com/hashicorp/go-multierror"
2829 "github.com/microcosm-cc/bluemonday"
30+ "k8s.io/client-go/transport"
2931
3032 "github.com/jetstack/preflight/api"
3133)
@@ -111,7 +113,10 @@ func NewVenafiCloudClient(agentMetadata *api.AgentMetadata, credentials *VenafiS
111113 credentials : credentials ,
112114 baseURL : baseURL ,
113115 accessToken : & venafiCloudAccessToken {},
114- Client : & http.Client {Timeout : time .Minute },
116+ Client : & http.Client {
117+ Timeout : time .Minute ,
118+ Transport : transport .DebugWrappers (http .DefaultTransport ),
119+ },
115120 uploaderID : uploaderID ,
116121 uploadPath : uploadPath ,
117122 privateKey : privateKey ,
@@ -168,7 +173,7 @@ func (c *VenafiSvcAccountCredentials) IsClientSet() (ok bool, why string) {
168173
169174// PostDataReadingsWithOptions uploads the slice of api.DataReading to the Venafi Cloud backend to be processed.
170175// The Options are then passed as URL params in the request
171- func (c * VenafiCloudClient ) PostDataReadingsWithOptions (readings []* api.DataReading , opts Options ) error {
176+ func (c * VenafiCloudClient ) PostDataReadingsWithOptions (ctx context. Context , readings []* api.DataReading , opts Options ) error {
172177 payload := api.DataReadingsPost {
173178 AgentMetadata : c .agentMetadata ,
174179 DataGatherTime : time .Now ().UTC (),
@@ -199,7 +204,7 @@ func (c *VenafiCloudClient) PostDataReadingsWithOptions(readings []*api.DataRead
199204 }
200205 venafiCloudUploadURL .RawQuery = query .Encode ()
201206
202- res , err := c .Post (venafiCloudUploadURL .String (), bytes .NewBuffer (data ))
207+ res , err := c .Post (ctx , venafiCloudUploadURL .String (), bytes .NewBuffer (data ))
203208 if err != nil {
204209 return err
205210 }
@@ -219,7 +224,7 @@ func (c *VenafiCloudClient) PostDataReadingsWithOptions(readings []*api.DataRead
219224
220225// PostDataReadings uploads the slice of api.DataReading to the Venafi Cloud backend to be processed for later
221226// viewing in the user-interface.
222- func (c * VenafiCloudClient ) PostDataReadings (_ string , _ string , readings []* api.DataReading ) error {
227+ func (c * VenafiCloudClient ) PostDataReadings (ctx context. Context , _ string , _ string , readings []* api.DataReading ) error {
223228 // orgID and clusterID are ignored in Venafi Cloud auth
224229
225230 payload := api.DataReadingsPost {
@@ -235,7 +240,7 @@ func (c *VenafiCloudClient) PostDataReadings(_ string, _ string, readings []*api
235240 if ! strings .HasSuffix (c .uploadPath , "/" ) {
236241 c .uploadPath = fmt .Sprintf ("%s/" , c .uploadPath )
237242 }
238- res , err := c .Post (filepath .Join (c .uploadPath , c .uploaderID ), bytes .NewBuffer (data ))
243+ res , err := c .Post (ctx , filepath .Join (c .uploadPath , c .uploaderID ), bytes .NewBuffer (data ))
239244 if err != nil {
240245 return err
241246 }
@@ -254,8 +259,8 @@ func (c *VenafiCloudClient) PostDataReadings(_ string, _ string, readings []*api
254259}
255260
256261// Post performs an HTTP POST request.
257- func (c * VenafiCloudClient ) Post (path string , body io.Reader ) (* http.Response , error ) {
258- token , err := c .getValidAccessToken ()
262+ func (c * VenafiCloudClient ) Post (ctx context. Context , path string , body io.Reader ) (* http.Response , error ) {
263+ token , err := c .getValidAccessToken (ctx )
259264 if err != nil {
260265 return nil , err
261266 }
@@ -278,9 +283,9 @@ func (c *VenafiCloudClient) Post(path string, body io.Reader) (*http.Response, e
278283// getValidAccessToken returns a valid access token. It will fetch a new access
279284// token from the auth server in case the current access token does not exist
280285// or it is expired.
281- func (c * VenafiCloudClient ) getValidAccessToken () (* venafiCloudAccessToken , error ) {
286+ func (c * VenafiCloudClient ) getValidAccessToken (ctx context. Context ) (* venafiCloudAccessToken , error ) {
282287 if c .accessToken == nil || time .Now ().Add (time .Minute ).After (c .accessToken .expirationTime ) {
283- err := c .updateAccessToken ()
288+ err := c .updateAccessToken (ctx )
284289 if err != nil {
285290 return nil , err
286291 }
@@ -289,7 +294,7 @@ func (c *VenafiCloudClient) getValidAccessToken() (*venafiCloudAccessToken, erro
289294 return c .accessToken , nil
290295}
291296
292- func (c * VenafiCloudClient ) updateAccessToken () error {
297+ func (c * VenafiCloudClient ) updateAccessToken (ctx context. Context ) error {
293298 jwtToken , err := c .generateAndSignJwtToken ()
294299 if err != nil {
295300 return err
@@ -302,7 +307,7 @@ func (c *VenafiCloudClient) updateAccessToken() error {
302307 tokenURL := fullURL (c .baseURL , accessTokenEndpoint )
303308
304309 encoded := values .Encode ()
305- request , err := http .NewRequest ( http .MethodPost , tokenURL , strings .NewReader (encoded ))
310+ request , err := http .NewRequestWithContext ( ctx , http .MethodPost , tokenURL , strings .NewReader (encoded ))
306311 if err != nil {
307312 return err
308313 }
0 commit comments