• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperledger / identus-cloud-agent / 10793991050

10 Sep 2024 01:56PM CUT coverage: 48.504% (-4.5%) from 52.962%
10793991050

push

web-flow
build: sbt and plugins dependency update (#1337)

Signed-off-by: Hyperledger Bot <hyperledger-bot@hyperledger.org>
Signed-off-by: Yurii Shynbuiev <yurii.shynbuiev@iohk.io>
Co-authored-by: Hyperledger Bot <hyperledger-bot@hyperledger.org>
Co-authored-by: Yurii Shynbuiev <yurii.shynbuiev@iohk.io>

7406 of 15269 relevant lines covered (48.5%)

0.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.75
/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/CloudAgentApp.scala
1
package org.hyperledger.identus.agent.server
2

3
import org.hyperledger.identus.agent.notification.WebhookPublisher
4
import org.hyperledger.identus.agent.server.config.AppConfig
5
import org.hyperledger.identus.agent.server.http.{ZHttp4sBlazeServer, ZHttpEndpoints}
6
import org.hyperledger.identus.agent.server.jobs.*
7
import org.hyperledger.identus.agent.walletapi.model.{Entity, Wallet, WalletSeed}
8
import org.hyperledger.identus.agent.walletapi.service.{EntityService, ManagedDIDService, WalletManagementService}
9
import org.hyperledger.identus.agent.walletapi.storage.DIDNonSecretStorage
10
import org.hyperledger.identus.castor.controller.{DIDRegistrarServerEndpoints, DIDServerEndpoints}
11
import org.hyperledger.identus.castor.core.service.DIDService
12
import org.hyperledger.identus.connect.controller.ConnectionServerEndpoints
13
import org.hyperledger.identus.connect.core.service.ConnectionService
14
import org.hyperledger.identus.credentialstatus.controller.CredentialStatusServiceEndpoints
15
import org.hyperledger.identus.event.controller.EventServerEndpoints
16
import org.hyperledger.identus.event.notification.EventNotificationConfig
17
import org.hyperledger.identus.iam.authentication.apikey.ApiKeyAuthenticator
18
import org.hyperledger.identus.iam.entity.http.EntityServerEndpoints
19
import org.hyperledger.identus.iam.wallet.http.WalletManagementServerEndpoints
20
import org.hyperledger.identus.issue.controller.IssueServerEndpoints
21
import org.hyperledger.identus.mercury.{DidOps, HttpClient}
22
import org.hyperledger.identus.oid4vci.CredentialIssuerServerEndpoints
23
import org.hyperledger.identus.pollux.core.service.{CredentialService, PresentationService}
24
import org.hyperledger.identus.pollux.credentialdefinition.CredentialDefinitionRegistryServerEndpoints
25
import org.hyperledger.identus.pollux.credentialschema.{
26
  SchemaRegistryServerEndpoints,
27
  VerificationPolicyServerEndpoints
28
}
29
import org.hyperledger.identus.pollux.vc.jwt.DidResolver as JwtDidResolver
30
import org.hyperledger.identus.presentproof.controller.PresentProofServerEndpoints
31
import org.hyperledger.identus.resolvers.DIDResolver
32
import org.hyperledger.identus.shared.models.{HexString, WalletAccessContext, WalletAdministrationContext, WalletId}
33
import org.hyperledger.identus.shared.utils.DurationOps.toMetricsSeconds
34
import org.hyperledger.identus.system.controller.SystemServerEndpoints
35
import org.hyperledger.identus.verification.controller.VcVerificationServerEndpoints
36
import zio.*
37
import zio.metrics.*
38

39
object CloudAgentApp {
40

41
  def run = for {
×
42
    _ <- AgentInitialization.run
×
43
    _ <- issueCredentialDidCommExchangesJob.debug.fork
×
44
    _ <- presentProofExchangeJob.debug.fork
×
45
    _ <- connectDidCommExchangesJob.debug.fork
×
46
    _ <- syncDIDPublicationStateFromDltJob.debug.fork
×
47
    _ <- syncRevocationStatusListsJob.debug.fork
×
48
    _ <- AgentHttpServer.run.tapDefect(e => ZIO.logErrorCause("Agent HTTP Server failure", e)).fork
×
49
    fiber <- DidCommHttpServer.run.tapDefect(e => ZIO.logErrorCause("DIDComm HTTP Server failure", e)).fork
×
50
    _ <- WebhookPublisher.layer.build.map(_.get[WebhookPublisher]).flatMap(_.run.debug.fork)
×
51
    _ <- fiber.join *> ZIO.log(s"Server End")
×
52
    _ <- ZIO.never
×
53
  } yield ()
×
54

55
  private val issueCredentialDidCommExchangesJob: RIO[
56
    AppConfig & DidOps & DIDResolver & JwtDidResolver & HttpClient & CredentialService & DIDNonSecretStorage &
57
      DIDService & ManagedDIDService & PresentationService & WalletManagementService,
58
    Unit
59
  ] =
60
    for {
×
61
      config <- ZIO.service[AppConfig]
×
62
      _ <- (IssueBackgroundJobs.issueCredentialDidCommExchanges @@ Metric
×
63
        .gauge("issuance_flow_did_com_exchange_job_ms_gauge")
×
64
        .trackDurationWith(_.toMetricsSeconds))
×
65
        .repeat(Schedule.spaced(config.pollux.issueBgJobRecurrenceDelay))
×
66
        .unit
67
    } yield ()
×
68

69
  private val presentProofExchangeJob: RIO[
70
    AppConfig & DidOps & DIDResolver & JwtDidResolver & HttpClient & PresentationService & CredentialService &
71
      DIDNonSecretStorage & DIDService & ManagedDIDService,
72
    Unit
73
  ] =
74
    for {
×
75
      config <- ZIO.service[AppConfig]
×
76
      _ <- (PresentBackgroundJobs.presentProofExchanges @@ Metric
×
77
        .gauge("present_proof_flow_did_com_exchange_job_ms_gauge")
×
78
        .trackDurationWith(_.toMetricsSeconds))
×
79
        .repeat(Schedule.spaced(config.pollux.presentationBgJobRecurrenceDelay))
×
80
        .unit
81
    } yield ()
×
82

83
  private val connectDidCommExchangesJob: RIO[
84
    AppConfig & DidOps & DIDResolver & HttpClient & ConnectionService & ManagedDIDService & DIDNonSecretStorage &
85
      WalletManagementService,
86
    Unit
87
  ] =
88
    for {
×
89
      config <- ZIO.service[AppConfig]
×
90
      _ <- (ConnectBackgroundJobs.didCommExchanges @@ Metric
×
91
        .gauge("connection_flow_did_com_exchange_job_ms_gauge")
×
92
        .trackDurationWith(_.toMetricsSeconds))
×
93
        .repeat(Schedule.spaced(config.connect.connectBgJobRecurrenceDelay))
×
94
        .unit
95
    } yield ()
×
96

97
  private val syncRevocationStatusListsJob = {
98
    for {
×
99
      config <- ZIO.service[AppConfig]
×
100
      _ <- (StatusListJobs.syncRevocationStatuses @@ Metric
×
101
        .gauge("revocation_status_list_sync_job_ms_gauge")
×
102
        .trackDurationWith(_.toMetricsSeconds))
×
103
        .repeat(Schedule.spaced(config.pollux.syncRevocationStatusesBgJobRecurrenceDelay))
×
104
    } yield ()
×
105
  }
106

107
  private val syncDIDPublicationStateFromDltJob: URIO[ManagedDIDService & WalletManagementService, Unit] =
108
    ZIO
×
109
      .serviceWithZIO[WalletManagementService](_.listWallets().map(_._1))
×
110
      .flatMap { wallets =>
111
        ZIO.foreach(wallets) { wallet =>
×
112
          DIDStateSyncBackgroundJobs.syncDIDPublicationStateFromDlt
×
113
            .provideSomeLayer(ZLayer.succeed(WalletAccessContext(wallet.id)))
×
114
        }
115
      }
116
      .catchAll(e => ZIO.logError(s"error while syncing DID publication state: $e"))
×
117
      .repeat(Schedule.spaced(10.seconds))
×
118
      .unit
119
      .provideSomeLayer(ZLayer.succeed(WalletAdministrationContext.Admin()))
×
120

121
}
122

123
object AgentHttpServer {
124
  val agentRESTServiceEndpoints = for {
×
125
    allCredentialDefinitionRegistryEndpoints <- CredentialDefinitionRegistryServerEndpoints.all
×
126
    allSchemaRegistryEndpoints <- SchemaRegistryServerEndpoints.all
×
127
    allVerificationPolicyEndpoints <- VerificationPolicyServerEndpoints.all
×
128
    allConnectionEndpoints <- ConnectionServerEndpoints.all
×
129
    allIssueEndpoints <- IssueServerEndpoints.all
×
130
    allStatusListEndpoints <- CredentialStatusServiceEndpoints.all
×
131
    allDIDEndpoints <- DIDServerEndpoints.all
×
132
    allDIDRegistrarEndpoints <- DIDRegistrarServerEndpoints.all
×
133
    allPresentProofEndpoints <- PresentProofServerEndpoints.all
×
134
    allVcVerificationEndpoints <- VcVerificationServerEndpoints.all
×
135
    allSystemEndpoints <- SystemServerEndpoints.all
×
136
    allEntityEndpoints <- EntityServerEndpoints.all
×
137
    allWalletManagementEndpoints <- WalletManagementServerEndpoints.all
×
138
    allEventEndpoints <- EventServerEndpoints.all
×
139
    allOIDCEndpoints <- CredentialIssuerServerEndpoints.all
×
140
  } yield allCredentialDefinitionRegistryEndpoints ++
×
141
    allSchemaRegistryEndpoints ++
142
    allVerificationPolicyEndpoints ++
143
    allConnectionEndpoints ++
144
    allDIDEndpoints ++
145
    allDIDRegistrarEndpoints ++
146
    allIssueEndpoints ++
147
    allStatusListEndpoints ++
148
    allPresentProofEndpoints ++
149
    allVcVerificationEndpoints ++
150
    allSystemEndpoints ++
151
    allEntityEndpoints ++
152
    allWalletManagementEndpoints ++
153
    allEventEndpoints ++
154
    allOIDCEndpoints
155
  def run =
×
156
    for {
×
157
      allEndpoints <- agentRESTServiceEndpoints
×
158
      allEndpointsWithDocumentation = ZHttpEndpoints.withDocumentations[Task](allEndpoints)
×
159
      server <- ZHttp4sBlazeServer.make("rest_api")
×
160
      appConfig <- ZIO.service[AppConfig]
×
161
      _ <- server.start(allEndpointsWithDocumentation, port = appConfig.agent.httpEndpoint.http.port).debug
×
162
    } yield ()
×
163
}
164

165
object AgentInitialization {
166

167
  private val defaultWalletId = WalletId.default
1✔
168
  private val defaultWallet = Wallet("default", defaultWalletId)
1✔
169
  private val defaultEntity = Entity.Default
170

171
  def run: RIO[AppConfig & WalletManagementService & EntityService & ApiKeyAuthenticator, Unit] =
1✔
172
    for {
1✔
173
      _ <- validateAppConfig
174
      _ <- initializeDefaultWallet
1✔
175
        .provideSomeLayer(ZLayer.succeed(WalletAdministrationContext.Admin()))
1✔
176
    } yield ()
1✔
177

178
  private val validateAppConfig =
179
    ZIO.serviceWithZIO[AppConfig](conf =>
1✔
180
      ZIO
1✔
181
        .fromEither(conf.validate)
1✔
182
        .mapError(msg => RuntimeException(s"Application configuration is invalid. $msg"))
1✔
183
    )
184

185
  private val initializeDefaultWallet =
186
    for {
1✔
187
      _ <- ZIO.logInfo("Initializing default wallet.")
1✔
188
      config <- ZIO.serviceWith[AppConfig](_.agent.defaultWallet)
1✔
189
      walletService <- ZIO.service[WalletManagementService]
1✔
190
      isDefaultWalletEnabled = config.enabled
191
      isDefaultWalletExist <- walletService
1✔
192
        .findWallet(defaultWalletId)
1✔
193
        .map(_.isDefined)
1✔
194
      _ <- ZIO.logInfo(s"Default wallet not enabled.").when(!isDefaultWalletEnabled)
1✔
195
      _ <- ZIO.logInfo(s"Default wallet already exist.").when(isDefaultWalletExist)
1✔
196
      _ <- createDefaultWallet.when(isDefaultWalletEnabled && !isDefaultWalletExist)
1✔
197
    } yield ()
1✔
198

199
  private val createDefaultWallet =
200
    for {
1✔
201
      walletService <- ZIO.service[WalletManagementService]
1✔
202
      entityService <- ZIO.service[EntityService]
1✔
203
      apiKeyAuth <- ZIO.service[ApiKeyAuthenticator]
1✔
204
      config <- ZIO.serviceWith[AppConfig](_.agent.defaultWallet)
1✔
205
      seed <- config.seed.fold(ZIO.none) { seedHex =>
1✔
206
        ZIO
1✔
207
          .fromTry(HexString.fromString(seedHex))
1✔
208
          .map(bytes => WalletSeed.fromByteArray(bytes.toByteArray).left.map(Exception(_)))
1✔
209
          .absolve
210
          .asSome
211
      }
212
      _ <- ZIO.logInfo(s"Default wallet seed is not provided. New seed will be generated.").when(seed.isEmpty)
1✔
213
      _ <- walletService
1✔
214
        .createWallet(defaultWallet, seed)
1✔
215
        .orDieAsUnmanagedFailure
1✔
216
      _ <- entityService.create(defaultEntity).orDieAsUnmanagedFailure
1✔
217
      _ <- apiKeyAuth.add(defaultEntity.id, config.authApiKey)
1✔
218
      _ <- config.webhookUrl.fold(ZIO.unit) { url =>
1✔
219
        val customHeaders = config.webhookApiKey.fold(Map.empty)(apiKey => Map("Authorization" -> s"Bearer $apiKey"))
1✔
220
        walletService
221
          .createWalletNotification(EventNotificationConfig(defaultWalletId, url, customHeaders))
1✔
222
          .orDieAsUnmanagedFailure
1✔
223
          .provide(ZLayer.succeed(WalletAccessContext(defaultWalletId)))
1✔
224
      }
225
    } yield ()
1✔
226

227
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc