Skip to content

Commit

Permalink
Merge branch 'master' into incremental-import
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergrabinski authored Apr 2, 2024
2 parents fef44de + 32f30d6 commit 96ad81a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import akka.actor.ActorSystem
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.Uri.Query
import akka.testkit.TestKit
import cats.effect.IO
import cats.implicits._
import ch.epfl.bluebrain.nexus.delta.kernel.search.Pagination.FromPagination
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.ScalaTestElasticSearchClientSetup
Expand All @@ -23,7 +24,7 @@ import ch.epfl.bluebrain.nexus.testkit.CirceLiteral
import ch.epfl.bluebrain.nexus.testkit.elasticsearch.ElasticSearchDocker
import ch.epfl.bluebrain.nexus.testkit.scalatest.ce.CatsEffectSpec
import io.circe.{Json, JsonObject}
import org.scalatest.DoNotDiscover
import org.scalatest.{Assertion, DoNotDiscover}
import org.scalatest.concurrent.Eventually

import scala.concurrent.duration._
Expand Down Expand Up @@ -260,23 +261,25 @@ class ElasticSearchClientSpec(override val docker: ElasticSearchDocker)
ElasticSearchAction.Update(index, "1", json"""{ "doc" : {"field2" : "value2"} }""")
)

def theCountShouldBe(count: Long): IO[Assertion] =
esClient.count(index.value).map(_ shouldEqual count)

