2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2019-06-05 12:12:34 -04:00
|
|
|
"archive/tar"
|
2019-05-14 16:14:39 -04:00
|
|
|
"archive/zip"
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2019-03-06 09:01:12 -05:00
|
|
|
"crypto/rand"
|
2019-05-14 16:14:39 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2018-12-17 05:27:07 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-05-14 16:14:39 -04:00
|
|
|
"path"
|
2018-12-17 05:27:07 -05:00
|
|
|
"testing"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2018-12-17 05:27:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type endpoint struct {
|
|
|
|
Foo string `json:"a_very_recognizable_field_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type context struct {
|
|
|
|
Bar string `json:"another_very_recognizable_field_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCfg = NewConfig(func() interface{} { return &context{} },
|
|
|
|
EndpointTypeGetter("ep1", func() interface{} { return &endpoint{} }),
|
|
|
|
EndpointTypeGetter("ep2", func() interface{} { return &endpoint{} }),
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExportImport(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
s := New(testDir, testCfg)
|
2019-04-18 09:12:30 -04:00
|
|
|
err = s.CreateOrUpdate(
|
|
|
|
Metadata{
|
2018-12-17 05:27:07 -05:00
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
2018-11-09 09:10:41 -05:00
|
|
|
Name: "source",
|
2018-12-17 05:27:07 -05:00
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
file1 := make([]byte, 1500)
|
|
|
|
rand.Read(file1)
|
|
|
|
file2 := make([]byte, 3700)
|
|
|
|
rand.Read(file2)
|
2019-04-18 09:12:30 -04:00
|
|
|
err = s.ResetEndpointTLSMaterial("source", "ep1", &EndpointTLSData{
|
2018-12-17 05:27:07 -05:00
|
|
|
Files: map[string][]byte{
|
2019-03-06 09:01:12 -05:00
|
|
|
"file1": file1,
|
|
|
|
"file2": file2,
|
2018-12-17 05:27:07 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
r := Export("source", s)
|
|
|
|
defer r.Close()
|
|
|
|
err = Import("dest", s, r)
|
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
srcMeta, err := s.GetMetadata("source")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
destMeta, err := s.GetMetadata("dest")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.DeepEqual(t, destMeta.Metadata, srcMeta.Metadata)
|
|
|
|
assert.DeepEqual(t, destMeta.Endpoints, srcMeta.Endpoints)
|
2019-04-18 09:12:30 -04:00
|
|
|
srcFileList, err := s.ListTLSFiles("source")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
destFileList, err := s.ListTLSFiles("dest")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.Equal(t, 1, len(destFileList))
|
|
|
|
assert.Equal(t, 1, len(srcFileList))
|
|
|
|
assert.Equal(t, 2, len(destFileList["ep1"]))
|
|
|
|
assert.Equal(t, 2, len(srcFileList["ep1"]))
|
2019-04-18 09:12:30 -04:00
|
|
|
srcData1, err := s.GetTLSData("source", "ep1", "file1")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.DeepEqual(t, file1, srcData1)
|
2019-04-18 09:12:30 -04:00
|
|
|
srcData2, err := s.GetTLSData("source", "ep1", "file2")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.DeepEqual(t, file2, srcData2)
|
2019-04-18 09:12:30 -04:00
|
|
|
destData1, err := s.GetTLSData("dest", "ep1", "file1")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file1, destData1)
|
2019-04-18 09:12:30 -04:00
|
|
|
destData2, err := s.GetTLSData("dest", "ep1", "file2")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file2, destData2)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
s := New(testDir, testCfg)
|
2019-04-18 09:12:30 -04:00
|
|
|
err = s.CreateOrUpdate(
|
|
|
|
Metadata{
|
2018-12-17 05:27:07 -05:00
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
2018-11-09 09:10:41 -05:00
|
|
|
Name: "source",
|
2018-12-17 05:27:07 -05:00
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
assert.NilError(t, s.ResetEndpointTLSMaterial("source", "ep1", &EndpointTLSData{
|
2018-12-17 05:27:07 -05:00
|
|
|
Files: map[string][]byte{
|
|
|
|
"file1": []byte("test-data"),
|
|
|
|
},
|
|
|
|
}))
|
2019-04-18 09:12:30 -04:00
|
|
|
assert.NilError(t, s.Remove("source"))
|
|
|
|
_, err = s.GetMetadata("source")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.Check(t, IsErrContextDoesNotExist(err))
|
2019-04-18 09:12:30 -04:00
|
|
|
f, err := s.ListTLSFiles("source")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, 0, len(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListEmptyStore(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
store := New(testDir, testCfg)
|
2019-04-18 09:12:30 -04:00
|
|
|
result, err := store.List()
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, len(result) == 0)
|
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
|
|
|
|
func TestErrHasCorrectContext(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
store := New(testDir, testCfg)
|
2019-04-18 09:12:30 -04:00
|
|
|
_, err = store.GetMetadata("no-exists")
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.ErrorContains(t, err, "no-exists")
|
|
|
|
assert.Check(t, IsErrContextDoesNotExist(err))
|
|
|
|
}
|
2019-05-14 16:14:39 -04:00
|
|
|
|
|
|
|
func TestDetectImportContentType(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
r := bufio.NewReader(buf)
|
|
|
|
ct, err := getImportContentType(r)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, zipType != ct)
|
|
|
|
}
|
|
|
|
|
2019-06-05 12:12:34 -04:00
|
|
|
func TestImportTarInvalid(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
|
|
|
|
tf := path.Join(testDir, "test.context")
|
|
|
|
|
|
|
|
f, err := os.Create(tf)
|
|
|
|
assert.NilError(t, err)
|
2019-10-29 05:24:55 -04:00
|
|
|
defer f.Close()
|
2019-06-05 12:12:34 -04:00
|
|
|
|
|
|
|
tw := tar.NewWriter(f)
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: "dummy-file",
|
|
|
|
Mode: 0600,
|
|
|
|
Size: int64(len("hello world")),
|
|
|
|
}
|
|
|
|
err = tw.WriteHeader(hdr)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = tw.Write([]byte("hello world"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
err = tw.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
source, err := os.Open(tf)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer source.Close()
|
|
|
|
var r io.Reader = source
|
|
|
|
s := New(testDir, testCfg)
|
|
|
|
err = Import("tarInvalid", s, r)
|
2020-09-16 10:35:04 -04:00
|
|
|
assert.ErrorContains(t, err, "unexpected context file")
|
2019-06-05 12:12:34 -04:00
|
|
|
}
|
|
|
|
|
2019-05-14 16:14:39 -04:00
|
|
|
func TestImportZip(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
|
|
|
|
zf := path.Join(testDir, "test.zip")
|
|
|
|
|
|
|
|
f, err := os.Create(zf)
|
|
|
|
assert.NilError(t, err)
|
2019-10-29 05:24:55 -04:00
|
|
|
defer f.Close()
|
2019-05-14 16:14:39 -04:00
|
|
|
w := zip.NewWriter(f)
|
|
|
|
|
|
|
|
meta, err := json.Marshal(Metadata{
|
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
|
|
|
Name: "source",
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
var files = []struct {
|
|
|
|
Name, Body string
|
|
|
|
}{
|
|
|
|
{"meta.json", string(meta)},
|
|
|
|
{path.Join("tls", "docker", "ca.pem"), string([]byte("ca.pem"))},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
f, err := w.Create(file.Name)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = f.Write([]byte(file.Body))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
source, err := os.Open(zf)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
ct, err := getImportContentType(bufio.NewReader(source))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, zipType, ct)
|
|
|
|
|
|
|
|
source, _ = os.Open(zf)
|
|
|
|
defer source.Close()
|
|
|
|
var r io.Reader = source
|
|
|
|
s := New(testDir, testCfg)
|
|
|
|
err = Import("zipTest", s, r)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
2019-06-05 12:12:34 -04:00
|
|
|
|
|
|
|
func TestImportZipInvalid(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
|
|
|
|
zf := path.Join(testDir, "test.zip")
|
|
|
|
|
|
|
|
f, err := os.Create(zf)
|
|
|
|
assert.NilError(t, err)
|
2019-10-29 05:24:55 -04:00
|
|
|
defer f.Close()
|
2019-06-05 12:12:34 -04:00
|
|
|
w := zip.NewWriter(f)
|
|
|
|
|
|
|
|
df, err := w.Create("dummy-file")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = df.Write([]byte("hello world"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
err = w.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
source, err := os.Open(zf)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer source.Close()
|
|
|
|
var r io.Reader = source
|
|
|
|
s := New(testDir, testCfg)
|
|
|
|
err = Import("zipInvalid", s, r)
|
2020-09-16 10:35:04 -04:00
|
|
|
assert.ErrorContains(t, err, "unexpected context file")
|
2019-06-05 12:12:34 -04:00
|
|
|
}
|