@@ -59,12 +59,12 @@ func (t *TerraformCloudClient) deleteVariablesFromTFC(specTFCVariables []*tfc.Va
59
59
return nil
60
60
}
61
61
62
- func (t * TerraformCloudClient ) createVariablesOnTFC (workspace * tfc.Workspace , specTFCVariables []* tfc.Variable , workspaceVariables []* tfc.Variable ) (bool , error ) {
62
+ func (t * TerraformCloudClient ) createVariablesOnTFC (workspace * tfc.Workspace , specTFCVariables []* tfc.Variable , workspaceVariables []* tfc.Variable , secretData map [ string ][] byte ) (bool , error ) {
63
63
updated := false
64
64
for _ , v := range specTFCVariables {
65
65
index := find (workspaceVariables , v .Key )
66
66
if index < 0 {
67
- err := t .CreateTerraformVariable (workspace , v )
67
+ err := t .CreateTerraformVariable (workspace , v , secretData )
68
68
if err != nil {
69
69
return false , err
70
70
}
@@ -104,15 +104,15 @@ func getNonSensitiveVariablesToUpdate(specTFCVariables []*tfc.Variable, workspac
104
104
return variablesToUpdate
105
105
}
106
106
107
- func getSensitiveVariablesToUpdate (specTFCVariables []* tfc.Variable , workspaceVariables []* tfc.Variable , secretsMountPath string ) ([]* tfc.Variable , error ) {
107
+ func getSensitiveVariablesToUpdate (specTFCVariables []* tfc.Variable , workspaceVariables []* tfc.Variable , secretsMountPath string , secretData map [ string ][] byte ) ([]* tfc.Variable , error ) {
108
108
variablesToUpdate := []* tfc.Variable {}
109
109
for _ , v := range specTFCVariables {
110
110
index := find (workspaceVariables , v .Key )
111
111
if index < 0 {
112
112
continue
113
113
}
114
114
if workspaceVariables [index ].Sensitive {
115
- if err := checkAndRetrieveIfSensitive (v , secretsMountPath ); err != nil {
115
+ if err := checkAndRetrieveIfSensitive (v , secretsMountPath , secretData ); err != nil {
116
116
return nil , err
117
117
}
118
118
v .ID = workspaceVariables [index ].ID
@@ -124,15 +124,15 @@ func getSensitiveVariablesToUpdate(specTFCVariables []*tfc.Variable, workspaceVa
124
124
return variablesToUpdate , nil
125
125
}
126
126
127
- func generateUpdateVariableList (specTFCVariables []* tfc.Variable , workspaceVariables []* tfc.Variable , secretsMountPath string ) ([]* tfc.Variable , error ) {
127
+ func generateUpdateVariableList (specTFCVariables []* tfc.Variable , workspaceVariables []* tfc.Variable , secretsMountPath string , secretData map [ string ][] byte ) ([]* tfc.Variable , error ) {
128
128
updateList := []* tfc.Variable {}
129
129
130
130
nonSensitiveVariablesToUpdate := getNonSensitiveVariablesToUpdate (specTFCVariables , workspaceVariables )
131
131
if len (nonSensitiveVariablesToUpdate ) == 0 {
132
132
return updateList , nil
133
133
}
134
134
135
- sensitiveVariablesToUpdate , err := getSensitiveVariablesToUpdate (specTFCVariables , workspaceVariables , secretsMountPath )
135
+ sensitiveVariablesToUpdate , err := getSensitiveVariablesToUpdate (specTFCVariables , workspaceVariables , secretsMountPath , secretData )
136
136
if err != nil {
137
137
return nonSensitiveVariablesToUpdate , err
138
138
}
@@ -143,7 +143,7 @@ func generateUpdateVariableList(specTFCVariables []*tfc.Variable, workspaceVaria
143
143
}
144
144
145
145
// CheckVariables creates, updates, or deletes variables as needed
146
- func (t * TerraformCloudClient ) CheckVariables (workspace string , specTFCVariables []* tfc.Variable ) (bool , error ) {
146
+ func (t * TerraformCloudClient ) CheckVariables (workspace string , specTFCVariables []* tfc.Variable , secretData map [ string ][] byte ) (bool , error ) {
147
147
tfcWorkspace , err := t .Client .Workspaces .Read (context .TODO (), t .Organization , workspace )
148
148
if err != nil {
149
149
return false , err
@@ -156,12 +156,12 @@ func (t *TerraformCloudClient) CheckVariables(workspace string, specTFCVariables
156
156
return false , err
157
157
}
158
158
159
- createdVariables , err := t .createVariablesOnTFC (tfcWorkspace , specTFCVariables , workspaceVariables )
159
+ createdVariables , err := t .createVariablesOnTFC (tfcWorkspace , specTFCVariables , workspaceVariables , secretData )
160
160
if err != nil {
161
161
return false , err
162
162
}
163
163
164
- variablesToUpdate , err := generateUpdateVariableList (specTFCVariables , workspaceVariables , t .SecretsMountPath )
164
+ variablesToUpdate , err := generateUpdateVariableList (specTFCVariables , workspaceVariables , t .SecretsMountPath , secretData )
165
165
if err != nil || len (variablesToUpdate ) == 0 {
166
166
return false , err
167
167
}
@@ -222,25 +222,28 @@ func (t *TerraformCloudClient) UpdateTerraformVariables(variables []*tfc.Variabl
222
222
return nil
223
223
}
224
224
225
- func checkAndRetrieveIfSensitive (variable * tfc.Variable , secretsMountPath string ) error {
226
- // Try to read variables with empty value from file. If the value isn't empty,
227
- // it was already read fromValue.SecretKeyRef.
228
- if variable .Sensitive && variable .Value == "" {
229
- filePath := fmt .Sprintf ("%s/%s" , secretsMountPath , variable .Key )
230
-
231
- data , err := ioutil .ReadFile (filePath )
232
- if err != nil {
233
- return fmt .Errorf ("could not get secret, %s" , err )
225
+ func checkAndRetrieveIfSensitive (variable * tfc.Variable , secretsMountPath string , secretData map [string ][]byte ) error {
226
+ if variable .Sensitive {
227
+ // First check if the key is in the namespaced Secret
228
+ if val , ok := secretData [variable .Key ]; ok {
229
+ variable .Value = string (val )
230
+ } else {
231
+ // Try to find key in the mounted Secret
232
+ filePath := fmt .Sprintf ("%s/%s" , secretsMountPath , variable .Key )
233
+ data , err := ioutil .ReadFile (filePath )
234
+ if err != nil {
235
+ return fmt .Errorf ("could not get secret, %s" , err )
236
+ }
237
+ secret := string (data )
238
+ variable .Value = secret
234
239
}
235
- secret := string (data )
236
- variable .Value = secret
237
240
}
238
241
return nil
239
242
}
240
243
241
244
// CreateTerraformVariable creates a Terraform variable based on key and value
242
- func (t * TerraformCloudClient ) CreateTerraformVariable (workspace * tfc.Workspace , variable * tfc.Variable ) error {
243
- if err := checkAndRetrieveIfSensitive (variable , t .SecretsMountPath ); err != nil {
245
+ func (t * TerraformCloudClient ) CreateTerraformVariable (workspace * tfc.Workspace , variable * tfc.Variable , secretData map [ string ][] byte ) error {
246
+ if err := checkAndRetrieveIfSensitive (variable , t .SecretsMountPath , secretData ); err != nil {
244
247
return err
245
248
}
246
249
options := tfc.VariableCreateOptions {
0 commit comments