7678fdbbd9347077e12bc6e3ecae7c312e746d78
[dcaegen2/collectors/hv-ves.git] /
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.simulators.xnf.impl.config
21
22
23 import arrow.core.None
24 import arrow.core.Some
25 import arrow.core.Try
26 import org.apache.commons.cli.CommandLine
27 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption
28 import org.onap.dcae.collectors.veshv.commandline.hasOption
29 import org.onap.dcae.collectors.veshv.commandline.stringValue
30 import org.onap.dcae.collectors.veshv.ssl.boundary.SecurityConfiguration
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 KEY_STORE_PASSWORD_FILE = "/etc/ves-hv/server.pass"
43 const val TRUST_STORE_FILE = "/etc/ves-hv/trust.p12"
44 const val TRUST_STORE_PASSWORD_FILE = "/etc/ves-hv/trust.pass"
45
46 fun createSecurityConfigurationProvider(cmdLine: CommandLine): Try<() -> SecurityConfiguration> =
47         if (shouldDisableSsl(cmdLine))
48             Try { { disabledSecurityConfiguration() } }
49         else
50             Try { { enabledSecurityConfiguration(cmdLine) } }
51
52 private fun shouldDisableSsl(cmdLine: CommandLine) = cmdLine.hasOption(CommandLineOption.SSL_DISABLE)
53
54 private fun disabledSecurityConfiguration() = SecurityConfiguration(None)
55
56 private fun enabledSecurityConfiguration(cmdLine: CommandLine): SecurityConfiguration {
57     val ksFile = cmdLine.stringValue(CommandLineOption.KEY_STORE_FILE, KEY_STORE_FILE)
58     val ksPass = cmdLine.stringValue(CommandLineOption.KEY_STORE_PASSWORD_FILE, KEY_STORE_PASSWORD_FILE)
59     val tsFile = cmdLine.stringValue(CommandLineOption.TRUST_STORE_FILE, TRUST_STORE_FILE)
60     val tsPass = cmdLine.stringValue(CommandLineOption.TRUST_STORE_PASSWORD_FILE, TRUST_STORE_PASSWORD_FILE)
61
62     val keys = ImmutableSecurityKeys.builder()
63             .keyStore(ImmutableSecurityKeysStore.of(pathFromFile(ksFile)))
64             .keyStorePassword(Passwords.fromPath(pathFromFile(ksPass)))
65             .trustStore(ImmutableSecurityKeysStore.of(pathFromFile(tsFile)))
66             .trustStorePassword(Passwords.fromPath(pathFromFile(tsPass)))
67             .build()
68
69     return SecurityConfiguration(Some(keys))
70 }
71
72 private fun pathFromFile(file: String) = Paths.get(file)