-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
78 lines (61 loc) · 1.57 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"io"
"log"
"os"
"github.com/BurntSushi/toml"
)
type config struct {
Token string `toml:"token"`
Domain string `toml:"domain"`
SubDomain string `toml:"subdomain"`
LogFile string `toml:"logfile"`
TTL *uint64 `toml:"ttl,omitempty"`
SetIPv6 bool `toml:"set_ipv6"`
}
const (
minTTLValue = 900
maxTTLValue = 1209600
)
const allowedPermissions = 0o600
func isPermissionsOk(f *os.File) bool {
finfo, err := f.Stat()
if err != nil {
log.Fatalf("error: %v\n", err)
}
permissions := finfo.Mode().Perm()
return permissions == allowedPermissions
}
func newConfigurationFromFile(path string) *config {
file, err := os.Open(path)
if err != nil {
log.Fatalf("can't open configuration file: %v\n", err)
}
defer closeResource(file)
if !isPermissionsOk(file) {
log.Fatalf("error: configuration file with sensitive information has insecure permissions\n")
}
conf := newConfiguration(file)
return conf
}
func verifyConfiguration(conf *config) {
if conf.Token == "" {
log.Fatal("missed mandatory configuration parameter 'token'")
}
if conf.Domain == "" {
log.Fatal("missed mandatory configuration parameter 'domain'")
}
if conf.TTL != nil {
if *conf.TTL < minTTLValue || *conf.TTL > maxTTLValue {
log.Fatalf("domain TTL value (=%d) exeeds permissible range (=[%d, %d])\n",
*conf.TTL, minTTLValue, maxTTLValue)
}
}
}
func newConfiguration(data io.Reader) *config {
var conf config
if _, err := toml.DecodeReader(data, &conf); err != nil {
log.Fatalf("Couldn't parse configuration file: %v", err)
}
return &conf
}