Adds configuration from config file.
This commit is contained in:
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
@ -9,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/logutils"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// getEnvString returns string from environment variable
|
||||
@ -71,35 +73,35 @@ func initialize() {
|
||||
getEnvInt("HTTP_CLIENT_IDLE_TIMEOUT", 5),
|
||||
"(HTTP_CLIENT_IDLE_TIMEOUT)\ntime in seconds that the internal http client will keep a connection open when idle")
|
||||
// Bind Config
|
||||
flag.StringVar(&config.NamedConfig.TTL,
|
||||
flag.StringVar(&config.Config.ZoneConfig.TTL,
|
||||
"bind-ttl",
|
||||
getEnvString("TTL", "1h"),
|
||||
"(TTL)\nBind zone time to live")
|
||||
flag.StringVar(&config.NamedConfig.Domain,
|
||||
flag.StringVar(&config.Config.ZoneConfig.Domain,
|
||||
"bind-domain",
|
||||
getEnvString("DOMAIN", "example.com"),
|
||||
"(DOMAIN)\nBind zone base domain")
|
||||
flag.StringVar(&config.NamedConfig.Email,
|
||||
flag.StringVar(&config.Config.ZoneConfig.Email,
|
||||
"bind-email",
|
||||
getEnvString("EMAIL", "domain-admin@example.com"),
|
||||
"(EMAIL)\nBind zone authority e-mail address")
|
||||
flag.StringVar(&config.NamedConfig.Timestamp,
|
||||
flag.StringVar(&config.Config.ZoneConfig.Serial,
|
||||
"bind-timestamp",
|
||||
getEnvString("TIMESTAMP", time.Now().In(config.TimeZone).Format("0601021504")),
|
||||
"(TIMESTAMP)\nBind zone serial number")
|
||||
flag.StringVar(&config.NamedConfig.Refresh,
|
||||
flag.StringVar(&config.Config.ZoneConfig.Refresh,
|
||||
"bind-refresh",
|
||||
getEnvString("REFRESH", "1h"),
|
||||
"(REFRESH)\nBind zone refresh time")
|
||||
flag.StringVar(&config.NamedConfig.Retry,
|
||||
flag.StringVar(&config.Config.ZoneConfig.Retry,
|
||||
"bind-retry",
|
||||
getEnvString("RETRY", "30m"),
|
||||
"(RETRY)\nBind zone retry time")
|
||||
flag.StringVar(&config.NamedConfig.Expire,
|
||||
flag.StringVar(&config.Config.ZoneConfig.Expire,
|
||||
"bind-expire",
|
||||
getEnvString("EXPIRE", "1w"),
|
||||
"(EXPIRE)\nBind zone expire time")
|
||||
flag.StringVar(&config.NamedConfig.Minimum,
|
||||
flag.StringVar(&config.Config.ZoneConfig.Minimum,
|
||||
"bind-minimum",
|
||||
getEnvString("MINIMUM", "1h"),
|
||||
"(MINIMUM)\nBind zone minimum time")
|
||||
@ -113,9 +115,13 @@ func initialize() {
|
||||
"(NS2)\nBind zone secondary name-server")
|
||||
// output file
|
||||
flag.StringVar(&config.BindOutputFileName,
|
||||
"filename",
|
||||
getEnvString("FILENAME", "./response-policy.bind"),
|
||||
"output",
|
||||
getEnvString("OUTPUT", "./response-policy.bind"),
|
||||
"(FILENAME)\nWrite local file to filename")
|
||||
flag.StringVar(&config.ConfigFileLocation,
|
||||
"config-file",
|
||||
getEnvString("CONFIG_FILE", ""),
|
||||
"(CONFIG_FILE)\nRead configuration from file")
|
||||
flag.Parse()
|
||||
|
||||
// set logging level
|
||||
@ -139,29 +145,60 @@ func initialize() {
|
||||
log.Printf("[DEBUG] configuration value set: HTTP_CLIENT_CONNECT_TIMEOUT = %v\n", strconv.Itoa(config.HTTPClientConnectTimeout))
|
||||
log.Printf("[DEBUG] configuration value set: HTTP_CLIENT_TLS_TIMEOUT = %v\n", strconv.Itoa(config.HTTPClientTLSHandshakeTimeout))
|
||||
log.Printf("[DEBUG] configuration value set: HTTP_CLIENT_IDLE_TIMEOUT = %v\n", strconv.Itoa(config.HTTPClientIdleTimeout))
|
||||
log.Printf("[DEBUG] configuration value set: TTL = %v\n", config.NamedConfig.TTL)
|
||||
log.Printf("[DEBUG] configuration value set: DOMAIN = %v\n", config.NamedConfig.Domain)
|
||||
log.Printf("[DEBUG] configuration value set: EMAIL = %v\n", config.NamedConfig.Email)
|
||||
log.Printf("[DEBUG] configuration value set: TIMESTAMP = %v\n", config.NamedConfig.Timestamp)
|
||||
log.Printf("[DEBUG] configuration value set: REFRESH = %v\n", config.NamedConfig.Refresh)
|
||||
log.Printf("[DEBUG] configuration value set: RETRY = %v\n", config.NamedConfig.Retry)
|
||||
log.Printf("[DEBUG] configuration value set: EXPIRE = %v\n", config.NamedConfig.Expire)
|
||||
log.Printf("[DEBUG] configuration value set: MINIMUM = %v\n", config.NamedConfig.Minimum)
|
||||
log.Printf("[DEBUG] configuration value set: TTL = %v\n", config.Config.ZoneConfig.TTL)
|
||||
log.Printf("[DEBUG] configuration value set: DOMAIN = %v\n", config.Config.ZoneConfig.Domain)
|
||||
log.Printf("[DEBUG] configuration value set: EMAIL = %v\n", config.Config.ZoneConfig.Email)
|
||||
log.Printf("[DEBUG] configuration value set: TIMESTAMP = %v\n", config.Config.ZoneConfig.Serial)
|
||||
log.Printf("[DEBUG] configuration value set: REFRESH = %v\n", config.Config.ZoneConfig.Refresh)
|
||||
log.Printf("[DEBUG] configuration value set: RETRY = %v\n", config.Config.ZoneConfig.Retry)
|
||||
log.Printf("[DEBUG] configuration value set: EXPIRE = %v\n", config.Config.ZoneConfig.Expire)
|
||||
log.Printf("[DEBUG] configuration value set: MINIMUM = %v\n", config.Config.ZoneConfig.Minimum)
|
||||
log.Printf("[DEBUG] configuration value set: NS1 = %v\n", ns1)
|
||||
log.Printf("[DEBUG] configuration value set: NS1 = %v\n", ns2)
|
||||
log.Printf("[DEBUG] configuration value set: CONFIG_FILE = %v\n", config.ConfigFileLocation)
|
||||
|
||||
// read config file
|
||||
var err error
|
||||
if config.ConfigFileLocation != "" {
|
||||
if config.Config, err = readConfigFile(config.ConfigFileLocation); err != nil {
|
||||
log.Fatalf("[FATAL] Invalid config file: %v\n", err)
|
||||
}
|
||||
if config.Config.ZoneConfig.Serial == "" {
|
||||
config.Config.ZoneConfig.Serial = time.Now().In(config.TimeZone).Format("0601021504")
|
||||
}
|
||||
}
|
||||
|
||||
// set bind-config nameservers
|
||||
if ns1 == "" {
|
||||
if ns1 != "" {
|
||||
config.Config.ZoneConfig.NameServers = append(config.Config.ZoneConfig.NameServers, ns1)
|
||||
}
|
||||
if ns2 != "" {
|
||||
config.Config.ZoneConfig.NameServers = append(config.Config.ZoneConfig.NameServers, ns2)
|
||||
}
|
||||
|
||||
if len(config.Config.ZoneConfig.NameServers) == 0 {
|
||||
log.Printf("[ERROR] A primary name-server must be identified.")
|
||||
flag.PrintDefaults()
|
||||
os.Exit(1)
|
||||
} else {
|
||||
config.NamedConfig.NameServers = append(config.NamedConfig.NameServers, ns1)
|
||||
}
|
||||
if ns2 != "" {
|
||||
config.NamedConfig.NameServers = append(config.NamedConfig.NameServers, ns2)
|
||||
}
|
||||
config.NamedConfig.Email = strings.Replace(config.NamedConfig.Email, "@", ".", -1)
|
||||
|
||||
// bind does not use "@", so we convert it to a "."
|
||||
config.Config.ZoneConfig.Email = strings.Replace(config.Config.ZoneConfig.Email, "@", ".", -1)
|
||||
|
||||
log.Printf("[DEBUG] Initialization Complete\n")
|
||||
}
|
||||
|
||||
func readConfigFile(configFileLocation string) (configFileStruct, error) {
|
||||
var output configFileStruct
|
||||
|
||||
rd, err := ioutil.ReadFile(configFileLocation)
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(rd, &output); err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user