2022-02-11 11:02:47 +01:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2022-02-14 17:22:30 +01:00
|
|
|
"time"
|
2022-02-11 11:02:47 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/logging"
|
2022-02-11 11:02:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
sslDisabledMode = "disable"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
Database string
|
|
|
|
MaxOpenConns uint32
|
2022-02-14 17:22:30 +01:00
|
|
|
MaxConnLifetime time.Duration
|
|
|
|
MaxConnIdleTime time.Duration
|
2022-02-16 13:30:49 +01:00
|
|
|
User
|
2022-02-11 11:02:47 +01:00
|
|
|
|
|
|
|
//Additional options to be appended as options=<Options>
|
|
|
|
//The value will be taken as is. Multiple options are space separated.
|
|
|
|
Options string
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:30:49 +01:00
|
|
|
type User struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
SSL SSL
|
|
|
|
}
|
|
|
|
|
2022-02-11 11:52:50 +01:00
|
|
|
type SSL struct {
|
2022-02-11 11:02:47 +01:00
|
|
|
// type of connection security
|
|
|
|
Mode string
|
|
|
|
// RootCert Path to the CA certificate
|
|
|
|
RootCert string
|
|
|
|
// Cert Path to the client certificate
|
|
|
|
Cert string
|
|
|
|
// Key Path to the client private key
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Config) checkSSL() {
|
2022-02-11 11:52:50 +01:00
|
|
|
if s.SSL.Mode == sslDisabledMode || s.SSL.Mode == "" {
|
|
|
|
s.SSL = SSL{Mode: sslDisabledMode}
|
2022-02-11 11:02:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.SSL.RootCert == "" {
|
|
|
|
logging.WithFields(
|
|
|
|
"cert set", s.SSL.Cert != "",
|
|
|
|
"key set", s.SSL.Key != "",
|
|
|
|
"rootCert set", s.SSL.RootCert != "",
|
|
|
|
).Fatal("at least ssl root cert has to be set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) String() string {
|
|
|
|
c.checkSSL()
|
|
|
|
fields := []string{
|
|
|
|
"host=" + c.Host,
|
|
|
|
"port=" + c.Port,
|
2022-02-16 13:30:49 +01:00
|
|
|
"user=" + c.Username,
|
2022-02-11 11:02:47 +01:00
|
|
|
"dbname=" + c.Database,
|
|
|
|
"application_name=zitadel",
|
|
|
|
"sslmode=" + c.SSL.Mode,
|
|
|
|
}
|
|
|
|
if c.Options != "" {
|
|
|
|
fields = append(fields, "options="+c.Options)
|
|
|
|
}
|
|
|
|
if c.Password != "" {
|
|
|
|
fields = append(fields, "password="+c.Password)
|
|
|
|
}
|
|
|
|
if c.SSL.Mode != sslDisabledMode {
|
|
|
|
fields = append(fields, "sslrootcert="+c.SSL.RootCert)
|
|
|
|
if c.SSL.Cert != "" {
|
|
|
|
fields = append(fields, "sslcert="+c.SSL.Cert)
|
|
|
|
}
|
|
|
|
if c.SSL.Key != "" {
|
|
|
|
fields = append(fields, "sslkey="+c.SSL.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(fields, " ")
|
|
|
|
}
|