Skip to content

Commit 97da1ee

Browse files
get-unique-id-func-refactored
1 parent 7008205 commit 97da1ee

File tree

3 files changed

+95
-85
lines changed

3 files changed

+95
-85
lines changed

internal/wrappers/client.go

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"net/http/httptrace"
1313
"net/url"
1414
"os"
15-
"os/user"
1615
"runtime"
1716
"strings"
1817
"sync"
@@ -21,13 +20,11 @@ import (
2120
applicationErrors "github.com/checkmarx/ast-cli/internal/constants/errors"
2221
"github.com/checkmarx/ast-cli/internal/logger"
2322
"github.com/golang-jwt/jwt/v5"
24-
"github.com/google/uuid"
2523

2624
"github.com/pkg/errors"
2725
"github.com/spf13/viper"
2826

2927
commonParams "github.com/checkmarx/ast-cli/internal/params"
30-
"github.com/checkmarx/ast-cli/internal/wrappers/configuration"
3128
"github.com/checkmarx/ast-cli/internal/wrappers/kerberos"
3229
"github.com/checkmarx/ast-cli/internal/wrappers/ntlm"
3330
)
@@ -984,85 +981,3 @@ func extractAZPFromToken(astToken string) (string, error) {
984981
}
985982
return azp, nil
986983
}
987-
988-
func GetUniqueID() string {
989-
var uniqueID string
990-
isAllowed := false
991-
accessToken, err := GetAccessToken()
992-
if err != nil {
993-
logger.PrintIfVerbose("Failed to get access token")
994-
return ""
995-
}
996-
parser := jwt.NewParser(jwt.WithoutClaimsValidation())
997-
token, _, err := parser.ParseUnverified(accessToken, jwt.MapClaims{})
998-
if err != nil {
999-
logger.PrintIfVerbose("Failed to parse JWT token " + err.Error())
1000-
return ""
1001-
}
1002-
claims, ok := token.Claims.(jwt.MapClaims)
1003-
if !ok {
1004-
logger.PrintIfVerbose("Failed to get JWT claims")
1005-
return ""
1006-
}
1007-
1008-
astLicense, ok := claims["ast-license"].(map[string]interface{})
1009-
if !ok {
1010-
logger.PrintIfVerbose("Failed to get ast-license from claims")
1011-
return ""
1012-
}
1013-
1014-
licenseData, ok := astLicense["LicenseData"].(map[string]interface{})
1015-
if !ok {
1016-
logger.PrintIfVerbose("Failed to get LicenseData from ast-license")
1017-
return ""
1018-
}
1019-
1020-
allowedEngines, ok := licenseData["allowedEngines"].([]interface{})
1021-
if !ok {
1022-
logger.PrintIfVerbose("Failed to get allowedEngines from LicenseData")
1023-
return ""
1024-
}
1025-
1026-
for _, engine := range allowedEngines {
1027-
engineStr, ok := engine.(string)
1028-
if !ok {
1029-
continue
1030-
}
1031-
if strings.EqualFold(engineStr, "Checkmarx Developer Assist") {
1032-
isAllowed = true
1033-
break
1034-
}
1035-
}
1036-
1037-
if !isAllowed {
1038-
logger.PrintIfVerbose("User does not not have permission to standalone dev asists feature")
1039-
return ""
1040-
}
1041-
uniqueID = viper.GetString(commonParams.UniqueIDConfigKey)
1042-
if uniqueID != "" {
1043-
return uniqueID
1044-
}
1045-
logger.PrintIfVerbose("Generating new unique id")
1046-
currentUser, err := user.Current()
1047-
if err != nil {
1048-
logger.PrintIfVerbose("Failed to get user: " + err.Error())
1049-
return ""
1050-
}
1051-
username := currentUser.Username
1052-
username = strings.TrimSpace(username)
1053-
logger.PrintIfVerbose("Username to be used for unique id: " + username)
1054-
if strings.Contains(username, "\\") {
1055-
username = strings.Split(username, "\\")[1]
1056-
}
1057-
uniqueID = uuid.New().String() + "_" + username
1058-
1059-
logger.PrintIfVerbose("Unique id: " + uniqueID)
1060-
viper.Set(commonParams.UniqueIDConfigKey, uniqueID)
1061-
configFilePath, _ := configuration.GetConfigFilePath()
1062-
err = configuration.SafeWriteSingleConfigKeyString(configFilePath, commonParams.UniqueIDConfigKey, uniqueID)
1063-
if err != nil {
1064-
logger.PrintIfVerbose("Failed to write config: " + err.Error())
1065-
return ""
1066-
}
1067-
return uniqueID
1068-
}

internal/wrappers/jwt-helper.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package wrappers
22

