Skip to content

Commit 5cea005

Browse files
Make writeCredentialsToCache func thread safe (AST-82856) (#1045)
* Make writeCredentialsToCache func thread safe * Fix trivyy --------- Co-authored-by: AlvoBen <[email protected]>
1 parent 9c5586b commit 5cea005

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM checkmarx/bash:5.2.37-r2-cbecd9aeaadc77@sha256:cbecd9aeaadc775906af3b4b0b03e05d5a4e68cb300d7db4579d88129b2eb028
1+
FROM checkmarx/bash:5.2.37-r2-c5dcfc6a2fbe1c@sha256:c5dcfc6a2fbe1c8f9d11bdf902b5485bb78b4733864a99806749d5e244a6b75e
22
USER nonroot
33

44
COPY cx /app/bin/cx

internal/wrappers/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http/httptrace"
1313
"net/url"
1414
"strings"
15+
"sync"
1516
"time"
1617

1718
applicationErrors "github.com/checkmarx/ast-cli/internal/constants/errors"
@@ -44,6 +45,10 @@ const (
4445
jsonContentType = "application/json"
4546
)
4647

48+
var (
49+
credentialsMutex sync.Mutex
50+
)
51+
4752
type ClientCredentialsInfo struct {
4853
AccessToken string `json:"access_token"`
4954
ExpiresIn int `json:"expires_in"`
@@ -478,6 +483,9 @@ func getClientCredentialsFromCache(tokenExpirySeconds int) string {
478483
}
479484

480485
func writeCredentialsToCache(accessToken string) {
486+
credentialsMutex.Lock()
487+
defer credentialsMutex.Unlock()
488+
481489
logger.PrintIfVerbose("Storing API access token to cache.")
482490
viper.Set(commonParams.AstToken, accessToken)
483491
cachedAccessToken = accessToken

internal/wrappers/client_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ package wrappers
22

33
import (
44
"errors"
5-
"github.com/stretchr/testify/assert"
5+
"fmt"
66
"net/http"
7+
"strconv"
8+
"strings"
9+
"sync"
710
"testing"
811
"time"
12+
13+
commonParams "github.com/checkmarx/ast-cli/internal/params"
14+
"github.com/spf13/viper"
15+
"github.com/stretchr/testify/assert"
916
)
1017

1118
type mockReadCloser struct{}
@@ -78,3 +85,31 @@ func TestRetryHTTPRequest_EndWithBadGateway(t *testing.T) {
7885
assert.NotNil(t, resp)
7986
assert.Equal(t, http.StatusBadGateway, resp.StatusCode)
8087
}
88+
89+
func TestConcurrentWriteCredentialsToCache(t *testing.T) {
90+
var wg sync.WaitGroup
91+
92+
for i := 0; i < 1000; i++ {
93+
wg.Add(1)
94+
go func(i int) {
95+
defer wg.Done()
96+
writeCredentialsToCache(fmt.Sprintf("testToken_%d", i))
97+
}(i)
98+
}
99+
wg.Wait()
100+
101+
token := viper.Get(commonParams.AstToken)
102+
assert.NotNil(t, token, "Token should not be nil")
103+
104+
tokenStr, ok := token.(string)
105+
assert.True(t, ok, "Token should be a string")
106+
107+
splitToken := strings.Split(tokenStr, "_")
108+
assert.Equal(t, 2, len(splitToken), "Token should split into 2 parts")
109+
assert.Equal(t, "testToken", splitToken[0], "Token prefix should be 'testToken'")
110+
111+
testTokenNumber, err := strconv.Atoi(splitToken[1])
112+
assert.NoError(t, err, "The token suffix should be a valid number")
113+
assert.True(t, testTokenNumber >= 0 && testTokenNumber < 1000,
114+
"The token number should be within the expected range")
115+
}

0 commit comments

Comments
 (0)