Skip to content

Commit

Permalink
Merge pull request #2 from sumup-oss/adding-functionality-to-read-key…
Browse files Browse the repository at this point in the history
…s-from-bytes

Adding functionality to read keys from []byte
  • Loading branch information
syndbg authored Apr 24, 2019
2 parents da8eec1 + 9efdfd8 commit 9b14db7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
12 changes: 3 additions & 9 deletions cmd/terraform/new_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
10 changes: 2 additions & 8 deletions cmd/terraform/new_resource_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand Down
11 changes: 11 additions & 0 deletions e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
stdOs "os"
"os/exec"
"runtime"

"github.com/sumup-oss/go-pkgs/os"
)
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions pkg/rsa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions pkg/vaulted/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 9b14db7

Please sign in to comment.