-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
149 lines (122 loc) · 3.17 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"errors"
"io/ioutil"
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/inconshreveable/log15"
)
type Config struct {
Domain string `toml:"domain"`
PrivateAccountAddress string `toml:"private_address"`
OutboundAddress string `toml:"outbound_address"`
AwsRegion string `toml:"aws_region"`
Bucket Bucket `toml:"bucket"`
Routes []Route `toml:"route"`
}
type Route struct {
Src string `toml:"src"`
Dst string `toml:"dst"`
SNS string `toml:"sns"`
Forward bool `toml:"forward"`
AllowSuspectMessages bool `toml:"allow_suspect_messages"`
Drop bool `toml:"drop"`
}
type Bucket struct {
Name string `toml:"name"`
MsgPrefix string `toml:"msg_prefix"`
ForwardMetaPrefix string `toml:"forward_meta_prefix"`
OutboxPrefix string `toml:"outbox_prefix"`
}
func (c *Config) PrivateAccountDomain() string {
parts := strings.Split(c.PrivateAccountAddress, "@")
return parts[len(parts)-1]
}
func (c *Config) PrivateAccountMailbox() string {
parts := strings.Split(c.PrivateAccountAddress, "@")
return parts[0]
}
func loadCloudConfig(lgr log15.Logger) *Config {
bucketName := os.Getenv("S3_CONFIG_BUCKET")
confPath := os.Getenv("S3_CONFIG_PATH")
if bucketName == "" || confPath == "" {
lgr.Error("no_s3_env_config_found")
return nil
}
sess := session.Must(session.NewSession())
s3client := s3.New(sess, &aws.Config{
Region: aws.String("us-east-1"),
})
confResp, err := s3client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(confPath),
})
if err != nil {
lgr.Error("fetch_lambda_conf_err", "err", err)
return nil
}
defer confResp.Body.Close()
var conf Config
_, err = toml.DecodeReader(confResp.Body, &conf)
if err != nil {
lgr.Error("toml_config_decode_err", "err", err)
return nil
}
return &conf
}
func loadConfig() *Config {
lgr := log15.New()
conf := loadCloudConfig(lgr)
if conf != nil {
lgr.Info("loaded_config_from_cloud")
return conf
}
return loadLocalConfig()
}
func loadLocalConfig() *Config {
tml, err := ioutil.ReadFile("config.toml")
if err != nil {
panic(err)
}
var conf Config
err = toml.Unmarshal(tml, &conf)
if err != nil {
panic(err)
}
err = conf.validate()
if err != nil {
panic(err)
}
return &conf
}
func (c *Config) validate() error {
if c.Domain == "" {
return errors.New("domain must be set")
}
if c.PrivateAccountAddress == "" {
return errors.New("private_address must be set")
}
if c.OutboundAddress == "" {
return errors.New("outbound_address must be set")
}
if c.AwsRegion == "" {
return errors.New("aws_region must be set")
}
if c.Bucket.Name == "" {
return errors.New("bucket.name must be set")
}
if c.Bucket.MsgPrefix == "" {
return errors.New("bucket.msg_prefix must be set")
}
if c.Bucket.ForwardMetaPrefix == "" {
return errors.New("bucket.forward_meta_prefix must be set")
}
if c.Bucket.OutboxPrefix == "" {
return errors.New("bucket.outbox_prefix must be set")
}
return nil
}