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

feat(prism-agent): CredentialSchema DAL, model, service and repositor… #425

Merged
merged 6 commits into from
Mar 9, 2023
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
Expand Up @@ -12,9 +12,9 @@ import io.iohk.atala.agent.walletapi.service.ManagedDIDService
import io.iohk.atala.resolvers.DIDResolver
import io.iohk.atala.agent.server.http.ZioHttpClient
import org.flywaydb.core.extensibility.AppliedMigration
import io.iohk.atala.pollux.service.{JdbcSchemaRegistryService, SchemaRegistryServiceInMemory}
import io.iohk.atala.pollux.core.service.CredentialSchemaServiceImpl
import io.iohk.atala.pollux.sql.repository.JdbcCredentialSchemaRepository
import io.iohk.atala.agent.walletapi.sql.JdbcDIDSecretStorage
import io.iohk.atala.pollux.schema.controller.VerificationPolicyControllerInMemory
import zhttp.http.*
import zhttp.service.Server
import zio.metrics.connectors.prometheus.PrometheusPublisher
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ import io.iohk.atala.pollux.core.service.{
PresentationService,
PresentationServiceImpl,
VerificationPolicyService,
VerificationPolicyServiceImpl
VerificationPolicyServiceImpl,
CredentialSchemaService,
CredentialSchemaServiceImpl
}
import io.iohk.atala.pollux.credentialschema.controller.{CredentialSchemaController, CredentialSchemaControllerImpl}
import io.iohk.atala.iris.proto.service.IrisServiceGrpc
import io.iohk.atala.iris.proto.service.IrisServiceGrpc.IrisServiceStub
import io.iohk.atala.pollux.core.repository.CredentialRepository
import io.iohk.atala.pollux.sql.repository.{
JdbcCredentialRepository,
JdbcPresentationRepository,
JdbcVerificationPolicyRepository,
JdbcCredentialSchemaRepository,
DbConfig as PolluxDbConfig
}
import io.iohk.atala.connect.sql.repository.DbConfig as ConnectDbConfig
Expand All @@ -66,8 +70,7 @@ import io.iohk.atala.prism.protos.node_api.NodeServiceGrpc

