Skip to content

Commit

Permalink
made assets, comm links, etc. pointers in model struct in preparation…
Browse files Browse the repository at this point in the history
… of better script integration
  • Loading branch information
joreiche committed Apr 7, 2024
1 parent b081d5b commit a98f0d6
Show file tree
Hide file tree
Showing 59 changed files with 436 additions and 363 deletions.
4 changes: 2 additions & 2 deletions cmd/raa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func calculateRelativeAttackerAttractiveness(input *types.ParsedModel, attractiv
}

// increase the RAA (relative attacker attractiveness) by one third (1/3) of the delta to the highest outgoing neighbour (if positive delta)
func calculatePivotingNeighbourEffectAdjustment(input *types.ParsedModel, techAsset types.TechnicalAsset) float64 {
func calculatePivotingNeighbourEffectAdjustment(input *types.ParsedModel, techAsset *types.TechnicalAsset) float64 {
if techAsset.OutOfScope {
return 0
}
Expand All @@ -153,7 +153,7 @@ func calculatePivotingNeighbourEffectAdjustment(input *types.ParsedModel, techAs

// The sum of all CIAs of the asset itself (fibonacci scale) plus the sum of the comm-links' transferred CIAs
// Multiplied by the quantity values of the data asset for C and I (not A)
func calculateAttackerAttractiveness(input *types.ParsedModel, techAsset types.TechnicalAsset) float64 {
func calculateAttackerAttractiveness(input *types.ParsedModel, techAsset *types.TechnicalAsset) float64 {
if techAsset.OutOfScope {
return 0
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/risk_demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (r customRiskRule) GenerateRisks(parsedModel *types.ParsedModel) []types.Ri
return generatedRisks
}

func createRisk(technicalAsset types.TechnicalAsset) types.Risk {
func createRisk(technicalAsset *types.TechnicalAsset) types.Risk {
risk := types.Risk{
CategoryId: CustomRiskRule.Category().Id,
Severity: types.CalculateSeverity(types.VeryLikely, types.MediumImpact),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down
192 changes: 100 additions & 92 deletions internal/threagile/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,106 +18,114 @@ func (what *Threagile) initExplain() *Threagile {

func (what *Threagile) initExplainNew() *Threagile {
explainCmd := &cobra.Command{
Use: "explain",
Use: common.ExplainCommand,
Short: "Explain an item",
}

what.rootCmd.AddCommand(explainCmd)

explainCmd.AddCommand(&cobra.Command{
Use: "risk",
Short: "Detailed explanation of why a risk was flagged",
Args: cobra.MinimumNArgs(1),
ArgAliases: []string{"risk_id", "..."},
RunE: func(cmd *cobra.Command, args []string) error {
cfg := what.readConfig(cmd, what.buildTimestamp)
progressReporter := common.DefaultProgressReporter{Verbose: cfg.Verbose}

// todo: reuse model if already loaded

result, runError := model.ReadAndAnalyzeModel(cfg, progressReporter)
if runError != nil {
cmd.Printf("Failed to read and analyze model: %v", runError)
return runError
}

_ = result
return fmt.Errorf("not implemneted yet")
explainCmd.AddCommand(
&cobra.Command{
Use: common.RiskItem,
Short: "Detailed explanation of why a risk was flagged",
Args: cobra.MinimumNArgs(1),
ArgAliases: []string{"risk_id", "..."},
RunE: what.explainRisk,
},
})

explainCmd.AddCommand(&cobra.Command{
Use: "rules",
Short: "Detailed explanation of all the risk rules",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Println(docs.Logo + "\n\n" + fmt.Sprintf(docs.VersionText, what.buildTimestamp))
cmd.Println("Explanation for risk rules:")
cmd.Println()
cmd.Println("----------------------")
cmd.Println("Custom risk rules:")
cmd.Println("----------------------")
customRiskRules := model.LoadCustomRiskRules(strings.Split(what.flags.customRiskRulesPluginFlag, ","), common.DefaultProgressReporter{Verbose: what.flags.verboseFlag})
for _, rule := range customRiskRules {
cmd.Printf("%v: %v\n", rule.Category().Id, rule.Category().Description)
}
cmd.Println()
cmd.Println("--------------------")
cmd.Println("Built-in risk rules:")
cmd.Println("--------------------")
cmd.Println()
for _, rule := range risks.GetBuiltInRiskRules() {
cmd.Printf("%v: %v\n", rule.Category().Id, rule.Category().Description)
}
cmd.Println()

return nil
&cobra.Command{
Use: common.RulesItem,
Short: "Detailed explanation of all the risk rules",
RunE: what.explainRules,
},
})

explainCmd.AddCommand(&cobra.Command{
Use: "macros",
Short: "Explain model macros",
Run: func(cmd *cobra.Command, args []string) {
cmd.Println(docs.Logo + "\n\n" + fmt.Sprintf(docs.VersionText, what.buildTimestamp))
cmd.Println("Explanation for the model macros:")
cmd.Println()
/* TODO finish plugin stuff
cmd.Println("Custom model macros:")
for _, macros := range macros.ListCustomMacros() {
details := macros.GetMacroDetails()
cmd.Println(details.ID, "-->", details.Title)
}
cmd.Println()
*/
cmd.Println("----------------------")
cmd.Println("Built-in model macros:")
cmd.Println("----------------------")
for _, macroList := range macros.ListBuiltInMacros() {
details := macroList.GetMacroDetails()
cmd.Printf("%v: %v\n", details.ID, details.Title)
}

cmd.Println()
&cobra.Command{
Use: common.MacrosItem,
Short: "Explain model macros",
Run: what.explainMacros,
},
})

explainCmd.AddCommand(&cobra.Command{
Use: "types",
Short: "Print type information (enum values to be used in models)",
Run: func(cmd *cobra.Command, args []string) {
cmd.Println(docs.Logo + "\n\n" + fmt.Sprintf(docs.VersionText, what.buildTimestamp))
fmt.Println("Explanation for the types:")
cmd.Println()
cmd.Println("The following types are available (can be extended for custom rules):")
cmd.Println()
for name, values := range types.GetBuiltinTypeValues(what.readConfig(cmd, what.buildTimestamp)) {
cmd.Println(name)
for _, candidate := range values {
cmd.Printf("\t %v: %v\n", candidate, candidate.Explain())
}
}
},
})
&cobra.Command{
Use: common.TypesItem,
Short: "Print type information (enum values to be used in models)",
Run: what.explainTypes,
})

return what
}

func (what *Threagile) explainRisk(cmd *cobra.Command, _ []string) error {
cfg := what.readConfig(cmd, what.buildTimestamp)
progressReporter := common.DefaultProgressReporter{Verbose: cfg.Verbose}

// todo: reuse model if already loaded

result, runError := model.ReadAndAnalyzeModel(cfg, progressReporter)
if runError != nil {
cmd.Printf("Failed to read and analyze model: %v", runError)
return runError
}

// todo: implement this

_ = result
return fmt.Errorf("not implemented yet")
}

func (what *Threagile) explainRules(cmd *cobra.Command, _ []string) error {
cmd.Println(docs.Logo + "\n\n" + fmt.Sprintf(docs.VersionText, what.buildTimestamp))
cmd.Println("Explanation for risk rules:")
cmd.Println()
cmd.Println("----------------------")
cmd.Println("Custom risk rules:")
cmd.Println("----------------------")
customRiskRules := model.LoadCustomRiskRules(strings.Split(what.flags.customRiskRulesPluginFlag, ","), common.DefaultProgressReporter{Verbose: what.flags.verboseFlag})
for _, rule := range customRiskRules {
cmd.Printf("%v: %v\n", rule.Category().Id, rule.Category().Description)
}
cmd.Println()
cmd.Println("--------------------")
cmd.Println("Built-in risk rules:")
cmd.Println("--------------------")
cmd.Println()
for _, rule := range risks.GetBuiltInRiskRules() {
cmd.Printf("%v: %v\n", rule.Category().Id, rule.Category().Description)
}
cmd.Println()

return nil
}

func (what *Threagile) explainMacros(cmd *cobra.Command, args []string) {
cmd.Println(docs.Logo + "\n\n" + fmt.Sprintf(docs.VersionText, what.buildTimestamp))
cmd.Println("Explanation for the model macros:")
cmd.Println()
/* TODO finish plugin stuff
cmd.Println("Custom model macros:")
for _, macros := range macros.ListCustomMacros() {
details := macros.GetMacroDetails()
cmd.Println(details.ID, "-->", details.Title)
}
cmd.Println()
*/
cmd.Println("----------------------")
cmd.Println("Built-in model macros:")
cmd.Println("----------------------")
for _, macroList := range macros.ListBuiltInMacros() {
details := macroList.GetMacroDetails()
cmd.Printf("%v: %v\n", details.ID, details.Title)
}

cmd.Println()
}

func (what *Threagile) explainTypes(cmd *cobra.Command, args []string) {
cmd.Println(docs.Logo + "\n\n" + fmt.Sprintf(docs.VersionText, what.buildTimestamp))
fmt.Println("Explanation for the types:")
cmd.Println()
cmd.Println("The following types are available (can be extended for custom rules):")
cmd.Println()
for name, values := range types.GetBuiltinTypeValues(what.readConfig(cmd, what.buildTimestamp)) {
cmd.Println(name)
for _, candidate := range values {
cmd.Printf("\t %v: %v\n", candidate, candidate.Explain())
}
}
}
60 changes: 45 additions & 15 deletions pkg/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ type Config struct {
RiskRulesPlugins []string
SkipRiskRules string
ExecuteModelMacro string
HideColumns []string
GroupByColumns []string
RiskExcel RiskExcelConfig

ServerMode bool
DiagramDPI int
Expand All @@ -57,10 +56,17 @@ type Config struct {
Attractiveness Attractiveness
}

type RiskExcelConfig struct {
HideColumns []string
SortByColumns []string
WidthOfColumns map[string]float64
}

func (c *Config) Defaults(buildTimestamp string) *Config {
*c = Config{
BuildTimestamp: buildTimestamp,
Verbose: false,
Interactive: false,

AppFolder: AppDir,
PluginFolder: PluginDir,
Expand All @@ -83,16 +89,21 @@ func (c *Config) Defaults(buildTimestamp string) *Config {
JsonStatsFilename: JsonStatsFilename,
TemplateFilename: TemplateFilename,
TechnologyFilename: "",
RAAPlugin: RAAPluginName,
RiskRulesPlugins: make([]string, 0),
HideColumns: make([]string, 0),
GroupByColumns: make([]string, 0),
SkipRiskRules: "",
ExecuteModelMacro: "",
ServerMode: false,
ServerPort: DefaultServerPort,

RAAPlugin: RAAPluginName,
RiskRulesPlugins: make([]string, 0),
SkipRiskRules: "",
ExecuteModelMacro: "",
RiskExcel: RiskExcelConfig{
HideColumns: make([]string, 0),
SortByColumns: make([]string, 0),
},

ServerMode: false,
DiagramDPI: DefaultDiagramDPI,
ServerPort: DefaultServerPort,
GraphvizDPI: DefaultGraphvizDPI,
MaxGraphvizDPI: MaxGraphvizDPI,
BackupHistoryFilesToKeep: DefaultBackupHistoryFilesToKeep,

AddModelTitle: false,
Expand Down Expand Up @@ -270,11 +281,30 @@ func (c *Config) Merge(config Config, values map[string]any) {
case strings.ToLower("RiskRulesPlugins"):
c.RiskRulesPlugins = config.RiskRulesPlugins

case strings.ToLower("HideColumns"):
c.HideColumns = append(c.HideColumns, config.HideColumns...)

case strings.ToLower("GroupByColumns"):
c.GroupByColumns = append(c.GroupByColumns, config.GroupByColumns...)
case strings.ToLower("RiskExcel"):
configMap, mapOk := values[key].(map[string]any)
if !mapOk {
continue
}

for valueName := range configMap {
switch strings.ToLower(valueName) {
case strings.ToLower("HideColumns"):
c.RiskExcel.HideColumns = append(c.RiskExcel.HideColumns, config.RiskExcel.HideColumns...)

case strings.ToLower("SortByColumns"):
c.RiskExcel.SortByColumns = append(c.RiskExcel.SortByColumns, config.RiskExcel.SortByColumns...)

case strings.ToLower("WidthOfColumns"):
if c.RiskExcel.WidthOfColumns == nil {
c.RiskExcel.WidthOfColumns = make(map[string]float64)
}

for name, value := range config.RiskExcel.WidthOfColumns {
c.RiskExcel.WidthOfColumns[name] = value
}
}
}

case strings.ToLower("SkipRiskRules"):
c.SkipRiskRules = config.SkipRiskRules
Expand Down
27 changes: 21 additions & 6 deletions pkg/common/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,41 @@ const (

RAAPluginName = "raa_calc"

DefaultDiagramDPI = 100
DefaultGraphvizDPI = 120
MinGraphvizDPI = 20
MaxGraphvizDPI = 300
DefaultBackupHistoryFilesToKeep = 50
)

const (
QuitCommand = "quit"
AnalyzeModelCommand = "analyze-model"
CreateExampleModelCommand = "create-example-model"
CreateStubModelCommand = "create-stub-model"
CreateEditingSupportCommand = "create-editing-support"
PrintVersionCommand = "version"
ListTypesCommand = "list-types"
ListRiskRulesCommand = "list-risk-rules"
ListModelMacrosCommand = "list-model-macros"
ExplainTypesCommand = "explain-types"
ExplainRiskRulesCommand = "explain-risk-rules"
ExplainRiskCommand = "explain-risk"
ExplainModelMacrosCommand = "explain-model-macros"
Print3rdPartyCommand = "print-3rd-party-licenses"
PrintLicenseCommand = "print-license"

CreateCommand = "create"
ExplainCommand = "explain"
ListCommand = "list"
PrintCommand = "print"
QuitCommand = "quit"
RunCommand = "run"
PrintVersionCommand = "version"
)

const (
EditingSupportItem = "editing-support"
ExampleItem = "example"
LicenseItem = "license"
MacrosItem = "macros"
ModelItem = "model"
RiskItem = "risk"
RulesItem = "rules"
StubItem = "stub"
TypesItem = "types"
)
Loading

0 comments on commit a98f0d6

Please sign in to comment.