Skip to content

Commit 016e3ee

Browse files
warning message for secret-detection
1 parent 5b84a1e commit 016e3ee

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

internal/commands/scan.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,13 +1064,6 @@ func validateScanTypes(cmd *cobra.Command, jwtWrapper wrappers.JWTWrapper, featu
10641064
userScanTypes = strings.Replace(strings.ToLower(userScanTypes), commonParams.ContainersTypeFlag, commonParams.ContainersType, 1)
10651065
userSCSScanTypes = strings.Replace(strings.ToLower(userSCSScanTypes), commonParams.SCSEnginesFlag, commonParams.ScsType, 1)
10661066

1067-
SCSScanTypes = strings.Split(userSCSScanTypes, ",")
1068-
if slices.Contains(SCSScanTypes, ScsSecretDetectionType) && !allowedEngines[commonParams.EnterpriseSecretsType] {
1069-
keys := reflect.ValueOf(allowedEngines).MapKeys()
1070-
err = errors.Errorf(engineNotAllowed, ScsSecretDetectionType, ScsSecretDetectionType, keys)
1071-
return err
1072-
}
1073-
10741067
scanTypes = strings.Split(userScanTypes, ",")
10751068
for _, scanType := range scanTypes {
10761069
if !allowedEngines[scanType] || (scanType == commonParams.ContainersType && !(containerEngineCLIEnabled.Status)) {
@@ -1079,6 +1072,14 @@ func validateScanTypes(cmd *cobra.Command, jwtWrapper wrappers.JWTWrapper, featu
10791072
return err
10801073
}
10811074
}
1075+
1076+
SCSScanTypes = strings.Split(userSCSScanTypes, ",")
1077+
if slices.Contains(SCSScanTypes, ScsSecretDetectionType) && !allowedEngines[commonParams.EnterpriseSecretsType] {
1078+
keys := reflect.ValueOf(allowedEngines).MapKeys()
1079+
err = errors.Errorf(engineNotAllowed, ScsSecretDetectionType, ScsSecretDetectionType, keys)
1080+
return err
1081+
}
1082+
10821083
} else {
10831084
for k := range allowedEngines {
10841085
if k == commonParams.ContainersType && !(containerEngineCLIEnabled.Status) {

internal/commands/scan_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,3 +1765,60 @@ func TestUploadZip_whenUserNotProvideZip_shouldReturnZipFilePathInFailureCase(t
17651765
assert.Assert(t, strings.Contains(err.Error(), "error from UploadFile"), err.Error())
17661766
assert.Equal(t, zipPath, "failureCase.zip")
17671767
}
1768+
1769+
func TestValidateScanTypes(t *testing.T) {
1770+
tests := []struct {
1771+
name string
1772+
userScanTypes string
1773+
userSCSScanTypes string
1774+
allowedEngines map[string]bool
1775+
containerEngineCLIEnabled bool
1776+
expectedError string
1777+
}{
1778+
{
1779+
name: "No licenses available",
1780+
userScanTypes: "scs",
1781+
userSCSScanTypes: "sast,secret-detection",
1782+
allowedEngines: map[string]bool{"scs": false, "enterprise-secrets": false},
1783+
containerEngineCLIEnabled: true,
1784+
expectedError: "It looks like the \"scs\" scan type does",
1785+
},
1786+
{
1787+
name: "SCS license available, secret-detection not available",
1788+
userScanTypes: "scs",
1789+
userSCSScanTypes: "secret-detection",
1790+
allowedEngines: map[string]bool{"scs": true, "enterprise-secrets": false},
1791+
containerEngineCLIEnabled: true,
1792+
expectedError: "It looks like the \"secret-detection\" scan type does not exist",
1793+
},
1794+
{
1795+
name: "All licenses available",
1796+
userScanTypes: "scs",
1797+
userSCSScanTypes: "secret-detection",
1798+
allowedEngines: map[string]bool{"scs": true, "enterprise-secrets": true},
1799+
containerEngineCLIEnabled: true,
1800+
expectedError: "",
1801+
},
1802+
}
1803+
1804+
for _, tt := range tests {
1805+
t.Run(tt.name, func(t *testing.T) {
1806+
cmd := &cobra.Command{}
1807+
cmd.Flags().String(commonParams.ScanTypes, tt.userScanTypes, "")
1808+
cmd.Flags().String(commonParams.SCSEnginesFlag, tt.userSCSScanTypes, "")
1809+
1810+
jwtWrapper := &mock.JWTMockWrapper{
1811+
CustomGetAllowedEngines: func(featureFlagsWrapper wrappers.FeatureFlagsWrapper) (map[string]bool, error) {
1812+
return tt.allowedEngines, nil
1813+
},
1814+
}
1815+
featureFlagsWrapper := &mock.FeatureFlagsMockWrapper{}
1816+
err := validateScanTypes(cmd, jwtWrapper, featureFlagsWrapper)
1817+
if tt.expectedError != "" {
1818+
assert.ErrorContains(t, err, tt.expectedError)
1819+
} else {
1820+
assert.NilError(t, err)
1821+
}
1822+
})
1823+
}
1824+
}

internal/wrappers/mock/jwt-helper-mock.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import (
77
)
88

99
type JWTMockWrapper struct {
10-
AIEnabled int
10+
AIEnabled int
11+
CustomGetAllowedEngines func(wrappers.FeatureFlagsWrapper) (map[string]bool, error)
1112
}
1213

1314
const AIProtectionDisabled = 1
1415

1516
// GetAllowedEngines mock for tests
16-
func (*JWTMockWrapper) GetAllowedEngines(featureFlagsWrapper wrappers.FeatureFlagsWrapper) (allowedEngines map[string]bool, err error) {
17+
func (j *JWTMockWrapper) GetAllowedEngines(featureFlagsWrapper wrappers.FeatureFlagsWrapper) (allowedEngines map[string]bool, err error) {
18+
if j.CustomGetAllowedEngines != nil {
19+
return j.CustomGetAllowedEngines(featureFlagsWrapper)
20+
}
1721
allowedEngines = make(map[string]bool)
1822
engines := []string{"sast", "iac-security", "sca", "api-security", "containers", "scs"}
1923
for _, value := range engines {

0 commit comments

Comments
 (0)