Skip to content

Commit

Permalink
feat(common): relax tenant names (#1081)
Browse files Browse the repository at this point in the history
* fix(tenant): issuer URIs were resolved even if authentication was not enabled
* feat(common): relax tenant names (from URI to String), following a change introduced in NGSI-LD v1.7.1
  • Loading branch information
bobeal authored Jan 18, 2024
1 parent 6fee953 commit 3561676
Show file tree
Hide file tree
Showing 63 changed files with 206 additions and 237 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ SUBSCRIPTION_STELLIO_URL=http://localhost:8080
# Sample default configuration for the default tenant
# Please note that issuer parameter is only used when authentication is enabled
APPLICATION_TENANTS_0_ISSUER=https://sso.eglobalmark.com/auth/realms/stellio
APPLICATION_TENANTS_0_URI=urn:ngsi-ld:tenant:default
APPLICATION_TENANTS_0_NAME=urn:ngsi-ld:tenant:default
APPLICATION_TENANTS_0_DBSCHEMA=public
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ services:
- SPRING_R2DBC_PASSWORD=${POSTGRES_PASS}
- APPLICATION_AUTHENTICATION_ENABLED=${STELLIO_AUTHENTICATION_ENABLED}
- APPLICATION_TENANTS_0_ISSUER=${APPLICATION_TENANTS_0_ISSUER}
- APPLICATION_TENANTS_0_URI=${APPLICATION_TENANTS_0_URI}
- APPLICATION_TENANTS_0_NAME=${APPLICATION_TENANTS_0_NAME}
- APPLICATION_TENANTS_0_DBSCHEMA=${APPLICATION_TENANTS_0_DBSCHEMA}
ports:
- "8083:8083"
Expand All @@ -78,7 +78,7 @@ services:
- SPRING_R2DBC_PASSWORD=${POSTGRES_PASS}
- APPLICATION_AUTHENTICATION_ENABLED=${STELLIO_AUTHENTICATION_ENABLED}
- APPLICATION_TENANTS_0_ISSUER=${APPLICATION_TENANTS_0_ISSUER}
- APPLICATION_TENANTS_0_URI=${APPLICATION_TENANTS_0_URI}
- APPLICATION_TENANTS_0_NAME=${APPLICATION_TENANTS_0_NAME}
- APPLICATION_TENANTS_0_DBSCHEMA=${APPLICATION_TENANTS_0_DBSCHEMA}
- SUBSCRIPTION_ENTITY-SERVICE-URL=${SUBSCRIPTION_ENTITY_SERVICE_URL}
- SUBSCRIPTION_STELLIO_URL=${SUBSCRIPTION_STELLIO_URL}
Expand Down
2 changes: 1 addition & 1 deletion search-service/config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ID>LongParameterList:AttributeInstance.kt$AttributeInstance.Companion$( temporalEntityAttribute: UUID, instanceId: URI = generateRandomInstanceId(), timeProperty: TemporalProperty? = TemporalProperty.OBSERVED_AT, modifiedAt: ZonedDateTime? = null, attributeMetadata: AttributeMetadata, payload: ExpandedAttributeInstance, time: ZonedDateTime, sub: String? = null )</ID>
<ID>LongParameterList:EntityEventService.kt$EntityEventService$( sub: String?, entityId: URI, attributeName: ExpandedTerm, datasetId: URI? = null, deleteAll: Boolean, contexts: List&lt;String&gt; )</ID>
<ID>LongParameterList:EntityEventService.kt$EntityEventService$( sub: String?, entityId: URI, jsonLdAttributes: Map&lt;String, Any&gt;, updateResult: UpdateResult, overwrite: Boolean, contexts: List&lt;String&gt; )</ID>
<ID>LongParameterList:EntityEventService.kt$EntityEventService$( updatedDetails: UpdatedDetails, sub: String?, tenantUri: URI, entityId: URI, entityTypesAndPayload: Pair&lt;List&lt;ExpandedTerm&gt;, String&gt;, serializedAttribute: Pair&lt;ExpandedTerm, String&gt;, overwrite: Boolean, contexts: List&lt;String&gt; )</ID>
<ID>LongParameterList:EntityEventService.kt$EntityEventService$( updatedDetails: UpdatedDetails, sub: String?, tenantName: String, entityId: URI, entityTypesAndPayload: Pair&lt;List&lt;ExpandedTerm&gt;, String&gt;, serializedAttribute: Pair&lt;ExpandedTerm, String&gt;, overwrite: Boolean, contexts: List&lt;String&gt; )</ID>
<ID>LongParameterList:TemporalEntityAttributeService.kt$TemporalEntityAttributeService$( entityId: URI, attributeName: ExpandedTerm, attributeMetadata: AttributeMetadata, createdAt: ZonedDateTime, attributePayload: ExpandedAttributeInstance, sub: Sub? )</ID>
<ID>LongParameterList:TemporalEntityAttributeService.kt$TemporalEntityAttributeService$( entityUri: URI, ngsiLdAttributes: List&lt;NgsiLdAttribute&gt;, expandedAttributes: ExpandedAttributes, createdAt: ZonedDateTime, observedAt: ZonedDateTime?, sub: Sub? )</ID>
<ID>LongParameterList:TemporalEntityAttributeService.kt$TemporalEntityAttributeService$( entityUri: URI, ngsiLdAttributes: List&lt;NgsiLdAttribute&gt;, expandedAttributes: ExpandedAttributes, disallowOverwrite: Boolean, createdAt: ZonedDateTime, sub: Sub? )</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import org.slf4j.LoggerFactory
import org.springframework.kafka.annotation.KafkaListener
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
import java.net.URI

@Component
class IAMListener(
Expand All @@ -51,13 +50,13 @@ class IAMListener(
internal suspend fun dispatchIamMessage(content: String) {
val authorizationEvent = JsonUtils.deserializeAs<EntityEvent>(content)
kotlin.runCatching {
val tenantUri = authorizationEvent.tenantUri
val tenantName = authorizationEvent.tenantName
when (authorizationEvent) {
is EntityCreateEvent -> createSubjectReferential(tenantUri, authorizationEvent)
is EntityDeleteEvent -> deleteSubjectReferential(tenantUri, authorizationEvent)
is AttributeAppendEvent -> updateSubjectProfile(tenantUri, authorizationEvent)
is AttributeReplaceEvent -> updateSubjectInfo(tenantUri, authorizationEvent)
is AttributeDeleteEvent -> removeSubjectFromGroup(tenantUri, authorizationEvent)
is EntityCreateEvent -> createSubjectReferential(tenantName, authorizationEvent)
is EntityDeleteEvent -> deleteSubjectReferential(tenantName, authorizationEvent)
is AttributeAppendEvent -> updateSubjectProfile(tenantName, authorizationEvent)
is AttributeReplaceEvent -> updateSubjectInfo(tenantName, authorizationEvent)
is AttributeDeleteEvent -> removeSubjectFromGroup(tenantName, authorizationEvent)
else ->
OperationNotSupportedException(unhandledOperationType(authorizationEvent.operationType)).left()
}
Expand All @@ -67,7 +66,7 @@ class IAMListener(
}

private suspend fun createSubjectReferential(
tenantUri: URI,
tenantName: String,
entityCreateEvent: EntityCreateEvent
): Either<APIException, Unit> = either {
val operationPayload = entityCreateEvent.operationPayload.deserializeAsMap()
Expand All @@ -84,7 +83,7 @@ class IAMListener(

mono {
subjectReferentialService.create(subjectReferential)
}.writeContextAndSubscribe(tenantUri, entityCreateEvent)
}.writeContextAndSubscribe(tenantName, entityCreateEvent)
}

private fun extractRoles(operationPayload: Map<String, Any>): List<GlobalRole>? =
Expand All @@ -97,16 +96,16 @@ class IAMListener(
} else null

private suspend fun deleteSubjectReferential(
tenantUri: URI,
tenantName: String,
entityDeleteEvent: EntityDeleteEvent
): Either<APIException, Unit> = either {
mono {
subjectReferentialService.delete(entityDeleteEvent.entityId.extractSub())
}.writeContextAndSubscribe(tenantUri, entityDeleteEvent)
}.writeContextAndSubscribe(tenantName, entityDeleteEvent)
}

private suspend fun updateSubjectProfile(
tenantUri: URI,
tenantName: String,
attributeAppendEvent: AttributeAppendEvent
): Either<APIException, Unit> = either {
val operationPayload = attributeAppendEvent.operationPayload.deserializeAsMap()
Expand Down Expand Up @@ -137,11 +136,11 @@ class IAMListener(
"Received unknown attribute name: ${attributeAppendEvent.attributeName}"
).left()
}
}.writeContextAndSubscribe(tenantUri, attributeAppendEvent)
}.writeContextAndSubscribe(tenantName, attributeAppendEvent)
}

private suspend fun updateSubjectInfo(
tenantUri: URI,
tenantName: String,
attributeReplaceEvent: AttributeReplaceEvent
): Either<APIException, Unit> = either {
val operationPayload = attributeReplaceEvent.operationPayload.deserializeAsMap()
Expand All @@ -153,26 +152,26 @@ class IAMListener(
subjectUuid,
newSubjectInfo
)
}.writeContextAndSubscribe(tenantUri, attributeReplaceEvent)
}.writeContextAndSubscribe(tenantName, attributeReplaceEvent)
}

