Skip to content

Commit 19e1adf

Browse files
authored
Merge pull request #259 from intelops/clickhouse_auth
added auth in clickhouse
2 parents 5fd64f0 + 09d6441 commit 19e1adf

File tree

4 files changed

+92
-27
lines changed

4 files changed

+92
-27
lines changed

agent/kubviz/k8smetrics_agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func main() {
127127
LogErr(err)
128128
err = RakeesOutput(config, js)
129129
LogErr(err)
130-
// getK8sEvents(clientset)
130+
//getK8sEvents(clientset)
131131
err = runTrivyScans(config, js)
132132
LogErr(err)
133133
err = RunKubeScore(clientset, js)

client/pkg/clickhouse/db_client.go

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,42 @@ type DBInterface interface {
5050

5151
func NewDBClient(conf *config.Config) (DBInterface, error) {
5252
ctx := context.Background()
53-
log.Println("Connecting to Clickhouse DB and creating schemas...")
54-
splconn, err := clickhouse.Open(&clickhouse.Options{
55-
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
56-
Debug: true,
57-
Debugf: func(format string, v ...any) {
58-
fmt.Printf(format, v)
59-
},
60-
Settings: clickhouse.Settings{
61-
"allow_experimental_object_type": 1,
62-
},
63-
})
53+
var connOptions clickhouse.Options
54+
55+
if conf.ClickHouseUsername != "" && conf.ClickHousePassword != "" {
56+
fmt.Println("Using provided username and password")
57+
connOptions = clickhouse.Options{
58+
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
59+
Debug: true,
60+
Auth: clickhouse.Auth{
61+
Username: conf.ClickHouseUsername,
62+
Password: conf.ClickHousePassword,
63+
},
64+
Debugf: func(format string, v ...interface{}) {
65+
fmt.Printf(format, v...)
66+
},
67+
Settings: clickhouse.Settings{
68+
"allow_experimental_object_type": 1,
69+
},
70+
}
71+
fmt.Printf("Connecting to ClickHouse using username and password")
72+
} else {
73+
fmt.Println("Using connection without username and password")
74+
connOptions = clickhouse.Options{
75+
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
76+
Debug: true,
77+
Debugf: func(format string, v ...interface{}) {
78+
fmt.Printf(format, v...)
79+
},
80+
Settings: clickhouse.Settings{
81+
"allow_experimental_object_type": 1,
82+
},
83+
}
84+
fmt.Printf("Connecting to ClickHouse without usename and password")
85+
86+
}
87+
88+
splconn, err := clickhouse.Open(&connOptions)
6489
if err != nil {
6590
return nil, err
6691
}
@@ -69,7 +94,7 @@ func NewDBClient(conf *config.Config) (DBInterface, error) {
6994
if exception, ok := err.(*clickhouse.Exception); ok {
7095
fmt.Printf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
7196
} else {
72-
fmt.Println(err)
97+
fmt.Println("Authentication error:", err) // Print the error message here
7398
}
7499
return nil, err
75100
}
@@ -80,17 +105,36 @@ func NewDBClient(conf *config.Config) (DBInterface, error) {
80105
// return nil, err
81106
// }
82107
// }
83-
stdconn := clickhouse.OpenDB(&clickhouse.Options{
84-
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
85-
})
108+
var connOption clickhouse.Options
109+
110+
if conf.ClickHouseUsername != "" && conf.ClickHousePassword != "" {
111+
fmt.Println("Using provided username and password")
112+
connOption = clickhouse.Options{
113+
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
114+
Debug: true,
115+
Auth: clickhouse.Auth{
116+
Username: conf.ClickHouseUsername,
117+
Password: conf.ClickHousePassword,
118+
},
119+
}
120+
} else {
121+
fmt.Println("Using connection without username and password")
122+
connOption = clickhouse.Options{
123+
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
124+
}
125+
}
126+
127+
stdconn := clickhouse.OpenDB(&connOption)
128+
86129
if err := stdconn.Ping(); err != nil {
87130
if exception, ok := err.(*clickhouse.Exception); ok {
88131
fmt.Printf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
89132
} else {
90-
fmt.Println(err)
133+
fmt.Println("Authentication error:", err)
91134
}
92135
return nil, err
93136
}
137+
94138
return &DBClient{splconn: splconn, conn: stdconn, conf: conf}, nil
95139
}
96140

client/pkg/config/config.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package config
22

33
type Config struct {
4-
NatsAddress string `envconfig:"NATS_ADDRESS"`
5-
NatsToken string `envconfig:"NATS_TOKEN"`
6-
DbPort int `envconfig:"DB_PORT"`
7-
DBAddress string `envconfig:"DB_ADDRESS"`
4+
NatsAddress string `envconfig:"NATS_ADDRESS"`
5+
NatsToken string `envconfig:"NATS_TOKEN"`
6+
DbPort int `envconfig:"DB_PORT"`
7+
DBAddress string `envconfig:"DB_ADDRESS"`
8+
ClickHouseUsername string `envconfig:"CLICKHOUSE_USERNAME"`
9+
ClickHousePassword string `envconfig:"CLICKHOUSE_PASSWORD"`
810
}

cmd/cli/config/config.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
)
1010

1111
type Config struct {
12-
DbPort int `envconfig:"DB_PORT" required:"true"`
13-
DBAddress string `envconfig:"DB_ADDRESS" required:"true"`
14-
SchemaPath string `envconfig:"SCHEMA_PATH" default:"/sql"`
12+
DbPort int `envconfig:"DB_PORT" required:"true"`
13+
DBAddress string `envconfig:"DB_ADDRESS" required:"true"`
14+
ClickHouseUsername string `envconfig:"CLICKHOUSE_USERNAME"`
15+
ClickHousePassword string `envconfig:"CLICKHOUSE_PASSWORD"`
16+
SchemaPath string `envconfig:"SCHEMA_PATH" default:"/sql"`
1517
}
1618

1719
func OpenClickHouseConn() (*sql.DB, *Config, error) {
@@ -20,9 +22,26 @@ func OpenClickHouseConn() (*sql.DB, *Config, error) {
2022
if err != nil {
2123
return nil, nil, err
2224
}
23-
conn := clickhouse.OpenDB(&clickhouse.Options{
24-
Addr: []string{fmt.Sprintf("%s:%d", cfg.DBAddress, cfg.DbPort)},
25-
})
25+
var options clickhouse.Options
26+
27+
if cfg.ClickHouseUsername != "" && cfg.ClickHousePassword != "" {
28+
fmt.Println("Using provided username and password")
29+
options = clickhouse.Options{
30+
Addr: []string{fmt.Sprintf("%s:%d", cfg.DBAddress, cfg.DbPort)},
31+
Debug: true,
32+
Auth: clickhouse.Auth{
33+
Username: cfg.ClickHouseUsername,
34+
Password: cfg.ClickHousePassword,
35+
},
36+
}
37+
} else {
38+
fmt.Println("Using connection without username and password")
39+
options = clickhouse.Options{
40+
Addr: []string{fmt.Sprintf("%s:%d", cfg.DBAddress, cfg.DbPort)},
41+
}
42+
}
43+
44+
conn := clickhouse.OpenDB(&options)
2645
if err := conn.Ping(); err != nil {
2746
if exception, ok := err.(*clickhouse.Exception); ok {
2847
return nil, nil, fmt.Errorf("[%d] %s %s", exception.Code, exception.Message, exception.StackTrace)

0 commit comments

Comments
 (0)