Skip to content

Commit

Permalink
feat: more details on errors during upgrade (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbluhm authored Aug 2, 2024
1 parent 89bd417 commit a76ff36
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
11 changes: 9 additions & 2 deletions acapy_wallet_upgrade/error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
"""Errors that can occur on upgrade."""


class UpgradeError(Exception):
pass
"""Raised on error during upgrade."""


class MissingWalletError(UpgradeError):
pass
"""Raised when a wallet is missing from the wallet keys input."""


class DecryptionFailedError(UpgradeError):
"""Raised when unable to decrypt an item from the source wallet."""
37 changes: 26 additions & 11 deletions acapy_wallet_upgrade/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import cbor2
import msgpack
import nacl.pwhash
from nacl.exceptions import CryptoError

from .db_connection import DbConnection, Wallet
from .error import UpgradeError, MissingWalletError
from .error import DecryptionFailedError, UpgradeError, MissingWalletError
from .pg_connection import PgConnection, PgWallet
from .pg_mwst_connection import PgMWSTConnection
from .sqlite_connection import SqliteConnection
Expand Down Expand Up @@ -182,16 +183,26 @@ async def update_items(
profile_key: dict,
):
progress = Progress("Migrating items...", interval=self.batch_size)
async for rows in wallet.fetch_pending_items(self.batch_size):
upd = []
for row in rows:
result = self.decrypt_item(
row, indy_key, b64=isinstance(wallet, PgWallet)
)
upd.append(self.update_item(result, profile_key))
await wallet.update_items(upd)
progress.update(len(upd))
progress.report()
decrypted_at_least_one = False
try:
async for rows in wallet.fetch_pending_items(self.batch_size):
upd = []
for row in rows:
result = self.decrypt_item(
row, indy_key, b64=isinstance(wallet, PgWallet)
)
decrypted_at_least_one = True
upd.append(self.update_item(result, profile_key))
await wallet.update_items(upd)
progress.update(len(upd))
progress.report()
except CryptoError as err:
if decrypted_at_least_one:
raise UpgradeError(
"Failed to decrypt an item after successfully decrypting others"
) from err
else:
raise DecryptionFailedError("Could not decrypt any items from wallet")

async def fetch_indy_key(self, wallet: Wallet, wallet_key: str) -> dict:
metadata_json = await wallet.get_metadata()
Expand Down Expand Up @@ -794,6 +805,10 @@ async def run(self):
profile_key = await self.init_profile(wallet, wallet_name, indy_key)
await self.update_items(wallet, indy_key, profile_key)
await new_db_conn.finish_upgrade()
except UpgradeError as err:
raise UpgradeError(
f"Failed to upgrade wallet {wallet_name}; bad wallet key given?"
) from err
finally:
await new_db_conn.close()

Expand Down

0 comments on commit a76ff36

Please sign in to comment.