Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migrate to quill for generic secret storage #1299

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading