2021-04-06 10:08:43 +00:00
|
|
|
package backup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/portainer/portainer/api/adminmonitor"
|
|
|
|
"github.com/portainer/portainer/api/crypto"
|
2022-05-22 05:34:09 +00:00
|
|
|
"github.com/portainer/portainer/api/demo"
|
2021-04-06 10:08:43 +00:00
|
|
|
"github.com/portainer/portainer/api/http/offlinegate"
|
|
|
|
i "github.com/portainer/portainer/api/internal/testhelpers"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func listFiles(dir string) []string {
|
|
|
|
items := make([]string, 0)
|
|
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if path == dir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
items = append(items, path)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(t *testing.T, list []string, path string) {
|
|
|
|
assert.Contains(t, list, path)
|
2022-10-17 18:29:12 +00:00
|
|
|
copyContent, _ := os.ReadFile(path)
|
2021-04-06 10:08:43 +00:00
|
|
|
assert.Equal(t, "content\n", string(copyContent))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_backupHandlerWithoutPassword_shouldCreateATarballArchive(t *testing.T) {
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"password":""}`))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
gate := offlinegate.NewOfflineGate()
|
|
|
|
adminMonitor := adminmonitor.New(time.Hour, nil, context.Background())
|
|
|
|
|
2022-05-22 05:34:09 +00:00
|
|
|
handlerErr := NewHandler(nil, i.NewDatastore(), gate, "./test_assets/handler_test", func() {}, adminMonitor, &demo.Service{}).backup(w, r)
|
2021-04-06 10:08:43 +00:00
|
|
|
assert.Nil(t, handlerErr, "Handler should not fail")
|
|
|
|
|
|
|
|
response := w.Result()
|
|
|
|
body, _ := io.ReadAll(response.Body)
|
|
|
|
|
2022-09-14 05:59:47 +00:00
|
|
|
tmpdir := t.TempDir()
|
2022-09-28 17:56:32 +00:00
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
archivePath := filepath.Join(tmpdir, "archive.tar.gz")
|
2022-10-17 18:29:12 +00:00
|
|
|
err := os.WriteFile(archivePath, body, 0600)
|
2021-04-06 10:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to save downloaded .tar.gz archive: ", err)
|
|
|
|
}
|
|
|
|
cmd := exec.Command("tar", "-xzf", archivePath, "-C", tmpdir)
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to extract archive: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createdFiles := listFiles(tmpdir)
|
|
|
|
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "portainer.key"))
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "portainer.pub"))
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "tls", "file1"))
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "tls", "file2"))
|
|
|
|
assert.NotContains(t, createdFiles, path.Join(tmpdir, "extra_file"))
|
|
|
|
assert.NotContains(t, createdFiles, path.Join(tmpdir, "extra_folder", "file1"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_backupHandlerWithPassword_shouldCreateEncryptedATarballArchive(t *testing.T) {
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"password":"secret"}`))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
gate := offlinegate.NewOfflineGate()
|
|
|
|
adminMonitor := adminmonitor.New(time.Hour, nil, nil)
|
|
|
|
|
2022-05-22 05:34:09 +00:00
|
|
|
handlerErr := NewHandler(nil, i.NewDatastore(), gate, "./test_assets/handler_test", func() {}, adminMonitor, &demo.Service{}).backup(w, r)
|
2021-04-06 10:08:43 +00:00
|
|
|
assert.Nil(t, handlerErr, "Handler should not fail")
|
|
|
|
|
|
|
|
response := w.Result()
|
|
|
|
body, _ := io.ReadAll(response.Body)
|
|
|
|
|
2022-09-14 05:59:47 +00:00
|
|
|
tmpdir := t.TempDir()
|
2022-09-28 17:56:32 +00:00
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
dr, err := crypto.AesDecrypt(bytes.NewReader(body), []byte("secret"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to decrypt archive")
|
|
|
|
}
|
|
|
|
|
|
|
|
archivePath := filepath.Join(tmpdir, "archive.tag.gz")
|
|
|
|
archive, _ := os.Create(archivePath)
|
|
|
|
defer archive.Close()
|
|
|
|
io.Copy(archive, dr)
|
|
|
|
|
|
|
|
cmd := exec.Command("tar", "-xzf", archivePath, "-C", tmpdir)
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to extract archive: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createdFiles := listFiles(tmpdir)
|
|
|
|
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "portainer.key"))
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "portainer.pub"))
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "tls", "file1"))
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "tls", "file2"))
|
|
|
|
assert.NotContains(t, createdFiles, path.Join(tmpdir, "extra_file"))
|
|
|
|
assert.NotContains(t, createdFiles, path.Join(tmpdir, "extra_folder", "file1"))
|
|
|
|
}
|