2017-08-03 17:24:27 +00:00
|
|
|
package file
|
2015-03-16 03:15:27 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2016-12-23 22:09:44 +00:00
|
|
|
"encoding/json"
|
2015-03-16 03:15:27 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-12-23 22:09:44 +00:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2015-03-16 03:15:27 +00:00
|
|
|
"testing"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2015-03-16 03:15:27 +00:00
|
|
|
)
|
|
|
|
|
2016-12-23 22:09:44 +00:00
|
|
|
func TestFileBackend_Base64URLEncoding(t *testing.T) {
|
|
|
|
backendPath, err := ioutil.TempDir("", "vault")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(backendPath)
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2016-12-23 22:09:44 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewFileBackend(map[string]string{
|
2016-12-23 22:09:44 +00:00
|
|
|
"path": backendPath,
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the entries. Length should be zero.
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := b.List(context.Background(), "")
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 0 {
|
|
|
|
t.Fatalf("bad: len(keys): expected: 0, actual: %d", len(keys))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a storage entry without base64 encoding the file name
|
|
|
|
rawFullPath := filepath.Join(backendPath, "_foo")
|
2017-08-03 17:24:27 +00:00
|
|
|
e := &physical.Entry{Key: "foo", Value: []byte("test")}
|
2016-12-23 22:09:44 +00:00
|
|
|
f, err := os.OpenFile(
|
|
|
|
rawFullPath,
|
|
|
|
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
|
|
|
|
0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
json.NewEncoder(f).Encode(e)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
// Get should work
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := b.Get(context.Background(), "foo")
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(out, e) {
|
|
|
|
t.Fatalf("bad: %v expected: %v", out, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the entries. There should be one entry.
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("bad: len(keys): expected: 1, actual: %d", len(keys))
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e)
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the entries again. There should still be one entry.
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("bad: len(keys): expected: 1, actual: %d", len(keys))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get should work
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), "foo")
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(out, e) {
|
|
|
|
t.Fatalf("bad: %v expected: %v", out, e)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "foo")
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), "foo")
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("bad: entry: expected: nil, actual: %#v", e)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 0 {
|
|
|
|
t.Fatalf("bad: len(keys): expected: 0, actual: %d", len(keys))
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err = os.OpenFile(
|
|
|
|
rawFullPath,
|
|
|
|
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
|
|
|
|
0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
json.NewEncoder(f).Encode(e)
|
|
|
|
f.Close()
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2016-12-23 22:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("bad: len(keys): expected: 1, actual: %d", len(keys))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 17:52:33 +00:00
|
|
|
func TestFileBackend_ValidatePath(t *testing.T) {
|
2017-05-12 18:12:53 +00:00
|
|
|
dir, err := ioutil.TempDir("", "vault")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2017-05-12 18:12:53 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewFileBackend(map[string]string{
|
2017-05-12 18:12:53 +00:00
|
|
|
"path": dir,
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2017-05-12 18:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Delete(context.Background(), "foo/bar/../zip"); err == nil {
|
2017-05-12 17:52:33 +00:00
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Delete(context.Background(), "foo/bar/zip"); err != nil {
|
2017-05-12 17:52:33 +00:00
|
|
|
t.Fatal("did not expect error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 03:15:27 +00:00
|
|
|
func TestFileBackend(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "vault")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewFileBackend(map[string]string{
|
2015-03-16 03:15:27 +00:00
|
|
|
"path": dir,
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2015-03-16 03:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, b)
|
2017-10-26 19:29:10 +00:00
|
|
|
|
|
|
|
// Underscores should not trip things up; ref GH-3476
|
|
|
|
e := &physical.Entry{Key: "_zip", Value: []byte("foobar")}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e)
|
2017-10-26 19:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
e = &physical.Entry{Key: "_zip/_zap", Value: []byte("boofar")}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e)
|
2017-10-26 19:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
e, err = b.Get(context.Background(), "_zip/_zap")
|
2017-10-26 19:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if e == nil {
|
|
|
|
t.Fatal("got nil entry")
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
vals, err := b.List(context.Background(), "")
|
2017-10-26 19:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(vals) != 2 || vals[0] == vals[1] {
|
|
|
|
t.Fatalf("bad: %v", vals)
|
|
|
|
}
|
|
|
|
for _, val := range vals {
|
|
|
|
if val != "_zip/" && val != "_zip" {
|
|
|
|
t.Fatalf("bad val: %v", val)
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
vals, err = b.List(context.Background(), "_zip/")
|
2017-10-26 19:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(vals) != 1 || vals[0] != "_zap" {
|
|
|
|
t.Fatalf("bad: %v", vals)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "_zip/_zap")
|
2017-10-26 19:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
vals, err = b.List(context.Background(), "")
|
2017-10-26 19:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(vals) != 1 || vals[0] != "_zip" {
|
|
|
|
t.Fatalf("bad: %v", vals)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "_zip")
|
2017-10-26 19:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
vals, err = b.List(context.Background(), "")
|
2017-10-26 19:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(vals) != 0 {
|
|
|
|
t.Fatalf("bad: %v", vals)
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
2015-03-16 03:15:27 +00:00
|
|
|
}
|