import java.io.IOException
import cats.implicits.*
import io.iohk.atala.pollux.schema.SchemaRegistryServerEndpoints
import io.iohk.atala.pollux.service.{JdbcSchemaRegistryService, SchemaRegistryService, SchemaRegistryServiceInMemory}
import io.iohk.atala.pollux.credentialschema.SchemaRegistryServerEndpoints
import io.iohk.atala.pollux.core.repository.PresentationRepository
import io.iohk.atala.pollux.core.model.error.PresentationError
import io.iohk.atala.pollux.core.model.error.CredentialServiceError
Expand All @@ -78,7 +81,7 @@ import io.iohk.atala.connect.sql.repository.JdbcConnectionRepository
import io.iohk.atala.mercury.protocol.connection.ConnectionRequest
import io.iohk.atala.mercury.protocol.connection.ConnectionResponse
import io.iohk.atala.connect.core.model.error.ConnectionServiceError
import io.iohk.atala.pollux.schema.{SchemaRegistryServerEndpoints, VerificationPolicyServerEndpoints}
import io.iohk.atala.pollux.credentialschema.{SchemaRegistryServerEndpoints, VerificationPolicyServerEndpoints}
import io.iohk.atala.connect.core.model.error.ConnectionServiceError
import io.iohk.atala.mercury.protocol.presentproof.*
import io.iohk.atala.agent.server.config.AgentConfig
Expand All @@ -94,7 +97,8 @@ import io.iohk.atala.resolvers.DIDResolver
import io.iohk.atala.pollux.vc.jwt.DidResolver as JwtDidResolver
import io.iohk.atala.pollux.vc.jwt.PrismDidResolver
import io.iohk.atala.mercury.DidAgent
import io.iohk.atala.pollux.schema.controller.{
import io.iohk.atala.pollux.credentialschema.controller.{
CredentialSchemaController,
VerificationPolicyController,
VerificationPolicyControllerImpl,
VerificationPolicyControllerInMemory
Expand All @@ -112,7 +116,7 @@ object Modules {
httpServerApp.unit
}

lazy val zioApp: RIO[SchemaRegistryService & VerificationPolicyController & AppConfig, Unit] = {
lazy val zioApp: RIO[CredentialSchemaController & VerificationPolicyController & AppConfig, Unit] = {
val zioHttpServerApp = for {
allSchemaRegistryEndpoints <- SchemaRegistryServerEndpoints.all
allVerificationPolicyEndpoints <- VerificationPolicyServerEndpoints.all
Expand Down Expand Up @@ -621,8 +625,11 @@ object RepoModule {
val connectionRepoLayer: TaskLayer[ConnectionRepository[Task]] =
RepoModule.connectTransactorLayer >>> JdbcConnectionRepository.layer

val credentialSchemaServiceLayer: TaskLayer[SchemaRegistryService] =
RepoModule.polluxTransactorLayer >>> JdbcSchemaRegistryService.layer
val credentialSchemaServiceLayer: TaskLayer[CredentialSchemaController] =
RepoModule.polluxTransactorLayer >>>
JdbcCredentialSchemaRepository.layer >>>
CredentialSchemaServiceImpl.layer >>>
CredentialSchemaControllerImpl.layer

val verificationPolicyServiceLayer: TaskLayer[VerificationPolicyController] =
RepoModule.polluxTransactorLayer >>>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.iohk.atala.agent.server.http

import zio.{Task, ZIO, ZLayer, URIO}
import io.iohk.atala.pollux.schema.SchemaRegistryServerEndpoints
import io.iohk.atala.pollux.service.SchemaRegistryService
import io.iohk.atala.pollux.credentialschema.SchemaRegistryServerEndpoints
import io.iohk.atala.pollux.credentialschema.controller.CredentialSchemaController
import sttp.tapir.redoc.bundle.RedocInterpreter
import sttp.tapir.swagger.bundle.SwaggerInterpreter
//import sttp.tapir.ztapir.{RichZServerEndpoint, ZServerEndpoint}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package io.iohk.atala.agent.server.http
import cats.implicits.*
import io.iohk.atala.agent.server.http.ZHttpEndpoints
import io.iohk.atala.api.http.BadRequest
import io.iohk.atala.pollux.schema.SchemaRegistryServerEndpoints
import io.iohk.atala.pollux.service.{SchemaRegistryService, SchemaRegistryServiceInMemory}
import io.iohk.atala.pollux.credentialschema.SchemaRegistryServerEndpoints
import io.iohk.atala.pollux.credentialschema.controller.CredentialSchemaController
import org.http4s.*
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.server.Router
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder}
import sttp.tapir.server.model.ValuedEndpointOutput

sealed trait FailureResponse
//TODO: Refactor: create a FailureResponse object and put all errors inside of it

case class NotFound(msg: String) extends FailureResponse

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package io.iohk.atala.pollux.schema
package io.iohk.atala.pollux.credentialschema

import io.iohk.atala.api.http.*
import io.iohk.atala.api.http.EndpointOutputs.*
import io.iohk.atala.api.http.codec.OrderCodec.*
import io.iohk.atala.api.http.model.{Order, PaginationInput}
import io.iohk.atala.api.http.{BadRequest, FailureResponse, InternalServerError, NotFound, RequestContext}
import io.iohk.atala.pollux.schema.model.{
VerifiableCredentialSchema,
VerifiableCredentialSchemaPage,
VerifiableCredentialSchemaInput
import io.iohk.atala.pollux.credentialschema.http.{
CredentialSchemaInput,
CredentialSchemaPageResponse,
CredentialSchemaResponse,
FilterInput
}
import io.iohk.atala.api.http.codec.OrderCodec.*
import sttp.model.StatusCode
import sttp.tapir.EndpointIO.Info
import sttp.tapir.extractFromRequest
import sttp.tapir.json.zio.jsonBody
import sttp.tapir.{
Endpoint,
EndpointInfo,
PublicEndpoint,
endpoint,
extractFromRequest,
oneOf,
oneOfDefaultVariant,
oneOfVariant,
Expand All @@ -25,30 +28,28 @@ import sttp.tapir.{
stringToPath
}
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder}
import sttp.model.StatusCode
import io.iohk.atala.api.http.EndpointOutputs._

import java.util.UUID

object SchemaRegistryEndpoints {

val createSchemaEndpoint: PublicEndpoint[
(RequestContext, VerifiableCredentialSchemaInput),
(RequestContext, CredentialSchemaInput),
FailureResponse,
VerifiableCredentialSchema,
CredentialSchemaResponse,
Any
] =
endpoint.post
.in(extractFromRequest[RequestContext](RequestContext.apply))
.in("schema-registry" / "schemas")
.in(
jsonBody[VerifiableCredentialSchemaInput]
jsonBody[CredentialSchemaInput]
.description(
"Create schema input object with the metadata and attributes"
)
)
.out(statusCode(StatusCode.Created))
.out(jsonBody[VerifiableCredentialSchema])
.out(jsonBody[CredentialSchemaResponse])
.errorOut(basicFailures)
.name("createSchema")
.summary("Publish new schema to the schema registry")
Expand All @@ -60,34 +61,34 @@ object SchemaRegistryEndpoints {
val getSchemaByIdEndpoint: PublicEndpoint[
(RequestContext, UUID),
FailureResponse,
VerifiableCredentialSchema,
CredentialSchemaResponse,
Any
] =
endpoint.get
.in(extractFromRequest[RequestContext](RequestContext.apply))
.in(
"schema-registry" / "schemas" / path[UUID]("id").description(
"Get the schema by id"
"schema-registry" / "schemas" / path[UUID]("guid").description(
"Globally unique identifier of the credential schema object"
)
)
.out(jsonBody[VerifiableCredentialSchema])
.out(jsonBody[CredentialSchemaResponse])
.errorOut(basicFailuresAndNotFound)
.name("getSchemaById")
.summary("Fetch the schema from the registry by id")
.summary("Fetch the schema from the registry by `guid`")
.description(
"Fetch the schema by the unique identifier. Verifiable Credential Schema in json format is returned."
"Fetch the credential schema by the unique identifier"
)
.tag("Schema Registry")

val lookupSchemasByQueryEndpoint: PublicEndpoint[
(
RequestContext,
VerifiableCredentialSchema.Filter,
FilterInput,
PaginationInput,
Option[Order]
),
FailureResponse,
VerifiableCredentialSchemaPage,
CredentialSchemaPageResponse,
Any
] =
endpoint.get
Expand All @@ -101,21 +102,18 @@ object SchemaRegistryEndpoints {
query[Option[String]]("version")
.and(
query[Option[String]]("tags")
.and(
query[Option[String]]("attributes")
)
)
)
)
.mapTo[VerifiableCredentialSchema.Filter]
.mapTo[FilterInput]
)
.in(
query[Option[Int]]("offset")
.and(query[Option[Int]]("limit"))
.mapTo[PaginationInput]
)
.in(query[Option[Order]]("order"))
.out(jsonBody[VerifiableCredentialSchemaPage])
.out(jsonBody[CredentialSchemaPageResponse])
.errorOut(basicFailures)
.name("lookupSchemasByQuery")
.summary("Lookup schemas by indexed fields")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.iohk.atala.pollux.credentialschema

import io.iohk.atala.api.http.model.{CollectionStats, Order, Pagination, PaginationInput}
import io.iohk.atala.api.http.{FailureResponse, InternalServerError, NotFound, RequestContext}
import io.iohk.atala.pollux.credentialschema.SchemaRegistryEndpoints.{
createSchemaEndpoint,
getSchemaByIdEndpoint,
lookupSchemasByQueryEndpoint,
testEndpoint
}
import io.iohk.atala.pollux.credentialschema.controller.{CredentialSchemaController, CredentialSchemaControllerLogic}
import io.iohk.atala.pollux.credentialschema.http.{
CredentialSchemaInput,
CredentialSchemaPageResponse,
CredentialSchemaResponse,
FilterInput
}
import sttp.tapir.redoc.RedocUIOptions
import sttp.tapir.redoc.bundle.RedocInterpreter
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.swagger.bundle.SwaggerInterpreter
import sttp.tapir.ztapir.*
import zio.{Task, URIO, ZIO, ZLayer}

import java.util.UUID

class SchemaRegistryServerEndpoints(
credentialSchemaController: CredentialSchemaController
) {
def throwableToInternalServerError(throwable: Throwable) =
ZIO.fail[FailureResponse](InternalServerError(throwable.getMessage))

val createSchemaServerEndpoint: ZServerEndpoint[Any, Any] =
createSchemaEndpoint.zServerLogic { case (ctx: RequestContext, schemaInput: CredentialSchemaInput) =>
credentialSchemaController.createSchema(schemaInput)(ctx)
}

val getSchemaByIdServerEndpoint: ZServerEndpoint[Any, Any] =
getSchemaByIdEndpoint.zServerLogic { case (ctx: RequestContext, guid: UUID) =>
credentialSchemaController.getSchemaByGuid(guid)(ctx)
}

val lookupSchemasByQueryServerEndpoint: ZServerEndpoint[Any, Any] =
lookupSchemasByQueryEndpoint.zServerLogic {
case (
ctx: RequestContext,
filter: FilterInput,
paginationInput: PaginationInput,
order: Option[Order]
) =>
credentialSchemaController.lookupSchemas(
filter,
paginationInput.toPagination,
order
)(ctx)
}

val testServerEndpoint: ZServerEndpoint[Any, Any] =
testEndpoint.zServerLogic(requestContext => ZIO.succeed(requestContext.request.toString))

val all: List[ZServerEndpoint[Any, Any]] =
List(
createSchemaServerEndpoint,
getSchemaByIdServerEndpoint,
lookupSchemasByQueryServerEndpoint,
testServerEndpoint
)
}

object SchemaRegistryServerEndpoints {
def all: URIO[CredentialSchemaController, List[ZServerEndpoint[Any, Any]]] = {
for {
schemaRegistryService <- ZIO.service[CredentialSchemaController]
schemaRegistryEndpoints = new SchemaRegistryServerEndpoints(
schemaRegistryService
)
} yield schemaRegistryEndpoints.all
}
}
Loading