private suspend fun removeSubjectFromGroup(
tenantUri: URI,
tenantName: String,
attributeDeleteEvent: AttributeDeleteEvent
): Either<APIException, Unit> = either {
mono {
subjectReferentialService.removeGroupMembershipToUser(
attributeDeleteEvent.entityId.extractSub(),
attributeDeleteEvent.datasetId!!.extractSub()
)
}.writeContextAndSubscribe(tenantUri, attributeDeleteEvent)
}.writeContextAndSubscribe(tenantName, attributeDeleteEvent)
}

private fun Mono<Either<APIException, Unit>>.writeContextAndSubscribe(
tenantUri: URI,
tenantName: String,
event: EntityEvent
) = this.contextWrite {
it.put(NGSILD_TENANT_HEADER, tenantUri)
it.put(NGSILD_TENANT_HEADER, tenantName)
}.subscribe {
it.fold({ apiException ->
if (apiException is OperationNotSupportedException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import org.slf4j.LoggerFactory
import org.springframework.kafka.annotation.KafkaListener
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
import java.net.URI

@Component
class ObservationEventListener(
Expand All @@ -42,17 +41,17 @@ class ObservationEventListener(
internal suspend fun dispatchObservationMessage(content: String) {
kotlin.runCatching {
val observationEvent = deserializeAs<EntityEvent>(content)
val tenantUri = observationEvent.tenantUri
val tenantName = observationEvent.tenantName
logger.debug(
"Handling event {} for entity {} in tenant {}",
observationEvent.operationType,
observationEvent.entityId,
observationEvent.tenantUri
observationEvent.tenantName
)
when (observationEvent) {
is EntityCreateEvent -> handleEntityCreate(tenantUri, observationEvent)
is AttributeUpdateEvent -> handleAttributeUpdateEvent(tenantUri, observationEvent)
is AttributeAppendEvent -> handleAttributeAppendEvent(tenantUri, observationEvent)
is EntityCreateEvent -> handleEntityCreate(tenantName, observationEvent)
is AttributeUpdateEvent -> handleAttributeUpdateEvent(tenantName, observationEvent)
is AttributeAppendEvent -> handleAttributeAppendEvent(tenantName, observationEvent)
else -> OperationNotSupportedException(unhandledOperationType(observationEvent.operationType)).left()
}
}.onFailure {
Expand All @@ -61,7 +60,7 @@ class ObservationEventListener(
}

suspend fun handleEntityCreate(
tenantUri: URI,
tenantName: String,
observationEvent: EntityCreateEvent
): Either<APIException, Unit> = either {
val expandedEntity = expandJsonLdEntity(
Expand All @@ -83,11 +82,11 @@ class ObservationEventListener(
observationEvent.contexts
)
}
}.writeContextAndSubscribe(tenantUri, observationEvent)
}.writeContextAndSubscribe(tenantName, observationEvent)
}

suspend fun handleAttributeUpdateEvent(
tenantUri: URI,
tenantName: String,
observationEvent: AttributeUpdateEvent
): Either<APIException, Unit> = either {
val expandedAttribute = expandAttribute(
Expand Down Expand Up @@ -121,11 +120,11 @@ class ObservationEventListener(
)
}
}
}.writeContextAndSubscribe(tenantUri, observationEvent)
}.writeContextAndSubscribe(tenantName, observationEvent)
}

suspend fun handleAttributeAppendEvent(
tenantUri: URI,
tenantName: String,
observationEvent: AttributeAppendEvent
): Either<APIException, Unit> = either {
val expandedAttribute = expandAttribute(
Expand Down Expand Up @@ -159,14 +158,14 @@ class ObservationEventListener(
)
}
}
}.writeContextAndSubscribe(tenantUri, observationEvent)
}.writeContextAndSubscribe(tenantName, observationEvent)
}

private fun Mono<Either<APIException, Job>>.writeContextAndSubscribe(
tenantUri: URI,
tenantName: String,
event: EntityEvent
) = this.contextWrite {
it.put(NGSILD_TENANT_HEADER, tenantUri)
it.put(NGSILD_TENANT_HEADER, tenantName)
}.subscribe { createResult ->
createResult.fold({
if (it is OperationNotSupportedException)
Expand Down
Loading

0 comments on commit 3561676

Please sign in to comment.