From 77513c2e97d009bb677eb689cbeb3dd945a5e366 Mon Sep 17 00:00:00 2001 From: Eddie Knight Date: Tue, 17 Oct 2023 20:21:10 -0500 Subject: [PATCH 1/2] Added movement result struct Signed-off-by: Eddie Knight --- raidengine/raidengine.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/raidengine/raidengine.go b/raidengine/raidengine.go index 74b6859..1e0ce92 100644 --- a/raidengine/raidengine.go +++ b/raidengine/raidengine.go @@ -17,13 +17,23 @@ import ( "github.com/privateerproj/privateer-sdk/utils" ) -// StrikeResult is a struct that contains the results of a test +// MovementResult is a struct that contains the results of a single step within a strike +type MovementResult struct { + Passed bool // Passed is true if the test passed + Description string // Description is a human-readable description of the test + Message string // Message is a human-readable description of the test result + Function string // Function is the name of the code that was executed + Value interface{} // Value is the object that was returned during the movement +} + +// StrikeResult is a struct that contains the results of a check for a single control type StrikeResult struct { - Passed bool // Passed is true if the test passed - Description string // Description is a human-readable description of the test - Message string // Message is a human-readable description of the test result - DocsURL string // DocsURL is a link to the documentation for the test - ControlID string // ControlID is the ID of the control that the test is validating + Passed bool // Passed is true if the test passed + Description string // Description is a human-readable description of the test + Message string // Message is a human-readable description of the test result + DocsURL string // DocsURL is a link to the documentation for the test + ControlID string // ControlID is the ID of the control that the test is validating + Movements map[string]MovementResult // Movements is a list of functions that were executed during the test } // RaidResults is a struct that contains the results of all strikes, orgainzed by name From e976863efc156192f4a91994bf1009ba76edba0e Mon Sep 17 00:00:00 2001 From: Eddie Knight Date: Tue, 17 Oct 2023 21:50:04 -0500 Subject: [PATCH 2/2] Moved more of the boilerplate into raidengine + improved outputs Signed-off-by: Eddie Knight --- raidengine/raidengine.go | 51 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/raidengine/raidengine.go b/raidengine/raidengine.go index 1e0ce92..6e0806b 100644 --- a/raidengine/raidengine.go +++ b/raidengine/raidengine.go @@ -13,6 +13,7 @@ import ( "time" "github.com/hashicorp/go-hclog" + "github.com/spf13/viper" "github.com/privateerproj/privateer-sdk/utils" ) @@ -43,7 +44,13 @@ type RaidResults struct { EndTime string // EndTime is the time the raid ended StrikeResults map[string]StrikeResult // StrikeResults is a map of strike names to their results } + +type Strikes interface { + SetLogger(loggerName string) +} + type Strike func() (strikeName string, result StrikeResult) + type cleanupFunc func() error var logger hclog.Logger @@ -55,16 +62,52 @@ var cleanup = func() error { return nil } -// Run is used to execute a list of strikes provided by a Raid and customize by user config -func Run(raidName string, strikes []Strike) error { - logger = GetLogger(raidName, false) +func Run(raidName string, availableStrikes map[string][]Strike, strikes Strikes) (err error) { + tacticsMultiple := fmt.Sprintf("raids.%s.tactics", raidName) + tacticSingular := fmt.Sprintf("raids.%s.tactic", raidName) + if viper.IsSet(tacticsMultiple) { + tactics := viper.GetStringSlice(tacticsMultiple) + for _, tactic := range tactics { + viper.Set(tacticSingular, tactic) + loggerName := fmt.Sprintf("%s-%s", raidName, tactic) + strikes.SetLogger(loggerName) + newErr := RunRaid(loggerName, getStrikes(raidName, availableStrikes)) + if newErr != nil { + if err != nil { + err = fmt.Errorf("%s\n%s", err.Error(), newErr.Error()) + } else { + err = newErr + } + } + } + return err + } + loggerName := fmt.Sprintf("%s-%s", raidName, viper.GetString(tacticSingular)) + strikes.SetLogger(loggerName) + return RunRaid(loggerName, getStrikes(raidName, availableStrikes)) // Return errors from strike executions +} + +// GetStrikes returns a list of probe objects +func getStrikes(raidName string, availableStrikes map[string][]Strike) []Strike { + tactic := viper.GetString(fmt.Sprintf("raids.%s.tactic", raidName)) + strikes := availableStrikes[tactic] + if len(strikes) == 0 { + message := fmt.Sprintf("No strikes were found for the provided strike set: %s", tactic) + logger.Error(message) + } + return strikes +} + +// RunRaid is used to execute a list of strikes provided by a Raid and customize by user config +func RunRaid(name string, strikes []Strike) error { + logger = GetLogger(name, false) closeHandler() var attempts int var failures int raidResults := &RaidResults{ - RaidName: raidName, + RaidName: name, StartTime: time.Now().String(), }