-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
86 lines (81 loc) · 2.3 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
package main
// nolint: lll
import (
"encoding/json"
"io/ioutil"
"github.com/brigadecore/brigade-dockerhub-gateway/internal/webhooks"
"github.com/brigadecore/brigade-foundations/file"
"github.com/brigadecore/brigade-foundations/http"
"github.com/brigadecore/brigade-foundations/os"
"github.com/brigadecore/brigade/sdk/v3/restmachinery"
"github.com/pkg/errors"
)
// apiClientConfig populates the Brigade SDK's APIClientOptions from
// environment variables.
func apiClientConfig() (string, string, restmachinery.APIClientOptions, error) {
opts := restmachinery.APIClientOptions{}
address, err := os.GetRequiredEnvVar("API_ADDRESS")
if err != nil {
return address, "", opts, err
}
token, err := os.GetRequiredEnvVar("API_TOKEN")
if err != nil {
return address, token, opts, err
}
opts.AllowInsecureConnections, err =
os.GetBoolFromEnvVar("API_IGNORE_CERT_WARNINGS", false)
return address, token, opts, err
}
// tokenFilterConfig populates config for the token filter.
func tokenFilterConfig() (webhooks.TokenFilterConfig, error) {
config := webhooks.NewTokenFilterConfig()
tokensPath, err := os.GetRequiredEnvVar("TOKENS_PATH")
if err != nil {
return config, err
}
var exists bool
if exists, err = file.Exists(tokensPath); err != nil {
return config, err
}
if !exists {
return config, errors.Errorf("file %s does not exist", tokensPath)
}
tokenBytes, err := ioutil.ReadFile(tokensPath)
if err != nil {
return config, err
}
plainTextTokens := map[string]string{}
if err :=
json.Unmarshal(tokenBytes, &plainTextTokens); err != nil {
return config, err
}
for _, token := range plainTextTokens {
config.AddToken(token)
}
return config, nil
}
// serverConfig populates configuration for the HTTP/S server from environment
// variables.
func serverConfig() (http.ServerConfig, error) {
config := http.ServerConfig{}
var err error
config.Port, err = os.GetIntFromEnvVar("PORT", 8080)
if err != nil {
return config, err
}
config.TLSEnabled, err = os.GetBoolFromEnvVar("TLS_ENABLED", false)
if err != nil {
return config, err
}
if config.TLSEnabled {
config.TLSCertPath, err = os.GetRequiredEnvVar("TLS_CERT_PATH")
if err != nil {
return config, err
}
config.TLSKeyPath, err = os.GetRequiredEnvVar("TLS_KEY_PATH")
if err != nil {
return config, err
}
}
return config, nil
}