Skip to content

Bump Misk & add audit client #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jooq = "3.18.25"
kotlin = "2.0.21"
kotlinxCoroutines = "1.10.1"
ktlint = "0.47.1"
misk = "2025.04.01.184033-1915641"
misk = "2025.04.04.132805-43bcba0"
okhttp = "5.0.0-alpha.14"
sqldelight = "2.0.2"
wire = "5.2.1"
Expand Down Expand Up @@ -71,6 +71,7 @@ kotlinxHtml = { module = "org.jetbrains.kotlinx:kotlinx-html-jvm", version = "0.
misk = { module = "com.squareup.misk:misk", version.ref = "misk" }
miskActions = { module = "com.squareup.misk:misk-actions", version.ref = "misk" }
miskAdmin = { module = "com.squareup.misk:misk-admin", version.ref = "misk" }
miskAuditClient = { module = "com.squareup.misk:misk-audit-client", version.ref = "misk" }
miskAwsDynamodb = { module = "com.squareup.misk:misk-aws-dynamodb", version.ref = "misk" }
miskAws2Dynamodb = { module = "com.squareup.misk:misk-aws2-dynamodb", version.ref = "misk" }
miskCore = { module = "com.squareup.misk:misk-core", version.ref = "misk" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import app.cash.backfila.client.misk.MiskBackfillModule
import app.cash.backfila.client.misk.hibernate.HibernateBackfillModule
import app.cash.backfila.embedded.EmbeddedBackfilaModule
import app.cash.backfila.service.BackfilaConfig
import app.cash.backfila.service.BackfillRunListener
import app.cash.backfila.service.SlackHelper
import app.cash.backfila.service.listener.BackfilaListenerModule
import app.cash.backfila.service.persistence.BackfilaDb
import app.cash.backfila.service.persistence.BackfilaPersistenceModule
import app.cash.backfila.service.runner.BackfillRunnerLoggingSetupProvider
Expand Down Expand Up @@ -49,9 +48,9 @@ class SelfBackfillTestingModule : KAbstractModule() {
web_url_root = "",
slack = null,
)
newMultibinder<BackfillRunListener>()
.addBinding()
.to(SlackHelper::class.java)

install(BackfilaListenerModule())

bind<BackfilaConfig>().toInstance(config)
install(DeploymentModule(TESTING))
install(LogCollectorModule())
Expand Down
2 changes: 2 additions & 0 deletions service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
implementation(libs.misk)
implementation(libs.miskActions)
implementation(libs.miskAdmin)
implementation(libs.miskAuditClient)
implementation(libs.miskCore)
implementation(libs.miskHibernate)
implementation(libs.miskHotwire)
Expand Down Expand Up @@ -77,6 +78,7 @@ dependencies {

testImplementation(libs.miskTesting)
testImplementation(libs.miskHibernateTesting)
testImplementation(testFixtures(libs.miskAuditClient))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

testImplementation(libs.junitApi)
testImplementation(libs.junitParams)
testImplementation(libs.junitEngine)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.cash.backfila.dashboard

import app.cash.backfila.service.BackfillRunListener
import app.cash.backfila.service.listener.BackfillRunListener
import app.cash.backfila.service.persistence.BackfilaDb
import app.cash.backfila.service.persistence.BackfillState
import app.cash.backfila.service.persistence.BackfillState.PAUSED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import app.cash.backfila.client.GrpcCallbackConnectorProvider
import app.cash.backfila.client.HttpCallbackConnectorProvider
import app.cash.backfila.dashboard.BackfilaDashboardModule
import app.cash.backfila.dashboard.BackfilaWebActionsModule
import app.cash.backfila.service.listener.BackfilaListenerModule
import app.cash.backfila.service.persistence.BackfilaPersistenceModule
import app.cash.backfila.service.runner.BackfillRunnerLoggingSetupProvider
import app.cash.backfila.service.runner.BackfillRunnerNoLoggingSetupProvider
Expand Down Expand Up @@ -43,9 +44,7 @@ class BackfilaServiceModule(
AccessAnnotationEntry<AdminDashboardAccess>(capabilities = listOf("backfila--owners")),
)

newMultibinder<BackfillRunListener>()
.addBinding()
.to(SlackHelper::class.java)
install(BackfilaListenerModule())

install(ConfigModule.create("backfila", config))
install(BackfilaPersistenceModule(config))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package app.cash.backfila.service.listener

import app.cash.backfila.service.BackfilaConfig
import app.cash.backfila.service.persistence.BackfilaDb
import app.cash.backfila.service.persistence.DbBackfillRun
import app.cash.backfila.ui.pages.BackfillShowAction
import javax.inject.Inject
import misk.audit.AuditClient
import misk.hibernate.Id
import misk.hibernate.Transacter
import misk.hibernate.load

class AuditClientListener @Inject constructor(
@BackfilaDb private val transacter: Transacter,
private val auditClient: AuditClient,
private val backfilaConfig: BackfilaConfig,
) : BackfillRunListener {
override fun runStarted(id: Id<DbBackfillRun>, user: String) {
val (backfillName, run, description) = transacter.transaction { session ->
val run = session.load<DbBackfillRun>(id)
AuditEvent(run.registered_backfill.name, run, "Backfill started by $user ${dryRunPrefix(run)}${nameAndId(run)}")
}
auditClient.logEvent(
target = backfillName,
description = description,
requestorLDAP = user,
applicationName = serviceName(run),
detailURL = idUrl(id),
)
}

override fun runPaused(id: Id<DbBackfillRun>, user: String) {
val (backfillName, run, description) = transacter.transaction { session ->
val run = session.load<DbBackfillRun>(id)
AuditEvent(run.registered_backfill.name, run, "Backfill paused by $user ${dryRunPrefix(run)}${nameAndId(run)}")
}
auditClient.logEvent(
target = backfillName,
description = description,
requestorLDAP = user,
applicationName = serviceName(run),
detailURL = idUrl(id),
)
}

override fun runErrored(id: Id<DbBackfillRun>) {
val (backfillName, run, description) = transacter.transaction { session ->
val run = session.load<DbBackfillRun>(id)
AuditEvent(run.registered_backfill.name, run, "Backfill paused due to error ${dryRunPrefix(run)}${nameAndId(run)}")
}
auditClient.logEvent(
target = backfillName,
description = description,
automatedChange = true,
applicationName = serviceName(run),
detailURL = idUrl(id),
)
}

override fun runCompleted(id: Id<DbBackfillRun>) {
val (backfillName, run, description) = transacter.transaction { session ->
val run = session.load<DbBackfillRun>(id)
AuditEvent(run.registered_backfill.name, run, "Backfill completed ${dryRunPrefix(run)}${nameAndId(run)}")
}
auditClient.logEvent(
target = backfillName,
description = description,
automatedChange = true,
applicationName = serviceName(run),
detailURL = idUrl(id),
)
}

private fun serviceName(run: DbBackfillRun) = if (run.service.variant == "default") {
run.service.registry_name
} else {
"${run.service.registry_name}/${run.service.variant}"
}

private fun nameAndId(run: DbBackfillRun) =
"[service=${serviceName(run)}][backfill=${run.registered_backfill.name}]" +
"[id=${run.id}]" + if (run.service.slack_channel != null) {
"[slackChannel=${run.service.slack_channel}]"
} else {
""
}

private fun dryRunPrefix(run: DbBackfillRun) =
if (run.dry_run) {
"[dryRun=true]"
} else {
""
}

private fun idUrl(id: Id<DbBackfillRun>): String = backfilaConfig.web_url_root + BackfillShowAction.path(id.id)

private data class AuditEvent(
val backfillName: String,
val run: DbBackfillRun,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is not to have the Db object?

val description: String,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package app.cash.backfila.service.listener

import misk.inject.KAbstractModule

class BackfilaListenerModule : KAbstractModule() {
override fun configure() {
newMultibinder<BackfillRunListener>()
multibind<BackfillRunListener>().to<AuditClientListener>()
multibind<BackfillRunListener>().to<SlackHelper>()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.cash.backfila.service
package app.cash.backfila.service.listener

import app.cash.backfila.service.persistence.DbBackfillRun
import misk.hibernate.Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.cash.backfila.service
package app.cash.backfila.service.listener

import app.cash.backfila.service.BackfilaConfig
import app.cash.backfila.service.persistence.BackfilaDb
import app.cash.backfila.service.persistence.DbBackfillRun
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import app.cash.backfila.protos.clientservice.RunBatchRequest
import app.cash.backfila.protos.clientservice.RunBatchResponse
import app.cash.backfila.service.BackfilaConfig
import app.cash.backfila.service.BackfilaMetrics
import app.cash.backfila.service.BackfillRunListener
import app.cash.backfila.service.listener.BackfillRunListener
import app.cash.backfila.service.persistence.BackfilaDb
import app.cash.backfila.service.persistence.BackfillState
import app.cash.backfila.service.persistence.DbBackfillRun
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app.cash.backfila.ui.actions
import app.cash.backfila.dashboard.CreateBackfillAction
import app.cash.backfila.protos.service.CreateBackfillRequest
import app.cash.backfila.ui.pages.BackfillCreateAction.BackfillCreateField
import app.cash.backfila.ui.pages.BackfillShowAction
import javax.inject.Inject
import javax.inject.Singleton
import misk.scope.ActionScoped
Expand Down Expand Up @@ -63,9 +64,9 @@ class BackfillCreateHandlerAction @Inject constructor(
val id = response.backfill_run_id

return Response(
body = "go to /backfills/$id".toResponseBody(),
body = "go to ${BackfillShowAction.path(id)}".toResponseBody(),
statusCode = 303,
headers = Headers.headersOf("Location", "/backfills/$id"),
headers = Headers.headersOf("Location", BackfillShowAction.path(id)),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import app.cash.backfila.client.Connectors
import app.cash.backfila.client.FakeBackfilaCallbackConnectorProvider
import app.cash.backfila.client.ForConnectors
import app.cash.backfila.service.BackfilaConfig
import app.cash.backfila.service.BackfillRunListener
import app.cash.backfila.service.SlackHelper
import app.cash.backfila.service.listener.BackfilaListenerModule
import app.cash.backfila.service.persistence.BackfilaDb
import app.cash.backfila.service.persistence.BackfilaPersistenceModule
import app.cash.backfila.service.runner.BackfillRunnerLoggingSetupProvider
Expand All @@ -21,6 +20,8 @@ import java.util.concurrent.Executors
import javax.inject.Singleton
import misk.MiskCaller
import misk.MiskTestingServiceModule
import misk.audit.FakeAuditClientModule
import misk.config.AppNameModule
import misk.environment.DeploymentModule
import misk.hibernate.HibernateTestingModule
import misk.inject.KAbstractModule
Expand Down Expand Up @@ -53,13 +54,13 @@ internal class BackfilaTestingModule : KAbstractModule() {
)
bind<BackfilaConfig>().toInstance(config)

newMultibinder<BackfillRunListener>()
.addBinding()
.to(SlackHelper::class.java)
install(BackfilaListenerModule())

install(DeploymentModule(wisp.deployment.TESTING))
install(LogCollectorModule())
install(MiskTestingServiceModule())
install(AppNameModule("backfila"))
install(FakeAuditClientModule())

install(HibernateTestingModule(BackfilaDb::class))
install(BackfilaPersistenceModule(config))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import app.cash.backfila.service.BackfilaServiceModule
import app.cash.backfila.service.persistence.DbBackfillRun
import misk.MiskApplication
import misk.MiskRealServiceModule
import misk.audit.NoOpAuditClientModule
import misk.environment.DeploymentModule
import misk.hibernate.Session
import misk.inject.KAbstractModule
Expand Down Expand Up @@ -124,6 +125,7 @@ fun main(args: Array<String>) {
DevelopmentAdminDashboardModule(),
BackfilaDefaultEndpointConfigModule(),
MiskRealServiceModule(),
NoOpAuditClientModule(),
).run(args)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import app.cash.backfila.development.DevelopmentAdminDashboardModule
import app.cash.backfila.development.ServiceHeaderInterceptor
import misk.MiskApplication
import misk.MiskRealServiceModule
import misk.audit.NoOpAuditClientModule
import misk.client.HttpClientEndpointConfig
import misk.client.HttpClientsConfig
import misk.client.HttpClientsConfigModule
Expand Down Expand Up @@ -57,6 +58,7 @@ fun main(args: Array<String>) {
FineDiningServiceModule(),
DevelopmentAdminDashboardModule(),
MiskRealServiceModule(),
NoOpAuditClientModule(),
).run(args)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import app.cash.backfila.development.DevelopmentAdminDashboardModule
import app.cash.backfila.development.ServiceHeaderInterceptor
import misk.MiskApplication
import misk.MiskRealServiceModule
import misk.audit.NoOpAuditClientModule
import misk.client.HttpClientEndpointConfig
import misk.client.HttpClientsConfig
import misk.client.HttpClientsConfigModule
Expand Down Expand Up @@ -64,6 +65,7 @@ class McDeesDevelopmentServiceBase {
),
DevelopmentAdminDashboardModule(),
MiskRealServiceModule(),
NoOpAuditClientModule(),
).run(args)
}

Expand Down
Loading
Loading