- switching to using the os.ExpandEnv methods rather than tokenizing the string

- updated the readme to reflect the changes
This commit is contained in:
Rohith 2016-07-13 15:55:50 +01:00
parent 01d41ece32
commit 9786a52e09
4 changed files with 11 additions and 23 deletions

View file

@ -94,7 +94,7 @@ The sidekick supports the following resource types: mysql, postgres, pki, aws, s
**Environment Variable Expansion** **Environment Variable Expansion**
The resource paths can contain environment variables which the sidekick will resolve beforehand. A use case being, using a environment The resource paths can contain environment variables which the sidekick will resolve beforehand. A use case being, using a environment
or domain within the resource e.g -cn=secret:secrets/myservice/%ENV%/config:fmt=yaml or domain within the resource e.g -cn=secret:secrets/myservice/${ENV}/config:fmt=yaml
**Output Formatting** **Output Formatting**

View file

@ -26,7 +26,7 @@ import (
const ( const (
Prog = "vault-sidekick" Prog = "vault-sidekick"
Version = "v0.1.2" Version = "v0.2.0"
) )
func main() { func main() {

View file

@ -25,10 +25,6 @@ import (
"time" "time"
) )
var (
envRegex = regexp.MustCompile("%[[:alnum:]_]+%")
)
// VaultResources is a collection of type resource // VaultResources is a collection of type resource
type VaultResources struct { type VaultResources struct {
// an array of resource to retrieve // an array of resource to retrieve
@ -41,7 +37,7 @@ func (r *VaultResources) Set(value string) error {
rn := defaultVaultResource() rn := defaultVaultResource()
// step: split on the ':' // step: split on the ':'
items := strings.Split(value, ":") items := strings.Split(os.ExpandEnv(value), ":")
if len(items) < 2 { if len(items) < 2 {
return fmt.Errorf("invalid resource, must have at least two sections TYPE:PATH") return fmt.Errorf("invalid resource, must have at least two sections TYPE:PATH")
} }
@ -52,15 +48,6 @@ func (r *VaultResources) Set(value string) error {
return fmt.Errorf("invalid resource, neither type or path can be empty") return fmt.Errorf("invalid resource, neither type or path can be empty")
} }
// step: look for any token in the resource
tokens := envRegex.FindAllStringSubmatch(items[1], -1)
if len(tokens) > 0 {
for _, x := range tokens {
// step: replace the token with the environment variable
items[1] = strings.Replace(items[1], x[0], os.Getenv(strings.Replace(x[0], "%", "", -1)), -1)
}
}
// step: extract the elements // step: extract the elements
rn.resource = items[0] rn.resource = items[0]
rn.path = items[1] rn.path = items[1]

View file

@ -21,6 +21,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"fmt"
) )
func TestSetResources(t *testing.T) { func TestSetResources(t *testing.T) {
@ -34,7 +35,7 @@ func TestSetResources(t *testing.T) {
assert.Nil(t, items.Set("pki:example-dot-com:common_name=blah.example.com")) assert.Nil(t, items.Set("pki:example-dot-com:common_name=blah.example.com"))
assert.Nil(t, items.Set("pki:example-dot-com:common_name=blah.example.com,file=/etc/certs/ssl/blah.example.com")) assert.Nil(t, items.Set("pki:example-dot-com:common_name=blah.example.com,file=/etc/certs/ssl/blah.example.com"))
assert.Nil(t, items.Set("pki:example-dot-com:common_name=blah.example.com,renew=true")) assert.Nil(t, items.Set("pki:example-dot-com:common_name=blah.example.com,renew=true"))
assert.Nil(t, items.Set("secret:secrets/%ENV%/me:file=filename.test,fmt=yaml")) assert.Nil(t, items.Set("secret:secrets/${ENV}/me:file=filename.test,fmt=yaml"))
assert.NotNil(t, items.Set("secret:")) assert.NotNil(t, items.Set("secret:"))
@ -51,25 +52,25 @@ func TestSetEnvironmentResource(t *testing.T) {
Vars map[string]string Vars map[string]string
}{ }{
{ {
ResourceText: "secret:secrets/%ENV/me:file=filename.test,fmt=yaml", ResourceText: "secret:secrets/${ENV}/me:file=filename.test,fmt=yaml",
ExpectedPath: "secrets/%ENV/me", ExpectedPath: "secrets//me",
}, },
{ {
ResourceText: "secret:secrets/%ENV%/me:file=filename.test,fmt=yaml", ResourceText: "secret:secrets/${ENV}/me:file=filename.test,fmt=yaml",
ExpectedPath: "secrets/dev/me", ExpectedPath: "secrets/dev/me",
Vars: map[string]string{ Vars: map[string]string{
"ENV": "dev", "ENV": "dev",
}, },
}, },
{ {
ResourceText: "secret:secrets/%ENV%/me/%ENV%:file=filename.test,fmt=yaml", ResourceText: "secret:secrets/${ENV}/me/${ENV}:file=filename.test,fmt=yaml",
ExpectedPath: "secrets/dev/me/dev", ExpectedPath: "secrets/dev/me/dev",
Vars: map[string]string{ Vars: map[string]string{
"ENV": "dev", "ENV": "dev",
}, },
}, },
{ {
ResourceText: "secret:secrets/%ENV%/me/%THING%:file=filename.test,fmt=yaml", ResourceText: "secret:secrets/${ENV}/me/${THING}:file=filename.test,fmt=yaml",
ExpectedPath: "secrets/dev/me/yes", ExpectedPath: "secrets/dev/me/yes",
Vars: map[string]string{ Vars: map[string]string{
"ENV": "dev", "ENV": "dev",
@ -77,7 +78,7 @@ func TestSetEnvironmentResource(t *testing.T) {
}, },
}, },
{ {
ResourceText: "secret:secrets/%KUBERNETES_NAMESPACE%/me:file=filename.test,fmt=yaml", ResourceText: "secret:secrets/${KUBERNETES_NAMESPACE}/me:file=filename.test,fmt=yaml,common_name=${KUBERNETES_NAMESPACE}.test",
ExpectedPath: "secrets/dev/me", ExpectedPath: "secrets/dev/me",
Vars: map[string]string{ Vars: map[string]string{
"KUBERNETES_NAMESPACE": "dev", "KUBERNETES_NAMESPACE": "dev",