-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_env_variables.go
211 lines (192 loc) · 8.11 KB
/
data_env_variables.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
package provider
import (
"context"
"crypto/sha256"
"fmt"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
"regexp"
)
type dataEnvVariables struct {
p *provider
}
func newDataEnvVariables(p *provider) (*dataEnvVariables, error) {
if p == nil {
return nil, fmt.Errorf("a provider is required")
}
return &dataEnvVariables{p: p}, nil
}
func (*dataEnvVariables) Schema(ctx context.Context) *tfprotov5.Schema {
attrs := []*tfprotov5.SchemaAttribute{
deprecatedIDAttribute(),
{
Name: "input_env_variables",
Type: tftypes.Map{ElementType: tftypes.String},
Description: "The raw environment variables before they are interpolated.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Required: true,
},
{
Name: "input_secrets",
Type: tftypes.Map{ElementType: tftypes.String},
Description: "The raw secrets before they are interpolated.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Required: true,
Sensitive: true,
},
{
Name: "env_variables",
Type: tftypes.Map{ElementType: tftypes.String},
Description: "The processed environment variables after they are interpolated.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
},
{
Name: "secrets",
Type: tftypes.Map{ElementType: tftypes.String},
Description: "The processed secrets after they are interpolated.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
Sensitive: true,
},
{
Name: "secret_refs",
Type: tftypes.Map{ElementType: tftypes.String},
Description: "Map of environment variables that refer to an existing secret key for their values.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Computed: true,
},
}
return &tfprotov5.Schema{
Version: 1,
Block: &tfprotov5.SchemaBlock{
Description: "Data source to interpolate any variables or env variables into their final values.",
DescriptionKind: tfprotov5.StringKindMarkdown,
Attributes: attrs,
},
}
}
func (d *dataEnvVariables) Validate(ctx context.Context, config map[string]tftypes.Value) ([]*tfprotov5.Diagnostic, error) {
inputEnvVariables := extractMapFromConfig(config, "input_env_variables")
inputSecrets := extractMapFromConfig(config, "input_secrets")
errors := make([]*tfprotov5.Diagnostic, 0)
for key, _ := range inputEnvVariables {
if !validEnvVariableKey(key) {
errors = append(errors, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: fmt.Sprintf("Invalid environment variable key: %s", key),
Detail: "An environment variable key can only contain letters, numbers, and the underscore character. It also can not begin with a number.",
})
}
}
for key, _ := range inputSecrets {
if !validEnvVariableKey(key) {
errors = append(errors, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: fmt.Sprintf("Invalid environment variable key: %s", key),
Detail: "An environment variable key can only contain letters, numbers, and the underscore character. It also can not begin with a number.",
})
}
}
return errors, nil
}
func (d *dataEnvVariables) Read(ctx context.Context, config map[string]tftypes.Value) (map[string]tftypes.Value, []*tfprotov5.Diagnostic, error) {
inputEnvVariables := extractMapFromConfig(config, "input_env_variables")
inputSecrets := extractMapFromConfig(config, "input_secrets")
tflog.Debug(ctx, "input_env_variables", inputEnvVariables)
tflog.Debug(ctx, "input_secrets", inputSecrets)
// make sure we copy these so our changes below don't affect the original values
envVariables := copyMap(inputEnvVariables)
secrets := copyMap(inputSecrets)
// if any env variables contain secret refs, pull them out
secretRefs := make(map[string]tftypes.Value, 0)
refRegexPattern := "{{\\s*secret\\((.+)\\)\\s*}}"
regex := regexp.MustCompile(refRegexPattern)
for k, v := range envVariables {
result := regex.FindStringSubmatch(extractStringFromTfValue(v))
// if we found a proper match, the result will contain a slice
// with the full match as the first item and the capture group as the second
if len(result) > 1 {
ref := result[1]
secretRefs[k] = tftypes.NewValue(tftypes.String, ref)
delete(envVariables, k)
}
}
regexPattern := "{{\\s*%s\\s*}}"
// we are going to first loop through all the input secrets
// find and replace this secret in all the rest of the env variables and secrets
for key, secret := range secrets {
regex := regexp.MustCompile(fmt.Sprintf(regexPattern, key))
// first try and replace in the env variables
for k, v := range envVariables {
result := regex.ReplaceAllString(extractStringFromTfValue(v), extractStringFromTfValue(secret))
// if a match was found and replaced, this env variable is now a secret
if result != extractStringFromTfValue(v) {
tflog.Debug(ctx, fmt.Sprintf("Found and replaced secret (%s) in env variable: %s", key, k), result)
delete(envVariables, k)
secrets[k] = tftypes.NewValue(tftypes.String, result)
}
}
// now do any replacements in the other secrets
for k, v := range secrets {
// we don't want to replace the secret with itself (this will prevent an infinite loop)
if k != key {
result := regex.ReplaceAllString(extractStringFromTfValue(v), extractStringFromTfValue(secret))
if result != extractStringFromTfValue(v) {
tflog.Debug(ctx, fmt.Sprintf("Found and replaced secret (%s) in secret: %s", key, k), result)
}
secrets[k] = tftypes.NewValue(tftypes.String, result)
}
}
}
// now we will loop through all the env variables
// find and replace this env variable in all the rest of the env variables and secrets
for key, value := range envVariables {
regex := regexp.MustCompile(fmt.Sprintf(regexPattern, key))
for k, v := range envVariables {
// we don't want to replace the env variable with itself (this will prevent an infinite loop)
if k != key {
result := regex.ReplaceAllString(extractStringFromTfValue(v), extractStringFromTfValue(value))
if result != extractStringFromTfValue(v) {
tflog.Debug(ctx, fmt.Sprintf("Found and replaced env variable (%s) in env variable: %s", key, k), result)
}
envVariables[k] = tftypes.NewValue(tftypes.String, result)
}
}
for k, v := range secrets {
result := regex.ReplaceAllString(extractStringFromTfValue(v), extractStringFromTfValue(value))
if result != extractStringFromTfValue(v) {
tflog.Debug(ctx, fmt.Sprintf("Found and replaced env variable (%s) in secret: %s", key, k), result)
}
secrets[k] = tftypes.NewValue(tftypes.String, result)
}
}
// calculate the unique id for this data source based on a hash of the resulting env variables and secrets
id := d.HashFromValues(envVariables, secrets, secretRefs)
tflog.Debug(ctx, "id", id)
tflog.Debug(ctx, "env_variables", envVariables)
tflog.Debug(ctx, "secrets", secrets)
return map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, id),
"input_env_variables": tftypes.NewValue(tftypes.Map{ElementType: tftypes.String}, inputEnvVariables),
"input_secrets": tftypes.NewValue(tftypes.Map{ElementType: tftypes.String}, inputSecrets),
"env_variables": tftypes.NewValue(tftypes.Map{ElementType: tftypes.String}, envVariables),
"secrets": tftypes.NewValue(tftypes.Map{ElementType: tftypes.String}, secrets),
"secret_refs": tftypes.NewValue(tftypes.Map{ElementType: tftypes.String}, secretRefs),
}, nil, nil
}
func (d *dataEnvVariables) HashFromValues(envVariables, secrets, secretRefs map[string]tftypes.Value) string {
hashString := ""
for k, v := range envVariables {
hashString += fmt.Sprintf("%s=%s;", k, extractStringFromTfValue(v))
}
for k, v := range secrets {
hashString += fmt.Sprintf("%s=%s;", k, extractStringFromTfValue(v))
}
for k, v := range secretRefs {
hashString += fmt.Sprintf("%s=%s;", k, extractStringFromTfValue(v))
}
sum := sha256.Sum256([]byte(hashString))
return fmt.Sprintf("%x", sum)
}