33
import (
4+
"os/user"
45
"strings"
56

7+
"github.com/checkmarx/ast-cli/internal/logger"
68
commonParams "github.com/checkmarx/ast-cli/internal/params"
9+
"github.com/checkmarx/ast-cli/internal/wrappers/configuration"
710
"github.com/checkmarx/ast-cli/internal/wrappers/utils"
811
"github.com/golang-jwt/jwt/v5"
12+
"github.com/google/uuid"
913
"github.com/pkg/errors"
14+
"github.com/spf13/viper"
1015
)
1116

1217
// JWTStruct model used to get all jwt fields
@@ -158,3 +163,49 @@ func (*JWTStruct) CheckPermissionByAccessToken(requiredPermission string) (hasPe
158163
}
159164
return permission, nil
160165
}
166+
167+
func GetUniqueID() string {
168+
var uniqueID string
169+
// Check License first
170+
jwtWrapper := NewJwtWrapper()
171+
isAllowed, err := jwtWrapper.IsAllowedEngine("Checkmarx Developer Assist")
172+
if err != nil {
173+
logger.PrintIfVerbose("Failed to check engine allowance: " + err.Error())
174+
return ""
175+
}
176+
if !isAllowed {
177+
logger.PrintIfVerbose("User does not have permission to standalone dev assists feature")
178+
return ""
179+
}
180+
181+
// Check if unique id is already set
182+
uniqueID = viper.GetString(commonParams.UniqueIDConfigKey)
183+
if uniqueID != "" {
184+
return uniqueID
185+
}
186+
187+
// Generate new unique id
188+
logger.PrintIfVerbose("Generating new unique id")
189+
currentUser, err := user.Current()
190+
if err != nil {
191+
logger.PrintIfVerbose("Failed to get user: " + err.Error())
192+
return ""
193+
}
194+
username := currentUser.Username
195+
username = strings.TrimSpace(username)
196+
logger.PrintIfVerbose("Username to be used for unique id: " + username)
197+
if strings.Contains(username, "\\") {
198+
username = strings.Split(username, "\\")[1]
199+
}
200+
uniqueID = uuid.New().String() + "_" + username
201+
202+
logger.PrintIfVerbose("Unique id: " + uniqueID)
203+
viper.Set(commonParams.UniqueIDConfigKey, uniqueID)
204+
configFilePath, _ := configuration.GetConfigFilePath()
205+
err = configuration.SafeWriteSingleConfigKeyString(configFilePath, commonParams.UniqueIDConfigKey, uniqueID)
206+
if err != nil {
207+
logger.PrintIfVerbose("Failed to write config: " + err.Error())
208+
return ""
209+
}
210+
return uniqueID
211+
}

internal/wrappers/jwt-helper_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package wrappers
22

33
import (
4+
"strings"
45
"testing"
56

7+
commonParams "github.com/checkmarx/ast-cli/internal/params"
8+
"github.com/spf13/viper"
69
"gotest.tools/assert"
710
)
811

@@ -71,3 +74,44 @@ func TestGetEnabledEngines(t *testing.T) {
7174
})
7275
}
7376
}
77+
78+
func TestGetUniqueID(t *testing.T) {
79+
// Save original value and restore after test
80+
originalID := viper.GetString(commonParams.UniqueIDConfigKey)
81+
defer viper.Set(commonParams.UniqueIDConfigKey, originalID)
82+
83+
t.Run("returns existing unique ID from config", func(t *testing.T) {
84+
// Setup: set existing ID
85+
existingID := "test-uuid-456_testuser"
86+
viper.Set(commonParams.UniqueIDConfigKey, existingID)
87+
88+
result := GetUniqueID()
89+
90+
if result != "" {
91+
assert.Equal(t, existingID, result)
92+
} else {
93+
t.Skip("Requires valid auth and 'Checkmarx Developer Assist' license")
94+
}
95+
})
96+
97+
t.Run("generates new unique ID when none exists", func(t *testing.T) {
98+
// Setup: clear existing ID
99+
viper.Set(commonParams.UniqueIDConfigKey, "")
100+
101+
result := GetUniqueID()
102+
103+
if result == "" {
104+
t.Skip("Requires valid auth and 'Checkmarx Developer Assist' license")
105+
return
106+
}
107+
108+
// Verify format: UUID_username
109+
assert.Assert(t, strings.Contains(result, "_"), "Should have UUID_username format")
110+
assert.Assert(t, len(result) > 36, "Should contain UUID and username")
111+
112+
// Verify no backslash (Windows domain stripped)
113+
parts := strings.Split(result, "_")
114+
assert.Assert(t, len(parts) >= 2, "Should have at least 2 parts")
115+
assert.Assert(t, !strings.Contains(parts[1], "\\"), "Username should not contain backslash")
116+
})
117+
}

0 commit comments

Comments
 (0)