Add SSL to InfluxDB sink

This commit is contained in:
Thomas Roehl 2021-06-30 16:56:47 +02:00
parent 34585d88df
commit 586c6c12ac
2 changed files with 12 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package sinks
import ( import (
"context" "context"
"crypto/tls"
"errors" "errors"
"fmt" "fmt"
influxdb2 "github.com/influxdata/influxdb-client-go/v2" influxdb2 "github.com/influxdata/influxdb-client-go/v2"
@ -19,14 +20,20 @@ type InfluxSink struct {
func (s *InfluxSink) connect() error { func (s *InfluxSink) connect() error {
var auth string var auth string
uri := fmt.Sprintf("http://%s:%s", s.host, s.port) var uri string
if s.ssl {
uri = fmt.Sprintf("https://%s:%s", s.host, s.port)
} else {
uri = fmt.Sprintf("http://%s:%s", s.host, s.port)
}
if len(s.user) == 0 { if len(s.user) == 0 {
auth = s.password auth = s.password
} else { } else {
auth = fmt.Sprintf("%s:%s", s.user, s.password) auth = fmt.Sprintf("%s:%s", s.user, s.password)
} }
log.Print("Using URI ", uri, " Org ", s.organization, " Bucket ", s.database) log.Print("Using URI ", uri, " Org ", s.organization, " Bucket ", s.database)
s.client = influxdb2.NewClient(uri, auth) s.client = influxdb2.NewClientWithOptions(uri, auth,
influxdb2.DefaultOptions().SetTLSConfig(&tls.Config{InsecureSkipVerify: true}))
s.writeApi = s.client.WriteAPIBlocking(s.organization, s.database) s.writeApi = s.client.WriteAPIBlocking(s.organization, s.database)
return nil return nil
} }
@ -45,6 +52,7 @@ func (s *InfluxSink) Init(config SinkConfig) error {
s.organization = config.Organization s.organization = config.Organization
s.user = config.User s.user = config.User
s.password = config.Password s.password = config.Password
s.ssl = config.SSL
return s.connect() return s.connect()
} }

View File

@ -12,6 +12,7 @@ type SinkConfig struct {
Password string `json:"password"` Password string `json:"password"`
Organization string `json:"organization"` Organization string `json:"organization"`
Type string `json:"type"` Type string `json:"type"`
SSL bool `json:"ssl"`
} }
type Sink struct { type Sink struct {
@ -21,6 +22,7 @@ type Sink struct {
password string password string
database string database string
organization string organization string
ssl bool
} }
type SinkFuncs interface { type SinkFuncs interface {