Try to read config from under /etc (#18)

This commit is contained in:
Joona Hoikkala
2017-11-14 00:42:30 +02:00
committed by GitHub
parent 9c639223ce
commit 9c54da3ee6
7 changed files with 41 additions and 28 deletions

View File

@ -24,7 +24,7 @@ ignored = ["gopkg.in/kataras/iris.v5", "github.com/iris-contrib/letsencrypt"]
[[constraint]] [[constraint]]
name = "github.com/kataras/iris" name = "github.com/kataras/iris"
version = "8.5.7" version = "8.5.8"
[[constraint]] [[constraint]]
name = "github.com/iris-contrib/middleware" name = "github.com/iris-contrib/middleware"

6
api.go
View File

@ -27,8 +27,8 @@ func (a authMiddleware) Serve(ctx iris.Context) {
// Password ok // Password ok
// Now test for the possibly limited ranges // Now test for the possibly limited ranges
if DNSConf.API.UseHeader { if Config.API.UseHeader {
ips := getIPListFromHeader(ctx.GetHeader(DNSConf.API.HeaderName)) ips := getIPListFromHeader(ctx.GetHeader(Config.API.HeaderName))
allowUpdate = au.allowedFromList(ips) allowUpdate = au.allowedFromList(ips)
} else { } else {
allowUpdate = au.allowedFrom(ctx.RemoteAddr()) allowUpdate = au.allowedFrom(ctx.RemoteAddr())
@ -72,7 +72,7 @@ func webRegisterPost(ctx iris.Context) {
regStatus = iris.StatusInternalServerError regStatus = iris.StatusInternalServerError
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error in registration") log.WithFields(log.Fields{"error": err.Error()}).Debug("Error in registration")
} else { } else {
regJSON = iris.Map{"username": nu.Username, "password": nu.Password, "fulldomain": nu.Subdomain + "." + DNSConf.General.Domain, "subdomain": nu.Subdomain, "allowfrom": nu.AllowFrom.ValidEntries()} regJSON = iris.Map{"username": nu.Username, "password": nu.Password, "fulldomain": nu.Subdomain + "." + Config.General.Domain, "subdomain": nu.Subdomain, "allowfrom": nu.AllowFrom.ValidEntries()}
regStatus = iris.StatusCreated regStatus = iris.StatusCreated
log.WithFields(log.Fields{"user": nu.Username.String()}).Debug("Created new user") log.WithFields(log.Fields{"user": nu.Username.String()}).Debug("Created new user")

10
db.go
View File

@ -7,10 +7,10 @@ import (
"regexp" "regexp"
"time" "time"
log "github.com/sirupsen/logrus"
_ "github.com/lib/pq" _ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/satori/go.uuid" "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
@ -62,7 +62,7 @@ func (d *acmedb) Register(afrom cidrslice) (ACMETxt, error) {
LastActive, LastActive,
AllowFrom) AllowFrom)
values($1, $2, $3, '', $4, $5)` values($1, $2, $3, '', $4, $5)`
if DNSConf.Database.Engine == "sqlite3" { if Config.Database.Engine == "sqlite3" {
regSQL = getSQLiteStmt(regSQL) regSQL = getSQLiteStmt(regSQL)
} }
sm, err := d.DB.Prepare(regSQL) sm, err := d.DB.Prepare(regSQL)
@ -87,7 +87,7 @@ func (d *acmedb) GetByUsername(u uuid.UUID) (ACMETxt, error) {
FROM records FROM records
WHERE Username=$1 LIMIT 1 WHERE Username=$1 LIMIT 1
` `
if DNSConf.Database.Engine == "sqlite3" { if Config.Database.Engine == "sqlite3" {
getSQL = getSQLiteStmt(getSQL) getSQL = getSQLiteStmt(getSQL)
} }
@ -126,7 +126,7 @@ func (d *acmedb) GetByDomain(domain string) ([]ACMETxt, error) {
FROM records FROM records
WHERE Subdomain=$1 LIMIT 1 WHERE Subdomain=$1 LIMIT 1
` `
if DNSConf.Database.Engine == "sqlite3" { if Config.Database.Engine == "sqlite3" {
getSQL = getSQLiteStmt(getSQL) getSQL = getSQLiteStmt(getSQL)
} }
@ -160,7 +160,7 @@ func (d *acmedb) Update(a ACMETxt) error {
UPDATE records SET Value=$1, LastActive=$2 UPDATE records SET Value=$1, LastActive=$2
WHERE Username=$3 AND Subdomain=$4 WHERE Username=$3 AND Subdomain=$4
` `
if DNSConf.Database.Engine == "sqlite3" { if Config.Database.Engine == "sqlite3" {
updSQL = getSQLiteStmt(updSQL) updSQL = getSQLiteStmt(updSQL)
} }

28
main.go
View File

@ -12,17 +12,21 @@ import (
func main() { func main() {
// Read global config // Read global config
configTmp := readConfig("config.cfg") var Config DNSConfig
DNSConf = configTmp if fileExists("/etc/acme-dns/config.cfg") {
Config = readConfig("/etc/acme-dns/config.cfg")
} else {
Config = readConfig("config.cfg")
}
setupLogging(DNSConf.Logconfig.Format, DNSConf.Logconfig.Level) setupLogging(Config.Logconfig.Format, Config.Logconfig.Level)
// Read the default records in // Read the default records in
RR.Parse(DNSConf.General) RR.Parse(Config.General)
// Open database // Open database
newDB := new(acmedb) newDB := new(acmedb)
err := newDB.Init(DNSConf.Database.Engine, DNSConf.Database.Connection) err := newDB.Init(Config.Database.Engine, Config.Database.Connection)
if err != nil { if err != nil {
log.Errorf("Could not open database [%v]", err) log.Errorf("Could not open database [%v]", err)
os.Exit(1) os.Exit(1)
@ -31,7 +35,7 @@ func main() {
defer DB.Close() defer DB.Close()
// DNS server // DNS server
startDNS(DNSConf.General.Listen, DNSConf.General.Proto) startDNS(Config.General.Listen, Config.General.Proto)
// HTTP API // HTTP API
startHTTPAPI() startHTTPAPI()
@ -42,21 +46,21 @@ func main() {
func startHTTPAPI() { func startHTTPAPI() {
api := iris.New() api := iris.New()
api.Use(cors.New(cors.Options{ api.Use(cors.New(cors.Options{
AllowedOrigins: DNSConf.API.CorsOrigins, AllowedOrigins: Config.API.CorsOrigins,
AllowedMethods: []string{"GET", "POST"}, AllowedMethods: []string{"GET", "POST"},
OptionsPassthrough: false, OptionsPassthrough: false,
Debug: DNSConf.General.Debug, Debug: Config.General.Debug,
})) }))
var ForceAuth = authMiddleware{} var ForceAuth = authMiddleware{}
api.Post("/register", webRegisterPost) api.Post("/register", webRegisterPost)
api.Post("/update", ForceAuth.Serve, webUpdatePost) api.Post("/update", ForceAuth.Serve, webUpdatePost)
host := DNSConf.API.Domain + ":" + DNSConf.API.Port host := Config.API.Domain + ":" + Config.API.Port
switch DNSConf.API.TLS { switch Config.API.TLS {
case "letsencrypt": case "letsencrypt":
api.Run(iris.AutoTLS(host, DNSConf.API.Domain, DNSConf.API.LEmail), iris.WithoutBodyConsumptionOnUnmarshal) api.Run(iris.AutoTLS(host, Config.API.Domain, Config.API.LEmail), iris.WithoutBodyConsumptionOnUnmarshal)
case "cert": case "cert":
api.Run(iris.TLS(host, DNSConf.API.TLSCertFullchain, DNSConf.API.TLSCertPrivkey), iris.WithoutBodyConsumptionOnUnmarshal) api.Run(iris.TLS(host, Config.API.TLSCertFullchain, Config.API.TLSCertPrivkey), iris.WithoutBodyConsumptionOnUnmarshal)
default: default:
api.Run(iris.Addr(host), iris.WithoutBodyConsumptionOnUnmarshal) api.Run(iris.Addr(host), iris.WithoutBodyConsumptionOnUnmarshal)
} }

View File

@ -26,19 +26,19 @@ var records = []string{
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
setupTestLogger() setupTestLogger()
setupConfig() setupConfig()
RR.Parse(DNSConf.General) RR.Parse(Config.General)
flag.Parse() flag.Parse()
newDb := new(acmedb) newDb := new(acmedb)
if *postgres { if *postgres {
DNSConf.Database.Engine = "postgres" Config.Database.Engine = "postgres"
err := newDb.Init("postgres", "postgres://acmedns:acmedns@localhost/acmedns") err := newDb.Init("postgres", "postgres://acmedns:acmedns@localhost/acmedns")
if err != nil { if err != nil {
fmt.Println("PostgreSQL integration tests expect database \"acmedns\" running in localhost, with username and password set to \"acmedns\"") fmt.Println("PostgreSQL integration tests expect database \"acmedns\" running in localhost, with username and password set to \"acmedns\"")
os.Exit(1) os.Exit(1)
} }
} else { } else {
DNSConf.Database.Engine = "sqlite3" Config.Database.Engine = "sqlite3"
_ = newDb.Init("sqlite3", ":memory:") _ = newDb.Init("sqlite3", ":memory:")
} }
DB = newDb DB = newDb
@ -78,7 +78,7 @@ func setupConfig() {
API: httpapicfg, API: httpapicfg,
} }
DNSConf = dnscfg Config = dnscfg
} }
func setupTestLogger() { func setupTestLogger() {

View File

@ -7,8 +7,8 @@ import (
"sync" "sync"
) )
// DNSConf is global configuration struct // Config is global configuration struct
var DNSConf DNSConfig var Config DNSConfig
// DB is used to access the database functions in acme-dns // DB is used to access the database functions in acme-dns
var DB database var DB database

11
util.go
View File

@ -3,14 +3,23 @@ package main
import ( import (
"crypto/rand" "crypto/rand"
"math/big" "math/big"
"os"
"regexp" "regexp"
"strings" "strings"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
"github.com/miekg/dns" "github.com/miekg/dns"
log "github.com/sirupsen/logrus"
) )
func fileExists(fname string) bool {
_, err := os.Stat(fname)
if err != nil {
return false
}
return true
}
func readConfig(fname string) DNSConfig { func readConfig(fname string) DNSConfig {
var conf DNSConfig var conf DNSConfig
// Practically never errors // Practically never errors