fc73d43f91d8aa79d08f4b5573e83a4ef6f6dcfc
[ccsdk/cds.git] /
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
28 class TLSAuthGrpcServerService(private val tlsAuthGrpcServerProperties: TLSAuthGrpcServerProperties)
29     : BluePrintGrpcServerService {
30
31     override fun serverBuilder(): NettyServerBuilder {
32         return NettyServerBuilder
33                 .forPort(tlsAuthGrpcServerProperties.port)
34                 .sslContext(sslContext())
35     }
36
37     fun sslContext(): SslContext {
38         val sslClientContextBuilder = SslContextBuilder
39                 .forServer(normalizedFile(tlsAuthGrpcServerProperties.certChain),
40                         normalizedFile(tlsAuthGrpcServerProperties.privateKey))
41
42         tlsAuthGrpcServerProperties.trustCertCollection?.let { trustCertFile ->
43             sslClientContextBuilder.trustManager(normalizedFile(trustCertFile))
44             sslClientContextBuilder.clientAuth(ClientAuth.REQUIRE)
45         }
46         return GrpcSslContexts.configure(sslClientContextBuilder).build()
47     }
48
49 }