Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added auth in clickhouse #259

Merged
merged 22 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion agent/kubviz/k8smetrics_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func main() {
LogErr(err)
err = RakeesOutput(config, js)
LogErr(err)
// getK8sEvents(clientset)
//getK8sEvents(clientset)
err = runTrivyScans(config, js)
LogErr(err)
err = RunKubeScore(clientset, js)
Expand Down
76 changes: 60 additions & 16 deletions client/pkg/clickhouse/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,42 @@ type DBInterface interface {

func NewDBClient(conf *config.Config) (DBInterface, error) {
ctx := context.Background()
log.Println("Connecting to Clickhouse DB and creating schemas...")
splconn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
Debug: true,
Debugf: func(format string, v ...any) {
fmt.Printf(format, v)
},
Settings: clickhouse.Settings{
"allow_experimental_object_type": 1,
},
})
var connOptions clickhouse.Options

if conf.ClickHouseUsername != "" && conf.ClickHousePassword != "" {
fmt.Println("Using provided username and password")
connOptions = clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
Debug: true,
Auth: clickhouse.Auth{
Username: conf.ClickHouseUsername,
Password: conf.ClickHousePassword,
},
Debugf: func(format string, v ...interface{}) {
fmt.Printf(format, v...)
},
Settings: clickhouse.Settings{
"allow_experimental_object_type": 1,
},
}
fmt.Printf("Connecting to ClickHouse using username and password")
} else {
fmt.Println("Using connection without username and password")
connOptions = clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
Debug: true,
Debugf: func(format string, v ...interface{}) {
fmt.Printf(format, v...)
},
Settings: clickhouse.Settings{
"allow_experimental_object_type": 1,
},
}
fmt.Printf("Connecting to ClickHouse without usename and password")

}

splconn, err := clickhouse.Open(&connOptions)
if err != nil {
return nil, err
}
Expand All @@ -69,7 +94,7 @@ func NewDBClient(conf *config.Config) (DBInterface, error) {
if exception, ok := err.(*clickhouse.Exception); ok {
fmt.Printf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
} else {
fmt.Println(err)
fmt.Println("Authentication error:", err) // Print the error message here
}
return nil, err
}
Expand All @@ -80,17 +105,36 @@ func NewDBClient(conf *config.Config) (DBInterface, error) {
// return nil, err
// }
// }
stdconn := clickhouse.OpenDB(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
})
var connOption clickhouse.Options

if conf.ClickHouseUsername != "" && conf.ClickHousePassword != "" {
fmt.Println("Using provided username and password")
connOption = clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
Debug: true,
Auth: clickhouse.Auth{
Username: conf.ClickHouseUsername,
Password: conf.ClickHousePassword,
},
}
} else {
fmt.Println("Using connection without username and password")
connOption = clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
}
}

stdconn := clickhouse.OpenDB(&connOption)

if err := stdconn.Ping(); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
fmt.Printf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
} else {
fmt.Println(err)
fmt.Println("Authentication error:", err)
}
return nil, err
}

return &DBClient{splconn: splconn, conn: stdconn, conf: conf}, nil
}

Expand Down
10 changes: 6 additions & 4 deletions client/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package config

type Config struct {
NatsAddress string `envconfig:"NATS_ADDRESS"`
NatsToken string `envconfig:"NATS_TOKEN"`
DbPort int `envconfig:"DB_PORT"`
DBAddress string `envconfig:"DB_ADDRESS"`
NatsAddress string `envconfig:"NATS_ADDRESS"`
NatsToken string `envconfig:"NATS_TOKEN"`
DbPort int `envconfig:"DB_PORT"`
DBAddress string `envconfig:"DB_ADDRESS"`
ClickHouseUsername string `envconfig:"CLICKHOUSE_USERNAME"`
ClickHousePassword string `envconfig:"CLICKHOUSE_PASSWORD"`
}
31 changes: 25 additions & 6 deletions cmd/cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
)

type Config struct {
DbPort int `envconfig:"DB_PORT" required:"true"`
DBAddress string `envconfig:"DB_ADDRESS" required:"true"`
SchemaPath string `envconfig:"SCHEMA_PATH" default:"/sql"`
DbPort int `envconfig:"DB_PORT" required:"true"`
DBAddress string `envconfig:"DB_ADDRESS" required:"true"`
ClickHouseUsername string `envconfig:"CLICKHOUSE_USERNAME"`
ClickHousePassword string `envconfig:"CLICKHOUSE_PASSWORD"`
SchemaPath string `envconfig:"SCHEMA_PATH" default:"/sql"`
}

func OpenClickHouseConn() (*sql.DB, *Config, error) {
Expand All @@ -20,9 +22,26 @@ func OpenClickHouseConn() (*sql.DB, *Config, error) {
if err != nil {
return nil, nil, err
}
conn := clickhouse.OpenDB(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", cfg.DBAddress, cfg.DbPort)},
})
var options clickhouse.Options

if cfg.ClickHouseUsername != "" && cfg.ClickHousePassword != "" {
fmt.Println("Using provided username and password")
options = clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", cfg.DBAddress, cfg.DbPort)},
Debug: true,
Auth: clickhouse.Auth{
Username: cfg.ClickHouseUsername,
Password: cfg.ClickHousePassword,
},
}
} else {
fmt.Println("Using connection without username and password")
options = clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", cfg.DBAddress, cfg.DbPort)},
}
}

conn := clickhouse.OpenDB(&options)
if err := conn.Ping(); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
return nil, nil, fmt.Errorf("[%d] %s %s", exception.Code, exception.Message, exception.StackTrace)
Expand Down
Loading