forked from garetht/amanar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_config.go
230 lines (193 loc) · 6.2 KB
/
process_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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package amanar
import (
"fmt"
"io"
"log"
)
type ConfigurationProcessor interface {
ProcessConfig()
}
type VaultConfigurationProcessor struct {
credentials *Credentials
vaultClient VaultClient
vaultAddress string
vaultConfiguration []VaultConfiguration
writer io.Writer
}
func (v VaultConfigurationProcessor) ProcessConfig() {
log.Printf("\n\n\n\n =========================== [VAULT ADDRESS %s] =========================== \n\n", v.vaultAddress)
err := v.vaultClient.Login()
if err != nil {
log.Fatalf("[VAULT AUTH] Could not log in to Vault: %s", err)
return
}
for _, configItem := range v.vaultConfiguration {
secret, err := v.vaultClient.GetCredential(configItem.VaultPath, configItem.VaultRole)
if err != nil {
log.Printf("[VAULT AUTH] Could not retrieve secret for vault path %s and vault role %s because %s. Skipping.", configItem.VaultPath, configItem.VaultRole, err)
continue
}
credentials, err := CreateCredentialsFromSecret(secret)
if err != nil {
log.Printf("[VAULT AUTH] Could not convert Vault secret into Amanar credentials because %s. Skipping.", err)
continue
}
log.Printf("[VAULT CONFIGURATION] %v:%v", configItem.VaultPath, configItem.VaultRole)
ProcessVaultConfigItem(&configItem.Configurables, credentials, v.writer)
}
}
type ConstantConfigurationProcessor struct {
constant Constant
writer io.Writer
}
func (c ConstantConfigurationProcessor) ProcessConfig() {
ProcessConstantConfigItem(c.constant, c.writer)
}
func NewConfigurationProcessor(githubToken string, ac AmanarConfiguration, writer io.Writer) (ConfigurationProcessor, error) {
if ac.Constant == nil && ac.VaultAddress == nil && ac.VaultConfiguration == nil {
return nil, fmt.Errorf("please provide either a Constant configuration or a Vault configuration")
}
if ac.Constant != nil && ac.VaultAddress != nil && ac.VaultConfiguration != nil {
return nil, fmt.Errorf("please provide only one of a Constant configuration and a Vault configuration")
}
if ac.VaultAddress != nil && ac.VaultConfiguration != nil {
var vc VaultClient = nil
if githubToken == "" {
vc = &DefaultVaultAuthClient{}
} else {
vc = &VaultGithubAuthClient{
GithubToken: githubToken,
VaultAddress: *ac.VaultAddress,
}
}
return VaultConfigurationProcessor{
vaultClient: vc,
vaultAddress: *ac.VaultAddress,
vaultConfiguration: ac.VaultConfiguration,
writer: writer,
}, nil
}
if ac.Constant != nil {
return ConstantConfigurationProcessor{
constant: *ac.Constant,
writer: writer,
}, nil
}
return nil, fmt.Errorf("please provide a full Constant configuration or a full Vault configuration")
}
func ProcessConstantConfigItem(constant Constant, writer io.Writer) {
var errs []error
templateSource, err := NewTemplateSource(&constant, writer)
if err != nil {
errs = append(errs, fmt.Errorf("could not create new template source: %w", err))
}
err = templateSource.WriteToDiskWithoutContext()
if err != nil {
errs = append(errs, fmt.Errorf("could not write constant to disk: %w", err))
}
if len(errs) > 0 {
for _, err := range errs {
log.Printf("[CONSTANT PROCESSING] Encountered errors processing constant: %#v. Processing constants that worked.", err)
}
}
}
func ProcessVaultConfigItem(configurables *Configurables, credentials *Credentials, writer io.Writer) {
var errs []error
var flows []Flower
for _, datasourceConfig := range configurables.IntellijDatasources {
flow, err := NewIntellijDatasourceFlow(&datasourceConfig)
if err != nil {
errs = append(errs, err)
continue
}
flows = append(flows, flow)
}
for _, runConfigurationsConfig := range configurables.IntellijRunConfigurations {
flow, err := NewIntellijRunConfigsFlow(&runConfigurationsConfig)
if err != nil {
errs = append(errs, err)
continue
}
flows = append(flows, flow)
}
for _, querious2Config := range configurables.Querious2Datasources {
flow, err := NewQuerious2Flow(&querious2Config)
if err != nil {
errs = append(errs, err)
continue
}
flows = append(flows, flow)
}
for _, sequelProConfig := range configurables.SequelProDatasources {
flow, err := NewSequelProFlow(&sequelProConfig)
if err != nil {
errs = append(errs, err)
continue
}
flows = append(flows, flow)
}
for _, sequelAceConfig := range configurables.SequelAceDatasources {
flow, err := NewSequelAceFlow(&sequelAceConfig)
if err != nil {
errs = append(errs, err)
continue
}
flows = append(flows, flow)
}
for _, posticoConfig := range configurables.PosticoDatasources {
flow, err := NewPosticoFlow(&posticoConfig)
if err != nil {
errs = append(errs, err)
continue
}
flows = append(flows, flow)
}
for _, shellConfig := range configurables.ShellDatasources {
flow, err := NewShellFlow(&shellConfig)
if err != nil {
errs = append(errs, err)
continue
}
flows = append(flows, flow)
}
for _, jsonConfig := range configurables.JSONDatasources {
flow, err := NewJSONFlow(&jsonConfig)
if err != nil {
errs = append(errs, err)
continue
}
flows = append(flows, flow)
}
for _, templateConfig := range configurables.TemplateDatasources {
flow, err := NewTemplateFlow(&templateConfig, writer)
if err != nil {
errs = append(errs, err)
continue
}
flows = append(flows, flow)
}
if len(errs) > 0 {
for _, err := range errs {
log.Printf("[FLOW PROCESSING] Encountered errors processing flow: %#v. Processing flows that worked.", err)
}
}
UpdateCredentials(flows, credentials)
return
}
func UpdateCredentials(flows []Flower, credentials *Credentials) {
var err error
for _, flow := range flows {
log.Printf("[%s] Beginning to update flow %#v with credentials %s", flow.Name(), flow, credentials)
err = flow.UpdateWithCredentials(credentials)
if err != nil {
log.Printf("[%s] Error when performing non-write update to flow %#v with credentials %s. Will not try and persist externally. Skipping ahead to next flow.", flow.Name(), flow, credentials)
log.Print(err)
continue
}
err = flow.PersistChanges()
if err != nil {
log.Printf("[%s] Error when persisting changes to to flow %#v with credentials %s. Skipping ahead to next flow.", flow.Name(), flow, credentials)
log.Print(err)
}
}
}