141 lines
4.4 KiB
Go
141 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/pyke369/golang-support/uconfig"
|
|
)
|
|
|
|
func loadConfig(configFile string) (*HTTPServer, error) {
|
|
// Parse the configuration file
|
|
config, err := uconfig.New(configFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
h := NewHTTPServer(
|
|
config.GetString("config.http.port", "127.0.0.01:8080"),
|
|
config.GetString("config.http.key", ""),
|
|
config.GetString("config.http.cert", ""),
|
|
config.GetString("config.http.crl", ""),
|
|
config.GetString("config.http.ca", ""),
|
|
config.GetString("config.pdns.api-url", "http://127.0.0.1:8081/api/v1/servers/localhost"),
|
|
config.GetString("config.pdns.api-key", ""),
|
|
int(config.GetInteger("config.pdns.timeout", 3)),
|
|
int(config.GetInteger("config.pdns.defaultTTL", 3600)),
|
|
)
|
|
populateHTTPServerZoneProfiles(config, h)
|
|
populateHTTPServerAcls(config, h)
|
|
populateHTTPServerProfiles(config, h)
|
|
populateJSONRPCAcls(config, h)
|
|
return h, err
|
|
}
|
|
|
|
// populateHTTPServerAcls parses the native ACL, and put them into the wanted structures
|
|
func populateHTTPServerAcls(config *uconfig.UConfig, h *HTTPServer) {
|
|
for _, acl := range config.GetPaths("config.pdnsAcls") {
|
|
h.AddPdnsACL(NewPdnsACL(
|
|
config.GetString(acl+".regexp", ""),
|
|
parseConfigArray(config, acl+".perms"),
|
|
parseConfigArray(config, acl+".profiles"),
|
|
))
|
|
}
|
|
}
|
|
|
|
// populateJSONRPCAcls parses the json RPC API ACL, and put them into the wanted structures
|
|
func populateJSONRPCAcls(config *uconfig.UConfig, h *HTTPServer) {
|
|
for _, acl := range config.GetPaths("config.jrpcAcls") {
|
|
perms := map[string][]string{}
|
|
for _, action := range config.GetPaths(acl + ".perms") {
|
|
perms[getSuffix(action)] = parseConfigArray(config, action)
|
|
}
|
|
h.AddjsonRPCACL(NewJSONRPCACL(
|
|
perms,
|
|
parseConfigArray(config, acl+".pgpProfiles"),
|
|
parseConfigArray(config, acl+".sslProfiles"),
|
|
))
|
|
}
|
|
}
|
|
|
|
// populateHTTPServerProfiles parses profiles, and put them into the wanted structures
|
|
func populateHTTPServerProfiles(config *uconfig.UConfig, h *HTTPServer) {
|
|
// convert the configuration into authProfiles
|
|
for _, profile := range config.GetPaths("config.profiles") {
|
|
if p, err := parseConfigProfile(config, profile); err == nil {
|
|
h.AddAuthProfile(getSuffix(profile), p)
|
|
} else {
|
|
log.Println(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// populateHTTPServerZoneProfiles parses the ZoneProfiles and put them into the wanted structures
|
|
func populateHTTPServerZoneProfiles(config *uconfig.UConfig, h *HTTPServer) {
|
|
for _, profile := range config.GetPaths("config.zoneProfile") {
|
|
if err := h.NewZoneProfile(
|
|
getSuffix(profile),
|
|
config.GetString(profile+".soa", ""),
|
|
config.GetBoolean(profile+".default", false),
|
|
config.GetBoolean(profile+".autoIncrement", true),
|
|
parseConfigArray(config, profile+".nameservers"),
|
|
parseConfigArray(config, profile+".whenRegexp"),
|
|
); err != nil {
|
|
log.Println(err)
|
|
}
|
|
for _, entry := range config.GetPaths(profile + ".populate") {
|
|
h.zoneProfiles[getSuffix(profile)].addDefaultEntry(
|
|
config.GetString(entry+".name", ""),
|
|
config.GetString(entry+".type", "txt"),
|
|
config.GetString(entry+".value", ""),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
// parseConfigProfile allows to use the adequate type of profile
|
|
func parseConfigProfile(config *uconfig.UConfig, profile string) (AuthProfile, error) {
|
|
t := config.GetString(profile+".type", "")
|
|
switch t {
|
|
case "regexp":
|
|
return NewAuthProfileRegexp(
|
|
config.GetString(profile+".subjectRegexp", ""),
|
|
config.GetString(profile+".pgpKeys", "")), nil
|
|
case "ldap":
|
|
return NewAuthProfileLdap(config.GetString(profile+".subjectRegexp", ""),
|
|
parseConfigArray(config, profile+".servers"),
|
|
config.GetString(profile+".bindCn", ""),
|
|
config.GetString(profile+".bindPw", ""),
|
|
config.GetString(profile+".baseDN", ""),
|
|
config.GetString(profile+".searchFilter", ""),
|
|
config.GetString(profile+".attribute", ""),
|
|
config.GetString(profile+".pgpAttribute", "pgpKey"),
|
|
parseConfigArray(config, profile+".validValues"),
|
|
config.GetBoolean(profile+".ssl", true),
|
|
)
|
|
default:
|
|
return nil, fmt.Errorf("Unknown profile type %s", t)
|
|
}
|
|
}
|
|
|
|
// Helper functions
|
|
|
|
// parseConfigArray parses a uconf array
|
|
func parseConfigArray(config *uconfig.UConfig, configpath string) []string {
|
|
result := []string{}
|
|
for _, i := range config.GetPaths(configpath) {
|
|
if s := config.GetString(i, ""); s == "" {
|
|
continue
|
|
} else {
|
|
result = append(result, s)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// getSuffix gets the last part of a config path
|
|
func getSuffix(s string) string {
|
|
sl := strings.Split(s, ".")
|
|
return sl[len(sl)-1]
|
|
}
|