Skip to content

Commit ce1c567

Browse files
pre receive validate command
1 parent 5d1c48e commit ce1c567

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

internal/commands/pre-receive.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package commands
22

33
import (
4+
"fmt"
45
prereceive "github.com/Checkmarx/secret-detection/pkg/hooks/pre-receive"
56
"github.com/MakeNowJust/heredoc"
7+
"github.com/checkmarx/ast-cli/internal/params"
68
"github.com/checkmarx/ast-cli/internal/wrappers"
79
"github.com/spf13/cobra"
10+
"log"
11+
)
12+
13+
const (
14+
SuccessFullSecretsLicenceValidation = "Successfully Validated the Enterprise Secrets licence!"
815
)
916

1017
func PreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command {
@@ -18,12 +25,13 @@ func PreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command {
1825
`,
1926
),
2027
}
21-
preReceiveCmd.AddCommand(scanSecretsPreReceiveCommand(jwtWrapper))
28+
preReceiveCmd.AddCommand(scanSecretsPreReceiveCommand())
29+
preReceiveCmd.AddCommand(validateSecretsLicence(jwtWrapper))
2230

2331
return preReceiveCmd
2432
}
2533

26-
func scanSecretsPreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command {
34+
func scanSecretsPreReceiveCommand() *cobra.Command {
2735
var configFile string
2836
scanPrereceiveCmd := &cobra.Command{
2937
Use: "secrets-scan",
@@ -35,9 +43,6 @@ func scanSecretsPreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command
3543
$ cx hooks pre-receive secrets-scan --config /path/to/config.yaml
3644
`,
3745
),
38-
PreRunE: func(cmd *cobra.Command, args []string) error {
39-
return validateLicense(jwtWrapper)
40-
},
4146
RunE: func(cmd *cobra.Command, args []string) error {
4247
return prereceive.Scan(configFile)
4348
},
@@ -47,3 +52,32 @@ func scanSecretsPreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command
4752

4853
return scanPrereceiveCmd
4954
}
55+
56+
func validateSecretsLicence(jwtWrapper wrappers.JWTWrapper) *cobra.Command {
57+
validateLicence := &cobra.Command{
58+
Use: "validate",
59+
Short: "Validates the license for pre-receive secret detection",
60+
Long: "Validates the license for pre-receive secret detection",
61+
Example: heredoc.Doc(
62+
`
63+
$ cx hooks pre-receive validate
64+
`,
65+
),
66+
RunE: checkLicence(jwtWrapper),
67+
}
68+
return validateLicence
69+
}
70+
71+
func checkLicence(jwtWrapper wrappers.JWTWrapper) func(cmd *cobra.Command, args []string) error {
72+
return func(cmd *cobra.Command, args []string) error {
73+
isAllowed, err := jwtWrapper.IsAllowedEngine(params.EnterpriseSecretsLabel)
74+
if err != nil {
75+
log.Fatalf("%s: %s", "Failed licence check", err)
76+
}
77+
if !isAllowed {
78+
log.Fatalf("Error: License validation failed. Please ensure your CxOne license includes Enterprise Secrets")
79+
}
80+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), SuccessFullSecretsLicenceValidation)
81+
return nil
82+
}
83+
}

internal/commands/pre_receive_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ func TestPreReceiveCommand_withWrongFlagConfig(t *testing.T) {
4747
)
4848
assert.NotNil(t, err)
4949
}
50+
51+
func TestPreReceiveCommand_Licence_success(t *testing.T) {
52+
cmd := createASTTestCommand()
53+
err := executeTestCommand(
54+
cmd,
55+
"hooks", "pre-receive", "validate")
56+
assert.Nil(t, err)
57+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ type JWTMockWrapper struct {
1313

1414
const AIProtectionDisabled = 1
1515

16+
var engines = []string{"sast", "sca", "api-security", "iac-security", "scs", "containers", "enterprise-secrets"}
17+
1618
// GetAllowedEngines mock for tests
1719
func (j *JWTMockWrapper) GetAllowedEngines(featureFlagsWrapper wrappers.FeatureFlagsWrapper) (allowedEngines map[string]bool, err error) {
1820
if j.CustomGetAllowedEngines != nil {
1921
return j.CustomGetAllowedEngines(featureFlagsWrapper)
2022
}
2123
allowedEngines = make(map[string]bool)
22-
engines := []string{"sast", "iac-security", "sca", "api-security", "containers", "scs"}
24+
2325
for _, value := range engines {
2426
allowedEngines[strings.ToLower(value)] = true
2527
}

test/integration/pre-receive_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ func TestPreReceive_IgnoreFolderExclusion_ConfigFile(t *testing.T) {
270270
assert.Contains(t, outputString, "No secrets detected by Cx Secret Scanner")
271271
}
272272

273+
func TestPre_Receive_Validate_Command_success(t *testing.T) {
274+
args := []string{
275+
"hooks",
276+
"pre-receive",
277+
"validate",
278+
}
279+
280+
err, _ := executeCommand(t, args...)
281+
assert.NoError(t, err, "Error should be nil")
282+
}
283+
273284
func setGlobalGitAccount(t *testing.T, repoName string) {
274285
// Set global git config
275286
username := os.Getenv("GITHUB_ACTOR")

0 commit comments

Comments
 (0)