Skip to content

Commit

Permalink
Merge branch 'master' into resolve-proxy-pass
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergrabinski authored Oct 13, 2023
2 parents 82138c2 + 9b96b5e commit d575bef
Show file tree
Hide file tree
Showing 98 changed files with 964 additions and 720 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import akka.actor.typed.scaladsl.LoggerOps
import akka.actor.typed.scaladsl.adapter._
import akka.actor.{ActorSystem => ActorSystemClassic}
import akka.http.scaladsl.Http
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._
import akka.http.scaladsl.server.{ExceptionHandler, RejectionHandler, Route, RouteResult}
import cats.effect.{ExitCode, Resource}
import ch.epfl.bluebrain.nexus.delta.config.{AppConfig, BuildInfo}
Expand Down Expand Up @@ -56,7 +57,7 @@ object Main extends BIOApp {
(cfg, config, cl, pluginDefs) <- Resource.eval(loadPluginsAndConfig(loaderConfig))
_ <- Resource.eval(KamonMonitoring.initialize(config))
modules = DeltaModule(cfg, config, cl)
(plugins, locator) <- WiringInitializer(modules, pluginDefs)
(plugins, locator) <- WiringInitializer(modules, pluginDefs).mapK(ioToTaskK)
_ <- bootstrap(locator, plugins)
} yield locator

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package ch.epfl.bluebrain.nexus.delta.plugin

import cats.effect.Resource
import cats.effect.{IO, Resource}
import cats.syntax.traverse._
import cats.syntax.flatMap._
import cats.syntax.monadError._
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._
import ch.epfl.bluebrain.nexus.delta.kernel.Logger
import ch.epfl.bluebrain.nexus.delta.sdk.error.PluginError.PluginInitializationError
import ch.epfl.bluebrain.nexus.delta.sdk.plugin.{Plugin, PluginDef}
import com.typesafe.scalalogging.Logger
import distage.{Injector, Roots}
import izumi.distage.model.Locator
import izumi.distage.model.definition.ModuleDef
import izumi.distage.modules.DefaultModule
import monix.bio.{IO, Task}

