Skip to content

Commit

Permalink
fix(prism-agent): validate application config during startup (#712)
Browse files Browse the repository at this point in the history
Signed-off-by: Pat Losoponkul <pat.losoponkul@iohk.io>
  • Loading branch information
patlo-iog authored Sep 13, 2023
1 parent 2e2a768 commit 46fd69b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,21 @@ object AgentInitialization {
private val defaultEntity = Entity.Default

def run: RIO[AppConfig & WalletManagementService & EntityService & ApiKeyAuthenticator, Unit] =
initializeDefaultWallet
for {
_ <- validateAppConfig
_ <- initializeDefaultWallet
} yield ()

private val validateAppConfig =
for {
config <- ZIO.service[AppConfig]
isApiKeyEnabled = config.agent.authentication.apiKey.enabled
isDefaultWalletEnabled = config.agent.defaultWallet.enabled
_ <- ZIO
.fail(RuntimeException("The default wallet cannot be disabled if the apikey authentication is disabled."))
.when(!isApiKeyEnabled && !isDefaultWalletEnabled)
.unit
} yield ()

private val initializeDefaultWallet =
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ object DIDRegistrarEndpoints {
Any
] = baseEndpoint.post
.in(jsonBody[CreateManagedDidRequest])
.errorOut(EndpointOutputs.basicFailuresWith(FailureVariant.unprocessableEntity, FailureVariant.notFound))
.errorOut(
EndpointOutputs
.basicFailuresWith(FailureVariant.unprocessableEntity, FailureVariant.notFound, FailureVariant.forbidden)
)
.out(statusCode(StatusCode.Created).description("Created unpublished DID."))
.out(jsonBody[CreateManagedDIDResponse])
.summary("Create unpublished DID and store it in Prism Agent's wallet")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object AgentInitializationSpec extends ZIOSpecDefault, PostgresTestContainerSupp

override def spec = {
val s = suite("AgentInitialization")(
validateAppConfigSpec,
initializeDefaultWalletSpec
) @@ TestAspect.before(DBTestUtils.runMigrationAgentDB)

Expand All @@ -47,6 +48,24 @@ object AgentInitializationSpec extends ZIOSpecDefault, PostgresTestContainerSupp
).provide(Runtime.removeDefaultLoggers)
}

private val validateAppConfigSpec = suite("validateAppConfig")(
test("do not fail when the config is valid")(
for {
_ <- AgentInitialization.run.overrideConfig()
} yield assertCompletes
),
test("fail when both apikey and default wallet are disabled")(
for {
exit <- AgentInitialization.run
.overrideConfig(
enableApiKey = false,
enableDefaultWallet = false
)
.exit
} yield assert(exit)(fails(isSubtype[RuntimeException](anything)))
)
)

private val initializeDefaultWalletSpec = suite("initializeDefaultWallet")(
test("do not create default wallet if disabled") {
for {
Expand Down Expand Up @@ -113,16 +132,25 @@ object AgentInitializationSpec extends ZIOSpecDefault, PostgresTestContainerSupp
enableDefaultWallet: Boolean = true,
seed: Option[String] = None,
webhookUrl: Option[URL] = None,
webhookApiKey: Option[String] = None
webhookApiKey: Option[String] = None,
enableApiKey: Boolean = true
): ZIO[R, E, A] = {
for {
appConfig <- ZIO.service[AppConfig]
agentConfig = appConfig.agent
defaultWalletConfig = agentConfig.defaultWallet
authConfig = agentConfig.authentication
apiKeyConfig = authConfig.apiKey
// consider using lens
result <- effect.provideSomeLayer(
ZLayer.succeed(
appConfig.copy(
agent = agentConfig.copy(
authentication = authConfig.copy(
apiKey = apiKeyConfig.copy(
enabled = enableApiKey
)
),
defaultWallet = defaultWalletConfig.copy(
enabled = enableDefaultWallet,
seed = seed,
Expand Down

0 comments on commit 46fd69b

Please sign in to comment.