hyperledger / aries-askar

Secure storage designed for Hyperledger Aries agents.
Apache License 2.0
59 stars 45 forks source link

Opening a session for a non existent profile does not throw an error #163

Closed TimoGlastra closed 1 year ago

TimoGlastra commented 1 year ago

Something in noticed with profile is that if I open a session for a profile that does not exist, it will not throw an error until a record is actually inserted of fetched from that session. This means I have to check whether a profile exists before opening, which is possible by calling createProfile. Or we have to fetch something to see if it works. I see some changes to the implementation of profiles, does this PR affect that behvaiour in any way?

What we do with normal wallets is that we try to open them, and if it fails we create them. This allows us to open it directly in all cases except the first time. While now we have to do a check every time that will fail everytime except the first time. It would be useful if opening a session would fail if the profile does not exist yet.

Originally posted by @TimoGlastra in https://github.com/hyperledger/aries-askar/pull/159#pullrequestreview-1560604404

TimoGlastra commented 1 year ago

To add some context.

This is what I currently have to write:

  // FIXME: opening a session for a profile that does not exist, will not throw an error until
  // the session is actually used. We can check if a profile exists by calling `createProfile`
  // which will throw a duplicate error, but that is not very efficient as it needs to be done
  // on every open.
  try {
    await this.store.createProfile(walletConfig.id)
  } catch (error) {
    if (isAskarError(error, AskarErrorCode.Duplicate)) {
      // The profile already exists. This is fine, we can continue
    } else {
      throw error
    }
  }

  this._session = await this.store.session(walletConfig.id).open()

This is what I'd like to write:

try {
  // FIXME: This does not throw an error if the profile does not exist
  this._session = await this.store.session(walletConfig.id).open()
} catch (error) {
  if (isAskarError(error, AskarErrorCode.NotFound)) {
    await this.store.createProfile(walletConfig.id)
    this._session = await this.store.session(walletConfig.id).open()
  } else {
    throw error
  }
}
andrewwhitehead commented 1 year ago

Hm, yes that's a good thing to fix. I considered adding an 'eager' flag when opening a session as it's currently delayed until it's needed. It might be sufficient to just ensure that the profile key is loaded, though, as it could already be cached.