/
config.go
209 lines (190 loc) · 6.4 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package cmd
import (
"crypto/tls"
"net/smtp"
"os"
"path/filepath"
"regexp"
"strings"
irma "github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/server"
"github.com/privacybydesign/irmago/server/keyshare"
"github.com/go-errors/errors"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func configureEmail() keyshare.EmailConfiguration {
// If username/password are specified for the email server, build an authentication object.
var emailAuth smtp.Auth
if viper.GetString("email_username") != "" {
emailAuth = smtp.PlainAuth(
"",
viper.GetString("email_username"),
viper.GetString("email_password"),
viper.GetString("email_hostname"),
)
}
return keyshare.EmailConfiguration{
EmailServer: viper.GetString("email_server"),
EmailAuth: emailAuth,
EmailFrom: viper.GetString("email_from"),
DefaultLanguage: viper.GetString("default_language"),
}
}
func configureIRMAServer() *server.Configuration {
return &server.Configuration{
SchemesPath: viper.GetString("schemes_path"),
SchemesAssetsPath: viper.GetString("schemes_assets_path"),
SchemesUpdateInterval: viper.GetInt("schemes_update"),
DisableSchemesUpdate: viper.GetInt("schemes_update") == 0,
IssuerPrivateKeysPath: viper.GetString("privkeys"),
RevocationDBType: viper.GetString("revocation_db_type"),
RevocationDBConnStr: viper.GetString("revocation_db_str"),
RevocationSettings: irma.RevocationSettings{},
URL: viper.GetString("url"),
DisableTLS: viper.GetBool("no_tls"),
Email: viper.GetString("email"),
EnableSSE: viper.GetBool("sse"),
Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"),
LogJSON: viper.GetBool("log_json"),
Logger: logger,
Production: viper.GetBool("production"),
MaxSessionLifetime: viper.GetInt("max_session_lifetime"),
JwtIssuer: viper.GetString("jwt_issuer"),
JwtPrivateKey: viper.GetString("jwt_privkey"),
JwtPrivateKeyFile: viper.GetString("jwt_privkey_file"),
AllowUnsignedCallbacks: viper.GetBool("allow_unsigned_callbacks"),
AugmentClientReturnURL: viper.GetBool("augment_client_return_url"),
}
}
func configureTLS() *tls.Config {
conf, err := server.TLSConf(
viper.GetString("tls_cert"),
viper.GetString("tls_cert_file"),
viper.GetString("tls_privkey"),
viper.GetString("tls_privkey_file"))
if err != nil {
die("", err)
}
return conf
}
func readConfig(cmd *cobra.Command, name, logname string, configpaths []string, productionDefaults map[string]interface{}) {
dashReplacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(dashReplacer)
viper.SetEnvPrefix(strings.ToUpper(name))
viper.AutomaticEnv()
// Bind cmd flags to viper, such that configuration files use underscores instead of dashes
f := cmd.Flags()
normalizeFunc := f.GetNormalizeFunc()
f.SetNormalizeFunc(func(fs *pflag.FlagSet, name string) pflag.NormalizedName {
return pflag.NormalizedName(dashReplacer.Replace(string(normalizeFunc(fs, name))))
})
if err := viper.BindPFlags(f); err != nil {
die("", err)
}
// Locate and read configuration file
confpath := viper.GetString("config")
if confpath != "" {
info, err := os.Stat(confpath)
if err != nil {
if os.IsNotExist(err) {
die("specified configuration file does not exist", nil)
} else {
die("failed to stat configuration file", err)
}
}
if info.IsDir() {
die("specified configuration file is a directory", nil)
}
dir, file := filepath.Dir(confpath), filepath.Base(confpath)
viper.SetConfigName(strings.TrimSuffix(file, filepath.Ext(file)))
viper.AddConfigPath(dir)
} else {
viper.SetConfigName(name)
for _, path := range configpaths {
viper.AddConfigPath(path)
}
}
err := viper.ReadInConfig() // Hold error checking until we know how much of it to log
// Create our logger instance
logger = server.NewLogger(viper.GetInt("verbose"), viper.GetBool("quiet"), viper.GetBool("log_json"))
// First log output: hello, development or production mode, log level
mode := "development"
if viper.GetBool("production") {
mode = "production"
for key, val := range productionDefaults {
viper.SetDefault(key, val)
}
}
logger.WithFields(logrus.Fields{
"version": irma.Version,
"mode": mode,
"verbosity": server.Verbosity(viper.GetInt("verbose")),
}).Info(logname + " running")
// Now we finally examine and log any error from viper.ReadInConfig()
if err != nil {
if _, notfound := err.(viper.ConfigFileNotFoundError); notfound {
logger.Info("No configuration file found")
} else {
die("", errors.WrapPrefix(err, "Failed to unmarshal configuration file at "+viper.ConfigFileUsed(), 0))
}
} else {
logger.Info("Config file: ", viper.ConfigFileUsed())
}
}
func handleMapOrString(key string, dest interface{}) error {
var m map[string]interface{}
var err error
if val, flagOrEnv := viper.Get(key).(string); !flagOrEnv || val != "" {
if m, err = cast.ToStringMapE(viper.Get(key)); err != nil {
return errors.WrapPrefix(err, "Failed to unmarshal "+key+" from flag or env var", 0)
}
}
if len(m) == 0 {
return nil
}
if err := mapstructure.Decode(m, dest); err != nil {
return errors.WrapPrefix(err, "Failed to unmarshal "+key+" from config file", 0)
}
return nil
}
func handlePermission(typ string) []string {
if !viper.IsSet(typ) {
if typ == "revoke_perms" || (viper.GetBool("production") && typ == "issue_perms") {
return []string{}
} else {
return []string{"*"}
}
}
perms := viper.GetStringSlice(typ)
if perms == nil {
return []string{}
}
return perms
}
// productionMode examines the arguments passed to the executable to see if --production is enabled.
// (This should really be done using viper, but when the help message is printed, viper is not yet
// initialized.)
func productionMode() bool {
r := regexp.MustCompile("^--production(=(.*))?$")
for _, arg := range os.Args {
matches := r.FindStringSubmatch(arg)
if len(matches) != 3 {
continue
}
if matches[1] == "" {
return true
}
return checkConfVal(matches[2])
}
return checkConfVal(os.Getenv("IRMASERVER_PRODUCTION"))
}
func checkConfVal(val string) bool {
lc := strings.ToLower(val)
return lc == "1" || lc == "true" || lc == "yes" || lc == "t"
}