Skip to content

Commit

Permalink
fix: lets move URL handling into the secreturl.Client
Browse files Browse the repository at this point in the history
and add support for `vault:` for the vault client and `local:` for the local file system client

Signed-off-by: James Strachan <james.strachan@gmail.com>

jenkins-x#4328
  • Loading branch information
jstrachan committed Jun 19, 2019
1 parent e0dabb0 commit 0f136a3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/helm/helm_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func DecorateWithSecrets(options *InstallChartOptions, vaultClient secreturl.Cli
if err != nil {
return cleanup, errors.Wrapf(err, "reading file %s", valueFile)
}
newValues, err := secreturl.ReplaceURIs(string(bytes), vaultClient)
newValues, err := vaultClient.ReplaceURIs(string(bytes))
if err != nil {
return cleanup, errors.Wrapf(err, "replacing vault URIs")
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/secreturl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ type Client interface {
// WriteObject writes a generic named object to the vault.
// The secret _must_ be serializable to JSON.
WriteObject(secretName string, secret interface{}) (map[string]interface{}, error)

// ReplaceURIs will replace any vault: URIs in a string (or whatever URL scheme the secret URL client supports
ReplaceURIs(text string) (string, error)
}
13 changes: 7 additions & 6 deletions pkg/secreturl/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"github.com/pkg/errors"
)

var vaultURIRegex = regexp.MustCompile(`vault:[-_\w\/:]*`)

// ReplaceURIs will replace any vault: URIs in a string, using the vault client
func ReplaceURIs(s string, client Client) (string, error) {
// ReplaceURIs will replace any URIs with the given regular expression and scheme using the secret URL client
func ReplaceURIs(s string, client Client, r *regexp.Regexp, schemePrefix string) (string, error) {
if !strings.HasSuffix(schemePrefix, ":") {
return s, fmt.Errorf("the scheme prefix should end with ':' but was %s", schemePrefix)
}
var err error
answer := vaultURIRegex.ReplaceAllStringFunc(s, func(found string) string {
answer := r.ReplaceAllStringFunc(s, func(found string) string {
// Stop once we have an error
if err == nil {
pathAndKey := strings.Trim(strings.TrimPrefix(found, "vault:"), "\"")
pathAndKey := strings.Trim(strings.TrimPrefix(found, schemePrefix), "\"")
parts := strings.Split(pathAndKey, ":")
if len(parts) != 2 {
err = errors.Errorf("cannot parse %s as path:key", pathAndKey)
Expand Down
7 changes: 7 additions & 0 deletions pkg/secreturl/localvault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"

"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/secreturl"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)

var localURIRegex = regexp.MustCompile(`local:[-_\w\/:]*`)

// FileSystemClient a local file system based client loading/saving content from the given URL
type FileSystemClient struct {
Dir string
Expand Down Expand Up @@ -66,6 +69,10 @@ func (c *FileSystemClient) WriteObject(secretName string, secret interface{}) (m
return c.Read(secretName)
}

func (c *FileSystemClient) ReplaceURIs(s string) (string, error) {
return secreturl.ReplaceURIs(s, c, localURIRegex, "local:")
}

func (c *FileSystemClient) fileName(secretName string) string {
return filepath.Join(c.Dir, secretName+".yaml")
}
11 changes: 11 additions & 0 deletions pkg/vault/vault_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"encoding/base64"
"fmt"
"net/url"
"regexp"

"github.com/hashicorp/vault/api"
"github.com/jenkins-x/jx/pkg/secreturl"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)
Expand All @@ -14,6 +16,8 @@ const (
yamlDataKey = "yaml"
)

var vaultURIRegex = regexp.MustCompile(`vault:[-_\w\/:]*`)

// Client is an interface for interacting with Vault
//go:generate pegomock generate github.com/jenkins-x/jx/pkg/vault Client -o mocks/vault_client.go
type Client interface {
Expand Down Expand Up @@ -42,6 +46,9 @@ type Client interface {

// Config gets the config required for configuring the official Vault CLI
Config() (vaultURL url.URL, vaultToken string, err error)

// ReplaceURIs will replace any vault: URIs in a string (or whatever URL scheme the secret URL client supports
ReplaceURIs(text string) (string, error)
}

// client is a hand wrapper around the official Vault API
Expand Down Expand Up @@ -171,3 +178,7 @@ func (v *client) Config() (vaultURL url.URL, vaultToken string, err error) {
parsed, err := url.Parse(v.client.Address())
return *parsed, v.client.Token(), err
}

func (v *client) ReplaceURIs(s string) (string, error) {
return secreturl.ReplaceURIs(s, v, vaultURIRegex, "vault:")
}

0 comments on commit 0f136a3

Please sign in to comment.