Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / grpc-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / grpc / service / TLSAuthGrpcServerService.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.service
18
19 import io.grpc.netty.GrpcSslContexts
20 import io.grpc.netty.NettyServerBuilder
21 import io.netty.handler.ssl.ClientAuth
22 import io.netty.handler.ssl.SslContext
23 import io.netty.handler.ssl.SslContextBuilder
24 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TLSAuthGrpcServerProperties
25 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
26
27 class TLSAuthGrpcServerService(private val tlsAuthGrpcServerProperties: TLSAuthGrpcServerProperties) :
28     BlueprintGrpcServerService {
29
30     override fun serverBuilder(): NettyServerBuilder {
31         return NettyServerBuilder
32             .forPort(tlsAuthGrpcServerProperties.port)
33             .sslContext(sslContext())
34     }
35
36     fun sslContext(): SslContext {
37         val sslClientContextBuilder = SslContextBuilder
38             .forServer(
39                 normalizedFile(tlsAuthGrpcServerProperties.certChain),
40                 normalizedFile(tlsAuthGrpcServerProperties.privateKey)
41             )
42
43         tlsAuthGrpcServerProperties.trustCertCollection?.let { trustCertFile ->
44             sslClientContextBuilder.trustManager(normalizedFile(trustCertFile))
45             sslClientContextBuilder.clientAuth(ClientAuth.REQUIRE)
46         }
47         return GrpcSslContexts.configure(sslClientContextBuilder).build()
48     }
49 }