mirror of
https://github.com/portainer/portainer.git
synced 2025-07-21 22:39:41 +02:00
* bootstrap encryption key * secret key message change in cli and secret key file content trimmed * Migrate encryption code to latest version * pull in newer code * tidying up * working data encryption layer * fix tests * remove stray comment * fix a few minor issues and improve the comments * split out databasefilename with param to two methods to be more obvious * DB encryption integration (#6374) * json methods moved under DBConnection * store encryption fixed * cleaned * review comments addressed * newstore value fixed * backup test updated * logrus format config updated * Fix for newStore Co-authored-by: Matt Hook <hookenz@gmail.com> * Minor improvements * Improve the export code. Add missing webhook for import * rename HelmUserRepositorys to HelmUserRepositories * fix logging messages * when starting portainer with a key (first use) http is disabled by default. But when starting fresh without a key, http is enabled? * Fix bug for default settings on new installs Co-authored-by: Prabhat Khera <prabhat.khera@portainer.io> Co-authored-by: Prabhat Khera <91852476+prabhat-org@users.noreply.github.com>
87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
package datastore
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
"github.com/portainer/portainer/api/database"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/portainer/portainer/api/filesystem"
|
|
)
|
|
|
|
var errTempDir = errors.New("can't create a temp dir")
|
|
|
|
func (store *Store) GetConnection() portainer.Connection {
|
|
return store.connection
|
|
}
|
|
|
|
func MustNewTestStore(init bool) (bool, *Store, func()) {
|
|
newStore, store, teardown, err := NewTestStore(init)
|
|
if err != nil {
|
|
if !errors.Is(err, errTempDir) {
|
|
teardown()
|
|
}
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return newStore, store, teardown
|
|
}
|
|
|
|
func NewTestStore(init bool) (bool, *Store, func(), error) {
|
|
// Creates unique temp directory in a concurrency friendly manner.
|
|
storePath, err := ioutil.TempDir("", "test-store")
|
|
if err != nil {
|
|
return false, nil, nil, errors.Wrap(errTempDir, err.Error())
|
|
}
|
|
|
|
fileService, err := filesystem.NewService(storePath, "")
|
|
if err != nil {
|
|
return false, nil, nil, err
|
|
}
|
|
|
|
connection, err := database.NewDatabase("boltdb", storePath, []byte("apassphrasewhichneedstobe32bytes"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
store := NewStore(storePath, fileService, connection)
|
|
newStore, err := store.Open()
|
|
if err != nil {
|
|
return newStore, nil, nil, err
|
|
}
|
|
|
|
if init {
|
|
err = store.Init()
|
|
if err != nil {
|
|
return newStore, nil, nil, err
|
|
}
|
|
}
|
|
|
|
if newStore {
|
|
// from MigrateData
|
|
store.VersionService.StoreDBVersion(portainer.DBVersion)
|
|
if err != nil {
|
|
return newStore, nil, nil, err
|
|
}
|
|
}
|
|
|
|
teardown := func() {
|
|
teardown(store, storePath)
|
|
}
|
|
|
|
return newStore, store, teardown, nil
|
|
}
|
|
|
|
func teardown(store *Store, storePath string) {
|
|
err := store.Close()
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
err = os.RemoveAll(storePath)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
}
|