-
Notifications
You must be signed in to change notification settings - Fork 26
Pre receive feature command (AST-89345) #1146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
4e10651
Moved to common hooks place
cx-anjali-deore 8bfedac
removed common hooks code
cx-anjali-deore 6f1dfbd
Added pre receive commands
cx-anjali-deore 5ea7ae9
Adding the commit version of secret detection
cx-anjali-deore 078ff10
Added pre receive in Hooks doc
cx-anjali-deore fd53889
formatted
cx-anjali-deore 991032a
typo changes
cx-anjali-deore 9af581b
formatted the file
cx-anjali-deore 5460a65
Adding secret detection to depgaurd allow list
cx-anjali-deore 87ccb08
removed whitespace
cx-anjali-deore 09cb077
formatted with gofmt
cx-anjali-deore be9617c
indentation
cx-anjali-deore 02c6c90
indentation
cx-anjali-deore 1a818f0
indentation
cx-anjali-deore b2e2f80
url and formatting fixes
cx-anjali-deore 38e76d3
indentation alignment
cx-anjali-deore 05a542b
Unit and integration tests
cx-anjali-deore 563d7fa
lint issues fixed
cx-anjali-deore f8ae885
formatted unit test file
cx-anjali-deore befbcf8
setting global config git
cx-anjali-deore ad0bd90
changing cx path
cx-anjali-deore 9ccb46b
adjusting cx path
cx-anjali-deore c24077d
check if cx path exist
cx-anjali-deore 66afd4b
check if cx path exist
cx-anjali-deore d123456
added cxPath
cx-anjali-deore 6a1f000
Adding githubUser and email
cx-anjali-deore 66ea998
renaming functions
cx-anjali-deore a474408
url and formatting fixes
cx-anjali-deore 43f1ba6
indentation alignment
cx-anjali-deore c44c097
Unit and integration tests
cx-anjali-deore e1f297c
lint issues fixed
cx-anjali-deore 96b005e
formatted unit test file
cx-anjali-deore 00190f8
setting global config git
cx-anjali-deore 5d5ff83
changing cx path
cx-anjali-deore 0e63927
adjusting cx path
cx-anjali-deore e17532c
check if cx path exist
cx-anjali-deore 81e8a18
check if cx path exist
cx-anjali-deore c322a61
added cxPath
cx-anjali-deore 885fb31
Adding githubUser and email
cx-anjali-deore 6217ff7
renaming functions
cx-anjali-deore 05f9a19
removed the git username
cx-anjali-deore 6ff768b
using github actor
cx-anjali-deore eec8f37
go mod file updated
cx-anjali-deore a13d354
Added additional tests for coverage
cx-anjali-deore 1ee40db
secret detection version upgrade
cx-anjali-deore 2982d85
updegraded the version
cx-anjali-deore ba74a52
updegraded the version
cx-anjali-deore 330986b
updegraded the version
cx-anjali-deore b1c2286
added latest version of secret detection 1.2.1
cx-anjali-deore 295ce26
pre receive validate command
cx-anjali-deore a6c54f5
updated the versions
cx-anjali-deore 8fc9a2b
formatted files
cx-anjali-deore 82ea04d
formatted imports
cx-anjali-deore 4c5a87a
reverted the changed manifest parser version back to main
cx-anjali-deore d1a714f
changed the help text
cx-anjali-deore 819c4f9
removed usused go sum
cx-anjali-deore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/MakeNowJust/heredoc" | ||
"github.com/checkmarx/ast-cli/internal/params" | ||
"github.com/checkmarx/ast-cli/internal/wrappers" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewHooksCommand creates the hooks command with pre-commit subcommand | ||
func NewHooksCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command { | ||
hooksCmd := &cobra.Command{ | ||
Use: "hooks", | ||
Short: "Manage Git hooks", | ||
Long: "The hooks command enables the ability to manage Git hooks for Checkmarx One.", | ||
Example: heredoc.Doc( | ||
` | ||
$ cx hooks pre-commit secrets-install-git-hook | ||
$ cx hooks pre-commit secrets-scan | ||
$ cx hooks pre-receive secrets-scan | ||
cx-anjali-deore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`, | ||
), | ||
Annotations: map[string]string{ | ||
"command:doc": heredoc.Doc( | ||
` | ||
https://checkmarx.com/resource/documents/en/34965-365503-hooks.html | ||
`, | ||
), | ||
}, | ||
} | ||
|
||
// Add pre-commit and pre-receive subcommand | ||
hooksCmd.AddCommand(PreCommitCommand(jwtWrapper)) | ||
hooksCmd.AddCommand(PreReceiveCommand(jwtWrapper)) | ||
|
||
return hooksCmd | ||
} | ||
|
||
func validateLicense(jwtWrapper wrappers.JWTWrapper) error { | ||
allowed, err := jwtWrapper.IsAllowedEngine(params.EnterpriseSecretsLabel) | ||
if err != nil { | ||
return errors.Wrapf(err, "Failed checking license") | ||
} | ||
if !allowed { | ||
return errors.New("Error: License validation failed. Please verify your CxOne license includes Enterprise Secrets.") | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
prereceive "github.com/Checkmarx/secret-detection/pkg/hooks/pre-receive" | ||
"github.com/MakeNowJust/heredoc" | ||
"github.com/checkmarx/ast-cli/internal/params" | ||
"github.com/checkmarx/ast-cli/internal/wrappers" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
SuccessFullSecretsLicenceValidation = "License for pre-receive secret detection has been validated successfully" | ||
) | ||
|
||
func PreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *cobra.Command { | ||
preReceiveCmd := &cobra.Command{ | ||
Use: "pre-receive", | ||
Short: "Manage pre-receive hooks and run secret detection scans", | ||
Long: "The pre-receive command is used for managing Git pre-receive hooks for secret detection", | ||
Example: heredoc.Doc( | ||
` | ||
$ cx hooks pre-receive secrets-scan | ||
`, | ||
), | ||
} | ||
preReceiveCmd.AddCommand(scanSecretsPreReceiveCommand()) | ||
preReceiveCmd.AddCommand(validateSecretsLicence(jwtWrapper)) | ||
|
||
return preReceiveCmd | ||
} | ||
|
||
func scanSecretsPreReceiveCommand() *cobra.Command { | ||
var configFile string | ||
scanPrereceiveCmd := &cobra.Command{ | ||
Use: "secrets-scan", | ||
Short: "Run a pre-receive secret detection scan on the pushed branch", | ||
Long: "Runs pre-receive secret detection scans on each pushed branch that is about to enter the remote git repository", | ||
Example: heredoc.Doc( | ||
` | ||
$ cx hooks pre-receive secrets-scan | ||
$ cx hooks pre-receive secrets-scan --config /path/to/config.yaml | ||
`, | ||
), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return prereceive.Scan(configFile) | ||
}, | ||
} | ||
|
||
scanPrereceiveCmd.Flags().StringVarP(&configFile, "config", "c", "", "path to config.yaml file") | ||
|
||
return scanPrereceiveCmd | ||
} | ||
|
||
func validateSecretsLicence(jwtWrapper wrappers.JWTWrapper) *cobra.Command { | ||
validateLicence := &cobra.Command{ | ||
Use: "validate", | ||
Short: "Validates the license for pre-receive secret detection", | ||
Long: "Validates the license for pre-receive secret detection", | ||
Example: heredoc.Doc( | ||
` | ||
$ cx hooks pre-receive validate | ||
`, | ||
), | ||
RunE: checkLicence(jwtWrapper), | ||
} | ||
return validateLicence | ||
} | ||
|
||
func checkLicence(jwtWrapper wrappers.JWTWrapper) func(cmd *cobra.Command, args []string) error { | ||
return func(cmd *cobra.Command, args []string) error { | ||
isAllowed, err := jwtWrapper.IsAllowedEngine(params.EnterpriseSecretsLabel) | ||
if err != nil { | ||
log.Fatalf("%s: %s", "Failed the licence check", err) | ||
} | ||
if !isAllowed { | ||
log.Fatalf("Error: License validation failed. Please ensure that your Checkmarx One license includes Enterprise Secrets") | ||
} | ||
_, _ = fmt.Fprintln(cmd.OutOrStdout(), SuccessFullSecretsLicenceValidation) | ||
return nil | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package commands | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/checkmarx/ast-cli/internal/wrappers/mock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPreReceiveCommand(t *testing.T) { | ||
mockJWT := &mock.JWTMockWrapper{} | ||
cmd := PreReceiveCommand(mockJWT) | ||
assert.NotNil(t, cmd) | ||
assert.Equal(t, "pre-receive", cmd.Use) | ||
subCmds := cmd.Commands() | ||
subCmdName := make([]string, len(subCmds)) | ||
for i, subCmd := range subCmds { | ||
subCmdName[i] = subCmd.Name() | ||
} | ||
expectedSubCmds := []string{ | ||
"secrets-scan", | ||
} | ||
|
||
for i, expectedSubCmd := range expectedSubCmds { | ||
assert.Contains(t, expectedSubCmd, subCmdName[i]) | ||
} | ||
} | ||
|
||
func TestPreReceiveCommand_withConfig(t *testing.T) { | ||
cmd := createASTTestCommand() | ||
workDir, _ := os.Getwd() | ||
configFile := filepath.Join(workDir, "config.yaml") | ||
_ = os.WriteFile(configFile, []byte(""), 0644) | ||
err := executeTestCommand( | ||
cmd, | ||
"hooks", "pre-receive", "secrets-scan", "--config", "config.yaml", | ||
) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestPreReceiveCommand_withWrongFlagConfig(t *testing.T) { | ||
err := execCmdNotNilAssertion( | ||
t, | ||
"hooks", "pre-receive", "secrets-scan", "--cf", "/path/config.yaml", | ||
) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestPreReceiveCommand_Licence_success(t *testing.T) { | ||
cmd := createASTTestCommand() | ||
err := executeTestCommand( | ||
cmd, | ||
"hooks", "pre-receive", "validate") | ||
assert.Nil(t, err) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.