Skip to content

Commit

Permalink
feat: change project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-keinan committed Oct 6, 2021
1 parent 935952b commit 82e53f3
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 123 deletions.
2 changes: 1 addition & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func initPluginWorker(plChan chan models.MeshCheckResults, completedChan chan bo
}

//StartCLICommand invoke cli lxd command mesh-kridik cli
func StartCLICommand(fm utils.FolderMgr, plChan chan models.MeshCheckResults, completedChan chan bool, ad ArgsData, cmdArgs []string, commands map[string]cli.CommandFactory, log *logger.LdxProbeLogger) {
func StartCLICommand(fm utils.FolderMgr, plChan chan models.MeshCheckResults, completedChan chan bool, ad ArgsData, cmdArgs []string, commands map[string]cli.CommandFactory, log *logger.MeshKridikLogger) {
// init plugin folders
initPluginFolders(fm)
// init plugin worker
Expand Down
14 changes: 7 additions & 7 deletions internal/cli/commands/command-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"time"
)

func printTestResults(at []*models.AuditBench, table *tablewriter.Table, category string) models.AuditTestTotals {
func printTestResults(at []*models.AuditBench, table *tablewriter.Table, category string) models.CheckTotals {
var (
warnCounter int
passCounter int
Expand Down Expand Up @@ -46,10 +46,10 @@ func printTestResults(at []*models.AuditBench, table *tablewriter.Table, categor
failCounter++
}
}
return models.AuditTestTotals{Fail: failCounter, Pass: passCounter, Warn: warnCounter}
return models.CheckTotals{Fail: failCounter, Pass: passCounter, Warn: warnCounter}
}

func printClassicTestResults(at []*models.AuditBench, log *logger.LdxProbeLogger) models.AuditTestTotals {
func printClassicTestResults(at []*models.AuditBench, log *logger.MeshKridikLogger) models.CheckTotals {
var (
warnCounter int
passCounter int
Expand All @@ -72,7 +72,7 @@ func printClassicTestResults(at []*models.AuditBench, log *logger.LdxProbeLogger
failCounter++
}
}
return models.AuditTestTotals{Fail: failCounter, Pass: passCounter, Warn: warnCounter}
return models.CheckTotals{Fail: failCounter, Pass: passCounter, Warn: warnCounter}
}

//AddFailedMessages add failed audit test to report data
Expand Down Expand Up @@ -112,7 +112,7 @@ func NewFileLoader() TestLoader {
//LoadAuditTests load audit test from benchmark folder
func (tl AuditTestLoader) LoadAuditTests(auditFiles []utils.FilesInfo) []*models.SubCategory {
auditTests := make([]*models.SubCategory, 0)
audit := models.Audit{}
audit := models.Check{}
for _, auditFile := range auditFiles {
err := yaml.Unmarshal([]byte(auditFile.Data), &audit)
if err != nil {
Expand Down Expand Up @@ -203,9 +203,9 @@ func filteredAuditBenchTests(auditTests []*models.SubCategory, pc []filters.Pred
return ft
}

func executeTests(ft []*models.SubCategory, execTestFunc func(ad *models.AuditBench) []*models.AuditBench, log *logger.LdxProbeLogger) []*models.SubCategory {
func executeTests(ft []*models.SubCategory, execTestFunc func(ad *models.AuditBench) []*models.AuditBench, log *logger.MeshKridikLogger) []*models.SubCategory {
completedTest := make([]*models.SubCategory, 0)
log.Console(ui.LxdAuditTest)
log.Console(ui.MeshCheck)
bar := pb.StartNew(len(ft)).Prefix("Executing LXD specs:")
for _, f := range ft {
tr := ui.ExecuteSpecs(f, execTestFunc)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/commands/command-helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestPrintTestResults(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tr models.AuditTestTotals
var tr models.CheckTotals
if tt.testType == "regular" {
tr = printTestResults(tt.tests, tablewriter.NewWriter(os.Stdout), tt.testCategory)
} else {
Expand Down
20 changes: 10 additions & 10 deletions internal/cli/commands/mesh-check.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ type MeshCheck struct {
CompletedChan chan bool
FilesInfo []utils.FilesInfo
Evaluator eval.CmdEvaluator
log *logger.LdxProbeLogger
log *logger.MeshKridikLogger
}

// ResultProcessor process audit results
type ResultProcessor func(at *models.AuditBench, isSucceeded bool) []*models.AuditBench

// ConsoleOutputGenerator print audit tests to stdout
var ConsoleOutputGenerator ui.OutputGenerator = func(at []*models.SubCategory, log *logger.LdxProbeLogger) {
grandTotal := make([]models.AuditTestTotals, 0)
var ConsoleOutputGenerator ui.OutputGenerator = func(at []*models.SubCategory, log *logger.MeshKridikLogger) {
grandTotal := make([]models.CheckTotals, 0)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Category", "Status", "Type", "Audit Test Description"})
table.SetHeader([]string{"Category", "Status", "Type", "Check Test Description"})
table.SetAutoWrapText(false)
table.SetBorder(true) // Set
for _, a := range at {
Expand All @@ -51,8 +51,8 @@ var ConsoleOutputGenerator ui.OutputGenerator = func(at []*models.SubCategory, l
}

// ClassicOutputGenerator print audit tests to stdout in classic view
var ClassicOutputGenerator ui.OutputGenerator = func(at []*models.SubCategory, log *logger.LdxProbeLogger) {
grandTotal := make([]models.AuditTestTotals, 0)
var ClassicOutputGenerator ui.OutputGenerator = func(at []*models.SubCategory, log *logger.MeshKridikLogger) {
grandTotal := make([]models.CheckTotals, 0)
for _, a := range at {
log.Console(fmt.Sprintf("%s %s\n", "[Category]", a.Name))
categoryTotal := printClassicTestResults(a.AuditTests, log)
Expand All @@ -61,7 +61,7 @@ var ClassicOutputGenerator ui.OutputGenerator = func(at []*models.SubCategory, l
log.Console(printFinalResults(grandTotal))
}

func printFinalResults(grandTotal []models.AuditTestTotals) string {
func printFinalResults(grandTotal []models.CheckTotals) string {
finalTotal := calculateFinalTotal(grandTotal)
passTest := colorstring.Color("[green]Pass:")
failTest := colorstring.Color("[red]Fail:")
Expand All @@ -70,7 +70,7 @@ func printFinalResults(grandTotal []models.AuditTestTotals) string {
return fmt.Sprintf("%s %s %d , %s %d , %s %d ", title, passTest, finalTotal.Pass, warnTest, finalTotal.Warn, failTest, finalTotal.Fail)
}

func calculateFinalTotal(granTotal []models.AuditTestTotals) models.AuditTestTotals {
func calculateFinalTotal(granTotal []models.CheckTotals) models.CheckTotals {
var (
warn int
fail int
Expand All @@ -81,11 +81,11 @@ func calculateFinalTotal(granTotal []models.AuditTestTotals) models.AuditTestTot
fail = fail + total.Fail
pass = pass + total.Pass
}
return models.AuditTestTotals{Pass: pass, Fail: fail, Warn: warn}
return models.CheckTotals{Pass: pass, Fail: fail, Warn: warn}
}

// ReportOutputGenerator print failed audit test to human report
var ReportOutputGenerator ui.OutputGenerator = func(at []*models.SubCategory, log *logger.LdxProbeLogger) {
var ReportOutputGenerator ui.OutputGenerator = func(at []*models.SubCategory, log *logger.MeshKridikLogger) {
for _, a := range at {
log.Table(reports.GenerateAuditReport(a.AuditTests))
}
Expand Down
10 changes: 5 additions & 5 deletions internal/cli/commands/mesh-check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestRunAuditTests(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ab := models.Audit{}
ab := models.Check{}
err := yaml.Unmarshal(readTestData(tt.testFile, t), &ab)
if err != nil {
t.Errorf("failed to Unmarshal test file %s error : %s", tt.testFile, err.Error())
Expand Down Expand Up @@ -144,15 +144,15 @@ func Test_sendResultToPlugin(t *testing.T) {

}
func Test_calculateFinalTotal(t *testing.T) {
att := make([]models.AuditTestTotals, 0)
atOne := models.AuditTestTotals{Fail: 2, Pass: 3, Warn: 1}
atTwo := models.AuditTestTotals{Fail: 1, Pass: 5, Warn: 7}
att := make([]models.CheckTotals, 0)
atOne := models.CheckTotals{Fail: 2, Pass: 3, Warn: 1}
atTwo := models.CheckTotals{Fail: 1, Pass: 5, Warn: 7}
att = append(att, atOne)
att = append(att, atTwo)
res := calculateFinalTotal(att)
assert.Equal(t, res.Warn, 8)
assert.Equal(t, res.Pass, 8)
assert.Equal(t, res.Fail, 3)
str := printFinalResults([]models.AuditTestTotals{res})
str := printFinalResults([]models.CheckTotals{res})
assert.Equal(t, str, "Test Result Total: \x1b[32mPass:\x1b[0m 8 , \x1b[33mWarn:\x1b[0m 8 , \x1b[31mFail:\x1b[0m 3 ")
}
26 changes: 0 additions & 26 deletions internal/logger/ldxlogger.go

This file was deleted.

26 changes: 26 additions & 0 deletions internal/logger/meshlogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package logger

import (
"log"
)

//MeshKridikLogger Object
type MeshKridikLogger struct {
}

//GetLog return native logger
func GetLog() *MeshKridikLogger {
return &MeshKridikLogger{}
}

//Console print to console
func (BLogger *MeshKridikLogger) Console(str string) {
log.SetFlags(0)
log.Print(str)
}

//Table print to console
func (BLogger *MeshKridikLogger) Table(v interface{}) {
log.SetFlags(0)
log.Print(v)
}
8 changes: 4 additions & 4 deletions internal/models/audit.go → internal/models/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"github.com/mitchellh/mapstructure"
)

//Audit data model
type Audit struct {
//Check data model
type Check struct {
BenchmarkType string `yaml:"benchmark_type"`
Categories []Category `yaml:"categories"`
}

//AuditTestTotals model
type AuditTestTotals struct {
//CheckTotals model
type CheckTotals struct {
Warn int
Pass int
Fail int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAuditBench_UnmarshalYAML(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ab := Audit{}
ab := Check{}
err := yaml.Unmarshal(readTestData(tt.fileName, t), &ab)
if err != nil {
t.Errorf("TestAuditBench_UnmarshalYAML failed to unmarshal json %v", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/reports/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func GenerateAuditReport(adtsReport []*models.AuditBench) *uitable.Table {
table.AddRow("Status:", status)
table.AddRow("Name:", failedAudit.Name)
table.AddRow("Description:", failedAudit.Description)
table.AddRow("Audit:", failedAudit.AuditCommand)
table.AddRow("Check:", failedAudit.AuditCommand)
table.AddRow("Remediation:", failedAudit.Remediation)
table.AddRow("References:", failedAudit.References)
table.AddRow("") // blank
Expand Down
58 changes: 3 additions & 55 deletions internal/startup/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,15 @@ func Test_CreateLxdBenchmarkFilesIfNotExist(t *testing.T) {
}
// generate test with packr
assert.Equal(t, bFiles[0].Name, common.FilesystemConfiguration)
assert.Equal(t, bFiles[1].Name, common.ConfigureSoftwareUpdates)
assert.Equal(t, bFiles[2].Name, common.ConfigureSudo)
assert.Equal(t, bFiles[3].Name, common.FilesystemIntegrityChecking)
assert.Equal(t, bFiles[4].Name, common.AdditionalProcessHardening)
assert.Equal(t, bFiles[5].Name, common.MandatoryAccessControl)
assert.Equal(t, bFiles[6].Name, common.WarningBanners)
assert.Equal(t, bFiles[7].Name, common.EnsureUpdates)
assert.Equal(t, bFiles[8].Name, common.InetdServices)
assert.Equal(t, bFiles[9].Name, common.SpecialPurposeServices)
assert.Equal(t, bFiles[10].Name, common.ServiceClients)
assert.Equal(t, bFiles[11].Name, common.NonessentialServices)
assert.Equal(t, bFiles[12].Name, common.NetworkParameters)
assert.Equal(t, bFiles[13].Name, common.NetworkParametersHost)
assert.Equal(t, bFiles[14].Name, common.TCPWrappers)
assert.Equal(t, bFiles[15].Name, common.FirewallConfiguration)
assert.Equal(t, bFiles[16].Name, common.ConfigureLogging)
assert.Equal(t, bFiles[17].Name, common.EnsureLogrotateConfigured)
assert.Equal(t, bFiles[18].Name, common.EnsureLogrotateAssignsAppropriatePermissions)
assert.Equal(t, bFiles[19].Name, common.ConfigureCron)
assert.Equal(t, bFiles[20].Name, common.SSHServerConfiguration)
assert.Equal(t, bFiles[21].Name, common.ConfigurePam)
assert.Equal(t, bFiles[22].Name, common.UserAccountsAndEnvironment)
assert.Equal(t, bFiles[23].Name, common.RootLoginRestrictedSystemConsole)
assert.Equal(t, bFiles[24].Name, common.EnsureAccessSuCommandRestricted)
assert.Equal(t, bFiles[25].Name, common.SystemFilePermissions)
assert.Equal(t, bFiles[26].Name, common.UserAndGroupSettings)
fm := utils.NewKFolder()
err = utils.CreateBenchmarkFolderIfNotExist("lxd", "v1.0.0", fm)
err = utils.CreateBenchmarkFolderIfNotExist("mesh", "v1.0.0", fm)
assert.NoError(t, err)
// save benchmark files to folder
err = SaveBenchmarkFilesIfNotExist("lxd", "v1.0.0", bFiles)
err = SaveBenchmarkFilesIfNotExist("mesh", "v1.0.0", bFiles)
assert.NoError(t, err)
// fetch files from benchmark folder
bFiles, err = utils.GetLxdBenchAuditFiles("lxd", "v1.0.0", fm)
bFiles, err = utils.GetLxdBenchAuditFiles("mesh", "v1.0.0", fm)
assert.Equal(t, bFiles[0].Name, common.FilesystemConfiguration)
assert.Equal(t, bFiles[1].Name, common.ConfigureSoftwareUpdates)
assert.Equal(t, bFiles[2].Name, common.ConfigureSudo)
assert.Equal(t, bFiles[3].Name, common.FilesystemIntegrityChecking)
assert.Equal(t, bFiles[4].Name, common.AdditionalProcessHardening)
assert.Equal(t, bFiles[5].Name, common.MandatoryAccessControl)
assert.Equal(t, bFiles[6].Name, common.WarningBanners)
assert.Equal(t, bFiles[7].Name, common.EnsureUpdates)
assert.Equal(t, bFiles[8].Name, common.InetdServices)
assert.Equal(t, bFiles[9].Name, common.SpecialPurposeServices)
assert.Equal(t, bFiles[10].Name, common.ServiceClients)
assert.Equal(t, bFiles[11].Name, common.NonessentialServices)
assert.Equal(t, bFiles[12].Name, common.NetworkParameters)
assert.Equal(t, bFiles[13].Name, common.NetworkParametersHost)
assert.Equal(t, bFiles[14].Name, common.TCPWrappers)
assert.Equal(t, bFiles[15].Name, common.FirewallConfiguration)
assert.Equal(t, bFiles[16].Name, common.ConfigureLogging)
assert.Equal(t, bFiles[17].Name, common.EnsureLogrotateConfigured)
assert.Equal(t, bFiles[18].Name, common.EnsureLogrotateAssignsAppropriatePermissions)
assert.Equal(t, bFiles[19].Name, common.ConfigureCron)
assert.Equal(t, bFiles[20].Name, common.SSHServerConfiguration)
assert.Equal(t, bFiles[21].Name, common.ConfigurePam)
assert.Equal(t, bFiles[22].Name, common.UserAccountsAndEnvironment)
assert.Equal(t, bFiles[23].Name, common.RootLoginRestrictedSystemConsole)
assert.Equal(t, bFiles[24].Name, common.EnsureAccessSuCommandRestricted)
assert.Equal(t, bFiles[25].Name, common.SystemFilePermissions)
assert.Equal(t, bFiles[26].Name, common.UserAndGroupSettings)
assert.NoError(t, err)
err = os.RemoveAll(utils.GetHomeFolder())
assert.NoError(t, err)
Expand Down
File renamed without changes.
Binary file removed pkg/img/lxc-probe-social.gif
Binary file not shown.
Binary file removed pkg/img/lxd-gopher.png
Binary file not shown.
Binary file removed pkg/img/lxd-probe-social.gif
Binary file not shown.
Binary file removed pkg/img/lxd-probe.gif
Binary file not shown.
File renamed without changes.
21 changes: 11 additions & 10 deletions ui/banners.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package ui

//LxdAuditTest banner
const LxdAuditTest = `
//MeshCheck banner
const MeshCheck = `
_ _ _____ _ _ ___ __ _ ___
| | (_) / __ \ | | (_) / / | / / | | | \ \
| | _ _ __ _ ___ __ | / \/ ___ _ __ | |_ __ _ _ _ __ ___ _ __ ___ | || | __ _____ / / | | __ ____| || |
| | | | '_ \| | | \ \/ / | | / _ \| '_ \| __/ _'' | | '_ \ / _ \ '__/ __ | || | \ \/ / __| / / | | \ \/ / _' || |
| |___| | | | | |_| |> < | \__/\ (_) | | | | || (_| | | | | | __/ | \__ \ | || |____> < (__ / / | |____> < (_| || |
\_____/_|_| |_|\__,_/_/\_\ \____/\___/|_| |_|\__\__,_|_|_| |_|\___|_| |___/ | |\_____/_/\_\___| /_/ \_____/_/\_\__,_|| |
\_\ /_/
__ __ _ _ __ _ _ _ _
| \/ | | | | |/ / (_) | (_) |
| \ / | ___ ___| |__ | ' / _ __ _ __| |_| | __
| |\/| |/ _ \/ __| '_ \ | < | '__| |/ _' | | |/ /
| | | | __/\__ \ | | | | . \| | | | (_| | | <
|_| |_|\___||___/_| |_| |_|\_\_| |_|\__,_|_|_|\_\
`
Expand Down
4 changes: 2 additions & 2 deletions ui/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

// OutputGenerator for audit results
type OutputGenerator func(at []*models.SubCategory, log *logger.LdxProbeLogger)
type OutputGenerator func(at []*models.SubCategory, log *logger.MeshKridikLogger)

//PrintOutput print audit test result to console
func PrintOutput(auditTests []*models.SubCategory, outputGenerator OutputGenerator, log *logger.LdxProbeLogger) {
func PrintOutput(auditTests []*models.SubCategory, outputGenerator OutputGenerator, log *logger.MeshKridikLogger) {
log.Console(auditResult)
outputGenerator(auditTests, log)
}
Expand Down

0 comments on commit 82e53f3

Please sign in to comment.