• 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

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

3
import com.typesafe.config.ConfigFactory
4
import doobie.util.transactor.Transactor
5
import io.grpc.ManagedChannelBuilder
6
import io.iohk.atala.prism.protos.node_api.NodeServiceGrpc
7
import org.hyperledger.identus.agent.server.config.{AppConfig, SecretStorageBackend, ValidatedVaultConfig}
8
import org.hyperledger.identus.agent.walletapi.service.{EntityService, WalletManagementService}
9
import org.hyperledger.identus.agent.walletapi.sql.{
10
  JdbcDIDSecretStorage,
11
  JdbcGenericSecretStorage,
12
  JdbcWalletSecretStorage
13
}
14
import org.hyperledger.identus.agent.walletapi.storage.{DIDSecretStorage, GenericSecretStorage, WalletSecretStorage}
15
import org.hyperledger.identus.agent.walletapi.vault.{
16
  VaultDIDSecretStorage,
17
  VaultKVClient,
18
  VaultKVClientImpl,
19
  VaultWalletSecretStorage,
20
  *
21
}
22
import org.hyperledger.identus.castor.core.service.DIDService
23
import org.hyperledger.identus.iam.authentication.admin.{
24
  AdminApiKeyAuthenticator,
25
  AdminApiKeyAuthenticatorImpl,
26
  AdminConfig
27
}
28
import org.hyperledger.identus.iam.authentication.apikey.{
29
  ApiKeyAuthenticator,
30
  ApiKeyAuthenticatorImpl,
31
  ApiKeyConfig,
32
  AuthenticationRepository
33
}
34
import org.hyperledger.identus.iam.authentication.oidc.{
35
  KeycloakAuthenticator,
36
  KeycloakAuthenticatorImpl,
37
  KeycloakClientImpl,
38
  KeycloakConfig,
39
  KeycloakEntity
40
}
41
import org.hyperledger.identus.iam.authorization.core.PermissionManagementService
42
import org.hyperledger.identus.iam.authorization.keycloak.admin.KeycloakPermissionManagementService
43
import org.hyperledger.identus.pollux.vc.jwt.{DidResolver as JwtDidResolver, PrismDidResolver}
44
import org.hyperledger.identus.shared.crypto.Apollo
45
import org.hyperledger.identus.shared.db.{ContextAwareTask, DbConfig, TransactorLayer}
46
import org.keycloak.authorization.client.AuthzClient
47
import zio.*
48
import zio.config.typesafe.TypesafeConfigProvider
49
import zio.http.Client
50

51
object SystemModule {
52

53
  val configLayer: ZLayer[Any, Config.Error, AppConfig] = ZLayer.fromZIO(
1✔
54
    for {
1✔
55
      ret: AppConfig <- TypesafeConfigProvider
1✔
56
        .fromTypesafeConfig(ConfigFactory.load())
1✔
57
        .load(AppConfig.config)
58
      _ <- ZIO.log(s"HTTP server endpoint is setup as '${ret.agent.httpEndpoint.publicEndpointUrl}'")
1✔
59
      _ <- ZIO.log(s"DIDComm server endpoint is setup as '${ret.agent.didCommEndpoint.publicEndpointUrl}'")
1✔
60
    } yield ret
1✔
61
  )
62

63
  val zioHttpClientLayer: ZLayer[Any, Throwable, Client] = {
64
    import zio.http.netty.NettyConfig
65
    import zio.http.{ConnectionPoolConfig, DnsResolver, ZClient}
66
    (ZLayer.fromZIO(
1✔
67
      for {
×
68
        appConfig <- ZIO.service[AppConfig].provide(SystemModule.configLayer)
×
69
      } yield ZClient.Config.default.copy(
70
        connectionPool = {
71
          val cpSize = appConfig.agent.httpClient.connectionPoolSize
72
          if (cpSize > 0) ConnectionPoolConfig.Fixed(cpSize)
×
73
          else ConnectionPoolConfig.Disabled
×
74
        },
75
        idleTimeout = Some(appConfig.agent.httpClient.idleTimeout),
76
        connectionTimeout = Some(appConfig.agent.httpClient.connectionTimeout),
77
      )
78
    ) ++
79
      ZLayer.succeed(NettyConfig.default) ++
×
80
      DnsResolver.default) >>> ZClient.live
81
  }
82
}
83

