From 67121fee94fd24f8075fe765ea2e7fc22e835d17 Mon Sep 17 00:00:00 2001 From: "Andrew (Paradi) Alexander" Date: Thu, 27 Jun 2024 20:24:36 +0200 Subject: [PATCH] Add Misk metadata for registered backfills (#392) Screenshot 2024-06-27 at 17 10 37 --- .../embedded/EmbeddedBackfilaModule.kt | 7 ++- .../internal/BoundingRangeStrategy.kt | 4 +- client-misk/build.gradle.kts | 1 + .../backfila/client/misk/BackfillMetadata.kt | 43 +++++++++++++++++++ .../client/misk/MiskBackfillModule.kt | 3 ++ gradle/libs.versions.toml | 3 +- .../selfbackfill/LocalBackfillingModule.kt | 2 +- .../selfbackfill/SelfBackfillTestingModule.kt | 2 +- .../development/BackfilaDevelopmentService.kt | 7 +-- .../DevelopmentAdminDashboardModule.kt | 32 ++++++++++++++ .../FineDiningDevelopmentService.kt | 4 +- .../mcdees/McDeesDevelopmentServiceBase.kt | 4 +- 12 files changed, 95 insertions(+), 17 deletions(-) create mode 100644 client-misk/src/main/kotlin/app/cash/backfila/client/misk/BackfillMetadata.kt create mode 100644 service/src/test/kotlin/app/cash/backfila/development/DevelopmentAdminDashboardModule.kt diff --git a/backfila-embedded/src/main/kotlin/app/cash/backfila/embedded/EmbeddedBackfilaModule.kt b/backfila-embedded/src/main/kotlin/app/cash/backfila/embedded/EmbeddedBackfilaModule.kt index 8684b307b..affc4c067 100644 --- a/backfila-embedded/src/main/kotlin/app/cash/backfila/embedded/EmbeddedBackfilaModule.kt +++ b/backfila-embedded/src/main/kotlin/app/cash/backfila/embedded/EmbeddedBackfilaModule.kt @@ -17,8 +17,11 @@ class EmbeddedBackfilaModule bind(BackfilaApi::class.java).to(EmbeddedBackfila::class.java) bind(Backfila::class.java).to(EmbeddedBackfila::class.java) bind(OnStartup::class.java).toInstance( - if (throwOnStartup) OnStartup.THROW_ON_STARTUP - else OnStartup.CONTINUE_ON_STARTUP + if (throwOnStartup) { + OnStartup.THROW_ON_STARTUP + } else { + OnStartup.CONTINUE_ON_STARTUP + }, ) } } diff --git a/client-misk-hibernate/src/main/kotlin/app/cash/backfila/client/misk/hibernate/internal/BoundingRangeStrategy.kt b/client-misk-hibernate/src/main/kotlin/app/cash/backfila/client/misk/hibernate/internal/BoundingRangeStrategy.kt index 6ce81262e..ba2facb8d 100644 --- a/client-misk-hibernate/src/main/kotlin/app/cash/backfila/client/misk/hibernate/internal/BoundingRangeStrategy.kt +++ b/client-misk-hibernate/src/main/kotlin/app/cash/backfila/client/misk/hibernate/internal/BoundingRangeStrategy.kt @@ -192,7 +192,7 @@ private fun , Pkey : Any> schemaAndTable(backfill: HibernateBack You are welcome to create a copy of the bounding range strategy limited to your service if you absolutely cannot have table name annotation on your entity class. - """.trimIndent() + """.trimIndent(), ) } return when { @@ -214,7 +214,7 @@ private fun , Pkey : Any> onlyTable(backfill: HibernateBackfill< You are welcome to create a copy of the bounding range strategy limited to your service if you absolutely cannot have table name annotation on your entity class. - """.trimIndent() + """.trimIndent(), ) } return "`$table`" diff --git a/client-misk/build.gradle.kts b/client-misk/build.gradle.kts index 431ab1aad..5a7b259f5 100644 --- a/client-misk/build.gradle.kts +++ b/client-misk/build.gradle.kts @@ -36,6 +36,7 @@ dependencies { implementation(libs.miskInject) implementation(libs.miskService) implementation(libs.wispLogging) + implementation(libs.wispMoshi) testImplementation(libs.assertj) testImplementation(libs.miskTesting) diff --git a/client-misk/src/main/kotlin/app/cash/backfila/client/misk/BackfillMetadata.kt b/client-misk/src/main/kotlin/app/cash/backfila/client/misk/BackfillMetadata.kt new file mode 100644 index 000000000..639a90a8c --- /dev/null +++ b/client-misk/src/main/kotlin/app/cash/backfila/client/misk/BackfillMetadata.kt @@ -0,0 +1,43 @@ +package app.cash.backfila.client.misk + +import app.cash.backfila.client.spi.BackfillBackend +import app.cash.backfila.client.spi.BackfillRegistration +import jakarta.inject.Inject +import misk.web.metadata.Metadata +import misk.web.metadata.MetadataProvider +import misk.web.metadata.toFormattedJson +import wisp.moshi.adapter +import wisp.moshi.defaultKotlinMoshi + +internal data class BackfillMetadata( + val name: String, + val description: String?, + val parametersClass: String?, +) + +internal data class BackfillsMetadata( + val backfills: Map>, +) : Metadata( + metadata = backfills, + prettyPrint = defaultKotlinMoshi + .adapter>>() + .toFormattedJson(backfills), + descriptionString = "Backfill classes registered with Backfila.", +) + +internal class BackfillMetadataProvider : MetadataProvider { + @Inject lateinit var backends: Set + + override val id = "backfila" + + override fun get(): BackfillsMetadata { + val backfills = backends.associate { it::class.simpleName!! to it.backfills().map { it.toMetadata() } } + return BackfillsMetadata(backfills) + } + + private fun BackfillRegistration.toMetadata() = BackfillMetadata( + name = name, + description = description, + parametersClass = parametersClass.simpleName, + ) +} diff --git a/client-misk/src/main/kotlin/app/cash/backfila/client/misk/MiskBackfillModule.kt b/client-misk/src/main/kotlin/app/cash/backfila/client/misk/MiskBackfillModule.kt index b857a8886..493d68028 100644 --- a/client-misk/src/main/kotlin/app/cash/backfila/client/misk/MiskBackfillModule.kt +++ b/client-misk/src/main/kotlin/app/cash/backfila/client/misk/MiskBackfillModule.kt @@ -11,6 +11,7 @@ import kotlin.reflect.KClass import misk.ServiceModule import misk.inject.KAbstractModule import misk.inject.toKey +import misk.web.metadata.MetadataModule /** * Backfila-using applications install at minimum 3 things. @@ -35,5 +36,7 @@ class MiskBackfillModule @JvmOverloads constructor( dependsOn = dependsOn, ), ) + + install(MetadataModule(BackfillMetadataProvider())) } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 70113cce5..441399f74 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,7 +2,7 @@ jooq = "3.18.1" ktlint = "0.47.1" kotlin = "1.9.23" -misk = "2024.05.07.211405-28ee413" +misk = "2024.06.26.052833-88986ac" [libraries] apacheCommonsLang3 = { module = "org.apache.commons:commons-lang3", version = "3.12.0" } @@ -83,3 +83,4 @@ wispClient = { module = "app.cash.wisp:wisp-client", version.ref = "misk" } wispConfig = { module = "app.cash.wisp:wisp-config", version.ref = "misk" } wispDeployment = { module = "app.cash.wisp:wisp-deployment", version.ref = "misk" } wispLogging = { module = "app.cash.wisp:wisp-logging", version.ref = "misk" } +wispMoshi = { module = "app.cash.wisp:wisp-moshi", version.ref = "misk" } diff --git a/service-self-backfill/src/main/kotlin/app/cash/backfila/service/selfbackfill/LocalBackfillingModule.kt b/service-self-backfill/src/main/kotlin/app/cash/backfila/service/selfbackfill/LocalBackfillingModule.kt index c2b9d0876..35026040a 100644 --- a/service-self-backfill/src/main/kotlin/app/cash/backfila/service/selfbackfill/LocalBackfillingModule.kt +++ b/service-self-backfill/src/main/kotlin/app/cash/backfila/service/selfbackfill/LocalBackfillingModule.kt @@ -1,9 +1,9 @@ package app.cash.backfila.service.selfbackfill +import app.cash.backfila.client.BackfilaCallbackConnectorProvider import app.cash.backfila.client.BackfilaClientConfig import app.cash.backfila.client.BackfilaClientLoggingSetupProvider import app.cash.backfila.client.BackfilaClientNoLoggingSetupProvider -import app.cash.backfila.client.BackfilaCallbackConnectorProvider import app.cash.backfila.client.ForConnectors import app.cash.backfila.client.internal.BackfilaClient import app.cash.backfila.client.misk.hibernate.HibernateBackfillModule diff --git a/service-self-backfill/src/test/kotlin/app/cash/backfila/service/selfbackfill/SelfBackfillTestingModule.kt b/service-self-backfill/src/test/kotlin/app/cash/backfila/service/selfbackfill/SelfBackfillTestingModule.kt index 36e179bfe..a7f5b2039 100644 --- a/service-self-backfill/src/test/kotlin/app/cash/backfila/service/selfbackfill/SelfBackfillTestingModule.kt +++ b/service-self-backfill/src/test/kotlin/app/cash/backfila/service/selfbackfill/SelfBackfillTestingModule.kt @@ -1,9 +1,9 @@ package app.cash.backfila.service.selfbackfill import app.cash.backfila.api.ServiceWebActionsModule +import app.cash.backfila.client.BackfilaCallbackConnectorProvider import app.cash.backfila.client.BackfilaClientLoggingSetupProvider import app.cash.backfila.client.BackfilaClientNoLoggingSetupProvider -import app.cash.backfila.client.BackfilaCallbackConnectorProvider import app.cash.backfila.client.BackfilaHttpClientConfig import app.cash.backfila.client.ForConnectors import app.cash.backfila.client.misk.MiskBackfillModule diff --git a/service/src/test/kotlin/app/cash/backfila/development/BackfilaDevelopmentService.kt b/service/src/test/kotlin/app/cash/backfila/development/BackfilaDevelopmentService.kt index a7cd50a6b..9c79979c3 100644 --- a/service/src/test/kotlin/app/cash/backfila/development/BackfilaDevelopmentService.kt +++ b/service/src/test/kotlin/app/cash/backfila/development/BackfilaDevelopmentService.kt @@ -17,7 +17,6 @@ import app.cash.backfila.service.BackfilaConfig import app.cash.backfila.service.BackfilaServiceModule import app.cash.backfila.service.persistence.DbBackfillRun import misk.MiskApplication -import misk.MiskCaller import misk.MiskRealServiceModule import misk.environment.DeploymentModule import misk.hibernate.Session @@ -26,12 +25,10 @@ import misk.jdbc.DataSourceClusterConfig import misk.jdbc.DataSourceClustersConfig import misk.jdbc.DataSourceConfig import misk.jdbc.DataSourceType -import misk.security.authz.DevelopmentOnly import misk.security.authz.FakeCallerAuthenticator import misk.security.authz.MiskCallerAuthenticator import misk.web.MiskWebModule import misk.web.WebConfig -import misk.web.dashboard.AdminDashboardModule import okio.ByteString.Companion.encodeUtf8 import wisp.deployment.Deployment @@ -54,8 +51,6 @@ fun main(args: Array) { ) install(MiskWebModule(webConfig)) multibind().to() - bind().annotatedWith() - .toInstance(MiskCaller(user = "testfila")) bind().to() newMapBinder(ForConnectors::class) @@ -126,7 +121,7 @@ fun main(args: Array) { support_button_url = "https://square.enterprise.slack.com/archives/C0151JAREE7", ), ), - AdminDashboardModule(isDevelopment = true), + DevelopmentAdminDashboardModule(), BackfilaDefaultEndpointConfigModule(), MiskRealServiceModule(), ).run(args) diff --git a/service/src/test/kotlin/app/cash/backfila/development/DevelopmentAdminDashboardModule.kt b/service/src/test/kotlin/app/cash/backfila/development/DevelopmentAdminDashboardModule.kt new file mode 100644 index 000000000..cdf8ea226 --- /dev/null +++ b/service/src/test/kotlin/app/cash/backfila/development/DevelopmentAdminDashboardModule.kt @@ -0,0 +1,32 @@ +package app.cash.backfila.development + +import misk.MiskCaller +import misk.inject.KAbstractModule +import misk.security.authz.AccessAnnotationEntry +import misk.security.authz.DevelopmentOnly +import misk.web.dashboard.AdminDashboardAccess +import misk.web.dashboard.AdminDashboardModule +import misk.web.metadata.all.AllMetadataAccess +import misk.web.metadata.all.AllMetadataModule +import misk.web.metadata.config.ConfigMetadataAction + +class DevelopmentAdminDashboardModule : KAbstractModule() { + override fun configure() { + install(AdminDashboardModule(isDevelopment = true, configTabMode = ConfigMetadataAction.ConfigTabMode.SHOW_REDACTED_EFFECTIVE_CONFIG)) + install(AllMetadataModule()) + multibind().toInstance( + AccessAnnotationEntry( + capabilities = listOf("admin_console"), + ), + ) + multibind().toInstance( + AccessAnnotationEntry( + capabilities = listOf("admin_console"), + services = listOf(), + ), + ) + // Setup authentication in the development environment + bind().annotatedWith() + .toInstance(MiskCaller(user = "testfila", capabilities = setOf("admin_console", "users"))) + } +} diff --git a/service/src/test/kotlin/app/cash/backfila/development/finedining/FineDiningDevelopmentService.kt b/service/src/test/kotlin/app/cash/backfila/development/finedining/FineDiningDevelopmentService.kt index 92dd869c2..03ed0d26a 100644 --- a/service/src/test/kotlin/app/cash/backfila/development/finedining/FineDiningDevelopmentService.kt +++ b/service/src/test/kotlin/app/cash/backfila/development/finedining/FineDiningDevelopmentService.kt @@ -3,6 +3,7 @@ package app.cash.backfila.development.finedining import app.cash.backfila.development.BackfilaDevelopmentLogging import app.cash.backfila.development.DevServiceConstants.Companion.BACKFILA_PORT import app.cash.backfila.development.DevServiceConstants.Companion.FINE_DINING_PORT +import app.cash.backfila.development.DevelopmentAdminDashboardModule import app.cash.backfila.development.ServiceHeaderInterceptor import misk.MiskApplication import misk.MiskRealServiceModule @@ -17,7 +18,6 @@ import misk.security.authz.FakeCallerAuthenticator import misk.security.authz.MiskCallerAuthenticator import misk.web.MiskWebModule import misk.web.WebConfig -import misk.web.dashboard.AdminDashboardModule import okhttp3.Interceptor import wisp.deployment.Deployment @@ -55,7 +55,7 @@ fun main(args: Array) { }, DeploymentModule(deployment), FineDiningServiceModule(), - AdminDashboardModule(isDevelopment = true), + DevelopmentAdminDashboardModule(), MiskRealServiceModule(), ).run(args) } diff --git a/service/src/test/kotlin/app/cash/backfila/development/mcdees/McDeesDevelopmentServiceBase.kt b/service/src/test/kotlin/app/cash/backfila/development/mcdees/McDeesDevelopmentServiceBase.kt index 79e32d56c..b711064c6 100644 --- a/service/src/test/kotlin/app/cash/backfila/development/mcdees/McDeesDevelopmentServiceBase.kt +++ b/service/src/test/kotlin/app/cash/backfila/development/mcdees/McDeesDevelopmentServiceBase.kt @@ -2,6 +2,7 @@ package app.cash.backfila.development.mcdees import app.cash.backfila.development.BackfilaDevelopmentLogging import app.cash.backfila.development.DevServiceConstants.Companion.BACKFILA_PORT +import app.cash.backfila.development.DevelopmentAdminDashboardModule import app.cash.backfila.development.ServiceHeaderInterceptor import misk.MiskApplication import misk.MiskRealServiceModule @@ -16,7 +17,6 @@ import misk.security.authz.FakeCallerAuthenticator import misk.security.authz.MiskCallerAuthenticator import misk.web.MiskWebModule import misk.web.WebConfig -import misk.web.dashboard.AdminDashboardModule import okhttp3.Interceptor import wisp.deployment.Deployment @@ -62,7 +62,7 @@ class McDeesDevelopmentServiceBase { variant = variant, port = port, ), - AdminDashboardModule(isDevelopment = true), + DevelopmentAdminDashboardModule(), MiskRealServiceModule(), ).run(args) }