|
| 1 | +package app.cash.backfila.service.listener |
| 2 | + |
| 3 | +import app.cash.backfila.service.BackfilaConfig |
| 4 | +import app.cash.backfila.service.persistence.BackfilaDb |
| 5 | +import app.cash.backfila.service.persistence.DbBackfillRun |
| 6 | +import app.cash.backfila.ui.pages.BackfillShowAction |
| 7 | +import javax.inject.Inject |
| 8 | +import misk.audit.AuditClient |
| 9 | +import misk.hibernate.Id |
| 10 | +import misk.hibernate.Transacter |
| 11 | +import misk.hibernate.load |
| 12 | + |
| 13 | +class AuditClientListener @Inject constructor( |
| 14 | + @BackfilaDb private val transacter: Transacter, |
| 15 | + private val auditClient: AuditClient, |
| 16 | + private val backfilaConfig: BackfilaConfig, |
| 17 | +) : BackfillRunListener { |
| 18 | + override fun runStarted(id: Id<DbBackfillRun>, user: String) { |
| 19 | + val (backfillName, run, description) = transacter.transaction { session -> |
| 20 | + val run = session.load<DbBackfillRun>(id) |
| 21 | + Triple(run.registered_backfill.name, run, "Backfill started by $user ${dryRunPrefix(run)}${nameAndId(run)}") |
| 22 | + } |
| 23 | + auditClient.logEvent( |
| 24 | + target = backfillName, |
| 25 | + description = description, |
| 26 | + requestorLDAP = user, |
| 27 | + applicationName = serviceName(run), |
| 28 | + detailURL = idUrl(id), |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + override fun runPaused(id: Id<DbBackfillRun>, user: String) { |
| 33 | + val (backfillName, run, description) = transacter.transaction { session -> |
| 34 | + val run = session.load<DbBackfillRun>(id) |
| 35 | + Triple(run.registered_backfill.name, run, "Backfill paused by $user ${dryRunPrefix(run)}${nameAndId(run)}") |
| 36 | + } |
| 37 | + auditClient.logEvent( |
| 38 | + target = backfillName, |
| 39 | + description = description, |
| 40 | + requestorLDAP = user, |
| 41 | + applicationName = serviceName(run), |
| 42 | + detailURL = idUrl(id), |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + override fun runErrored(id: Id<DbBackfillRun>) { |
| 47 | + val (backfillName, run, description) = transacter.transaction { session -> |
| 48 | + val run = session.load<DbBackfillRun>(id) |
| 49 | + Triple(run.registered_backfill.name, run, "Backfill paused due to error ${dryRunPrefix(run)}${nameAndId(run)}") |
| 50 | + } |
| 51 | + auditClient.logEvent( |
| 52 | + target = backfillName, |
| 53 | + description = description, |
| 54 | + automatedChange = true, |
| 55 | + applicationName = serviceName(run), |
| 56 | + detailURL = idUrl(id), |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + override fun runCompleted(id: Id<DbBackfillRun>) { |
| 61 | + val (backfillName, run, description) = transacter.transaction { session -> |
| 62 | + val run = session.load<DbBackfillRun>(id) |
| 63 | + Triple(run.registered_backfill.name, run, "Backfill completed ${dryRunPrefix(run)}${nameAndId(run)}") |
| 64 | + } |
| 65 | + auditClient.logEvent( |
| 66 | + target = backfillName, |
| 67 | + description = description, |
| 68 | + automatedChange = true, |
| 69 | + applicationName = serviceName(run), |
| 70 | + detailURL = idUrl(id), |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + private fun serviceName(run: DbBackfillRun) = if (run.service.variant == "default") { |
| 75 | + run.service.registry_name |
| 76 | + } else { |
| 77 | + "${run.service.registry_name}/${run.service.variant}" |
| 78 | + } |
| 79 | + |
| 80 | + private fun nameAndId(run: DbBackfillRun) = |
| 81 | + "[service=${serviceName(run)}][backfill=${run.registered_backfill.name}]" + |
| 82 | + "[id=${run.id}]" + if (run.service.slack_channel != null) { |
| 83 | + "[slackChannel=${run.service.slack_channel}]" |
| 84 | + } else { |
| 85 | + "" |
| 86 | + } |
| 87 | + |
| 88 | + private fun dryRunPrefix(run: DbBackfillRun) = |
| 89 | + if (run.dry_run) { |
| 90 | + "[dryRun=true]" |
| 91 | + } else { |
| 92 | + "" |
| 93 | + } |
| 94 | + |
| 95 | + private fun idUrl(id: Id<DbBackfillRun>): String = backfilaConfig.web_url_root + BackfillShowAction.path(id.id) |
| 96 | +} |
0 commit comments