object WiringInitializer {

private val logger = Logger[WiringInitializer.type]
private val logger = Logger.cats[WiringInitializer.type]

/**
* Combines the [[ModuleDef]] of the passed ''serviceModule'' with the ones provided by the plugins. Afterwards
Expand All @@ -21,23 +24,25 @@ object WiringInitializer {
def apply(
serviceModule: ModuleDef,
pluginsDef: List[PluginDef]
): Resource[Task, (List[Plugin], Locator)] = {
): Resource[IO, (List[Plugin], Locator)] = {
val pluginsInfoModule = new ModuleDef { make[List[PluginDef]].from(pluginsDef) }
val appModules = (serviceModule :: pluginsInfoModule :: pluginsDef.map(_.module)).merge

// workaround for: java.lang.NoClassDefFoundError: zio/blocking/package$Blocking$Service
implicit val defaultModule: DefaultModule[Task] = DefaultModule.empty
Injector[Task]()
implicit val defaultModule: DefaultModule[IO] = DefaultModule.empty
Injector[IO]()
.produce(appModules, Roots.Everything)
.toCats
.evalMap { locator =>
IO.traverse(pluginsDef) { plugin =>
Task.delay(logger.info(s"Initializing plugin ${plugin.info.name}...")) >>
plugin.initialize(locator).tapEval { _ =>
Task.delay(logger.info(s"Plugin ${plugin.info.name} initialized."))
}
}.map(_ -> locator)
.mapError(e => PluginInitializationError(e.getMessage))
pluginsDef
.traverse { plugin =>
logger.info(s"Initializing plugin ${plugin.info.name}...") >>
toCatsIO(plugin.initialize(locator)).flatTap { _ =>
logger.info(s"Plugin ${plugin.info.name} initialized.")
}
}
.map(_ -> locator)
.adaptError(e => PluginInitializationError(e.getMessage))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package ch.epfl.bluebrain.nexus.delta.routes
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{Directive1, Route}
import cats.effect.IO
import cats.implicits._
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.RemoteContextResolution
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder
import ch.epfl.bluebrain.nexus.delta.rdf.utils.JsonKeyOrdering
import ch.epfl.bluebrain.nexus.delta.routes.OrganizationsRoutes.OrganizationInput
import ch.epfl.bluebrain.nexus.delta.sdk.OrganizationResource
import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck
import ch.epfl.bluebrain.nexus.delta.sdk.ce.DeltaDirectives.{emit => emitCE}
import ch.epfl.bluebrain.nexus.delta.sdk.ce.DeltaDirectives._
import ch.epfl.bluebrain.nexus.delta.sdk.circe.CirceUnmarshalling
import ch.epfl.bluebrain.nexus.delta.sdk.directives.DeltaDirectives._
import ch.epfl.bluebrain.nexus.delta.sdk.directives.{AuthDirectives, DeltaSchemeDirectives}
import ch.epfl.bluebrain.nexus.delta.sdk.identities.Identities
import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.Caller
import ch.epfl.bluebrain.nexus.delta.sdk.implicits._
import ch.epfl.bluebrain.nexus.delta.sdk.model.BaseUri
import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, ResourceF}
import ch.epfl.bluebrain.nexus.delta.sdk.model.search.SearchParams.OrganizationSearchParams
import ch.epfl.bluebrain.nexus.delta.sdk.model.search.SearchResults._
import ch.epfl.bluebrain.nexus.delta.sdk.model.search.{PaginationConfig, SearchResults}
Expand All @@ -29,7 +29,6 @@ import io.circe.Decoder
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto.deriveConfiguredDecoder
import kamon.instrumentation.akka.http.TracingDirectives.operationName
import monix.execution.Scheduler

import scala.annotation.nowarn

Expand All @@ -54,7 +53,6 @@ final class OrganizationsRoutes(
)(implicit
baseUri: BaseUri,
paginationConfig: PaginationConfig,
s: Scheduler,
cr: RemoteContextResolution,
ordering: JsonKeyOrdering
) extends AuthDirectives(identities, aclCheck)
Expand All @@ -76,6 +74,14 @@ final class OrganizationsRoutes(
)
}

private def emitMetadata(value: IO[OrganizationResource]) = {
emit(
value
.mapValue(_.metadata)
.attemptNarrow[OrganizationRejection]
)
}

def routes: Route =
baseUriPrefix(baseUri.prefix) {
pathPrefix("orgs") {
Expand All @@ -88,7 +94,12 @@ final class OrganizationsRoutes(
implicit val searchJsonLdEncoder: JsonLdEncoder[SearchResults[OrganizationResource]] =
searchResultsJsonLdEncoder(Organization.context, pagination, uri)

emit(organizations.list(pagination, params, order).widen[SearchResults[OrganizationResource]])
emit(
organizations
.list(pagination, params, order)
.widen[SearchResults[OrganizationResource]]
.attemptNarrow[OrganizationRejection]
)
}
},
(resolveOrg & pathEndOrSingleSlash) { id =>
Expand All @@ -99,7 +110,10 @@ final class OrganizationsRoutes(
authorizeFor(id, orgs.write).apply {
// Update organization
entity(as[OrganizationInput]) { case OrganizationInput(description) =>
emit(organizations.update(id, description, rev).mapValue(_.metadata))
emitMetadata(
organizations
.update(id, description, rev)
)
}
}
}
Expand All @@ -108,9 +122,9 @@ final class OrganizationsRoutes(
authorizeFor(id, orgs.read).apply {
parameter("rev".as[Int].?) {
case Some(rev) => // Fetch organization at specific revision
emit(organizations.fetchAt(id, rev).leftWiden[OrganizationRejection])
emit(organizations.fetchAt(id, rev).attemptNarrow[OrganizationRejection])
case None => // Fetch organization
emit(organizations.fetch(id).leftWiden[OrganizationRejection])
emit(organizations.fetch(id).attemptNarrow[OrganizationRejection])

}
}
Expand All @@ -120,12 +134,14 @@ final class OrganizationsRoutes(
concat(
parameter("rev".as[Int]) { rev =>
authorizeFor(id, orgs.write).apply {
emit(organizations.deprecate(id, rev).mapValue(_.metadata))
emitMetadata(
organizations.deprecate(id, rev)
)
}
},
parameter("prune".requiredValue(true)) { _ =>
authorizeFor(id, orgs.delete).apply {
emitCE(orgDeleter.delete(id).attemptNarrow[OrganizationRejection])
emit(orgDeleter.delete(id).attemptNarrow[OrganizationRejection])
}
}
)
Expand All @@ -138,7 +154,12 @@ final class OrganizationsRoutes(
(put & authorizeFor(label, orgs.create)) {
// Create organization
entity(as[OrganizationInput]) { case OrganizationInput(description) =>
emit(StatusCodes.Created, organizations.create(label, description).mapValue(_.metadata))
val response: IO[Either[OrganizationRejection, ResourceF[Organization.Metadata]]] =
organizations.create(label, description).mapValue(_.metadata).attemptNarrow[OrganizationRejection]
emit(
StatusCodes.Created,
response
)
}
}
}
Expand Down Expand Up @@ -171,7 +192,6 @@ object OrganizationsRoutes {
)(implicit
baseUri: BaseUri,
paginationConfig: PaginationConfig,
s: Scheduler,
cr: RemoteContextResolution,
ordering: JsonKeyOrdering
): Route =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.epfl.bluebrain.nexus.delta.routes

import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{MalformedRequestContentRejection, Route}
import cats.effect.IO
import cats.implicits._
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.contexts
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.JsonLdContext.keywords
Expand All @@ -10,23 +11,23 @@ import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder
import ch.epfl.bluebrain.nexus.delta.rdf.utils.JsonKeyOrdering
import ch.epfl.bluebrain.nexus.delta.routes.PermissionsRoutes.PatchPermissions._
import ch.epfl.bluebrain.nexus.delta.routes.PermissionsRoutes._
import ch.epfl.bluebrain.nexus.delta.sdk.PermissionsResource
import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck
import ch.epfl.bluebrain.nexus.delta.sdk.acls.model.AclAddress
import ch.epfl.bluebrain.nexus.delta.sdk.ce.DeltaDirectives._
import ch.epfl.bluebrain.nexus.delta.sdk.circe.CirceUnmarshalling
import ch.epfl.bluebrain.nexus.delta.sdk.directives.AuthDirectives
import ch.epfl.bluebrain.nexus.delta.sdk.directives.DeltaDirectives._
import ch.epfl.bluebrain.nexus.delta.sdk.identities.Identities
import ch.epfl.bluebrain.nexus.delta.sdk.implicits._
import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, ResourceF}
import ch.epfl.bluebrain.nexus.delta.sdk.permissions.Permissions
import ch.epfl.bluebrain.nexus.delta.sdk.permissions.Permissions.{permissions => permissionsPerms}
import ch.epfl.bluebrain.nexus.delta.sdk.permissions.model.Permission
import ch.epfl.bluebrain.nexus.delta.sdk.permissions.model.{Permission, PermissionsRejection}
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto.deriveConfiguredDecoder
import io.circe.syntax._
import io.circe.{Decoder, Json}
import kamon.instrumentation.akka.http.TracingDirectives.operationName
import monix.execution.Scheduler

import scala.annotation.nowarn

Expand All @@ -42,7 +43,6 @@ import scala.annotation.nowarn
*/
final class PermissionsRoutes(identities: Identities, permissions: Permissions, aclCheck: AclCheck)(implicit
baseUri: BaseUri,
s: Scheduler,
cr: RemoteContextResolution,
ordering: JsonKeyOrdering
) extends AuthDirectives(identities, aclCheck)
Expand All @@ -65,16 +65,16 @@ final class PermissionsRoutes(identities: Identities, permissions: Permissions,
get {
authorizeFor(AclAddress.Root, permissionsPerms.read).apply {
parameter("rev".as[Int].?) {
case Some(rev) => emit(permissions.fetchAt(rev))
case None => emit(permissions.fetch)
case Some(rev) => emitPermissions(permissions.fetchAt(rev))
case None => emitPermissions(permissions.fetch)
}
}
},
// Replace permissions
(put & parameter("rev" ? 0)) { rev =>
authorizeFor(AclAddress.Root, permissionsPerms.write).apply {
entity(as[PatchPermissions]) {
case Replace(set) => emit(permissions.replace(set, rev).map(_.void))
case Replace(set) => emitVoid(permissions.replace(set, rev))
case _ =>
reject(
malformedContent(s"Value for field '${keywords.tpe}' must be 'Replace' when using 'PUT'.")
Expand All @@ -86,8 +86,8 @@ final class PermissionsRoutes(identities: Identities, permissions: Permissions,
(patch & parameter("rev" ? 0)) { rev =>
authorizeFor(AclAddress.Root, permissionsPerms.write).apply {
entity(as[PatchPermissions]) {
case Append(set) => emit(permissions.append(set, rev).map(_.void))
case Subtract(set) => emit(permissions.subtract(set, rev).map(_.void))
case Append(set) => emitVoid(permissions.append(set, rev))
case Subtract(set) => emitVoid(permissions.subtract(set, rev))
case _ =>
reject(
malformedContent(
Expand All @@ -101,7 +101,7 @@ final class PermissionsRoutes(identities: Identities, permissions: Permissions,
delete {
authorizeFor(AclAddress.Root, permissionsPerms.write).apply {
parameter("rev".as[Int]) { rev =>
emit(permissions.delete(rev).map(_.void))
emitVoid(permissions.delete(rev))
}
}
}
Expand All @@ -113,6 +113,14 @@ final class PermissionsRoutes(identities: Identities, permissions: Permissions,
}
}

private def emitVoid(value: IO[PermissionsResource]) = {
emit(value.map(_.void).attemptNarrow[PermissionsRejection])
}

private def emitPermissions(value: IO[PermissionsResource]) = {
emit(value.attemptNarrow[PermissionsRejection])
}

private def malformedContent(field: String) =
MalformedRequestContentRejection(field, new IllegalArgumentException())
}
Expand All @@ -125,7 +133,6 @@ object PermissionsRoutes {
*/
def apply(identities: Identities, permissions: Permissions, aclCheck: AclCheck)(implicit
baseUri: BaseUri,
s: Scheduler,
cr: RemoteContextResolution,
ordering: JsonKeyOrdering
): Route =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._
import cats.effect.IO
import cats.implicits._
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.{contexts, schemas}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.{ContextValue, RemoteContextResolution}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder
Expand Down
Loading

0 comments on commit d575bef

Please sign in to comment.