{
for {
// Indexing and checking count
_ <- esClient.bulk(operations)
_ <- esClient.refresh(index)
original <- esClient.count(index.value)
_ = original shouldEqual 2L
_ <- esClient.bulk(operations)
_ <- esClient.refresh(index)
_ <- eventually { theCountShouldBe(2L) }
// Deleting document matching the given query
query = jobj"""{"query": {"bool": {"must": {"term": {"field1": 3} } } } }"""
_ <- esClient.deleteByQuery(query, index)
query = jobj"""{"query": {"bool": {"must": {"term": {"field1": 3} } } } }"""
_ <- esClient.deleteByQuery(query, index)
// Checking docs again
newCount <- esClient.count(index.value)
_ = newCount shouldEqual 1L
doc1 <- esClient.getSource[Json](index, "1").attemptNarrow[HttpClientError]
_ = doc1.rightValue
doc2 <- esClient.getSource[Json](index, "2").attemptNarrow[HttpClientError]
_ = doc2.leftValue.errorCode.value shouldEqual StatusCodes.NotFound
_ <- esClient.refresh(index)
_ <- eventually { theCountShouldBe(1L) }
doc1 <- esClient.getSource[Json](index, "1").attemptNarrow[HttpClientError]
_ = doc1.rightValue
doc2 <- esClient.getSource[Json](index, "2").attemptNarrow[HttpClientError]
_ = doc2.leftValue.errorCode.value shouldEqual StatusCodes.NotFound
} yield ()
}.accepted
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ object ResolversImpl {
uuidF: UUIDF
): Resolvers = {
def priorityAlreadyExists(ref: ProjectRef, self: Iri, priority: Priority): IO[Unit] = {
sql"SELECT id FROM scoped_states WHERE type = ${Resolvers.entityType} AND org = ${ref.organization} AND project = ${ref.project} AND id != $self AND (value->'value'->'priority')::int = ${priority.value} "
sql"""SELECT id FROM scoped_states
WHERE type = ${Resolvers.entityType}
AND org = ${ref.organization} AND project = ${ref.project}
AND id != $self
AND (value->'deprecated')::boolean = false
AND (value->'value'->'priority')::int = ${priority.value}"""
.query[Iri]
.option
.transact(xas.read)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import ch.epfl.bluebrain.nexus.delta.kernel.utils.UUIDF
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.{contexts, nxv, schema}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.api.{JsonLdApi, JsonLdJavaApi}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.RemoteContextResolution
import ch.epfl.bluebrain.nexus.delta.sdk.ConfigFixtures
import ch.epfl.bluebrain.nexus.delta.sdk.generators.ProjectGen
import ch.epfl.bluebrain.nexus.delta.sdk.generators.ResolverGen.{resolverResourceFor, sourceFrom, sourceWithoutId}
import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.Caller
Expand All @@ -24,6 +23,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.model.ResolverRejection.{Inco
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.model.ResolverValue.{CrossProjectValue, InProjectValue}
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.model._
import ch.epfl.bluebrain.nexus.delta.sdk.resources.Resources
import ch.epfl.bluebrain.nexus.delta.sdk.{ConfigFixtures, ResolverResource}
import ch.epfl.bluebrain.nexus.delta.sourcing.EntityDependencyStore
import ch.epfl.bluebrain.nexus.delta.sourcing.model.EntityDependency.DependsOn
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.{Authenticated, Group, User}
Expand Down Expand Up @@ -620,6 +620,22 @@ class ResolversImplSpec extends CatsEffectSpec with DoobieScalaTestFixture with
}

}

"validating priority" should {
"allow creating a resolver with the same priority as a deprecated resolver" in {
givenADeprecatedInProjectResolver(666) { resolver =>
resolvers.create(nxv + genString(), resolver.value.project, InProjectValue(resolver.value.priority))
}
}
}

def givenADeprecatedInProjectResolver(priority: Int)(test: ResolverResource => IO[ResolverResource]) = {
val id = nxv + genString()
val resolverValue = InProjectValue(Priority.unsafe(priority))
resolvers.create(id, projectRef, resolverValue).accepted
val deprecatedResolver = resolvers.deprecate(id, projectRef, 1).accepted
test(deprecatedResolver).accepted
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ package ch.epfl.bluebrain.nexus.ship

import cats.effect.IO
import ch.epfl.bluebrain.nexus.delta.kernel.utils.ClasspathResourceLoader
import ch.epfl.bluebrain.nexus.delta.plugins.blazegraph.model.{contexts => bgContexts}
import ch.epfl.bluebrain.nexus.delta.plugins.compositeviews.model.{contexts => compositeViewContexts}
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.model.{contexts => esContexts}
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.contexts
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.api.JsonLdApi
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.{ContextValue, RemoteContextResolution}
import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.ResolverContextResolution
import ch.epfl.bluebrain.nexus.delta.sdk.resources.FetchResource
import ch.epfl.bluebrain.nexus.delta.sourcing.Transactors
import ch.epfl.bluebrain.nexus.delta.sourcing.config.EventLogConfig
import ch.epfl.bluebrain.nexus.ship.acls.AclWiring
import ch.epfl.bluebrain.nexus.ship.acls.AclWiring.alwaysAuthorize
import ch.epfl.bluebrain.nexus.ship.resolvers.ResolverWiring

import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.model.{contexts => esContexts}
import ch.epfl.bluebrain.nexus.delta.plugins.blazegraph.model.{contexts => bgContexts}
import ch.epfl.bluebrain.nexus.delta.plugins.compositeviews.model.{contexts => compositeViewContexts}

object ContextWiring {

implicit private val loader: ClasspathResourceLoader = ClasspathResourceLoader.withContext(getClass)
Expand Down Expand Up @@ -53,12 +51,11 @@ object ContextWiring {
clock: EventClock,
xas: Transactors
)(implicit jsonLdApi: JsonLdApi): IO[ResolverContextResolution] = {
val aclCheck = AclCheck(AclWiring.acls(config, clock, xas))
val resolvers = ResolverWiring.resolvers(fetchContext, config, clock, xas)

for {
rcr <- remoteContextResolution
} yield ResolverContextResolution(aclCheck, resolvers, rcr, fetchResource)
} yield ResolverContextResolution(alwaysAuthorize, resolvers, rcr, fetchResource)
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
package ch.epfl.bluebrain.nexus.ship.acls

import cats.effect.{Clock, IO}
import ch.epfl.bluebrain.nexus.delta.sdk.acls.{Acls, AclsImpl}
import cats.effect.IO
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.identities.model.Caller
import ch.epfl.bluebrain.nexus.delta.sdk.permissions.model.Permission
import ch.epfl.bluebrain.nexus.delta.sourcing.Transactors
import ch.epfl.bluebrain.nexus.delta.sourcing.config.EventLogConfig
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity

import scala.collection.immutable

object AclWiring {

def acls(config: EventLogConfig, clock: Clock[IO], xas: Transactors): Acls = {
val permissionSet = Set(Permission.unsafe("resources/read"))
AclsImpl(
IO.pure(permissionSet),
AclsImpl.findUnknownRealms(xas),
permissionSet,
config,
xas,
clock
)
def alwaysAuthorize: AclCheck = new AclCheck {
override def authorizeForOr[E <: Throwable](path: AclAddress, permission: Permission, identities: Set[Identity])(
onError: => E
): IO[Unit] = IO.unit

override def authorizeFor(path: AclAddress, permission: Permission, identities: Set[Identity]): IO[Boolean] =
IO.pure(true)

override def authorizeForEveryOr[E <: Throwable](path: AclAddress, permissions: Set[Permission])(onError: => E)(
implicit caller: Caller
): IO[Unit] = IO.unit

override def mapFilterOrRaise[A, B](
values: immutable.Iterable[A],
extractAddressPermission: A => (AclAddress, Permission),
onAuthorized: A => B,
onFailure: AclAddress => IO[Unit]
)(implicit caller: Caller): IO[Set[B]] =
IO.pure(values.map(onAuthorized).toSet)

override def mapFilterAtAddressOrRaise[A, B](
values: immutable.Iterable[A],
address: AclAddress,
extractPermission: A => Permission,
onAuthorized: A => B,
onFailure: AclAddress => IO[Unit]
)(implicit caller: Caller): IO[Set[B]] =
IO.pure(values.map(onAuthorized).toSet)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ch.epfl.bluebrain.nexus.ship.resources

import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.api.JsonLdApi
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.RemoteContextResolution
import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.ResourceResolution
import ch.epfl.bluebrain.nexus.delta.sdk.resources.Resources.ResourceLog
Expand All @@ -11,7 +10,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.schemas.FetchSchema
import ch.epfl.bluebrain.nexus.delta.sourcing.config.EventLogConfig
import ch.epfl.bluebrain.nexus.delta.sourcing.{ScopedEventLog, Transactors}
import ch.epfl.bluebrain.nexus.ship.EventClock
import ch.epfl.bluebrain.nexus.ship.acls.AclWiring
import ch.epfl.bluebrain.nexus.ship.acls.AclWiring.alwaysAuthorize
import ch.epfl.bluebrain.nexus.ship.resolvers.ResolverWiring

object ResourceWiring {
Expand All @@ -27,10 +26,9 @@ object ResourceWiring {
): (ResourceLog, FetchResource) = {
val rcr = RemoteContextResolution.never // TODO: Use correct RemoteContextResolution
val detectChange = DetectChange(false)
val aclCheck = AclCheck(AclWiring.acls(config, clock, xas))
val resolvers = ResolverWiring.resolvers(fetchContext, config, clock, xas)
val resourceResolution =
ResourceResolution.schemaResource(aclCheck, resolvers, fetchSchema, excludeDeprecated = false)
ResourceResolution.schemaResource(alwaysAuthorize, resolvers, fetchSchema, excludeDeprecated = false)
val validate = ValidateResource(resourceResolution)(rcr)
val resourceDef = Resources.definition(validate, detectChange, clock)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package ch.epfl.bluebrain.nexus.ship.schemas
import cats.effect.IO
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.api.JsonLdApi
import ch.epfl.bluebrain.nexus.delta.rdf.shacl.ShaclShapesGraph
import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext
import ch.epfl.bluebrain.nexus.delta.sdk.resources.FetchResource
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas.SchemaLog
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.{FetchSchema, SchemaImports, Schemas, ValidateSchema}
import ch.epfl.bluebrain.nexus.delta.sourcing.config.EventLogConfig
import ch.epfl.bluebrain.nexus.delta.sourcing.{ScopedEventLog, Transactors}
import ch.epfl.bluebrain.nexus.ship.acls.AclWiring
import ch.epfl.bluebrain.nexus.ship.acls.AclWiring.alwaysAuthorize
import ch.epfl.bluebrain.nexus.ship.resolvers.ResolverWiring
import ch.epfl.bluebrain.nexus.ship.{ContextWiring, EventClock}

Expand All @@ -31,9 +30,8 @@ object SchemaWiring {
)(implicit
jsonLdApi: JsonLdApi
): SchemaImports = {
val aclCheck = AclCheck(AclWiring.acls(config, clock, xas))
val resolvers = ResolverWiring.resolvers(fetchContext, config, clock, xas)
SchemaImports(aclCheck, resolvers, fetchSchema, fetchResource)
SchemaImports(alwaysAuthorize, resolvers, fetchSchema, fetchResource)
}

private def validateSchema(implicit api: JsonLdApi): IO[ValidateSchema] =
Expand Down

0 comments on commit 96ad81a

Please sign in to comment.