From 89ed3947ffc234c62b9110c51b8e0e47ff4af59a Mon Sep 17 00:00:00 2001 From: Mateusz Hromada Date: Mon, 4 Dec 2023 18:27:57 +0100 Subject: [PATCH] feat(providers): add basic test for GKMS Signed-off-by: Mateusz Hromada # Conflicts: # pkg/stringprovider/stringprovider.go # vals.go --- vals_gkms_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 vals_gkms_test.go diff --git a/vals_gkms_test.go b/vals_gkms_test.go new file mode 100644 index 0000000..06e91fc --- /dev/null +++ b/vals_gkms_test.go @@ -0,0 +1,66 @@ +package vals + +import ( + "fmt" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestValues_GKMS(t *testing.T) { + // TODO + // create gkms and encrypt test value + // gcloud kms keyrings create "test" --location "global" + // gcloud kms keys create "default" --location "global" --keyring "test" --purpose "encryption" + // echo -n "test_value" \ + // | gcloud kms encrypt \ + // --location "global" \ + // --keyring "test" \ + // --key "default" \ + // --plaintext-file - \ + // --ciphertext-file - \ + // | base64 -w0 \ + // | tr '/+' '_-' + // + // run with: + // + // go test -run '^(TestValues_GKMS)$' + + type testcase struct { + template map[string]interface{} + expected map[string]interface{} + } + + plain_value := "test_value" + encrypted_value := "CiQAmPqoGAKT97oUK0DdiI_cLDm3j6iPDK4-TJ3yQII-snFHCckSMwAkTpnEoD5wOeRaZrt3eC1ewFMuw617fqqjTStrsar9ciGERzk5t6uMgA0HKzSxGMdjHQ==" + + project := "test-project" + location := "global" + keyring := "test" + crypto_key := "default" + + testcases := []testcase{ + { + template: map[string]interface{}{ + "test_key": fmt.Sprintf("ref+gkms://%s?project=%s&location=%s&keyring=%s&crypto_key=%s", encrypted_value, project, location, keyring, crypto_key), + }, + expected: map[string]interface{}{ + "test_key": plain_value, + }, + }, + } + + for i := range testcases { + tc := testcases[i] + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + vals, err := Eval(tc.template) + if err != nil { + t.Fatalf("%v", err) + } + diff := cmp.Diff(tc.expected, vals) + if diff != "" { + t.Errorf("unexpected diff: %s", diff) + } + }) + } +}