Skip to content

Commit

Permalink
SW-6267 Publish rate-limited events as system user (#2616)
Browse files Browse the repository at this point in the history
Rate-limited events were being published as the current user the first time they
fired during the rate limiting period, and without any current user at all if they
were deferred until the end of the period. The latter caused event handlers to
fail if they included permission checks.

Make the behavior of deferred and immediate publication consistent by publishing
the events as the system user.
  • Loading branch information
sgrimm authored Nov 19, 2024
1 parent 6f46a89 commit 5db05c6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.terraformation.backend.ratelimit

import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.databind.ObjectMapper
import com.terraformation.backend.customer.model.SystemUser
import com.terraformation.backend.db.asNonNullable
import com.terraformation.backend.db.default_schema.tables.references.RATE_LIMITED_EVENTS
import com.terraformation.backend.log.perClassLogger
Expand Down Expand Up @@ -29,6 +30,7 @@ class RateLimitedEventPublisherImpl(
private val dslContext: DSLContext,
private val eventPublisher: ApplicationEventPublisher,
private val objectMapper: ObjectMapper,
private val systemUser: SystemUser,
) : RateLimitedEventPublisher {
private val log = perClassLogger()

Expand Down Expand Up @@ -111,7 +113,7 @@ class RateLimitedEventPublisherImpl(
}

if (canPublishEventNow) {
eventPublisher.publishEvent(event)
systemUser.run { eventPublisher.publishEvent(event) }
} else if (eventRecordVanished) {
if (canRetry) {
publishOrDefer(event, false)
Expand Down Expand Up @@ -190,7 +192,7 @@ class RateLimitedEventPublisherImpl(

eventsToPublish.forEach { event ->
try {
eventPublisher.publishEvent(event)
systemUser.run { eventPublisher.publishEvent(event) }
} catch (e: Exception) {
log.error("Error publishing pending event $event", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.terraformation.backend.RunsAsUser
import com.terraformation.backend.TestClock
import com.terraformation.backend.TestEventPublisher
import com.terraformation.backend.customer.model.SystemUser
import com.terraformation.backend.customer.model.TerrawareUser
import com.terraformation.backend.db.DatabaseTest
import com.terraformation.backend.db.default_schema.ProjectId
Expand All @@ -25,6 +26,7 @@ class RateLimitedEventPublisherTest : DatabaseTest(), RunsAsUser {
dslContext,
eventPublisher,
jacksonObjectMapper(),
SystemUser(usersDao),
)
}

Expand Down

0 comments on commit 5db05c6

Please sign in to comment.