84
object AppModule {
85
  val apolloLayer: ULayer[Apollo] = Apollo.layer
×
86

87
  val didJwtResolverLayer: URLayer[DIDService, JwtDidResolver] =
88
    ZLayer.fromFunction(PrismDidResolver(_))
×
89

90
  val builtInAuthenticatorLayer: URLayer[
91
    AppConfig & AuthenticationRepository & EntityService & WalletManagementService,
92
    ApiKeyAuthenticator & AdminApiKeyAuthenticator
93
  ] =
94
    ZLayer.makeSome[
×
95
      AppConfig & AuthenticationRepository & EntityService & WalletManagementService,
96
      ApiKeyAuthenticator & AdminApiKeyAuthenticator
97
    ](
×
98
      AdminConfig.layer,
99
      ApiKeyConfig.layer,
100
      AdminApiKeyAuthenticatorImpl.layer,
101
      ApiKeyAuthenticatorImpl.layer,
102
    )
103

104
  val keycloakAuthenticatorLayer: RLayer[
105
    AppConfig & WalletManagementService & Client & PermissionManagementService[KeycloakEntity],
106
    KeycloakAuthenticator
107
  ] =
108
    ZLayer.fromZIO {
×
109
      ZIO
×
110
        .serviceWith[AppConfig](_.agent.authentication.keycloak.enabled)
×
111
        .map { isEnabled =>
112
          if (!isEnabled) KeycloakAuthenticatorImpl.disabled
×
113
          else
114
            ZLayer.makeSome[
×
115
              AppConfig & WalletManagementService & Client & PermissionManagementService[KeycloakEntity],
116
              KeycloakAuthenticator
117
            ](
×
118
              KeycloakConfig.layer,
119
              KeycloakAuthenticatorImpl.layer,
120
              KeycloakClientImpl.authzClientLayer,
×
121
              KeycloakClientImpl.layer
122
            )
123
        }
124
    }.flatten
125

126
  val keycloakPermissionManagementLayer
127
      : RLayer[AppConfig & WalletManagementService & Client, PermissionManagementService[KeycloakEntity]] = {
128
    ZLayer.fromZIO {
×
129
      ZIO
×
130
        .serviceWith[AppConfig](_.agent.authentication.keycloak.enabled)
×
131
        .map { isEnabled =>
132
          if (!isEnabled) KeycloakPermissionManagementService.disabled
×
133
          else
134
            ZLayer.makeSome[AppConfig & WalletManagementService & Client, PermissionManagementService[KeycloakEntity]](
×
135
              KeycloakClientImpl.authzClientLayer,
×
136
              KeycloakClientImpl.layer,
137
              KeycloakConfig.layer,
138
              KeycloakPermissionManagementService.layer
139
            )
140
        }
141
    }.flatten
142
  }
143
}
144

145
object GrpcModule {
146
  val prismNodeStubLayer: TaskLayer[NodeServiceGrpc.NodeServiceStub] = {
147
    val stubLayer = ZLayer.fromZIO(
×
148
      ZIO
×
149
        .service[AppConfig]
150
        .map(_.prismNode.service)
151
        .flatMap(config =>
152
          if (config.usePlainText) {
×
153
            ZIO.attempt(
×
154
              NodeServiceGrpc.stub(ManagedChannelBuilder.forAddress(config.host, config.port).usePlaintext.build)
×
155
            )
156
          } else {
×
157
            ZIO.attempt(
×
158
              NodeServiceGrpc.stub(ManagedChannelBuilder.forAddress(config.host, config.port).build)
×
159
            )
160
          }
161
        )
162
    )
163
    SystemModule.configLayer >>> stubLayer
×
164
  }
165
}
166

