From 747d2f6bb0a6105000d153d98e2231b7f74d8609 Mon Sep 17 00:00:00 2001 From: Eduardo Lopez Date: Mon, 13 Aug 2018 14:27:13 -0700 Subject: [PATCH] Fix template encoding + add test #18 --- pkg/provider/data_lambda.go | 16 ++++++------ pkg/provider/data_lambda_test.go | 44 ++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/pkg/provider/data_lambda.go b/pkg/provider/data_lambda.go index f11ced53..c368bace 100644 --- a/pkg/provider/data_lambda.go +++ b/pkg/provider/data_lambda.go @@ -3,13 +3,13 @@ package provider import ( "archive/zip" "bytes" - "html/template" "io" "io/ioutil" "os" "path" "path/filepath" "sort" + "text/template" "github.com/chanzuckerberg/terraform-provider-bless/pkg/util" "github.com/gobuffalo/packr" @@ -189,13 +189,13 @@ func (l *resourceLambda) getBlessConfig(d *schema.ResourceData) (io.Reader, erro return nil, errors.Wrap(err, "Could not load template") } blessConfig := blessConfig{ - Name: d.Get(schemaServiceName).(string), - LoggingLevel: d.Get(schemaLoggingLevel).(string), - UsernameValidation: d.Get(schemaUsernameValidation).(string), - EncryptedPassword: d.Get(schemaEncryptedPassword).(string), - EncryptedPrivateKey: d.Get(schemaEncryptedPrivateKey).(string), - KMSAuthKeyID: d.Get(schemaKMSAuthKeyID).(string), - KMSAuthRemoteUsernamesAllowed: d.Get(schemaKMSAuthRemoteUsernamesAllowed).(string), + Name: d.Get(schemaServiceName).(string), + LoggingLevel: d.Get(schemaLoggingLevel).(string), + UsernameValidation: d.Get(schemaUsernameValidation).(string), + EncryptedPassword: d.Get(schemaEncryptedPassword).(string), + EncryptedPrivateKey: d.Get(schemaEncryptedPrivateKey).(string), + KMSAuthKeyID: d.Get(schemaKMSAuthKeyID).(string), + KMSAuthRemoteUsernamesAllowed: d.Get(schemaKMSAuthRemoteUsernamesAllowed).(string), KMSAuthValidateRemoteUsernameAgainstIAMGroups: d.Get(schemaKMSAuthValidateRemoteUsernameAgainstIAMGroups).(bool), KMSAuthIAMGroupNameFormat: d.Get(schemaKMSAuthIAMGroupNameFormat).(string), } diff --git a/pkg/provider/data_lambda_test.go b/pkg/provider/data_lambda_test.go index c05b1278..543234c9 100644 --- a/pkg/provider/data_lambda_test.go +++ b/pkg/provider/data_lambda_test.go @@ -1,6 +1,9 @@ package provider_test import ( + "archive/zip" + "bufio" + "strings" "testing" r "github.com/hashicorp/terraform/helper/resource" @@ -24,7 +27,7 @@ func TestLambdaCreate(t *testing.T) { data "bless_lambda" "zip" { encrypted_ca = "aaaa" - encrypted_password = "bbbb" + encrypted_password = "bb+bb" service_name = "test" kmsauth_key_id = "keyID" output_path = "/tmp/test.zip" @@ -32,7 +35,7 @@ func TestLambdaCreate(t *testing.T) { data "bless_lambda" "zip2" { encrypted_ca = "aaaa" - encrypted_password = "bbbb" + encrypted_password = "bb+bb" service_name = "test" kmsauth_key_id = "keyID" output_path = "/tmp/test2.zip" @@ -52,6 +55,8 @@ func TestLambdaCreate(t *testing.T) { a.NotEmpty(output1) a.NotEmpty(output2) // Check hashes are equal + + validateBlessConfig(t, "/tmp/test.zip") a.Equal(output1, output2) return nil }, @@ -102,3 +107,38 @@ func TestLambdaCreate(t *testing.T) { }, }) } + +func validateBlessConfig(t *testing.T, zipPath string) { + a := assert.New(t) + + r, err := zip.OpenReader(zipPath) + a.Nil(err) + defer r.Close() + + configFound := false + privateKeyFound := false + + for _, f := range r.File { + if f.Name != "bless_deploy.cfg" { + continue + } + configFound = true + fc, err := f.Open() + a.Nil(err) + defer fc.Close() + scanner := bufio.NewScanner(fc) + + for scanner.Scan() { + if strings.Contains(scanner.Text(), "bb+bb") { + privateKeyFound = true + break + } + } + a.Nil(scanner.Err()) + + break // Found the config file + } + + a.True(configFound) + a.True(privateKeyFound) +}