Skip to content

Commit

Permalink
test(migration): minor cleanup (#1403)
Browse files Browse the repository at this point in the history
Signed-off-by: blu3beri <blu3beri@proton.me>
  • Loading branch information
berendsliedrecht committed Mar 28, 2023
1 parent 50e877d commit 7459509
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class IndySdkToAskarMigrationUpdater {
* This doubles checks some fields as later it might be possiblt to run this function
*/
private async migrate() {
const specUri = this.dbPath
const specUri = this.backupFile
const kdfLevel = this.walletConfig.keyDerivationMethod ?? 'ARGON2I_MOD'
const walletName = this.walletConfig.id
const walletKey = this.walletConfig.key
Expand Down Expand Up @@ -137,7 +137,7 @@ export class IndySdkToAskarMigrationUpdater {
* Temporary backup location of the pre-migrated script
*/
private get backupFile() {
return `${this.fs.tempPath}/${this.walletConfig.id}.bak.db`
return `${this.fs.tempPath}/${this.walletConfig.id}.db`
}

/**
Expand All @@ -161,35 +161,6 @@ export class IndySdkToAskarMigrationUpdater {
}
}

/**
* Reverts backed up database file to the original path, if its missing, and
* deletes the backup. We do some additional, possible redundant, exists checks
* here to be extra sure that only a happy flow occurs.
*/
private async restoreDatabase() {
// "Impossible" state. Since we do not continue if `this.backupDatabase()`
// fails, this file should always be there. If this error is thrown, we
// cannot correctly restore the state.
if (!(await this.fs.exists(this.backupFile))) {
throw new IndySdkToAskarMigrationError('Backup file could not be found while trying to restore the state')
}

/**
* Since we used `copy` to get the file, it should still be there. We
* double-check here to be sure.
*/
if (!(await this.fs.exists(this.dbPath))) {
return
} else {
this.agent.config.logger.trace(`Moving '${this.backupFile}' back to the original path: '${this.dbPath}`)

// Move the backedup file back to the original path
await this.fs.copyFile(this.backupFile, this.dbPath)

this.agent.config.logger.trace(`Cleaned up the backed up file at '${this.backupFile}'`)
}
}

// Delete the backup as `this.fs.copyFile` only copies and no deletion
// Since we use `tempPath` which is cleared when certain events happen,
// e.g. cron-job and system restart (depending on the os) we could omit
Expand All @@ -204,7 +175,7 @@ export class IndySdkToAskarMigrationUpdater {
* to the `FileSystem.dataPath`.
*/
private async moveToNewLocation() {
const src = this.dbPath
const src = this.backupFile
// New path for the database
const dest = this.newWalletPath

Expand Down Expand Up @@ -243,7 +214,7 @@ export class IndySdkToAskarMigrationUpdater {

const keyMethod =
this.walletConfig?.keyDerivationMethod == KeyDerivationMethod.Raw ? StoreKeyMethod.Raw : StoreKeyMethod.Kdf
this.store = await Store.open({ uri: `sqlite://${this.dbPath}`, passKey: this.walletConfig.key, keyMethod })
this.store = await Store.open({ uri: `sqlite://${this.backupFile}`, passKey: this.walletConfig.key, keyMethod })

// Update the values to reflect the new structure
await this.updateKeys()
Expand All @@ -256,8 +227,6 @@ export class IndySdkToAskarMigrationUpdater {
} catch (err) {
this.agent.config.logger.error('Migration failed. Restoring state.')

await this.restoreDatabase()

throw new IndySdkToAskarMigrationError(`Migration failed. State has been restored. ${err.message}`, {
cause: err.cause,
})
Expand Down
141 changes: 68 additions & 73 deletions packages/indy-sdk-to-askar-migration/tests/migrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,85 +15,52 @@ import { IndySdkToAskarMigrationError } from '../src/errors/IndySdkToAskarMigrat

// FIXME: Re-include in tests when NodeJS wrapper performance is improved
describeRunInNodeVersion([18], 'Indy SDK To Askar Migration', () => {
const config: InitConfig = {
label: 'test-agent',
walletConfig: {
id: `walletwallet.0-${utils.uuid()}`,
key: 'GfwU1DC7gEZNs3w41tjBiZYj7BNToDoFEqKY6wZXqs1A',
keyDerivationMethod: KeyDerivationMethod.Raw,
},
}

const invalidConfig: InitConfig = {
label: 'invalid-test-agent',
walletConfig: {
id: `walletwallet.1-${utils.uuid()}`,
key: 'GfwU1DC7gEZNs3w41tjBiZYj7BNToDoFEqKY6wZXqs1A',
keyDerivationMethod: KeyDerivationMethod.Raw,
},
}

const invalidAgent = new Agent({
config: invalidConfig,
modules: {
indySdk: new IndySdkModule({ indySdk: indy }),
},
dependencies: agentDependencies,
})

const invalidNewAgent = new Agent({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
config: { ...invalidConfig, walletConfig: { ...invalidConfig.walletConfig!, key: 'wrong-key' } },
modules: {
askar: new AskarModule({
ariesAskar,
}),
},
dependencies: agentDependencies,
})

const oldAgent = new Agent({
config,
modules: {
indySdk: new IndySdkModule({ indySdk: indy }),
},
dependencies: agentDependencies,
})

const newAgent = new Agent({
config,
modules: {
askar: new AskarModule({
ariesAskar,
}),
},
dependencies: agentDependencies,
})

const oldAgentDbPath = `${homedir()}/.indy_client/wallet/${oldAgent.config.walletConfig?.id}/sqlite.db`
const invalidAgentDbPath = `${homedir()}/.indy_client/wallet/${invalidAgent.config.walletConfig?.id}/sqlite.db`

beforeAll(() => {
registerAriesAskar({ askar: ariesAskar })
})

test('indy-sdk sqlite to aries-askar sqlite', async () => {
test('indy-sdk sqlite to aries-askar sqlite successful migration', async () => {
const indySdkAndAskarConfig: InitConfig = {
label: `indy | indy-sdk sqlite to aries-askar sqlite successful migration | ${utils.uuid()}`,
walletConfig: {
id: `indy-sdk sqlite to aries-askar sqlite successful migration | ${utils.uuid()}`,
key: 'GfwU1DC7gEZNs3w41tjBiZYj7BNToDoFEqKY6wZXqs1A',
keyDerivationMethod: KeyDerivationMethod.Raw,
},
}

const indySdkAgent = new Agent({
config: indySdkAndAskarConfig,
modules: { indySdk: new IndySdkModule({ indySdk: indy }) },
dependencies: agentDependencies,
})

const indySdkAgentDbPath = `${homedir()}/.indy_client/wallet/${indySdkAndAskarConfig.walletConfig?.id}/sqlite.db`

const genericRecordContent = { foo: 'bar' }

await oldAgent.initialize()
await indySdkAgent.initialize()

const record = await indySdkAgent.genericRecords.save({ content: genericRecordContent })

const record = await oldAgent.genericRecords.save({ content: genericRecordContent })
await indySdkAgent.shutdown()

await oldAgent.shutdown()
const askarAgent = new Agent({
config: indySdkAndAskarConfig,
modules: { askar: new AskarModule({ ariesAskar }) },
dependencies: agentDependencies,
})

const updater = await IndySdkToAskarMigrationUpdater.initialize({ dbPath: oldAgentDbPath, agent: newAgent })
const updater = await IndySdkToAskarMigrationUpdater.initialize({ dbPath: indySdkAgentDbPath, agent: askarAgent })
await updater.update()

await newAgent.initialize()
await askarAgent.initialize()

await expect(newAgent.genericRecords.findById(record.id)).resolves.toMatchObject({ content: genericRecordContent })
await expect(askarAgent.genericRecords.findById(record.id)).resolves.toMatchObject({
content: genericRecordContent,
})

await newAgent.shutdown()
await askarAgent.shutdown()
})

/*
Expand All @@ -104,24 +71,52 @@ describeRunInNodeVersion([18], 'Indy SDK To Askar Migration', () => {
* - Check if the record can still be accessed
*/
test('indy-sdk sqlite to aries-askar sqlite fails and restores', async () => {
const indySdkAndAskarConfig: InitConfig = {
label: `indy | indy-sdk sqlite to aries-askar sqlite fails and restores | ${utils.uuid()}`,
walletConfig: {
id: `indy-sdk sqlite to aries-askar sqlite fails and restores | ${utils.uuid()}`,
key: 'GfwU1DC7gEZNs3w41tjBiZYj7BNToDoFEqKY6wZXqs1A',
keyDerivationMethod: KeyDerivationMethod.Raw,
},
}

const indySdkAgent = new Agent({
config: indySdkAndAskarConfig,
modules: { indySdk: new IndySdkModule({ indySdk: indy }) },
dependencies: agentDependencies,
})

const indySdkAgentDbPath = `${homedir()}/.indy_client/wallet/${indySdkAndAskarConfig.walletConfig?.id}/sqlite.db`

const genericRecordContent = { foo: 'bar' }

await invalidAgent.initialize()
await indySdkAgent.initialize()

const record = await invalidAgent.genericRecords.save({ content: genericRecordContent })
const record = await indySdkAgent.genericRecords.save({ content: genericRecordContent })

await invalidAgent.shutdown()
await indySdkAgent.shutdown()

const askarAgent = new Agent({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
config: { ...indySdkAndAskarConfig, walletConfig: { ...indySdkAndAskarConfig.walletConfig!, key: 'wrong-key' } },
modules: {
askar: new AskarModule({
ariesAskar,
}),
},
dependencies: agentDependencies,
})

const updater = await IndySdkToAskarMigrationUpdater.initialize({
dbPath: invalidAgentDbPath,
agent: invalidNewAgent,
dbPath: indySdkAgentDbPath,
agent: askarAgent,
})

await expect(updater.update()).rejects.toThrowError(IndySdkToAskarMigrationError)

await invalidAgent.initialize()
await indySdkAgent.initialize()

await expect(invalidAgent.genericRecords.findById(record.id)).resolves.toMatchObject({
await expect(indySdkAgent.genericRecords.findById(record.id)).resolves.toMatchObject({
content: genericRecordContent,
})
})
Expand Down

0 comments on commit 7459509

Please sign in to comment.