@@ -3,6 +3,7 @@ package client
33import (
44 "bytes"
55 "compress/gzip"
6+ "context"
67 "crypto"
78 "crypto/ecdsa"
89 "crypto/ed25519"
@@ -172,7 +173,7 @@ func (c *VenafiSvcAccountCredentials) IsClientSet() (ok bool, why string) {
172173
173174// PostDataReadingsWithOptions uploads the slice of api.DataReading to the Venafi Cloud backend to be processed.
174175// The Options are then passed as URL params in the request
175- func (c * VenafiCloudClient ) PostDataReadingsWithOptions (readings []* api.DataReading , opts Options ) error {
176+ func (c * VenafiCloudClient ) PostDataReadingsWithOptions (ctx context. Context , readings []* api.DataReading , opts Options ) error {
176177 payload := api.DataReadingsPost {
177178 AgentMetadata : c .agentMetadata ,
178179 DataGatherTime : time .Now ().UTC (),
@@ -203,7 +204,7 @@ func (c *VenafiCloudClient) PostDataReadingsWithOptions(readings []*api.DataRead
203204 }
204205 venafiCloudUploadURL .RawQuery = query .Encode ()
205206
206- res , err := c .Post (venafiCloudUploadURL .String (), bytes .NewBuffer (data ))
207+ res , err := c .Post (ctx , venafiCloudUploadURL .String (), bytes .NewBuffer (data ))
207208 if err != nil {
208209 return err
209210 }
@@ -223,7 +224,7 @@ func (c *VenafiCloudClient) PostDataReadingsWithOptions(readings []*api.DataRead
223224
224225// PostDataReadings uploads the slice of api.DataReading to the Venafi Cloud backend to be processed for later
225226// viewing in the user-interface.
226- func (c * VenafiCloudClient ) PostDataReadings (_ string , _ string , readings []* api.DataReading ) error {
227+ func (c * VenafiCloudClient ) PostDataReadings (ctx context. Context , _ string , _ string , readings []* api.DataReading ) error {
227228 // orgID and clusterID are ignored in Venafi Cloud auth
228229
229230 payload := api.DataReadingsPost {
@@ -239,7 +240,7 @@ func (c *VenafiCloudClient) PostDataReadings(_ string, _ string, readings []*api
239240 if ! strings .HasSuffix (c .uploadPath , "/" ) {
240241 c .uploadPath = fmt .Sprintf ("%s/" , c .uploadPath )
241242 }
242- 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 ))
243244 if err != nil {
244245 return err
245246 }
@@ -258,8 +259,8 @@ func (c *VenafiCloudClient) PostDataReadings(_ string, _ string, readings []*api
258259}
259260
260261// Post performs an HTTP POST request.
261- func (c * VenafiCloudClient ) Post (path string , body io.Reader ) (* http.Response , error ) {
262- 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 )
263264 if err != nil {
264265 return nil , err
265266 }
@@ -280,7 +281,7 @@ func (c *VenafiCloudClient) Post(path string, body io.Reader) (*http.Response, e
280281 encodedBody = compressed
281282 }
282283
283- req , err := http .NewRequest ( http .MethodPost , fullURL (c .baseURL , path ), encodedBody )
284+ req , err := http .NewRequestWithContext ( ctx , http .MethodPost , fullURL (c .baseURL , path ), encodedBody )
284285 if err != nil {
285286 return nil , err
286287 }
@@ -310,9 +311,9 @@ func (c *VenafiCloudClient) Post(path string, body io.Reader) (*http.Response, e
310311// getValidAccessToken returns a valid access token. It will fetch a new access
311312// token from the auth server in case the current access token does not exist
312313// or it is expired.
313- func (c * VenafiCloudClient ) getValidAccessToken () (* venafiCloudAccessToken , error ) {
314+ func (c * VenafiCloudClient ) getValidAccessToken (ctx context. Context ) (* venafiCloudAccessToken , error ) {
314315 if c .accessToken == nil || time .Now ().Add (time .Minute ).After (c .accessToken .expirationTime ) {
315- err := c .updateAccessToken ()
316+ err := c .updateAccessToken (ctx )
316317 if err != nil {
317318 return nil , err
318319 }
@@ -321,7 +322,7 @@ func (c *VenafiCloudClient) getValidAccessToken() (*venafiCloudAccessToken, erro
321322 return c .accessToken , nil
322323}
323324
324- func (c * VenafiCloudClient ) updateAccessToken () error {
325+ func (c * VenafiCloudClient ) updateAccessToken (ctx context. Context ) error {
325326 jwtToken , err := c .generateAndSignJwtToken ()
326327 if err != nil {
327328 return err
@@ -334,7 +335,7 @@ func (c *VenafiCloudClient) updateAccessToken() error {
334335 tokenURL := fullURL (c .baseURL , accessTokenEndpoint )
335336
336337 encoded := values .Encode ()
337- request , err := http .NewRequest ( http .MethodPost , tokenURL , strings .NewReader (encoded ))
338+ request , err := http .NewRequestWithContext ( ctx , http .MethodPost , tokenURL , strings .NewReader (encoded ))
338339 if err != nil {
339340 return err
340341 }
0 commit comments