forked from algolia/sup3rS3cretMes5age
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
74 lines (61 loc) · 3.23 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
package main
import (
"log"
"os"
"strings"
)
type conf struct {
HttpBindingAddress string
HttpsBindingAddress string
HttpsRedirectEnabled bool
TLSAutoDomain string
TLSCertFilepath string
TLSCertKeyFilepath string
}
const HttpBindingAddressVarenv = "SUPERSECRETMESSAGE_HTTP_BINDING_ADDRESS"
const HttpsBindingAddressVarenv = "SUPERSECRETMESSAGE_HTTPS_BINDING_ADDRESS"
const HttpsRedirectEnabledVarenv = "SUPERSECRETMESSAGE_HTTPS_REDIRECT_ENABLED"
const TLSAutoDomainVarenv = "SUPERSECRETMESSAGE_TLS_AUTO_DOMAIN"
const TLSCertFilepathVarenv = "SUPERSECRETMESSAGE_TLS_CERT_FILEPATH"
const TLSCertKeyFilepathVarenv = "SUPERSECRETMESSAGE_TLS_CERT_KEY_FILEPATH"
func loadConfig() conf {
var cnf conf
cnf.HttpBindingAddress = os.Getenv(HttpBindingAddressVarenv)
cnf.HttpsBindingAddress = os.Getenv(HttpsBindingAddressVarenv)
cnf.HttpsRedirectEnabled = strings.ToLower(os.Getenv(HttpsRedirectEnabledVarenv)) == "true"
cnf.TLSAutoDomain = os.Getenv(TLSAutoDomainVarenv)
cnf.TLSCertFilepath = os.Getenv(TLSCertFilepathVarenv)
cnf.TLSCertKeyFilepath = os.Getenv(TLSCertKeyFilepathVarenv)
if cnf.TLSAutoDomain != "" && (cnf.TLSCertFilepath != "" || cnf.TLSCertKeyFilepath != "") {
log.Fatalf("Auto TLS (%s) is mutually exclusive with manual TLS (%s and %s)", TLSAutoDomainVarenv,
TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
if (cnf.TLSCertFilepath != "" && cnf.TLSCertKeyFilepath == "") ||
(cnf.TLSCertFilepath == "" && cnf.TLSCertKeyFilepath != "") {
log.Fatalf("Both certificate filepath (%s) and certificate key filepath (%s) must be set when using manual TLS",
TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
if cnf.HttpsBindingAddress == "" && (cnf.TLSAutoDomain != "" || cnf.TLSCertFilepath != "") {
log.Fatalf("HTTPS binding address (%s) must be set when using either auto TLS (%s) or manual TLS (%s and %s)",
HttpsBindingAddressVarenv, TLSAutoDomainVarenv, TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
if cnf.HttpsRedirectEnabled && cnf.TLSAutoDomain == "" && cnf.TLSCertFilepath == "" {
log.Fatalf("Either auto TLS (%s) or manual TLS (%s and %s) must be enabled to enable HTTPS redirection (%s)",
TLSAutoDomainVarenv, TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv, HttpsRedirectEnabledVarenv)
}
if cnf.HttpBindingAddress == "" && cnf.TLSAutoDomain == "" && cnf.TLSCertFilepath == "" {
log.Fatalf("HTTP binding address (%s) must be set if auto TLS (%s) and manual TLS (%s and %s) are both disabled",
HttpBindingAddressVarenv, TLSAutoDomainVarenv, TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
if cnf.HttpsBindingAddress != "" && cnf.TLSAutoDomain == "" && cnf.TLSCertFilepath == "" {
log.Fatalf("HTTPS binding address (%s) is set but neither auto TLS (%s) nor manual TLS (%s and %s) are enabled",
HttpsBindingAddressVarenv, TLSAutoDomainVarenv, TLSCertFilepathVarenv, TLSCertKeyFilepathVarenv)
}
log.Println("[INFO] HTTP Binding Address:", cnf.HttpBindingAddress)
log.Println("[INFO] HTTPS Binding Address:", cnf.HttpsBindingAddress)
log.Println("[INFO] HTTPS Redirect enabled:", cnf.HttpsRedirectEnabled)
log.Println("[INFO] TLS Auto Domain:", cnf.TLSAutoDomain)
log.Println("[INFO] TLS Cert Filepath:", cnf.TLSCertFilepath)
log.Println("[INFO] TLS Cert Key Filepath:", cnf.TLSCertKeyFilepath)
return cnf
}