167
object RepoModule {
168

169
  def polluxDbConfigLayer(appUser: Boolean = true): TaskLayer[DbConfig] = {
×
170
    val dbConfigLayer = ZLayer.fromZIO {
×
171
      ZIO.service[AppConfig].map(_.pollux.database).map(_.dbConfig(appUser = appUser))
×
172
    }
173
    SystemModule.configLayer >>> dbConfigLayer
×
174
  }
175

176
  val polluxContextAwareTransactorLayer: TaskLayer[Transactor[ContextAwareTask]] =
177
    polluxDbConfigLayer() >>> TransactorLayer.contextAwareTask
×
178

179
  val polluxTransactorLayer: TaskLayer[Transactor[Task]] =
180
    polluxDbConfigLayer(appUser = false) >>> TransactorLayer.task
×
181

182
  def connectDbConfigLayer(appUser: Boolean = true): TaskLayer[DbConfig] = {
×
183
    val dbConfigLayer = ZLayer.fromZIO {
×
184
      ZIO.service[AppConfig].map(_.connect.database).map(_.dbConfig(appUser = appUser))
×
185
    }
186
    SystemModule.configLayer >>> dbConfigLayer
×
187
  }
188

189
  val connectContextAwareTransactorLayer: TaskLayer[Transactor[ContextAwareTask]] =
190
    connectDbConfigLayer() >>> TransactorLayer.contextAwareTask
×
191

192
  val connectTransactorLayer: TaskLayer[Transactor[Task]] =
193
    connectDbConfigLayer(appUser = false) >>> TransactorLayer.task
×
194

195
  def agentDbConfigLayer(appUser: Boolean = true): TaskLayer[DbConfig] = {
×
196
    val dbConfigLayer = ZLayer.fromZIO {
×
197
      ZIO.service[AppConfig].map(_.agent.database).map(_.dbConfig(appUser = appUser))
×
198
    }
199
    SystemModule.configLayer >>> dbConfigLayer
×
200
  }
201

202
  val agentContextAwareTransactorLayer: TaskLayer[Transactor[ContextAwareTask]] =
203
    agentDbConfigLayer() >>> TransactorLayer.contextAwareTask
×
204

205
  val agentTransactorLayer: TaskLayer[Transactor[Task]] =
206
    agentDbConfigLayer(appUser = false) >>> TransactorLayer.task
×
207

208
  val vaultClientLayer: TaskLayer[VaultKVClient] = {
209
    val vaultClient = ZLayer {
×
210
      for {
×
211
        config <- ZIO
×
212
          .service[AppConfig]
213
          .map(_.agent.secretStorage.vault)
214
          .someOrFailException
215
          .logError("Vault config is not found")
216
        _ <- ZIO.logInfo("Vault client config loaded. Address: " + config.address)
×
217
        vaultKVClient <- ZIO
×
218
          .fromEither(config.validate)
×
219
          .mapError(Exception(_))
×
220
          .flatMap {
221
            case ValidatedVaultConfig.TokenAuth(address, token) =>
×
222
              ZIO.logInfo("Using Vault token authentication") *> VaultKVClientImpl.fromToken(address, token)
×
223
            case ValidatedVaultConfig.AppRoleAuth(address, roleId, secretId) =>
×
224
              ZIO.logInfo("Using Vault AppRole authentication") *>
×
225
                VaultKVClientImpl.fromAppRole(address, roleId, secretId)
×
226
          }
227
      } yield vaultKVClient
228
    }
229

230
    SystemModule.configLayer ++ SystemModule.zioHttpClientLayer >>> vaultClient
×
231
  }
232

233
  val allSecretStorageLayer: TaskLayer[DIDSecretStorage & WalletSecretStorage & GenericSecretStorage] = {
234
    SystemModule.configLayer
×
235
      .tap { config =>
236
        val backend = config.get.agent.secretStorage.backend
×
237
        ZIO.logInfo(s"Using '${backend}' as a secret storage backend")
×
238
      }
239
      .flatMap { config =>
240
        val secretStorageConfig = config.get.agent.secretStorage
×
241
        val useSemanticPath = secretStorageConfig.vault.map(_.useSemanticPath).getOrElse(true)
×
242
        secretStorageConfig.backend match {
243
          case SecretStorageBackend.vault =>
×
244
            ZLayer.make[DIDSecretStorage & WalletSecretStorage & GenericSecretStorage](
×
245
              VaultDIDSecretStorage.layer(useSemanticPath),
×
246
              VaultGenericSecretStorage.layer(useSemanticPath),
×
247
              VaultWalletSecretStorage.layer,
×
248
              vaultClientLayer,
249
            )
250
          case SecretStorageBackend.postgres =>
×
251
            ZLayer.make[DIDSecretStorage & WalletSecretStorage & GenericSecretStorage](
×
252
              JdbcDIDSecretStorage.layer,
253
              JdbcGenericSecretStorage.layer,
254
              JdbcWalletSecretStorage.layer,
255
              agentContextAwareTransactorLayer,
256
            )
257
        }
258
      }
259
  }
260

261
}
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