-
-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
- Loading branch information
1 parent
b9cda02
commit 64041c1
Showing
6 changed files
with
201 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package data | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/hairyhenderson/gomplate/vault" | ||
) | ||
|
||
func parseVaultParams(sourceURL *url.URL, args []string) (params map[string]interface{}, p string, err error) { | ||
p = sourceURL.Path | ||
params = make(map[string]interface{}) | ||
for key, val := range sourceURL.Query() { | ||
params[key] = strings.Join(val, " ") | ||
} | ||
|
||
if len(args) == 1 { | ||
parsed, err := url.Parse(args[0]) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
if parsed.Path != "" { | ||
p = p + "/" + parsed.Path | ||
} | ||
|
||
for key, val := range parsed.Query() { | ||
params[key] = strings.Join(val, " ") | ||
} | ||
} | ||
return params, p, nil | ||
} | ||
|
||
func readVault(source *Source, args ...string) ([]byte, error) { | ||
if source.VC == nil { | ||
source.VC = vault.New(source.URL) | ||
source.VC.Login() | ||
} | ||
|
||
params, p, err := parseVaultParams(source.URL, args) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data []byte | ||
|
||
source.Type = "application/json" | ||
if len(params) > 0 { | ||
data, err = source.VC.Write(p, params) | ||
} else if strings.HasSuffix(p, "/") { | ||
source.Type = "application/array+json" | ||
data, err = source.VC.List(p) | ||
} else { | ||
data, err = source.VC.Read(p) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(data) == 0 { | ||
return nil, errors.Errorf("no value found for path %s", p) | ||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package data | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/hairyhenderson/gomplate/vault" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadVault(t *testing.T) { | ||
expected := "{\"value\":\"foo\"}\n" | ||
server, v := vault.MockServer(200, `{"data":`+expected+`}`) | ||
defer server.Close() | ||
|
||
source := &Source{ | ||
Alias: "foo", | ||
URL: &url.URL{Scheme: "vault", Path: "/secret/foo"}, | ||
Ext: "", | ||
Type: "text/plain", | ||
VC: v, | ||
} | ||
|
||
r, err := readVault(source) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(expected), r) | ||
|
||
r, err = readVault(source, "bar") | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(expected), r) | ||
|
||
r, err = readVault(source, "?param=value") | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(expected), r) | ||
|
||
source.URL, _ = url.Parse("vault:///secret/foo?param1=value1¶m2=value2") | ||
r, err = readVault(source) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(expected), r) | ||
|
||
expected = "[\"one\",\"two\"]\n" | ||
server, source.VC = vault.MockServer(200, `{"data":{"keys":`+expected+`}}`) | ||
defer server.Close() | ||
source.URL, _ = url.Parse("vault:///secret/foo/") | ||
r, err = readVault(source) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(expected), r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters