Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 46d9b36

Browse files
committed
Adding support for variable value from secretRef
1 parent 0c0b52b commit 46d9b36

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

workspacehelper/k8s_secret.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package workspacehelper
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/hashicorp/terraform-k8s/api/v1alpha1"
8+
corev1 "k8s.io/api/core/v1"
9+
"k8s.io/apimachinery/pkg/types"
10+
)
11+
12+
// GetSecretData retrieves the data from a secret in a given namespace
13+
func (r *WorkspaceHelper) GetSecretData(namespace string, name string) (map[string][]byte, error) {
14+
// If no secretName defined, return empty map
15+
if name == "" {
16+
return make(map[string][]byte), nil
17+
}
18+
19+
r.reqLogger.Info("Getting Secret", "Namespace", namespace, "Name", name)
20+
21+
secret := &corev1.Secret{}
22+
err := r.client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, secret)
23+
24+
if err != nil {
25+
r.reqLogger.Error(err, "Failed to get Secret", "Namespace", namespace, "Name", name)
26+
27+
return nil, err
28+
}
29+
30+
return secret.Data, nil
31+
}
32+
33+
// GetSecretForVariable retrieves the sensitive value associated with the variable from a secret
34+
func (r *WorkspaceHelper) GetSecretForVariable(namespace string, variable *v1alpha1.Variable) error {
35+
if variable.Sensitive == false {
36+
return nil
37+
}
38+
39+
if variable.ValueFrom.SecretKeyRef == nil {
40+
err := errors.New("Include Secret in ValueFrom")
41+
42+
r.reqLogger.Error(err, "No Secret specified", "Namespace", namespace, "Variable", variable.Key)
43+
44+
return err
45+
}
46+
47+
r.reqLogger.Info("Checking Secret for variable", "Namespace", namespace, "Variable", variable.Key)
48+
49+
name := variable.ValueFrom.SecretKeyRef.LocalObjectReference.Name
50+
key := variable.ValueFrom.SecretKeyRef.Key
51+
52+
data, err := r.GetSecretData(namespace, name)
53+
if err != nil {
54+
return err
55+
}
56+
57+
value, ok := data[key]
58+
if !ok {
59+
err := errors.New("Include Secret key reference in ValueFrom")
60+
61+
r.reqLogger.Error(err, "No Secret key specified", "Namespace", namespace, "Name", name, "Key", key)
62+
63+
return err
64+
}
65+
66+
variable.Value = string(value)
67+
68+
return nil
69+
}

workspacehelper/tfc_variable.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,11 @@ func (t *TerraformCloudClient) UpdateTerraformVariables(variables []*tfc.Variabl
223223
}
224224

225225
func checkAndRetrieveIfSensitive(variable *tfc.Variable, secretsMountPath string) error {
226-
if variable.Sensitive {
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 == "" {
227229
filePath := fmt.Sprintf("%s/%s", secretsMountPath, variable.Key)
230+
228231
data, err := ioutil.ReadFile(filePath)
229232
if err != nil {
230233
return fmt.Errorf("could not get secret, %s", err)

workspacehelper/workspace_helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ func (r *WorkspaceHelper) updateVariables(instance *appv1alpha1.Workspace) (bool
293293
if err != nil {
294294
return false, err
295295
}
296+
err = r.GetSecretForVariable(instance.Namespace, variable)
297+
if err != nil {
298+
return false, err
299+
}
296300
}
297301

298302
specTFCVariables := MapToTFCVariable(instance.Spec.Variables)

0 commit comments

Comments
 (0)