1
1
package commands
2
2
3
3
import (
4
+ "fmt"
4
5
prereceive "github.com/Checkmarx/secret-detection/pkg/hooks/pre-receive"
5
6
"github.com/MakeNowJust/heredoc"
7
+ "github.com/checkmarx/ast-cli/internal/params"
6
8
"github.com/checkmarx/ast-cli/internal/wrappers"
7
9
"github.com/spf13/cobra"
10
+ "log"
11
+ )
12
+
13
+ const (
14
+ SuccessFullSecretsLicenceValidation = "Successfully Validated the Enterprise Secrets licence!"
8
15
)
9
16
10
17
func PreReceiveCommand (jwtWrapper wrappers.JWTWrapper ) * cobra.Command {
@@ -18,12 +25,13 @@ func PreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command {
18
25
` ,
19
26
),
20
27
}
21
- preReceiveCmd .AddCommand (scanSecretsPreReceiveCommand (jwtWrapper ))
28
+ preReceiveCmd .AddCommand (scanSecretsPreReceiveCommand ())
29
+ preReceiveCmd .AddCommand (validateSecretsLicence (jwtWrapper ))
22
30
23
31
return preReceiveCmd
24
32
}
25
33
26
- func scanSecretsPreReceiveCommand (jwtWrapper wrappers. JWTWrapper ) * cobra.Command {
34
+ func scanSecretsPreReceiveCommand () * cobra.Command {
27
35
var configFile string
28
36
scanPrereceiveCmd := & cobra.Command {
29
37
Use : "secrets-scan" ,
@@ -35,9 +43,6 @@ func scanSecretsPreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command
35
43
$ cx hooks pre-receive secrets-scan --config /path/to/config.yaml
36
44
` ,
37
45
),
38
- PreRunE : func (cmd * cobra.Command , args []string ) error {
39
- return validateLicense (jwtWrapper )
40
- },
41
46
RunE : func (cmd * cobra.Command , args []string ) error {
42
47
return prereceive .Scan (configFile )
43
48
},
@@ -47,3 +52,32 @@ func scanSecretsPreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command
47
52
48
53
return scanPrereceiveCmd
49
54
}
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
+ }
0 commit comments