-
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
Open
cx-anjali-deore
wants to merge
54
commits into
main
Choose a base branch
from
feature/pre-receive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
e42cc3d
Moved to common hooks place
cx-anjali-deore d287c4b
removed common hooks code
cx-anjali-deore b302afc
Added pre receive commands
cx-anjali-deore f37bb27
Adding the commit version of secret detection
cx-anjali-deore 93ec1ee
Added pre receive in Hooks doc
cx-anjali-deore d9aa18e
formatted
cx-anjali-deore 7c493c2
typo changes
cx-anjali-deore 1dfe755
formatted the file
cx-anjali-deore 115655e
Adding secret detection to depgaurd allow list
cx-anjali-deore 3da2035
removed whitespace
cx-anjali-deore 67852a9
formatted with gofmt
cx-anjali-deore 22452ff
indentation
cx-anjali-deore de8151b
indentation
cx-anjali-deore 834ea6c
indentation
cx-anjali-deore 9c5c8c5
url and formatting fixes
cx-anjali-deore 53dc14a
indentation alignment
cx-anjali-deore 1e8c4ae
Unit and integration tests
cx-anjali-deore 0f5a3b4
lint issues fixed
cx-anjali-deore 7cd195b
formatted unit test file
cx-anjali-deore d54f6bb
setting global config git
cx-anjali-deore 9e1b19f
changing cx path
cx-anjali-deore 5b10c5b
adjusting cx path
cx-anjali-deore f573902
check if cx path exist
cx-anjali-deore df5695c
check if cx path exist
cx-anjali-deore 48339d1
added cxPath
cx-anjali-deore 67a2ab8
Adding githubUser and email
cx-anjali-deore bdd2931
renaming functions
cx-anjali-deore 9b1f99b
url and formatting fixes
cx-anjali-deore 22e4b03
indentation alignment
cx-anjali-deore 5e2b2cd
Unit and integration tests
cx-anjali-deore a717987
lint issues fixed
cx-anjali-deore 34646d6
formatted unit test file
cx-anjali-deore 2cd4dff
setting global config git
cx-anjali-deore aeb1fc7
changing cx path
cx-anjali-deore f12c04d
adjusting cx path
cx-anjali-deore df0845c
check if cx path exist
cx-anjali-deore 7245ce8
check if cx path exist
cx-anjali-deore 471c608
added cxPath
cx-anjali-deore 69fbbd1
Adding githubUser and email
cx-anjali-deore 85157f7
renaming functions
cx-anjali-deore 63a975c
removed the git username
cx-anjali-deore 3450218
using github actor
cx-anjali-deore 50313d1
go mod file updated
cx-anjali-deore 08ac679
Added additional tests for coverage
cx-anjali-deore 1600a56
secret detection version upgrade
cx-anjali-deore 7108369
degraded the version
cx-anjali-deore 073ff3c
updegraded the version
cx-anjali-deore 15c0d3e
updegraded the version
cx-anjali-deore 0c23521
updegraded the version
cx-anjali-deore b53fb4f
updegraded the version
cx-anjali-deore c9c278e
Fix found vulnerabilities on trivy (#1192)
cx-margarita-levitm 27fbb23
added latest version of secret detection 1.2.1
cx-anjali-deore 59c87e2
added previous version
cx-anjali-deore 212f21d
go mod tidy
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,49 @@ | ||
package commands | ||
|
||
import ( | ||
prereceive "github.com/Checkmarx/secret-detection/pkg/hooks/pre-receive" | ||
"github.com/MakeNowJust/heredoc" | ||
"github.com/checkmarx/ast-cli/internal/wrappers" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
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(jwtWrapper)) | ||
|
||
return preReceiveCmd | ||
} | ||
|
||
func scanSecretsPreReceiveCommand(jwtWrapper wrappers.JWTWrapper) *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 | ||
`, | ||
), | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return validateLicense(jwtWrapper) | ||
}, | ||
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 | ||
} |
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 ( | ||
"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) | ||
} |
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,2 @@ | ||
exclude_path: | ||
- "*.txt" |
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,2 @@ | ||
exclude_path: | ||
- "integration/*" |
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,3 @@ | ||
ignore_result_id: | ||
- "986e5d5a73bcc5972e2d697ec0846db79b03dd75" | ||
- "976014fdd1ef77be2c2ff1c1b6a3fe63d2b237f4" |
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,2 @@ | ||
ignore_rule_id: | ||
- "github-pat" |
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.