-
Notifications
You must be signed in to change notification settings - Fork 220
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
refactor: remove global variables for all commands #572
Open
aymene01
wants to merge
2
commits into
DataDog:main
Choose a base branch
from
aymene01:refactor/detonate-cmd-cleanup-param
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.
+80
−65
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -2,67 +2,70 @@ package main | |
|
||
import ( | ||
"errors" | ||
"log" | ||
"os" | ||
|
||
"github.com/datadog/stratus-red-team/v2/pkg/stratus" | ||
"github.com/datadog/stratus-red-team/v2/pkg/stratus/runner" | ||
"github.com/spf13/cobra" | ||
"log" | ||
"os" | ||
) | ||
|
||
var flagForceCleanup bool | ||
var flagCleanupAll bool | ||
|
||
func buildCleanupCmd() *cobra.Command { | ||
var flagForceCleanup bool | ||
var flagCleanupAll bool | ||
|
||
cleanupCmd := &cobra.Command{ | ||
Use: "cleanup [attack-technique-id]... | --all", | ||
Aliases: []string{"clean"}, | ||
Short: "Cleans up any leftover infrastructure or configuration from a TTP.", | ||
Example: "stratus cleanup aws.defense-evasion.cloudtrail-stop\nstratus cleanup --all", | ||
DisableFlagsInUseLine: true, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 && flagCleanupAll { | ||
if !flagCleanupAll { | ||
return errors.New("pass the ID of the technique to clean up, or --all") | ||
} | ||
return nil | ||
if len(args) == 0 && !flagCleanupAll { | ||
return errors.New("pass the ID of the technique to clean up, or --all") | ||
} | ||
|
||
// Ensure the technique IDs are valid | ||
_, err := resolveTechniques(args) | ||
if len(args) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this check? (it wasn'tthere before) |
||
// Ensure the technique IDs are valid | ||
_, err := resolveTechniques(args) | ||
return err | ||
} | ||
|
||
return err | ||
return nil | ||
}, | ||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return getTechniquesCompletion(toComplete), cobra.ShellCompDirectiveNoFileComp | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) > 0 { | ||
techniques, _ := resolveTechniques(args) | ||
doCleanupCmd(techniques) | ||
doCleanupCmd(techniques, flagForceCleanup) | ||
return nil | ||
} else if flagCleanupAll { | ||
// clean up all techniques that are not in the COLD state | ||
doCleanupAllCmd() | ||
doCleanupAllCmd(flagForceCleanup) | ||
return nil | ||
} else { | ||
return errors.New("pass the ID of the technique to clean up, or --all") | ||
} | ||
return errors.New("pass the ID of the technique to clean up, or --all") | ||
}, | ||
} | ||
|
||
cleanupCmd.Flags().BoolVarP(&flagForceCleanup, "force", "f", false, "Force cleanup even if the technique is already COLD") | ||
cleanupCmd.Flags().BoolVarP(&flagCleanupAll, "all", "", false, "Clean up all techniques that are not in COLD state") | ||
|
||
return cleanupCmd | ||
} | ||
|
||
func doCleanupCmd(techniques []*stratus.AttackTechnique) { | ||
func doCleanupCmd(techniques []*stratus.AttackTechnique, forceCleanup bool) { | ||
workerCount := len(techniques) | ||
techniquesChan := make(chan *stratus.AttackTechnique, workerCount) | ||
errorsChan := make(chan error, workerCount) | ||
|
||
for i := 0; i < workerCount; i++ { | ||
go cleanupCmdWorker(techniquesChan, errorsChan) | ||
go cleanupCmdWorker(techniquesChan, errorsChan, forceCleanup) | ||
} | ||
for i := range techniques { | ||
techniquesChan <- techniques[i] | ||
|
||
for _, technique := range techniques { | ||
techniquesChan <- technique | ||
} | ||
close(techniquesChan) | ||
|
||
|
@@ -73,16 +76,16 @@ func doCleanupCmd(techniques []*stratus.AttackTechnique) { | |
} | ||
} | ||
|
||
func cleanupCmdWorker(techniques <-chan *stratus.AttackTechnique, errors chan<- error) { | ||
func cleanupCmdWorker(techniques <-chan *stratus.AttackTechnique, errors chan<- error, forceCleanup bool) { | ||
for technique := range techniques { | ||
stratusRunner := runner.NewRunner(technique, flagForceCleanup) | ||
stratusRunner := runner.NewRunner(technique, forceCleanup) | ||
err := stratusRunner.CleanUp() | ||
errors <- err | ||
} | ||
} | ||
|
||
func doCleanupAllCmd() { | ||
func doCleanupAllCmd(forceCleanup bool) { | ||
log.Println("Cleaning up all techniques that have been warmed-up or detonated") | ||
availableTechniques := stratus.GetRegistry().ListAttackTechniques() | ||
doCleanupCmd(availableTechniques) | ||
doCleanupCmd(availableTechniques, forceCleanup) | ||
} |
This file contains 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be
flagCleanupAll
like before?