Skip to content

Commit

Permalink
Hotfix: add type for recover, and some cleanups (#260)
Browse files Browse the repository at this point in the history
* Hotfix: add type for recover, and some cleanups

* Hotfix: add type for recover, and come cleanups - update proto enum
  • Loading branch information
jsonsivar authored Nov 25, 2020
1 parent b8edce8 commit 5b732c1
Show file tree
Hide file tree
Showing 11 changed files with 727 additions and 691 deletions.
10 changes: 8 additions & 2 deletions core/space/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,21 @@ type KeyBackupType int

const (
PASSWORD KeyBackupType = 0
ETH KeyBackupType = 1
GOOGLE KeyBackupType = 1
TWITTER KeyBackupType = 2
EMAIL KeyBackupType = 3
)

func (b KeyBackupType) String() string {
switch b {
case 0:
return "password"
case 1:
return "eth"
return "google"
case 2:
return "twitter"
case 3:
return "email"
default:
return fmt.Sprintf("%d", int(b))
}
Expand Down
9 changes: 5 additions & 4 deletions core/space/services/services_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/FleekHQ/space-daemon/core/backup"
"github.com/FleekHQ/space-daemon/core/space/domain"
"github.com/FleekHQ/space-daemon/core/vault"
"github.com/libp2p/go-libp2p-core/crypto"
)
Expand Down Expand Up @@ -67,8 +68,8 @@ func (s *Space) RecoverKeysByLocalBackup(ctx context.Context, path string) error
}

