forked from moonstream-to/waggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
233 lines (203 loc) · 6.8 KB
/
settings.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
var (
AWS_REGION = os.Getenv("AWS_REGION")
MOONSTREAM_ACCESS_TOKEN = os.Getenv("MOONSTREAM_ACCESS_TOKEN")
MOONSTREAM_API_URL = os.Getenv("MOONSTREAM_API_URL")
MOONSTREAM_API_TIMEOUT_SECONDS = os.Getenv("MOONSTREAM_API_TIMEOUT_SECONDS")
BROOD_API_URL = os.Getenv("BUGOUT_AUTH_URL")
SPIRE_API_URL = os.Getenv("BUGOUT_SPIRE_URL")
BUGOUT_API_TIMEOUT_SECONDS = os.Getenv("BUGOUT_API_TIMEOUT_SECONDS")
BUGOUT_WAGGLE_ADMIN_ACCESS_TOKEN = os.Getenv("BUGOUT_WAGGLE_ADMIN_ACCESS_TOKEN")
BUGOUT_METATX_JOBS_JOURNAL_ID = os.Getenv("BUGOUT_METATX_JOBS_JOURNAL_ID")
BUGOUT_ACCESS_TOKEN = os.Getenv("BUGOUT_ACCESS_TOKEN")
WAGGLE_CORS_ALLOWED_ORIGINS = os.Getenv("WAGGLE_CORS_ALLOWED_ORIGINS")
BUGOUT_RESOURCE_TYPE_WAGGLE_ACCESS = "waggle-access"
CASER = cases.Title(language.English)
)
type ServerSignerConfigParsed struct {
Keyfile []byte
Password string
}
type ServerSignerConfig struct {
Keyfile string `json:"keyfile"`
KeyfileType string `json:"keyfile_type"`
Password string `json:"password"`
PasswordType string `json:"password_type"`
}
type ServerConfig struct {
AccessResourceId string `json:"access_resource_id"`
Signers []ServerSignerConfig `json:"signers"`
}
type KeyfileType string
type PasswordType string
const (
AwsSecretKeyfile KeyfileType = "aws_secret"
EnvVarKeyfilePath KeyfileType = "env_var"
TextFileKeyfilePath KeyfileType = "file"
AwsSecretPassword PasswordType = "aws_secret"
EnvVarPassword PasswordType = "env_var"
PlainTextPassword PasswordType = "plaintext"
TextFilePassword PasswordType = "file"
)
func BadCharsCheck(input string) error {
badChars := []string{"%", "&", ";", ">", "<"}
for _, badChar := range badChars {
if strings.Contains(input, badChar) {
return fmt.Errorf("bad char in path: %s", badChar)
}
}
return nil
}
func GetAwsSecret(name string) (string, error) {
if AWS_REGION == "" {
return "", fmt.Errorf("AWS_REGION is not specified")
}
awsConfig, awsConfigErr := config.LoadDefaultConfig(context.TODO(), config.WithRegion(AWS_REGION))
if awsConfigErr != nil {
return "", fmt.Errorf("AWS config load error, err: %v", awsConfigErr)
}
// Create Secrets Manager client
svc := secretsmanager.NewFromConfig(awsConfig)
input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(name),
VersionStage: aws.String("AWSCURRENT"), // VersionStage defaults to AWSCURRENT if unspecified
}
result, getSecretErr := svc.GetSecretValue(context.TODO(), input)
if getSecretErr != nil {
return "", fmt.Errorf("AWS get secret error, err: %v", getSecretErr.Error())
}
// Decrypts secret using the associated KMS key
return *result.SecretString, nil
}
func GetFileContent(path string) ([]byte, error) {
_, osStatErr := os.Stat(path)
if osStatErr != nil {
if os.IsNotExist(osStatErr) {
log.Printf("File %s not found, err: %v\n", path, osStatErr)
return nil, osStatErr
}
log.Printf("Error due checking config path %s, err: %v\n", path, osStatErr)
return nil, osStatErr
}
dataRaw, readErr := os.ReadFile(path)
if readErr != nil {
return nil, readErr
}
return dataRaw, nil
}
// ParseKeyfileInput parses keyfile depends on keyfile type.
func ParseKeyfileInput(input string, inputType KeyfileType) ([]byte, error) {
switch inputType {
case AwsSecretKeyfile:
if badCharsCheckErr := BadCharsCheck(input); badCharsCheckErr != nil {
return nil, badCharsCheckErr
}
keyfileRaw, getSsmErr := GetAwsSecret(input)
if getSsmErr != nil {
return nil, getSsmErr
}
keyfile, decodeErr := base64.StdEncoding.DecodeString(keyfileRaw)
if decodeErr != nil {
return nil, decodeErr
}
return keyfile, nil
case EnvVarKeyfilePath:
if badCharsCheckErr := BadCharsCheck(input); badCharsCheckErr != nil {
return nil, badCharsCheckErr
}
keyfilePath := os.Getenv(input)
if keyfilePath == "" {
return nil, fmt.Errorf("empty environment variable %s input", input)
}
keyfile, getFileErr := GetFileContent(keyfilePath)
return keyfile, getFileErr
case TextFileKeyfilePath:
if badCharsCheckErr := BadCharsCheck(input); badCharsCheckErr != nil {
return nil, badCharsCheckErr
}
keyfile, getFileErr := GetFileContent(input)
return keyfile, getFileErr
default:
return nil, fmt.Errorf("unsupported input type provided")
}
}
// ParsePasswordInput parses password for keyfile depends on password type.
func ParsePasswordInput(input string, inputType PasswordType) (string, error) {
switch inputType {
case AwsSecretPassword:
if badCharsCheckErr := BadCharsCheck(input); badCharsCheckErr != nil {
return "", badCharsCheckErr
}
password, getSsmErr := GetAwsSecret(input)
return password, getSsmErr
case EnvVarPassword:
if badCharsCheckErr := BadCharsCheck(input); badCharsCheckErr != nil {
return "", badCharsCheckErr
}
password := os.Getenv(input)
if password == "" {
return "", fmt.Errorf("empty environment variable %s input", input)
}
return password, nil
case PlainTextPassword:
return input, nil
case TextFilePassword:
if badCharsCheckErr := BadCharsCheck(input); badCharsCheckErr != nil {
return "", badCharsCheckErr
}
password, getFileErr := GetFileContent(input)
return string(password), getFileErr
default:
return "", fmt.Errorf("unsupported input type provided")
}
}
// ReadConfig parses list of configuration file paths to list of Application configuration
func ReadConfig(rawConfigPath string) ([]ServerSignerConfigParsed, string, error) {
configPath := strings.TrimSuffix(rawConfigPath, "/")
_, err := os.Stat(configPath)
if err != nil {
if os.IsNotExist(err) {
return nil, "", fmt.Errorf("file %s not found, err: %v", configPath, err)
}
return nil, "", fmt.Errorf("error due checking config path %s, err: %v", configPath, err)
}
rawBytes, err := os.ReadFile(configPath)
if err != nil {
log.Fatal(err)
}
configsTemp := &ServerConfig{}
err = json.Unmarshal(rawBytes, configsTemp)
if err != nil {
return nil, "", err
}
configParsed := make([]ServerSignerConfigParsed, len(configsTemp.Signers)-1)
for _, s := range configsTemp.Signers {
keyfile, keyParseErr := ParseKeyfileInput(s.Keyfile, KeyfileType(s.KeyfileType))
if keyParseErr != nil {
return nil, "", keyParseErr
}
password, passParseErr := ParsePasswordInput(s.Password, PasswordType(s.PasswordType))
if passParseErr != nil {
return nil, "", passParseErr
}
configParsed = append(configParsed, ServerSignerConfigParsed{
Keyfile: keyfile,
Password: password,
})
}
return configParsed, configsTemp.AccessResourceId, nil
}