Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/cisextractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"gopkg.in/yaml.v3"
)

// describes location of a rule in a CIS benchmark
// describes location of a rule in a CIS benchmark
type Location struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Expand All @@ -33,11 +33,12 @@ type namedValue struct {

// Rule describes a CIS benchmark rule
type Rule struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Automated bool `yaml:"automated"`
Location []Location `yaml:"location,omitempty"`
Sections map[string]string `yaml:"-,inline"`
ID string `yaml:"id"`
Name string `yaml:"name"`
Automated bool `yaml:"automated"`
SuffixTableType string `yaml:"suffixTableType"`
Location []Location `yaml:"location,omitempty"`
Sections map[string]string `yaml:"-,inline"`
}

// start arguments
Expand Down Expand Up @@ -173,10 +174,11 @@ func removeSuffixAny(s string, suffix []string) string {
}

// split a title to id, name and determine if it is an actual rule and automated or not
func splitTitle(title string) (id, name string, isActualRule bool, automated bool, err error) {
func splitTitle(title string) (id, name string, isActualRule bool, automated bool, suffixTableType string, err error) {
// initial values
isActualRule = false
automated = false
suffixTableType = "Automated"
// rule types
rStr := []string{"(Automated)", "(Scored)", "(Manual)", "(Not Scored)"}
// if it has any of the above strings as suffix, it is a rule
Expand All @@ -186,6 +188,10 @@ func splitTitle(title string) (id, name string, isActualRule bool, automated boo
if hasSuffixAny(title, rStr[0:2]) {
automated = true
}
if hasSuffixAny(title, rStr[1:3]) {
suffixTableType = "Scored"
}

// now remove the suffix for cleanup
title = removeSuffixAny(title, rStr)
}
Expand Down Expand Up @@ -282,7 +288,7 @@ func prepareRules(titles []string) (noRuleCount int, ruleIDToName map[string]str
ruleIDToName = map[string]string{}
for _, title := range titles {
// for each title from the ToC, get the ID, if it is an rule and if it is automated
id, name, isActualRule, automated, err := splitTitle(title)
id, name, isActualRule, automated, suffixtabletype, err := splitTitle(title)
if err != nil {
fmt.Println(err)
continue
Expand All @@ -292,10 +298,11 @@ func prepareRules(titles []string) (noRuleCount int, ruleIDToName map[string]str
// if it is a rule, build the Rule object
if isActualRule {
rule := Rule{
ID: id,
Automated: automated,
Name: name,
Sections: map[string]string{},
ID: id,
Automated: automated,
SuffixTableType: suffixtabletype,
Name: name,
Sections: map[string]string{},
}
// append it to our Rule array
rules = append(rules, rule)
Expand Down Expand Up @@ -385,9 +392,12 @@ func writeResultFile(populatedRules []Rule, outFileW string) {
sectionNames := []string{"Profile Applicability", "Description", "Rationale", "Audit", "Remediation", "Impact", "Default Value", "References", "CIS Controls"}
// holder
sectionKeyNames := []string{}
//code for determining if automated or scored is used in this document
suffixTableType := populatedRules[0].SuffixTableType

// first columns of the CSV
csvrecords := [][]string{
{"ID", "Name", "Location", "Automated"},
{"ID", "Name", "Location", suffixTableType},
}
// append the rule sections to our first CSV line (headers)
for _, section := range sectionNames {
Expand Down