From 9efdfd80bc3939512013b11abdf15656a44b0827 Mon Sep 17 00:00:00 2001 From: Svetlozar Dimitrov Date: Wed, 24 Apr 2019 22:53:06 +0300 Subject: [PATCH] Adding functionality to read keys from []byte Fixing tests for windows --- cmd/terraform/new_resource.go | 12 +++------ .../new_resource_integration_test.go | 10 ++----- e2e/e2e.go | 11 ++++++++ pkg/rsa/service.go | 8 ++++++ pkg/vaulted/utils.go | 27 +++++++++++++++++++ 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/cmd/terraform/new_resource.go b/cmd/terraform/new_resource.go index 172e835..49deace 100644 --- a/cmd/terraform/new_resource.go +++ b/cmd/terraform/new_resource.go @@ -16,13 +16,13 @@ package terraform import ( "fmt" - "path/filepath" - "strings" "github.com/palantir/stacktrace" "github.com/spf13/cobra" "github.com/sumup-oss/go-pkgs/os" + "github.com/sumup-oss/vaulted/pkg/vaulted" + "github.com/sumup-oss/vaulted/cli" "github.com/sumup-oss/vaulted/cmd/external_interfaces" "github.com/sumup-oss/vaulted/pkg/terraform" @@ -123,13 +123,7 @@ func NewNewResourceCommand( if outFilePath == "" { fullResourceName = resourceName } else { - resourcePrefix := strings.Replace(outFilePath, ".", "_", -1) - resourcePrefix = strings.Replace( - resourcePrefix, - string(filepath.Separator), - "_", - -1, - ) + resourcePrefix := vaulted.SanitizeFilename(outFilePath) fullResourceName = fmt.Sprintf("%s_%s", resourcePrefix, resourceName) } diff --git a/cmd/terraform/new_resource_integration_test.go b/cmd/terraform/new_resource_integration_test.go index d9ccf21..d80a7a6 100644 --- a/cmd/terraform/new_resource_integration_test.go +++ b/cmd/terraform/new_resource_integration_test.go @@ -21,8 +21,8 @@ import ( "crypto/x509" "encoding/pem" "fmt" + "github.com/sumup-oss/vaulted/pkg/vaulted" "path/filepath" - "strings" "testing" "github.com/sumup-oss/vaulted/pkg/testutils" @@ -174,13 +174,7 @@ func TestNewResourceCmd_Execute(t *testing.T) { regexMatches := testutils.NewTerraformRegex.FindAllStringSubmatch(string(outContent), -1) assert.Equal(t, 1, len(regexMatches)) - resourcePrefix := strings.Replace(string(outPathFlag), ".", "_", -1) - resourcePrefix = strings.Replace( - resourcePrefix, - string(filepath.Separator), - "_", - -1, - ) + resourcePrefix := vaulted.SanitizeFilename(outPathFlag) fullResourceName := fmt.Sprintf("%s_%s", resourcePrefix, resourceNameArg) resource := regexMatches[0] diff --git a/e2e/e2e.go b/e2e/e2e.go index 30061aa..347123f 100644 --- a/e2e/e2e.go +++ b/e2e/e2e.go @@ -20,6 +20,7 @@ import ( "log" stdOs "os" "os/exec" + "runtime" "github.com/sumup-oss/go-pkgs/os" ) @@ -79,6 +80,16 @@ func GoBuild(osExecutor os.OsExecutor) string { log.Fatal(err) } + // NOTE: On windows the temp file created in the previous step cannot be overwritten + err = osExecutor.Remove(tmpFilename) + if err != nil { + log.Fatal(err) + } + + if runtime.GOOS == "windows" { + tmpFilename += ".exe" + } + cmd := exec.Command( "go", "build", diff --git a/pkg/rsa/service.go b/pkg/rsa/service.go index b1bfad1..9ed75cb 100644 --- a/pkg/rsa/service.go +++ b/pkg/rsa/service.go @@ -62,6 +62,10 @@ func (s *Service) ReadPublicKeyFromPath(publicKeyPath string) (*rsa.PublicKey, e ) } + return s.ReadPublicKeyFromBytes(publicKeyContent) +} + +func (s *Service) ReadPublicKeyFromBytes(publicKeyContent []byte) (*rsa.PublicKey, error) { block, _ := pem.Decode(publicKeyContent) if block == nil || block.Type != pemBlockPublicKeyName { return nil, errDecodePublicKeyPem @@ -92,6 +96,10 @@ func (s *Service) ReadPrivateKeyFromPath(privateKeyPath string) (*rsa.PrivateKey ) } + return s.ReadPrivateKeyFromBytes(privateKeyContent) +} + +func (s *Service) ReadPrivateKeyFromBytes(privateKeyContent []byte) (*rsa.PrivateKey, error) { block, _ := pem.Decode(privateKeyContent) if block == nil || block.Type != pemBlockPrivateKeyName { return nil, errDecodePrivateKeyPem diff --git a/pkg/vaulted/utils.go b/pkg/vaulted/utils.go index db2867b..279d297 100644 --- a/pkg/vaulted/utils.go +++ b/pkg/vaulted/utils.go @@ -14,6 +14,14 @@ package vaulted +import ( + "path/filepath" + "regexp" + "strings" +) + +var replaceWindowsDriveRegex = regexp.MustCompile(`(?i)[a-z]:\\`) + func Contains(array []string, needle string) bool { for _, v := range array { if v == needle { @@ -22,3 +30,22 @@ func Contains(array []string, needle string) bool { } return false } + +func SanitizeFilename(filename string) string { + sanitizedName := strings.Replace(filename, ".", "_", -1) + sanitizedName = replaceWindowsDriveRegex.ReplaceAllString(sanitizedName, "") + sanitizedName = strings.Replace( + sanitizedName, + string(filepath.Separator), + "_", + -1, + ) + // NOTE: We need to replace the unix file separator, too because on windows the "/" will not be replaced + sanitizedName = strings.Replace( + sanitizedName, + "/", + "_", + -1, + ) + return sanitizedName +}