Elffuss BraidHub

kikocis/dataspace-portal public

fix: concurrency issues while requesting CaaS (#384)

Co-authored-by: Richard Treier <richard.treier@sovity.de>
Changeset582bd8848e4abd458c787e09686ceb3e7432a41fb3af75caf13dde9d4af90dfb
AuthorKamil Czaja <46053356+kamilczaja@users.noreply.github.com> · 2024-12-02 08:52 +01:00
CommitterGitHub <noreply@github.com> · 2024-12-02 07:52 UTC
Parents0851d8a418d8

Browse the files at this changeset

6 files changed

modified CHANGELOG.md +2 −1
@@ -24,9 +24,10 @@
2424 - Fixed final step not showing when registering a central component ([#305](https://github.com/sovity/authority-portal/issues/305))
2525 - Fixed My Organization page not updated when switching between environments ([#255](https://github.com/sovity/authority-portal/issues/255))
2626 - Added live update when deactivating/reactivating users ([#287](https://github.com/sovity/authority-portal/issues/287))
27-- Fixed Website title not updating in some scenarios [#237](https://github.com/sovity/authority-portal/issues/237)
27+- Fixed Website title not updating in some scenarios ([#237](https://github.com/sovity/authority-portal/issues/237))
2828 - Fixed security vulnerabilities
2929 - Fixed the user not being redirected to the correct URL after login ([#324](https://github.com/sovity/authority-portal/issues/324))
30+- Fixed an issue wherein it was possible to bypass the CaaS request limit in an organization ([PR #384](https://github.com/sovity/authority-portal/pull/384))
3031
3132 ### Known issues
3233
modified authority-portal-backend/authority-portal-quarkus/build.gradle.kts +1 −0
@@ -61,6 +61,7 @@
6161 // https://github.com/quarkusio/quarkus/wiki/Migration-Guide-3.0#fixation-of-the-mockito-subclass-mockmaker
6262 exclude(group = libs.mockito.subclass.get().group, module = libs.mockito.subclass.get().name)
6363 }
64+ testImplementation("io.quarkus:quarkus-test-security")
6465 testImplementation(libs.bundles.assertj)
6566 testImplementation(libs.awaitility)
6667 testImplementation(libs.bundles.mockito)
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/seeds/utils/ScenarioData.kt +17 −0
@@ -14,6 +14,7 @@
1414 package de.sovity.authorityportal.seeds.utils
1515
1616 import com.fasterxml.jackson.databind.ObjectMapper
17+import de.sovity.authorityportal.db.jooq.Tables
1718 import de.sovity.authorityportal.db.jooq.enums.ConnectorContractOffersExceeded
1819 import de.sovity.authorityportal.db.jooq.enums.ConnectorDataOffersExceeded
1920 import de.sovity.authorityportal.db.jooq.enums.ConnectorOnlineStatus
@@ -57,6 +58,22 @@
5758 private val contractOffers = mutableListOf<ContractOfferRecord>()
5859 private val crawlerEventLogEntries = mutableListOf<CrawlerEventLogRecord>()
5960
61+ companion object {
62+ fun uninstall(dsl: DSLContext) {
63+ dsl.deleteFrom(Tables.CRAWLER_EVENT_LOG).execute()
64+ dsl.deleteFrom(Tables.CONTRACT_OFFER).execute()
65+ dsl.deleteFrom(Tables.DATA_OFFER_VIEW_COUNT).execute()
66+ dsl.deleteFrom(Tables.DATA_OFFER).execute()
67+ dsl.deleteFrom(Tables.COMPONENT).execute()
68+ dsl.deleteFrom(Tables.CONNECTOR).execute()
69+ dsl.update(Tables.USER)
70+ .set(Tables.USER.ORGANIZATION_ID, null as String?)
71+ .execute()
72+ dsl.deleteFrom(Tables.ORGANIZATION).execute()
73+ dsl.deleteFrom(Tables.USER).execute()
74+ }
75+ }
76+
6077 fun install(dsl: DSLContext) {
6178 val userOrgMap = users.associate { it.id to it.organizationId }
6279 users.forEach {
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/UiResourceImpl.kt +3 −2
@@ -64,11 +64,12 @@
6464 import de.sovity.authorityportal.web.pages.usermanagement.UserRoleApiService
6565 import de.sovity.authorityportal.web.pages.usermanagement.UserUpdateApiService
6666 import de.sovity.authorityportal.web.pages.userregistration.UserRegistrationApiService
67+import io.quarkus.arc.Lock
6768 import jakarta.annotation.security.PermitAll
6869 import jakarta.enterprise.context.ApplicationScoped
6970 import jakarta.transaction.Transactional
7071
71-@PermitAll // auth checks will be in code in this unit
72+@PermitAll
7273 @ApplicationScoped
7374 class UiResourceImpl(
7475 val authUtils: AuthUtils,
@@ -412,7 +413,7 @@
412413 )
413414 }
414415
415- @Transactional
416+ @Lock
416417 override fun createCaas(environmentId: String, caasRequest: CreateCaasRequest): CreateConnectorResponse {
417418 authUtils.requiresRole(Roles.UserRoles.PARTICIPANT_CURATOR)
418419 authUtils.requiresMemberOfAnyOrganization()
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/pages/connectormanagement/CaasManagementApiService.kt +2 −0
@@ -31,6 +31,7 @@
3131 import de.sovity.authorityportal.web.utils.idmanagement.DataspaceComponentIdUtils
3232 import io.quarkus.logging.Log
3333 import jakarta.enterprise.context.ApplicationScoped
34+import jakarta.transaction.Transactional
3435 import org.eclipse.microprofile.config.inject.ConfigProperty
3536 import kotlin.jvm.optionals.getOrNull
3637
@@ -48,6 +49,7 @@
4849 @ConfigProperty(name = "quarkus.oidc-client.sovity.client-enabled") val isCaasClientEnabled: Boolean
4950 ) {
5051
52+ @Transactional(Transactional.TxType.REQUIRES_NEW)
5153 fun createCaas(
5254 organizationId: String,
5355 userId: String,
modified authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/connector/CaasManagementApiServiceTest.kt +65 −2
@@ -38,9 +38,12 @@
3838 import io.quarkus.test.InjectMock
3939 import io.quarkus.test.TestTransaction
4040 import io.quarkus.test.junit.QuarkusTest
41+import io.quarkus.test.security.TestSecurity
4142 import jakarta.inject.Inject
4243 import org.assertj.core.api.Assertions.assertThat
44+import org.flywaydb.core.Flyway
4345 import org.jooq.DSLContext
46+import org.junit.jupiter.api.AfterEach
4447 import org.junit.jupiter.api.Test
4548 import org.junit.jupiter.api.extension.ExtendWith
4649 import org.mockito.junit.jupiter.MockitoExtension
@@ -48,6 +51,10 @@
4851 import org.mockito.kotlin.eq
4952 import org.mockito.kotlin.whenever
5053 import java.time.OffsetDateTime
54+import java.util.concurrent.CountDownLatch
55+import java.util.concurrent.ExecutorService
56+import java.util.concurrent.TimeUnit
57+import java.util.concurrent.atomic.AtomicInteger
5158
5259 @QuarkusTest
5360 @ExtendWith(MockitoExtension::class)
@@ -65,11 +72,18 @@
6572 @Inject
6673 lateinit var clientIdUtils: ClientIdUtils
6774
75+ @Inject
76+ lateinit var executorService: ExecutorService
77+
6878 @InjectMock
6979 lateinit var caasClient: CaasClient
7080
81+ @AfterEach
82+ fun cleanup() {
83+ ScenarioData.uninstall(dsl)
84+ }
85+
7186 @Test
72- @TestTransaction
7387 fun `create caas creates connector with caas configuration`() {
7488 // arrange
7589 val now = OffsetDateTime.now()
@@ -143,7 +157,6 @@
143157 }
144158
145159 @Test
146- @TestTransaction
147160 fun `check free caas slots returns the correct amount`() {
148161 // arrange
149162 useDevUser(0, 0, setOf(Roles.UserRoles.PARTICIPANT_CURATOR))
@@ -166,4 +179,54 @@
166179 assertThat(result.limit).isEqualTo(0)
167180 assertThat(result.current).isEqualTo(0)
168181 }
182+
183+ @Test
184+ @TestSecurity(authorizationEnabled = false)
185+ fun `create caas endpoint should lock correctly`() {
186+ // arrange
187+ val now = OffsetDateTime.now()
188+ val i = AtomicInteger(0)
189+
190+ useDevUser(0, 0, setOf(Roles.UserRoles.PARTICIPANT_CURATOR))
191+ useMockNow(now)
192+
193+ ScenarioData().apply {
194+ organization(0, 0)
195+ user(0, 0)
196+
197+ scenarioInstaller.install(this)
198+ }
199+
200+ whenever(caasClient.validateSubdomain(any())).thenReturn(true)
201+ whenever(caasClient.requestCaas(any())).thenReturn(
202+ CaasPortalResponse().apply {
203+ value = CaasDetails(connectorId = dummyDevConnectorId(0, 0))
204+ }
205+ )
206+
207+ // act
208+ val count = 10
209+ val latch = CountDownLatch(count)
210+
211+ repeat(count) {
212+ executorService.execute {
213+ uiResource.createCaas(
214+ "test", CreateCaasRequest(
215+ connectorSubdomain = "test-caas-${i.addAndGet(1)}",
216+ connectorTitle = "Test CaaS-${i.addAndGet(1)}",
217+ connectorDescription = "Connector-as-a-service for testing purposes"
218+ )
219+ )
220+ latch.countDown()
221+ }
222+ }
223+ latch.await(20, TimeUnit.SECONDS)
224+
225+ // assert
226+ val connectors = dsl.selectFrom(Tables.CONNECTOR)
227+ .where(Tables.CONNECTOR.TYPE.eq(ConnectorType.CAAS))
228+ .fetch()
229+
230+ assertThat(connectors).hasSize(1)
231+ }
169232 }