suporte a dynamic dns
This commit is contained in:
@@ -17,6 +17,17 @@ type Config struct {
|
||||
PDNSAPIKey string `yaml:"pdns_api_key" env:"PDNS_API_KEY"`
|
||||
PDNSServerID string `yaml:"pdns_server_id" env:"PDNS_SERVER_ID" env-default:"localhost"`
|
||||
Auth AuthConfig `yaml:"auth"`
|
||||
Database Database `yaml:"database"`
|
||||
}
|
||||
|
||||
// Database contains application database configuration.
|
||||
type Database struct {
|
||||
MySQL MySQLDatabase `yaml:"mysql"`
|
||||
}
|
||||
|
||||
// MySQLDatabase contains MySQL connection settings.
|
||||
type MySQLDatabase struct {
|
||||
DSN string `yaml:"dsn" env:"DATABASE_MYSQL_DSN"`
|
||||
}
|
||||
|
||||
type AuthConfig struct {
|
||||
@@ -63,6 +74,9 @@ func LoadFile(path string) (Config, error) {
|
||||
if cfg.PDNSAPIKey == "" {
|
||||
return Config{}, errors.New("PDNS_API_KEY is required")
|
||||
}
|
||||
if cfg.Database.MySQL.DSN == "" {
|
||||
return Config{}, errors.New("DATABASE_MYSQL_DSN is required")
|
||||
}
|
||||
if err := validateAuth(cfg.Auth); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
@@ -113,6 +127,7 @@ func normalize(cfg *Config) {
|
||||
cfg.PDNSAPIURL = strings.TrimSpace(cfg.PDNSAPIURL)
|
||||
cfg.PDNSAPIKey = strings.TrimSpace(cfg.PDNSAPIKey)
|
||||
cfg.PDNSServerID = strings.TrimSpace(cfg.PDNSServerID)
|
||||
cfg.Database.MySQL.DSN = strings.TrimSpace(cfg.Database.MySQL.DSN)
|
||||
cfg.Auth.LDAP.URL = strings.TrimSpace(cfg.Auth.LDAP.URL)
|
||||
cfg.Auth.LDAP.BindDN = strings.TrimSpace(cfg.Auth.LDAP.BindDN)
|
||||
cfg.Auth.LDAP.BindPassword = strings.TrimSpace(cfg.Auth.LDAP.BindPassword)
|
||||
|
||||
@@ -13,6 +13,9 @@ addr: ":9000"
|
||||
pdns_api_url: "http://pdns.example.test:8081"
|
||||
pdns_api_key: "from-file"
|
||||
pdns_server_id: "authoritative"
|
||||
database:
|
||||
mysql:
|
||||
dsn: "user:pass@tcp(mysql:3306)/pdns_admin"
|
||||
auth:
|
||||
disabled: true
|
||||
`)
|
||||
@@ -34,16 +37,23 @@ auth:
|
||||
if cfg.PDNSServerID != "authoritative" {
|
||||
t.Fatalf("unexpected server id: %q", cfg.PDNSServerID)
|
||||
}
|
||||
if cfg.Database.MySQL.DSN != "user:pass@tcp(mysql:3306)/pdns_admin" {
|
||||
t.Fatalf("unexpected mysql dsn: %q", cfg.Database.MySQL.DSN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFileEnvironmentOverridesYAML(t *testing.T) {
|
||||
clearConfigEnv(t)
|
||||
t.Setenv("PDNS_API_KEY", "from-env")
|
||||
t.Setenv("PDNS_API_URL", "http://env.example.test:8081")
|
||||
t.Setenv("DATABASE_MYSQL_DSN", "env:pass@tcp(mysql:3306)/pdns_admin")
|
||||
path := writeConfig(t, `
|
||||
pdns_api_url: "http://file.example.test:8081"
|
||||
pdns_api_key: "from-file"
|
||||
pdns_server_id: "from-file"
|
||||
database:
|
||||
mysql:
|
||||
dsn: "file:pass@tcp(mysql:3306)/pdns_admin"
|
||||
auth:
|
||||
disabled: true
|
||||
`)
|
||||
@@ -62,11 +72,15 @@ auth:
|
||||
if cfg.PDNSServerID != "from-file" {
|
||||
t.Fatalf("expected yaml server id, got %q", cfg.PDNSServerID)
|
||||
}
|
||||
if cfg.Database.MySQL.DSN != "env:pass@tcp(mysql:3306)/pdns_admin" {
|
||||
t.Fatalf("expected env mysql dsn, got %q", cfg.Database.MySQL.DSN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFileUsesDefaultsWithoutYAML(t *testing.T) {
|
||||
clearConfigEnv(t)
|
||||
t.Setenv("PDNS_API_KEY", "secret")
|
||||
t.Setenv("DATABASE_MYSQL_DSN", "user:pass@tcp(mysql:3306)/pdns_admin")
|
||||
t.Setenv("AUTH_DISABLED", "true")
|
||||
|
||||
cfg, err := LoadFile("")
|
||||
@@ -87,6 +101,7 @@ func TestLoadFileUsesDefaultsWithoutYAML(t *testing.T) {
|
||||
|
||||
func TestLoadFileRequiresAPIKey(t *testing.T) {
|
||||
clearConfigEnv(t)
|
||||
t.Setenv("DATABASE_MYSQL_DSN", "user:pass@tcp(mysql:3306)/pdns_admin")
|
||||
|
||||
_, err := LoadFile("")
|
||||
if err == nil {
|
||||
@@ -94,10 +109,24 @@ func TestLoadFileRequiresAPIKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFileRequiresMySQLDSN(t *testing.T) {
|
||||
clearConfigEnv(t)
|
||||
t.Setenv("PDNS_API_KEY", "secret")
|
||||
t.Setenv("AUTH_DISABLED", "true")
|
||||
|
||||
_, err := LoadFile("")
|
||||
if err == nil {
|
||||
t.Fatal("expected missing mysql dsn error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFileReadsLDAPConfig(t *testing.T) {
|
||||
clearConfigEnv(t)
|
||||
path := writeConfig(t, `
|
||||
pdns_api_key: "secret"
|
||||
database:
|
||||
mysql:
|
||||
dsn: "user:pass@tcp(mysql:3306)/pdns_admin"
|
||||
auth:
|
||||
ldap:
|
||||
url: ldap://ldap.example.com:389
|
||||
@@ -139,6 +168,9 @@ func TestLoadFileLDAPEnvironmentOverridesYAML(t *testing.T) {
|
||||
t.Setenv("AUTH_LDAP_BIND_PASSWORD", "from-env")
|
||||
path := writeConfig(t, `
|
||||
pdns_api_key: "secret"
|
||||
database:
|
||||
mysql:
|
||||
dsn: "user:pass@tcp(mysql:3306)/pdns_admin"
|
||||
auth:
|
||||
ldap:
|
||||
url: ldap://file.example.com:389
|
||||
@@ -162,6 +194,7 @@ auth:
|
||||
func TestLoadFileRequiresLDAPUnlessAuthDisabled(t *testing.T) {
|
||||
clearConfigEnv(t)
|
||||
t.Setenv("PDNS_API_KEY", "secret")
|
||||
t.Setenv("DATABASE_MYSQL_DSN", "user:pass@tcp(mysql:3306)/pdns_admin")
|
||||
|
||||
_, err := LoadFile("")
|
||||
if err == nil {
|
||||
@@ -252,6 +285,7 @@ func clearConfigEnv(t *testing.T) {
|
||||
"PDNS_API_URL",
|
||||
"PDNS_API_KEY",
|
||||
"PDNS_SERVER_ID",
|
||||
"DATABASE_MYSQL_DSN",
|
||||
"AUTH_DISABLED",
|
||||
"AUTH_LDAP_URL",
|
||||
"AUTH_LDAP_START_TLS",
|
||||
|
||||
Reference in New Issue
Block a user