Skip to content
Open
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
13 changes: 13 additions & 0 deletions pkg/client/client_venafi_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ func (c *VenafiCloudClient) PostDataReadingsWithOptions(ctx context.Context, rea
defer res.Body.Close()

if code := res.StatusCode; code < 200 || code >= 300 {
if code == http.StatusUnauthorized {
// The token may be stale, for example after the cluster moved to another sub-TSG.
// Drop it so that the next attempt fetches a new token instead of reusing this one.
c.invalidateAccessToken()
}
errorContent := ""
body, err := io.ReadAll(res.Body)
if err == nil {
Expand Down Expand Up @@ -271,6 +276,14 @@ func (c *VenafiCloudClient) getValidAccessToken(ctx context.Context) (*venafiClo
return token, nil
}

// invalidateAccessToken discards the cached access token so that the next
// request fetches a new one.
func (c *VenafiCloudClient) invalidateAccessToken() {
c.lock.Lock()
c.accessToken = &venafiCloudAccessToken{}
c.lock.Unlock()
}

func (c *VenafiCloudClient) updateAccessToken(ctx context.Context) error {
jwtToken, err := c.generateAndSignJwtToken()
if err != nil {
Expand Down
67 changes: 67 additions & 0 deletions pkg/client/client_venafi_cloud_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package client

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"

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

// A 401 on upload must drop the cached token so the next upload re-authenticates,
// rather than resending the rejected token until it expires on its own.
func TestVenafiCloudClient_ReauthenticatesOn401(t *testing.T) {
keyFile := withFile(t, fakePrivKeyPEM)

var tokenRequests, uploadRequests int
failNextUpload := true

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == accessTokenEndpoint {
tokenRequests++
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(accessTokenInformation{
AccessToken: fmt.Sprintf("token-%d", tokenRequests),
Type: "bearer",
ExpiresIn: 3600,
})
return
}

uploadRequests++
if failNextUpload {
failNextUpload = false
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`unauthorized`))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"status":"success"}`))
}))
defer server.Close()

credentials := &VenafiSvcAccountCredentials{ClientID: "test-client-id", PrivateKeyFile: keyFile}
metadata := &api.AgentMetadata{Version: "test-version", ClusterID: "test-cluster"}

client, err := NewVenafiCloudClient(metadata, credentials, server.URL, "uploader-1", "")
require.NoError(t, err)

readings := []*api.DataReading{{DataGatherer: "g", Timestamp: api.Time{}, Data: &api.DynamicData{}}}
opts := Options{ClusterName: "test-cluster"}

// First upload is rejected with 401. The token (still far from expiry) must be dropped.
err = client.PostDataReadingsWithOptions(t.Context(), readings, opts)
require.Error(t, err)
require.Equal(t, 1, tokenRequests, "one token minted for the first upload")

// Second upload: the dropped token forces a fresh authentication, so it succeeds.
// Without the 401 handling the cached token would be reused (no second token request).
err = client.PostDataReadingsWithOptions(t.Context(), readings, opts)
require.NoError(t, err)
require.Equal(t, 2, tokenRequests, "the 401 forced a re-authentication")
require.Equal(t, 2, uploadRequests)
}
Loading