Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not log ec2 keypair #1480

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions modules/test-structure/save_test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func formatPackerOptionsPath(testFolder string) string {
// SaveEc2KeyPair serializes and saves an Ec2KeyPair into the given folder. This allows you to create an Ec2KeyPair during setup
// and to reuse that Ec2KeyPair later during validation and teardown.
func SaveEc2KeyPair(t testing.TestingT, testFolder string, keyPair *aws.Ec2Keypair) {
SaveTestData(t, formatEc2KeyPairPath(testFolder), true, keyPair)
saveTestData(t, formatEc2KeyPairPath(testFolder), true, keyPair, false)
}

// LoadEc2KeyPair loads and unserializes an Ec2KeyPair from the given folder. This allows you to reuse an Ec2KeyPair that was
Expand Down Expand Up @@ -193,6 +193,15 @@ func FormatTestDataPath(testFolder string, filename string) string {
// any contents that exist in the file found at `path` will be overwritten. This has the potential for causing duplicated resources
// and should be used with caution. If `overwrite` is `false`, the save will be skipped and a warning will be logged.
func SaveTestData(t testing.TestingT, path string, overwrite bool, value interface{}) {
saveTestData(t, path, overwrite, value, true)
}

// saveTestData serializes and saves a value used at test time to the given path. This allows you to create some sort of test data
// (e.g., TerraformOptions) during setup and to reuse this data later during validation and teardown. If `overwrite` is `true`,
// any contents that exist in the file found at `path` will be overwritten. This has the potential for causing duplicated resources
// and should be used with caution. If `overwrite` is `false`, the save will be skipped and a warning will be logged.
// If `loggedVal` is `true`, the value will be logged as JSON.
func saveTestData(t testing.TestingT, path string, overwrite bool, value interface{}, loggedVal bool) {
logger.Default.Logf(t, "Storing test data in %s so it can be reused later", path)

if IsTestDataPresent(t, path) {
Expand All @@ -209,7 +218,9 @@ func SaveTestData(t testing.TestingT, path string, overwrite bool, value interfa
t.Fatalf("Failed to convert value %s to JSON: %v", path, err)
}

logger.Default.Logf(t, "Marshalled JSON: %s", string(bytes))
if loggedVal {
logger.Default.Logf(t, "Marshalled JSON: %s", string(bytes))
}

parentDir := filepath.Dir(path)
if err := os.MkdirAll(parentDir, 0777); err != nil {
Expand Down
42 changes: 42 additions & 0 deletions modules/test-structure/save_test_data_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package test_structure

import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/files"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/ssh"
"github.com/gruntwork-io/terratest/modules/terraform"
gotesting "github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type testData struct {
Expand Down Expand Up @@ -272,3 +280,37 @@ func TestSaveAndLoadKubectlOptions(t *testing.T) {
actualData := LoadKubectlOptions(t, tmpFolder)
assert.Equal(t, expectedData, actualData)
}

type tStringLogger struct {
sb strings.Builder
}

func (l *tStringLogger) Logf(t gotesting.TestingT, format string, args ...interface{}) {
l.sb.WriteString(fmt.Sprintf(format, args...))
l.sb.WriteRune('\n')
}

func TestSaveAndLoadEC2KeyPair(t *testing.T) {
def, slogger := logger.Default, &tStringLogger{}
logger.Default = logger.New(slogger)
t.Cleanup(func() {
logger.Default = def
})

keyPair, err := ssh.GenerateRSAKeyPairE(t, 2048)
require.NoError(t, err)
ec2KeyPair := &aws.Ec2Keypair{
KeyPair: keyPair,
Name: "test-ec2-key-pair",
Region: "us-east-1",
}
storedEC2KeyPair, err := json.Marshal(ec2KeyPair)
require.NoError(t, err)

tmpFolder := t.TempDir()
SaveEc2KeyPair(t, tmpFolder, ec2KeyPair)
loadedEC2KeyPair := LoadEc2KeyPair(t, tmpFolder)
assert.Equal(t, ec2KeyPair, loadedEC2KeyPair)

assert.NotContains(t, slogger.sb.String(), string(storedEC2KeyPair), "stored ec2 key pair should not be logged")
}