Skip to content

Commit

Permalink
Added verbose, HTML report for postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
klouddb committed Mar 20, 2023
1 parent 951c7d2 commit 09ce2d7
Show file tree
Hide file tree
Showing 32 changed files with 1,512 additions and 620 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ linters:
- gosec
- gocritic
- vet
- revive
# - revive
- deadcode
- errcheck
- gosimple
Expand Down
79 changes: 78 additions & 1 deletion cmd/ciscollector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import (
"encoding/json"
"fmt"
"os"
"reflect"
"strings"
"time"

"github.com/jedib0t/go-pretty/text"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/klouddb/klouddbshield/htmlreport"
"github.com/klouddb/klouddbshield/mysql"
"github.com/klouddb/klouddbshield/pkg/config"
"github.com/klouddb/klouddbshield/pkg/mysqldb"
Expand All @@ -33,6 +38,10 @@ func main() {

// Program context
ctx := context.Background()
if cnf.App.VerbosePostgres {
runPostgresByControl(ctx, cnf)
return
}
if cnf.App.RunMySql {
runMySql(ctx, cnf)
}
Expand All @@ -42,8 +51,63 @@ func main() {
if cnf.App.RunRds {
runRDS(ctx, cnf)
}

}
func runPostgresByControl(ctx context.Context, cnf *config.Config) {
postgresDatabase := cnf.Postgres
postgresStore, _, err := postgresdb.Open(*postgresDatabase)
if err != nil {
return
}
result := postgres.CheckByControl(postgresStore, ctx, cnf.App.Control)
if result == nil {
os.Exit(1)
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
if result.Status == "Pass" {
t.AppendSeparator()
color := text.FgGreen
t.AppendRow(table.Row{"Status", color.Sprintf("%s", result.Status)})

} else {
t.AppendSeparator()
color := text.FgRed
t.AppendRow(table.Row{"Status", color.Sprintf("%s", result.Status)})
t.AppendSeparator()
switch ty := result.FailReason.(type) {

case string:
t.AppendRow(table.Row{"Fail Reason", result.FailReason})
case []map[string]interface{}:
failReason := ""
for _, n := range ty {
for key, value := range n {
failReason += fmt.Sprintf("%s:%v, ", key, value)
}
failReason += "\n"

}
t.AppendRow(table.Row{"Fail Reason", failReason})
default:
var r = reflect.TypeOf(t)
fmt.Printf("Other:%v\n", r)
}

}
t.AppendSeparator()
t.AppendRow(table.Row{"Rationale", result.Rationale})
t.AppendSeparator()
t.AppendRow(table.Row{"Title", result.Title})
t.AppendSeparator()
t.AppendRow(table.Row{"Procedure", result.Procedure})
t.AppendSeparator()
t.AppendRow(table.Row{"Control", result.Control})
t.AppendSeparator()
t.AppendRow(table.Row{"References", result.References})
t.SetStyle(table.StyleLight)
t.Render()
}
func runMySql(ctx context.Context, cnf *config.Config) {
// for _, mySQL := range cnf.MySQL {
// Open Postgres store connection and ping it
Expand Down Expand Up @@ -82,18 +146,31 @@ func runPostgres(ctx context.Context, cnf *config.Config) {
fmt.Println("**********listOfResults*************\n", string(jsonData))
}
fmt.Println("postgressecreport.json file generated")
data := htmlreport.GenerateHTMLReport(listOfResults, "Postgres")
htmldata := []byte(data)
err = os.WriteFile("postgressecreport.html", htmldata, 0600)
if err != nil {
log.Error().Err(err).Msg("Unable to generate postgressecreport.html file: " + err.Error())
fmt.Println("**********listOfResults*************\n", data)
}
fmt.Println("postgressecreport.html file generated")
}

func runRDS(ctx context.Context, cnf *config.Config) {
fmt.Println("running RDS ")
rds.Validate()
listOfResults := rds.PerformAllChecks(ctx)

jsonData, err := json.MarshalIndent(listOfResults, "", " ")
if err != nil {
fmt.Println("error marshaling list of results", err)
return
}
err = os.WriteFile("rdssecreport.json", jsonData, 0600)

output := strings.ReplaceAll(string(jsonData), `\n`, "\n")

// write output data to file
err = os.WriteFile("rdssecreport.json", []byte(output), 0600)
if err != nil {
log.Error().Err(err).Msg("Unable to generate rdssecreport.json file: " + err.Error())
fmt.Println("**********listOfResults*************\n", string(jsonData))
Expand Down
20 changes: 18 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ require (
github.com/go-sql-driver/mysql v1.7.0
github.com/rs/zerolog v1.29.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
golang.org/x/net v0.8.0
)

require (
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-openapi/errors v0.20.2 // indirect
github.com/go-openapi/strfmt v0.21.3 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
go.mongodb.org/mongo-driver v1.10.0 // indirect
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/jedib0t/go-pretty/v6 v6.4.6
github.com/lib/pq v1.10.7
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand All @@ -22,8 +38,8 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 09ce2d7

Please sign in to comment.