Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions node/backup/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ import (
"github.com/status-im/status-go/signal"
)

//go:generate go tool mockgen -package=mock_backup_controller -source controller.go -destination=mock/mock_backup_controller.go

type BackupConfig struct {
PrivateKey []byte
FileNameGetter func() (string, error)
BackupEnabled bool
Interval time.Duration
PrivateKey []byte
FileNameProvider FilenameProvider
BackupEnabled bool
Interval time.Duration
}

type FilenameProvider interface {
GetBackupFilename() (string, error)
}

type BackupProvider interface {
Expand Down Expand Up @@ -52,8 +58,8 @@ func NewController(config BackupConfig, logger *zap.Logger) (*Controller, error)
if len(config.PrivateKey) == 0 {
return nil, errors.New("private key must be provided")
}
if config.FileNameGetter == nil {
return nil, errors.New("filename getter must be provided")
if common.IsNil(config.FileNameProvider) {
return nil, errors.New("filename provider must be provided")
}

return &Controller{
Expand Down Expand Up @@ -111,7 +117,7 @@ func (c *Controller) PerformBackup() (string, error) {
return "", err
}

fileName, err := c.config.FileNameGetter()
fileName, err := c.config.FileNameProvider.GetBackupFilename()
if err != nil {
return "", err
}
Expand Down
12 changes: 10 additions & 2 deletions node/backup/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"testing"

"github.com/brianvoe/gofakeit/v6"
"go.uber.org/mock/gomock"

"go.uber.org/zap"

"github.com/stretchr/testify/require"

mock_backup_controller "github.com/status-im/status-go/node/backup/mock"
)

type Foo struct {
Expand Down Expand Up @@ -55,9 +58,14 @@ func TestController(t *testing.T) {
logger, err := zap.NewDevelopment()
require.NoError(t, err)
filename := t.TempDir() + "/test_backup.bak"

ctrl := gomock.NewController(t)
filenameProvider := mock_backup_controller.NewMockFilenameProvider(ctrl)
filenameProvider.EXPECT().GetBackupFilename().Return(filename, nil).AnyTimes()

controller, err := NewController(BackupConfig{
FileNameGetter: func() (string, error) { return filename, nil },
PrivateKey: []byte("0123456789abcdef0123456789abcdef"),
FileNameProvider: filenameProvider,
PrivateKey: []byte("0123456789abcdef0123456789abcdef"),
}, logger)
require.NoError(t, err)

Expand Down
58 changes: 31 additions & 27 deletions node/get_status_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,34 +248,11 @@ func (n *StatusNode) StartLocalBackup() error {

privateKey := chatAccount.PrivateKey()

filenameGetter := func() (string, error) {
backupPath, err := n.accountsSrvc.GetBackupPath()
if err != nil {
return "", err
}

compressedPubKey, err := utils.SerializePublicKey(crypto.CompressPubkey(&privateKey.PublicKey))
if err != nil {
return "", err
}

var backupDir string
if backupPath != "" {
backupDir = backupPath
} else {
backupDir = filepath.Join(n.config.RootDataDir, "backups")
}

fullPath := filepath.Join(backupDir, fmt.Sprintf("%s_user_data.bkp", compressedPubKey[len(compressedPubKey)-6:]))

return fullPath, nil
}

n.localBackup, err = backup.NewController(backup.BackupConfig{
PrivateKey: crypto.Keccak256(crypto.FromECDSA(privateKey)),
FileNameGetter: filenameGetter,
BackupEnabled: true,
Interval: time.Minute * 30,
PrivateKey: crypto.Keccak256(crypto.FromECDSA(privateKey)),
FileNameProvider: n,
BackupEnabled: true,
Interval: time.Minute * 30,
}, n.logger.Named("LocalBackup"))
if err != nil {
return err
Expand All @@ -298,6 +275,33 @@ func (n *StatusNode) StartLocalBackup() error {
return nil
}

func (n *StatusNode) GetBackupFilename() (string, error) {
chatAccount, err := n.gethAccountsManager.SelectedChatAccount()
if err != nil {
return "", err
}

privateKey := chatAccount.PrivateKey()

backupPath, err := n.accountsSrvc.GetBackupPath()
if err != nil {
return "", err
}

compressedPubKey, err := utils.SerializePublicKey(crypto.CompressPubkey(&privateKey.PublicKey))
if err != nil {
return "", err
}

if backupPath == "" {
return "", errors.New("backup path is not set")
}

fullPath := filepath.Join(backupPath, fmt.Sprintf("%s_user_data.bkp", compressedPubKey[len(compressedPubKey)-6:]))

return fullPath, nil
}

func (n *StatusNode) PerformLocalBackup() (string, error) {
return n.localBackup.PerformBackup()
}
Expand Down
7 changes: 7 additions & 0 deletions tests-functional/tests/test_local_backup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
import re

import pytest

from clients.status_backend import StatusBackend
from resources.constants import Account, user_1
from resources.test_data import profile_showcase_utils
from utils import fake
from clients.api import ApiResponseError

test_one_to_one_chat_id = (
"0x043329fc08727f15c4ec9a17bf7d6a3dc44e9d0d8f782a55804f3660b28827194a365dc1765cf96b6fa5cd666b9ee5298e8ac82f51f7952c4110cbf321d4f63864"
Expand Down Expand Up @@ -194,6 +196,11 @@ def test_local_backup(self, tmp_path):
assert group_chat_recovered, "Group chat was not restored correctly"
assert one_on_one_chat_recovered, "One-to-one chat was not restored correctly"

# Test that it fails if no backupPath is set
backend_client.settings_service.save_setting("backup-path", "")
with pytest.raises(ApiResponseError, match=re.escape("backup path is not set")):
backend_client.api_request_json("PerformLocalBackup", "")

# Change the backup path (Docker restricts access to a lot of folders)
backend_client.settings_service.save_setting("backup-path", "/usr/status-user/backups")

Expand Down
Loading