Skip to content

Commit

Permalink
allow secrets with data or string data
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Sep 18, 2024
1 parent bbb6b2a commit 237557c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
30 changes: 25 additions & 5 deletions src/internal/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"sort"
"strings"

"encoding/base64"

"github.com/Masterminds/semver/v3"
"github.com/defenseunicorns/pkg/helpers/v2"
fluxHelmCtrl "github.com/fluxcd/helm-controller/api/v2"
Expand Down Expand Up @@ -48,20 +50,38 @@ func getValuesFromManifest(valuesFileManifest string) (string, error) {
if err := yaml.Unmarshal(file, &resource); err != nil {
return "", err
}
if resource.GetKind() != "Secret" && resource.GetKind() != "ConfigMap" {
return "", errors.New("values manifests must be a Secret or ConfigMap")
}
data, found, err := unstructured.NestedStringMap(resource.Object, "data")
if err != nil || !found {
var data map[string]string
var found bool
var base64Decode bool
if resource.GetKind() == "Secret" {
data, found, err = unstructured.NestedStringMap(resource.Object, "stringData")
if err != nil || !found {
data, found, err = unstructured.NestedStringMap(resource.Object, "data")
if err != nil || !found {
return "", fmt.Errorf("failed to get data from resource: %w", err)
}
base64Decode = true
}
} else if resource.GetKind() == "ConfigMap" {
data, found, err = unstructured.NestedStringMap(resource.Object, "data")
if err != nil || !found {
return "", fmt.Errorf("failed to get data from resource: %w", err)
}
} else {
return "", errors.New("values manifests must be a Secret or ConfigMap")
}

valuesYaml, found := data["values.yaml"]
if !found {
return "", errors.New("values.yaml key must exist in data")
}
if base64Decode {
b, err := base64.StdEncoding.DecodeString(valuesYaml)
if err != nil {
return "", err
}
valuesYaml = string(b)
}
return valuesYaml, nil
}

Expand Down
10 changes: 8 additions & 2 deletions src/internal/bigbang/bigbang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,17 @@ func TestGetValuesFromManifest(t *testing.T) {
expectedErr error
}{
{
name: "Valid Secret",
fileName: "valid_secret.yaml",
name: "Valid Secret string data",
fileName: "valid_secret_string_data.yaml",
expectedOutput: "key: value\n",
expectedErr: nil,
},
{
name: "Valid Secret regular data",
fileName: "valid_secret_data.yaml",
expectedOutput: "key: value",
expectedErr: nil,
},
{
name: "Valid ConfigMap",
fileName: "valid_configmap.yaml",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: Secret
metadata:
name: test-secret
data:
values.yaml: a2V5OiB2YWx1ZQ==
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ apiVersion: v1
kind: Secret
metadata:
name: test-secret
data:
stringData:
values.yaml: |
key: value

0 comments on commit 237557c

Please sign in to comment.