Skip to content

Commit

Permalink
fix: migrate to quill for generic secret storage (#1299)
Browse files Browse the repository at this point in the history
Signed-off-by: Pat Losoponkul <[email protected]>
  • Loading branch information
patlo-iog authored Aug 23, 2024
1 parent 6f8c9ff commit e077cdd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,55 +1,39 @@
package org.hyperledger.identus.agent.walletapi.sql

import doobie.*
import doobie.implicits.*
import doobie.postgres.implicits.*
import io.getquill.JsonValue
import org.hyperledger.identus.agent.walletapi.sql.model.GenericSecretSql
import org.hyperledger.identus.agent.walletapi.sql.model as db
import org.hyperledger.identus.agent.walletapi.storage.{GenericSecret, GenericSecretStorage}
import org.hyperledger.identus.shared.db.ContextAwareTask
import org.hyperledger.identus.shared.db.Implicits.*
import org.hyperledger.identus.shared.models.WalletAccessContext
import zio.*
import zio.json.ast.Json

import java.time.Instant

class JdbcGenericSecretStorage(xa: Transactor[ContextAwareTask]) extends GenericSecretStorage {

override def set[K, V](key: K, secret: V)(implicit ev: GenericSecret[K, V]): RIO[WalletAccessContext, Unit] = {
val keyPath = ev.keyPath(key)
val payload = ev.encodeValue(secret)
val cxnIO = (now: Instant) => sql"""
| INSERT INTO public.generic_secret(
| key,
| payload,
| created_at,
| wallet_id
| ) values (
| ${keyPath},
| ${payload},
| ${now},
| current_setting('app.current_wallet_id')::UUID
| )
""".stripMargin.update

for {
now <- Clock.instant
_ <- cxnIO(now).run.transactWallet(xa)
walletId <- ZIO.serviceWith[WalletAccessContext](_.walletId)
s = db.GenericSecret(key = keyPath, payload = JsonValue(payload), createdAt = now, walletId = walletId)
_ <- GenericSecretSql
.insert(s)
.transactWallet(xa)
} yield ()
}

override def get[K, V](key: K)(implicit ev: GenericSecret[K, V]): RIO[WalletAccessContext, Option[V]] = {
val keyPath = ev.keyPath(key)
val cxnIO = sql"""
| SELECT payload
| FROM public.generic_secret
| WHERE key = ${keyPath}
""".stripMargin
.query[Json]
.option

cxnIO
GenericSecretSql
.findByKey(keyPath)
.transactWallet(xa)
.flatMap(_.fold(ZIO.none)(json => ZIO.fromTry(ev.decodeValue(json)).asSome))
.flatMap(
_.headOption
.fold(ZIO.none)(row => ZIO.fromTry(ev.decodeValue(row.payload.value)).asSome)
)
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.hyperledger.identus.agent.walletapi.sql.model

import io.getquill.{SnakeCase, *}
import io.getquill.context.json.PostgresJsonExtensions
import io.getquill.doobie.DoobieContext
import org.hyperledger.identus.shared.models.WalletId
import zio.json.ast.Json

import java.time.Instant

final case class GenericSecret(
key: String,
payload: JsonValue[Json],
createdAt: Instant,
walletId: WalletId
)

object GenericSecretSql extends DoobieContext.Postgres(SnakeCase), PostgresJsonExtensions {
def insert(secret: GenericSecret) = run {
quote(
query[GenericSecret].insertValue(lift(secret))
)
}

def findByKey(key: String) = run {
quote(
query[GenericSecret].filter(_.key == lift(key)).take(1)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ object Wallet {
}

object WalletSql extends DoobieContext.Postgres(SnakeCase) {

def insert(wallet: Wallet) = run {
quote(
query[Wallet]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import org.hyperledger.identus.pollux.core.service.serdes.{AnoncredCredentialPro
import org.hyperledger.identus.pollux.sdjwt.{HolderPrivateKey, PresentationCompact}
import org.hyperledger.identus.pollux.vc.jwt.{Issuer, PresentationPayload, W3cCredentialPayload}
import org.hyperledger.identus.shared.models.*
import zio.{mock, IO, UIO, URLayer, ZIO, ZLayer}
import zio.{mock, Duration, IO, UIO, URLayer, ZIO, ZLayer}
import zio.json.*
import zio.mock.{Mock, Proxy}
import zio.Duration

import java.time.Instant
import java.util.UUID
Expand Down

0 comments on commit e077cdd

Please sign in to comment.