Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/gorm.io/driver/postgre…
Browse files Browse the repository at this point in the history
…s-1.2.0
  • Loading branch information
yoavrotems authored Oct 27, 2021
2 parents 6b0fadd + a75afff commit 1574ff1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 29 deletions.
109 changes: 83 additions & 26 deletions cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,106 @@ import (
"gorm.io/gorm"
)

func savePgsql(jsonInfo string) {
envVars := map[string]string{
"PGSQL_HOST": viper.GetString("PGSQL_HOST"),
"PGSQL_USER": viper.GetString("PGSQL_USER"),
"PGSQL_DBNAME": viper.GetString("PGSQL_DBNAME"),
"PGSQL_SSLMODE": viper.GetString("PGSQL_SSLMODE"),
"PGSQL_PASSWORD": viper.GetString("PGSQL_PASSWORD"),
type PsqlConnInfo struct {
Host string
User string
DbName string
SslMode string
Password string
}

func getPsqlConnInfo() (PsqlConnInfo, error) {
var host string
if value := viper.GetString("PGSQL_HOST"); value != "" {
host = value
} else {
return PsqlConnInfo{}, fmt.Errorf("%s_PGSQL_HOST env var is required", envVarsPrefix)
}

for k, v := range envVars {
if v == "" {
exitWithError(fmt.Errorf("environment variable %s is missing", envVarsPrefix+"_"+k))
}
var user string
if value := viper.GetString("PGSQL_USER"); value != "" {
user = value
} else {
return PsqlConnInfo{}, fmt.Errorf("%s_PGSQL_USER env var is required", envVarsPrefix)
}

var dbName string
if value := viper.GetString("PGSQL_DBNAME"); value != "" {
dbName = value
} else {
return PsqlConnInfo{}, fmt.Errorf("%s_PGSQL_USER env var is required", envVarsPrefix)
}

var sslMode string
if value := viper.GetString("PGSQL_SSLMODE"); value != "" {
sslMode = value
} else {
return PsqlConnInfo{}, fmt.Errorf("%s_PGSQL_SSLMODE env var is required", envVarsPrefix)
}

connInfo := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=%s password=%s",
envVars["PGSQL_HOST"],
envVars["PGSQL_USER"],
envVars["PGSQL_DBNAME"],
envVars["PGSQL_SSLMODE"],
envVars["PGSQL_PASSWORD"],
var password string
if value := viper.GetString("PGSQL_PASSWORD"); value != "" {
password = value
} else {
return PsqlConnInfo{}, fmt.Errorf("%s_PGSQL_PASSWORD env var is required", envVarsPrefix)
}

return PsqlConnInfo{
Host: host,
User: user,
DbName: dbName,
SslMode: sslMode,
Password: password,
}, nil
}

func (c *PsqlConnInfo) toString() string {
return fmt.Sprintf("host=%s user=%s dbname=%s sslmode=%s password=%s",
c.Host,
c.User,
c.DbName,
c.SslMode,
c.Password,
)
}

hostname, err := os.Hostname()
func savePgsql(jsonInfo string) {
var hostname string
if value := viper.GetString("K8S_HOST"); value != "" {
// Adhere to the ScanHost column definition below
if len(value) > 63 {
exitWithError(fmt.Errorf("%s_K8S_HOST value's length must be less than 63 chars", envVarsPrefix))
}

hostname = value
} else {
host, err := os.Hostname()
if err != nil {
exitWithError(fmt.Errorf("received error looking up hostname: %s", err))
}

hostname = host
}

PsqlConnInfo, err := getPsqlConnInfo()
if err != nil {
exitWithError(fmt.Errorf("received error looking up hostname: %s", err))
exitWithError(err)
}

timestamp := time.Now()
db, err := gorm.Open(postgres.Open(PsqlConnInfo.toString()), &gorm.Config{})
if err != nil {
exitWithError(fmt.Errorf("received error connecting to database: %s", err))
}

timestamp := time.Now()
type ScanResult struct {
gorm.Model
ScanHost string `gorm:"type:varchar(63) not null"` // https://www.ietf.org/rfc/rfc1035.txt
ScanTime time.Time `gorm:"not null"`
ScanInfo string `gorm:"type:jsonb not null"`
}

db, err := gorm.Open(postgres.Open(connInfo), &gorm.Config{})
if err != nil {
exitWithError(fmt.Errorf("received error connecting to database: %s", err))
}

db.Debug().AutoMigrate(&ScanResult{})
db.Save(&ScanResult{ScanHost: hostname, ScanTime: timestamp, ScanInfo: jsonInfo})
glog.V(2).Info(fmt.Sprintf("successfully stored result to: %s", envVars["PGSQL_HOST"]))
glog.V(2).Info(fmt.Sprintf("successfully stored result to: %s", PsqlConnInfo.Host))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/aquasecurity/kube-bench
go 1.16

require (
github.com/aws/aws-sdk-go v1.41.6
github.com/aws/aws-sdk-go v1.41.11
github.com/fatih/color v1.13.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/magiconair/properties v1.8.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/aws/aws-sdk-go v1.41.6 h1:ojO1jWhE3lkJlTFQOq0rlWZ11q18LIdsZNtGJ07FFEA=
github.com/aws/aws-sdk-go v1.41.6/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go v1.41.11 h1:QLouWsiYQ8i22kD8k58Dpdhio1A0MpT7bg9ZNXqEjuI=
github.com/aws/aws-sdk-go v1.41.11/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down

0 comments on commit 1574ff1

Please sign in to comment.