forked from devsisters/shardcake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrpcAuthExampleSpec.scala
More file actions
64 lines (57 loc) · 2.24 KB
/
GrpcAuthExampleSpec.scala
File metadata and controls
64 lines (57 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package example
import com.devsisters.shardcake._
import com.devsisters.shardcake.interfaces.{ Pods, Storage }
import io.grpc.{ Metadata, Status }
import scalapb.zio_grpc.{ ZClientInterceptor, ZTransform }
import zio.test._
import zio.{ Config => _, _ }
object GrpcAuthExampleSpec extends ZIOSpecDefault {
private val validAuthenticationKey = "validAuthenticationKey"
private val authKey = Metadata.Key.of("authentication-key", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
private val config = ZLayer.succeed(Config.default.copy(simulateRemotePods = true))
private def grpcConfigLayer(clientAuthKey: String): ULayer[GrpcConfig] =
ZLayer.succeed(
GrpcConfig.default.copy(
clientInterceptors = Seq(
ZClientInterceptor.headersUpdater((_, _, md) => md.put(authKey, clientAuthKey).unit)
),
serverInterceptors = Seq(
ZTransform { requestContext =>
for {
authenticated <- requestContext.metadata.get(authKey).map(_.contains(validAuthenticationKey))
_ <- ZIO.when(!authenticated)(ZIO.fail(Status.UNAUTHENTICATED.asException))
} yield requestContext
}
)
)
)
def spec: Spec[TestEnvironment with Scope, Any] =
suite("GrpcAuthExampleSpec")(
test("auth example for gRPC") {
val podAddress = PodAddress("localhost", 54321)
ZIO.scoped {
for {
_ <- Sharding.registerScoped
podsClient <- ZIO.service[Pods]
invalidPodsClient <- ZIO
.service[Pods]
.provide(
grpcConfigLayer("invalid"),
GrpcPods.live
)
validRequest <- podsClient.ping(podAddress).exit
invalidRequest <- invalidPodsClient.ping(podAddress).exit
} yield assertTrue(validRequest.isSuccess, invalidRequest.isFailure)
}
}
).provide(
ShardManagerClient.local,
Storage.memory,
config,
grpcConfigLayer(validAuthenticationKey),
Sharding.live,
KryoSerialization.live,
GrpcPods.live,
GrpcShardingService.live
)
}