Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-ssl / src / main / kotlin / org / onap / dcae / collectors / veshv / ssl / boundary / utils.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.ssl.boundary
21
22 import arrow.core.None
23 import arrow.core.Option
24 import arrow.core.Some
25 import arrow.core.fix
26 import arrow.instances.option.monad.monad
27 import arrow.typeclasses.binding
28 import org.apache.commons.cli.CommandLine
29 import org.onap.dcae.collectors.veshv.domain.JdkKeys
30 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
31 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption
32 import org.onap.dcae.collectors.veshv.utils.commandline.hasOption
33 import org.onap.dcae.collectors.veshv.utils.commandline.stringValue
34 import java.io.File
35
36 /**
37  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
38  * @since September 2018
39  */
40
41
42 const val KEY_STORE_FILE = "/etc/ves-hv/server.p12"
43 const val TRUST_STORE_FILE = "/etc/ves-hv/trust.p12"
44
45 fun createSecurityConfiguration(cmdLine: CommandLine): Option<SecurityConfiguration> {
46     val sslDisable = cmdLine.hasOption(CommandLineOption.SSL_DISABLE)
47
48     return if (sslDisable) disabledSecurityConfiguration(sslDisable) else enabledSecurityConfiguration(cmdLine)
49 }
50
51 private fun disabledSecurityConfiguration(sslDisable: Boolean): Some<SecurityConfiguration> {
52     return Some(SecurityConfiguration(
53             sslDisable = sslDisable,
54             keys = None
55     ))
56 }
57
58 private fun enabledSecurityConfiguration(cmdLine: CommandLine): Option<SecurityConfiguration> {
59     return Option.monad().binding {
60         val ksFile = cmdLine.stringValue(CommandLineOption.KEY_STORE_FILE, KEY_STORE_FILE)
61         val ksPass = cmdLine.stringValue(CommandLineOption.KEY_STORE_PASSWORD).bind()
62         val tsFile = cmdLine.stringValue(CommandLineOption.TRUST_STORE_FILE, TRUST_STORE_FILE)
63         val tsPass = cmdLine.stringValue(CommandLineOption.TRUST_STORE_PASSWORD).bind()
64
65         val keys = JdkKeys(
66                 keyStore = streamFromFile(ksFile),
67                 keyStorePassword = ksPass.toCharArray(),
68                 trustStore = streamFromFile(tsFile),
69                 trustStorePassword = tsPass.toCharArray()
70         )
71
72         SecurityConfiguration(
73                 sslDisable = false,
74                 keys = Some(keys)
75         )
76     }.fix()
77 }
78
79 private fun streamFromFile(file: String) = { File(file).inputStream() }