This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
214 lines (186 loc) · 5.59 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
)
type applicationConfig struct {
SecretProvider string
K8SServiceAccount string
K8SServiceAccountToken string
TemplatePath string
VaultToken string
VaultSecretURL string
AWSSecretName string
AWSRegion string
AWSKeyID string
AWSKeySecret string
AWSSessionToken string
AzureTenantID string
AzureClientID string
AzureClientSecret string
AzureVault string
Debug *bool
}
func initializeConfiguration() applicationConfig {
// default configuration
c := &applicationConfig{}
c.SecretProvider = "none"
c.AWSRegion = "us-east-1"
// cli arguments
argTemplatePath := flag.String("template", "", "Path to template file - /app/config/production.template\nenv: TEMPLATE_PATH\n")
c.Debug = flag.Bool("debug", false, "Enable debug information\n")
// Vault arguments
argVaultSecret := flag.String("vault-secret", "", "Vault secret URL - https://vault.example.io/v1/storage/secret\nenv: VAULT_SECRET\n")
argVaultToken := flag.String("vault-token", "", "Vault authentication token\nenv: VAULT_TOKEN")
// Aws arguments
argAWSSecretName := flag.String("aws-secret-name", "", "AWS secret name - example-project-backend\nenv: AWS_SECRET_NAME\n")
argAWSRegion := flag.String("aws-region", "", "AWS region - us-east-1\nenv: AWS_REGION\n")
argAWSKeyID := flag.String("aws-key-id", "", "AWS account ID\nenv: AWS_ACCESS_KEY_ID\n")
argAWSKeySecret := flag.String("aws-key-secret", "", "AWS account secret\nAWS_SECRET_ACCESS_KEY\n")
argAWSSessionToken := flag.String("aws-session-token", "", "AWS session token secret\nAWS_SESSION_TOKEN\n")
// Azure arguments
argAzureTenantID := flag.String("azure-tenant-id", "", "Azure tenant ID\nenv: AZURE_TENANT_ID\n")
argAzureClientID := flag.String("azure-client-id", "", "Azure client ID\nenv: AZURE_CLIENT_ID\n")
argAzureClientSecret := flag.String("azure-client-secret", "", "Azure client Secret\nenv: AZURE_CLIENT_SECRET\n")
argAzureVault := flag.String("azure-vault", "", "Azure keyvault storage URL - https://example-key-vault.vault.azure.net/\nenv: AZURE_VAULT\n")
flag.Parse()
// vault secret
envVaultSecret := os.Getenv("VAULT_SECRET")
if envVaultSecret != "" {
c.VaultSecretURL = envVaultSecret
c.SecretProvider = "vault"
}
if *argVaultSecret != "" {
c.VaultSecretURL = *argVaultSecret
c.SecretProvider = "vault"
}
// vault token
envVaultToken := os.Getenv("VAULT_TOKEN")
if envVaultToken != "" {
c.VaultToken = envVaultToken
}
if *argVaultToken != "" {
c.VaultToken = *argVaultToken
}
// aws secret name
envAWSSecretName := os.Getenv("AWS_SECRET_NAME")
if envAWSSecretName != "" {
c.AWSSecretName = envAWSSecretName
c.SecretProvider = "aws"
}
if *argAWSSecretName != "" {
c.AWSSecretName = *argAWSSecretName
c.SecretProvider = "aws"
}
// aws region
envAWSRegion := os.Getenv("AWS_DEFAULT_REGION")
if envAWSRegion != "" {
c.AWSRegion = envAWSRegion
}
if *argAWSRegion != "" {
c.AWSRegion = *argAWSRegion
}
// aws id
envAWSKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
if envAWSKeyID != "" {
c.AWSKeyID = envAWSKeyID
}
if *argAWSKeyID != "" {
c.AWSKeyID = *argAWSKeyID
}
// aws secret
envAWSKeySecret := os.Getenv("AWS_SECRET_ACCESS_KEY")
if envAWSKeySecret != "" {
c.AWSKeySecret = envAWSKeySecret
}
if *argAWSKeySecret != "" {
c.AWSKeySecret = *argAWSKeySecret
}
// aws session token
envAWSSessionToken := os.Getenv("AWS_SESSION_TOKEN")
if envAWSKeySecret != "" {
c.AWSSessionToken = envAWSSessionToken
}
if *argAWSKeySecret != "" {
c.AWSSessionToken = *argAWSSessionToken
}
envAzureTenantID := os.Getenv("AZURE_TENANT_ID")
if envAzureTenantID != "" {
c.AzureTenantID = envAzureTenantID
}
if *argAWSKeySecret != "" {
c.AzureTenantID = *argAzureTenantID
}
envAzureClientID := os.Getenv("AZURE_CLIENT_ID")
if envAzureClientID != "" {
c.AzureClientID = envAzureClientID
}
if *argAWSKeySecret != "" {
c.AzureClientID = *argAzureClientID
}
envAzureClientSecret := os.Getenv("AZURE_CLIENT_SECRET")
if envAzureClientSecret != "" {
c.AzureClientSecret = envAzureClientSecret
}
if *argAWSKeySecret != "" {
c.AzureClientSecret = *argAzureClientSecret
}
envAzureVault := os.Getenv("AZURE_VAULT")
if envAzureVault != "" {
c.AzureVault = envAzureVault
c.SecretProvider = "azure"
}
if *argAWSKeySecret != "" {
c.AzureVault = *argAzureVault
c.SecretProvider = "azure"
}
// template
envTemplatePath := os.Getenv("TEMPLATE_PATH")
if envTemplatePath != "" {
c.TemplatePath = envTemplatePath
}
if *argTemplatePath != "" {
c.TemplatePath = *argTemplatePath
}
return *c
}
func main() {
// initialize configuration
var secrets string
var err error
config := initializeConfiguration()
if *config.Debug {
fmt.Printf("Secret provider: %s", config.SecretProvider)
}
// get secrets
switch config.SecretProvider {
case "vault":
secrets, err = vaultGetSecret(config)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
case "aws":
secrets, err = getAWSSecretString(config.AWSSecretName, config.AWSRegion, config.AWSKeyID, config.AWSKeySecret, config.AWSSessionToken)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
case "azure":
secrets, err = getAzureSecrets(config.AzureVault)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}
// render output as template,json or text
secrets, err = renderOutput(secrets, config.TemplatePath)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
// show secrets output and exit
fmt.Println(secrets)
os.Exit(0)
}