// Uses vault service to fetch and decrypt a keypair set
func (s *Space) RecoverKeysByPassphrase(ctx context.Context, uuid string, pass string) error {
items, err := s.vault.Retrieve(uuid, pass)
func (s *Space) RecoverKeysByPassphrase(ctx context.Context, uuid string, pass string, backupType domain.KeyBackupType) error {
items, err := s.vault.Retrieve(uuid, pass, backupType)
if err != nil {
return err
}
Expand Down Expand Up @@ -101,7 +102,7 @@ func (s *Space) RecoverKeysByPassphrase(ctx context.Context, uuid string, pass s
}

// Uses the vault service to securely store the current keypair
func (s *Space) BackupKeysByPassphrase(ctx context.Context, uuid string, pass string, backupType string) error {
func (s *Space) BackupKeysByPassphrase(ctx context.Context, uuid string, pass string, backupType domain.KeyBackupType) error {
tokens, err := s.GetAPISessionTokens(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -139,7 +140,7 @@ func (s *Space) BackupKeysByPassphrase(ctx context.Context, uuid string, pass st

// Tests a passphrase without storing anything to check if the passphrase is correct
func (s *Space) TestPassphrase(ctx context.Context, uuid string, pass string) error {
items, err := s.vault.Retrieve(uuid, pass)
items, err := s.vault.Retrieve(uuid, pass, domain.PASSWORD)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions core/space/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type Service interface {
DeleteKeypair(ctx context.Context) error
GetMnemonic(ctx context.Context) (mnemonic string, err error)
RestoreKeyPairFromMnemonic(ctx context.Context, mnemonic string) error
RecoverKeysByPassphrase(ctx context.Context, uuid string, pass string) error
BackupKeysByPassphrase(ctx context.Context, uuid string, pass string, backupType string) error
RecoverKeysByPassphrase(ctx context.Context, uuid string, pass string, backupType domain.KeyBackupType) error
BackupKeysByPassphrase(ctx context.Context, uuid string, pass string, backupType domain.KeyBackupType) error
TestPassphrase(ctx context.Context, uuid string, pass string) error
GetPublicKey(ctx context.Context) (string, error)
GetHubAuthToken(ctx context.Context) (string, error)
Expand Down
6 changes: 3 additions & 3 deletions core/space/space_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ func TestService_VaultBackup(t *testing.T) {

pass := "strawberry123"
uuid := "c907e7ef-7b36-4ab1-8a56-f788d7526a2c"
backupType := "password"
backupType := domain.PASSWORD
ctx := context.Background()
mnemonic := "clog chalk blame black uncover frame before decide tuition maple crowd uncle"

Expand Down Expand Up @@ -802,13 +802,13 @@ func TestService_VaultRestore(t *testing.T) {

mockItems := []vault.VaultItem{mockItem}

mockVault.On("Retrieve", uuid, pass).Return(mockItems, nil)
mockVault.On("Retrieve", uuid, pass, domain.PASSWORD).Return(mockItems, nil)

mockKeychain.On("ImportExistingKeyPair", mock.Anything, mock.Anything).Return(nil)

textileClient.On("RestoreDB", mock.Anything).Return(nil)

err := sv.RecoverKeysByPassphrase(ctx, uuid, pass)
err := sv.RecoverKeysByPassphrase(ctx, uuid, pass, domain.PASSWORD)
assert.Nil(t, err)
mockKeychain.AssertCalled(t, "ImportExistingKeyPair", mockPrivKey, mnemonic)
}
Expand Down
16 changes: 9 additions & 7 deletions core/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ type StoredVault struct {
}

type retrieveVaultRequest struct {
Vsk string `json:"vsk"`
Vsk string `json:"vsk"`
Type string `json:"type"`
}

type retrieveVaultResponse struct {
EncryptedVault string `json:"encryptedVault"`
}

type Vault interface {
Store(uuid string, passphrase string, backupType string, apiToken string, items []VaultItem) (*StoredVault, error)
Retrieve(uuid string, passphrase string) ([]VaultItem, error)
Store(uuid string, passphrase string, backupType domain.KeyBackupType, apiToken string, items []VaultItem) (*StoredVault, error)
Retrieve(uuid string, passphrase string, backupType domain.KeyBackupType) ([]VaultItem, error)
}

func New(vaultAPIURL string, vaultSaltSecret string) *vault {
Expand All @@ -74,7 +75,7 @@ func New(vaultAPIURL string, vaultSaltSecret string) *vault {
}
}

func (v *vault) Store(uuid string, passphrase string, backupType string, apiToken string, items []VaultItem) (*StoredVault, error) {
func (v *vault) Store(uuid string, passphrase string, backupType domain.KeyBackupType, apiToken string, items []VaultItem) (*StoredVault, error) {
// Generate vault file
vf, err := json.Marshal(items)
if err != nil {
Expand All @@ -97,7 +98,7 @@ func (v *vault) Store(uuid string, passphrase string, backupType string, apiToke
storeRequest := &storeVaultRequest{
Vault: base64.RawStdEncoding.EncodeToString(encVf),
Vsk: base64.RawStdEncoding.EncodeToString(vsk),
Type: backupType,
Type: domain.KeyBackupType(backupType).String(),
}
reqJSON, err := json.Marshal(storeRequest)
if err != nil {
Expand Down Expand Up @@ -128,7 +129,7 @@ func (v *vault) Store(uuid string, passphrase string, backupType string, apiToke
return result, nil
}

func (v *vault) Retrieve(uuid string, passphrase string) ([]VaultItem, error) {
func (v *vault) Retrieve(uuid string, passphrase string, backupType domain.KeyBackupType) ([]VaultItem, error) {
// Compute vault key
vk := v.computeVk(uuid, passphrase, VkVersion1)

Expand All @@ -137,7 +138,8 @@ func (v *vault) Retrieve(uuid string, passphrase string) ([]VaultItem, error) {

// Send retrieve request to vault service
reqJSON, err := json.Marshal(&retrieveVaultRequest{
Vsk: base64.RawStdEncoding.EncodeToString(vsk),
Vsk: base64.RawStdEncoding.EncodeToString(vsk),
Type: domain.KeyBackupType(backupType).String(),
})
if err != nil {
return nil, err
Expand Down
9 changes: 5 additions & 4 deletions core/vault/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http/httptest"
"testing"

"github.com/FleekHQ/space-daemon/core/space/domain"
"github.com/FleekHQ/space-daemon/core/vault"
"github.com/stretchr/testify/assert"
)
Expand All @@ -26,7 +27,7 @@ func TestVault_StoreAndRetrieve(t *testing.T) {
_, _ = w.Write([]byte(`{}`))
}

testBackupType := "password"
testBackupType := domain.PASSWORD

serverMock := func() *httptest.Server {
handler := http.NewServeMux()
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestVault_StoreAndRetrieve(t *testing.T) {
testSaltSecret,
)

retrievedItems, err := v2.Retrieve(testUuid, testPassphrase)
retrievedItems, err := v2.Retrieve(testUuid, testPassphrase, domain.PASSWORD)
assert.Nil(t, err)
if err != nil {
return
Expand All @@ -98,7 +99,7 @@ func TestVault_StoreServerError(t *testing.T) {
},
}

testBackupType := "password"
testBackupType := domain.PASSWORD

storeVaultMock := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
Expand Down Expand Up @@ -151,7 +152,7 @@ func TestVault_RetrieveServerError(t *testing.T) {
testSaltSecret,
)

retrievedItems, err := v.Retrieve(testUuid, testPassphrase)
retrievedItems, err := v.Retrieve(testUuid, testPassphrase, domain.PASSWORD)

assert.NotNil(t, err)
assert.Nil(t, retrievedItems)
Expand Down
4 changes: 2 additions & 2 deletions grpc/handlers_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

func (srv *grpcServer) BackupKeysByPassphrase(ctx context.Context, request *pb.BackupKeysByPassphraseRequest) (*pb.BackupKeysByPassphraseResponse, error) {
resp := &pb.BackupKeysByPassphraseResponse{}
err := srv.sv.BackupKeysByPassphrase(ctx, request.Uuid, request.Passphrase, domain.KeyBackupType(request.Type).String())
err := srv.sv.BackupKeysByPassphrase(ctx, request.Uuid, request.Passphrase, domain.KeyBackupType(request.Type))

return resp, err
}

func (srv *grpcServer) RecoverKeysByPassphrase(ctx context.Context, request *pb.RecoverKeysByPassphraseRequest) (*pb.RecoverKeysByPassphraseResponse, error) {
resp := &pb.RecoverKeysByPassphraseResponse{}
err := srv.sv.RecoverKeysByPassphrase(ctx, request.Uuid, request.Passphrase)
err := srv.sv.RecoverKeysByPassphrase(ctx, request.Uuid, request.Passphrase, domain.KeyBackupType(request.Type))

return resp, err
}
Expand Down
Loading

0 comments on commit 5b732c1

Please sign in to comment.