/
config.go
197 lines (183 loc) · 5.34 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
package tools
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"os"
"time"
"github.com/meilisearch/meilisearch-go"
uuid "github.com/satori/go.uuid"
"github.com/spf13/viper"
)
var Cfg Config
var Loc *time.Location
func LoadConfig() {
initCache()
var err error
Loc, err = time.LoadLocation("Europe/Berlin")
if err != nil {
logger.Error("tools.config.LoadConfig: can't get time.location", "err", err)
}
initConfig()
}
const rsaKeySize = 2048
func initConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/TUM-Live/")
viper.AddConfigPath("$HOME/.TUM-Live")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
if errors.Is(err, viper.ConfigFileNotFoundError{}) {
logger.Warn("tools.config.LoadConfig: can't find config file", "err", err)
} else {
panic(fmt.Errorf("fatal error config file: %v", err))
}
}
logger.Info("Using Config file " + viper.ConfigFileUsed())
err = viper.Unmarshal(&Cfg)
if err != nil {
panic(fmt.Errorf("fatal error config file: %v", err))
}
// set defaults
if Cfg.WorkerToken == "" {
Cfg.WorkerToken = uuid.NewV4().String()
viper.Set("workerToken", Cfg.WorkerToken)
err = viper.WriteConfig()
if err != nil {
logger.Warn("Can't write out config ", "err", err)
}
}
if Cfg.JWTKey == nil {
logger.Info("Generating new JWT key")
JWTKey, err := rsa.GenerateKey(rand.Reader, rsaKeySize)
if err != nil {
logger.Error("Can't generate JWT key", "err", err)
}
armoured := string(pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(JWTKey),
},
))
viper.Set("jwtKey", armoured)
err = viper.WriteConfig()
if err != nil {
logger.Warn("Can't write out config ", "err", err)
}
jwtKey = JWTKey
} else {
k, _ := pem.Decode([]byte(*Cfg.JWTKey))
key, err := x509.ParsePKCS1PrivateKey(k.Bytes)
if err != nil {
logger.Error("Can't parse JWT key", "err", err)
return
}
jwtKey = key
}
// allow overwriting database host with env var, mainly for testing with docker-compose
if os.Getenv("DBHOST") != "" {
Cfg.Db.Host = os.Getenv("DBHOST")
}
}
type Config struct {
Lrz struct {
Name string `yaml:"name"`
Email string `yaml:"email"`
Phone string `yaml:"phone"`
UploadURL string `yaml:"uploadUrl"`
SubDir string `yaml:"subDir"`
} `yaml:"lrz"`
Mail MailConfig `yaml:"mail"`
Db struct {
User string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"database"`
Host string `yaml:"host"`
Port uint `yaml:"port"`
} `yaml:"db"`
Campus struct {
Base string `yaml:"base"`
Tokens []string `yaml:"tokens"`
CampusProxy *struct {
Host string `yaml:"host"`
Scheme string `yaml:"scheme"`
} `yaml:"campusProxy"`
RelevantOrgs *[]string `yaml:"relevantOrgs"`
} `yaml:"campus"`
Ldap struct {
URL string `yaml:"url"`
User string `yaml:"user"`
Password string `yaml:"password"`
BaseDn string `yaml:"baseDn"`
UserDn string `yaml:"userDn"`
UseForLogin bool `yaml:"useForLogin"`
} `yaml:"ldap"`
Saml *struct {
IdpMetadataURL string `yaml:"idpMetadataURL"`
Cert string `yaml:"cert"`
Privkey string `yaml:"privkey"`
EntityID string `yaml:"entityID"`
RootURLs []string `yaml:"rootURL"`
IdpName string `yaml:"idpName"`
IdpColor string `yaml:"idpColor"`
} `yaml:"saml"`
Paths struct {
Static string `yaml:"static"`
Mass string `yaml:"mass"`
Branding string `yaml:"branding"`
} `yaml:"paths"`
Auths struct {
SmpUser string `yaml:"smpUser"` // todo, do we need this? Should this be in the lecture_halls table?
SmpPassword string `yaml:"smpPassword"`
PwrCrtlAuth string `yaml:"pwrCrtlAuth"`
CamAuth string `yaml:"camAuth"`
} `yaml:"auths"`
Alerts *struct {
Matrix *struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Homeserver string `yaml:"homeserver"`
LogRoomID string `yaml:"logRoomID"`
AlertRoomID string `yaml:"alertRoomId"`
} `yaml:"matrix"`
} `yaml:"alerts"`
VoiceService *struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
}
IngestBase string `yaml:"ingestBase"`
WebUrl string `yaml:"webUrl"`
WorkerToken string `yaml:"workerToken"` // used for workers to join the worker pool
JWTKey *string `yaml:"jwtKey"`
Meili *struct {
Host string `yaml:"host"`
ApiKey string `yaml:"apiKey"`
} `yaml:"meili"`
VodURLTemplate string `yaml:"vodURLTemplate"`
CanonicalURL string `yaml:"canonicalURL"`
}
type MailConfig struct {
Sender string `yaml:"sender"`
Server string `yaml:"server"`
SMIMECert string `yaml:"SMIMECert"`
SMIMEKey string `yaml:"SMIMEKey"`
MaxMailsPerMinute int `yaml:"maxMailsPerMinute"`
}
func (Config) GetJWTKey() *rsa.PrivateKey {
return jwtKey
}
var ErrMeiliNotConfigured = errors.New("meilisearch is not configured")
func (c Config) GetMeiliClient() (*meilisearch.Client, error) {
if c.Meili == nil {
return nil, ErrMeiliNotConfigured
}
return meilisearch.NewClient(meilisearch.ClientConfig{Host: c.Meili.Host, APIKey: c.Meili.ApiKey}), nil
}
var jwtKey *rsa.PrivateKey
// CookieSecure sets whether to use secure cookies or not, defaults to false in dev mode, true in production
var CookieSecure = false