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]]
name = "github.com/kataras/iris"
version = "8.5.7"
version = "8.5.8"
[[constraint]]
name = "github.com/iris-contrib/middleware"

6
api.go
View File

@ -27,8 +27,8 @@ func (a authMiddleware) Serve(ctx iris.Context) {
// Password ok
// Now test for the possibly limited ranges
if DNSConf.API.UseHeader {
ips := getIPListFromHeader(ctx.GetHeader(DNSConf.API.HeaderName))
if Config.API.UseHeader {
ips := getIPListFromHeader(ctx.GetHeader(Config.API.HeaderName))
allowUpdate = au.allowedFromList(ips)
} else {
allowUpdate = au.allowedFrom(ctx.RemoteAddr())
@ -72,7 +72,7 @@ func webRegisterPost(ctx iris.Context) {
regStatus = iris.StatusInternalServerError
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error in registration")
} 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
log.WithFields(log.Fields{"user": nu.Username.String()}).Debug("Created new user")

10
db.go
View File

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

28
main.go
View File

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

View File

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

View File

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

11
util.go
View File

@ -3,14 +3,23 @@ package main
import (
"crypto/rand"
"math/big"
"os"
"regexp"
"strings"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
"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 {
var conf DNSConfig
// Practically never errors