fb14263996ac46bac9c601d967d5adf40d080cfa
[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.Some
24 import arrow.core.Try
25 import arrow.core.getOrElse
26 import org.apache.commons.cli.CommandLine
27 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
28 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption
29 import org.onap.dcae.collectors.veshv.utils.commandline.hasOption
30 import org.onap.dcae.collectors.veshv.utils.commandline.stringValue
31 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeys
32 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeysStore
33 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords
34 import java.nio.file.Paths
35
36 /**
37  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
38  * @since September 2018
39  */
40
41 const val KEY_STORE_FILE = "/etc/ves-hv/server.p12"
42 const val TRUST_STORE_FILE = "/etc/ves-hv/trust.p12"
43
44 fun createSecurityConfiguration(cmdLine: CommandLine): Try<SecurityConfiguration> =
45         if (cmdLine.hasOption(CommandLineOption.SSL_DISABLE))
46             Try { disabledSecurityConfiguration() }
47         else
48             enabledSecurityConfiguration(cmdLine)
49
50 private fun disabledSecurityConfiguration() = SecurityConfiguration(keys = None)
51
52 private fun enabledSecurityConfiguration(cmdLine: CommandLine) = Try {
53     val ksFile = cmdLine.stringValue(CommandLineOption.KEY_STORE_FILE, KEY_STORE_FILE)
54     val ksPass = cmdLine.stringValue(CommandLineOption.KEY_STORE_PASSWORD).getOrElse { "" }
55     val tsFile = cmdLine.stringValue(CommandLineOption.TRUST_STORE_FILE, TRUST_STORE_FILE)
56     val tsPass = cmdLine.stringValue(CommandLineOption.TRUST_STORE_PASSWORD).getOrElse { "" }
57
58     val keys = ImmutableSecurityKeys.builder()
59             .keyStore(ImmutableSecurityKeysStore.of(pathFromFile(ksFile)))
60             .keyStorePassword(Passwords.fromString(ksPass))
61             .trustStore(ImmutableSecurityKeysStore.of(pathFromFile(tsFile)))
62             .trustStorePassword(Passwords.fromString(tsPass))
63             .build()
64
65     SecurityConfiguration(keys = Some(keys))
66 }
67
68 private fun pathFromFile(file: String) = Paths.get(file)