X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=sources%2Fhv-collector-ssl%2Fsrc%2Fmain%2Fkotlin%2Forg%2Fonap%2Fdcae%2Fcollectors%2Fveshv%2Fssl%2Fboundary%2Futils.kt;fp=sources%2Fhv-collector-ssl%2Fsrc%2Fmain%2Fkotlin%2Forg%2Fonap%2Fdcae%2Fcollectors%2Fveshv%2Fssl%2Fboundary%2Futils.kt;h=fb14263996ac46bac9c601d967d5adf40d080cfa;hb=82b27ff5bccc925fe03d05f259cf881fafc8a1ce;hp=d3640c877f9b397da24db61dd7be21fcd593bb7c;hpb=dc47bd1847a46fe0ad0ca6c10a4d61f829f4c0c6;p=dcaegen2%2Fcollectors%2Fhv-ves.git diff --git a/sources/hv-collector-ssl/src/main/kotlin/org/onap/dcae/collectors/veshv/ssl/boundary/utils.kt b/sources/hv-collector-ssl/src/main/kotlin/org/onap/dcae/collectors/veshv/ssl/boundary/utils.kt index d3640c87..fb142639 100644 --- a/sources/hv-collector-ssl/src/main/kotlin/org/onap/dcae/collectors/veshv/ssl/boundary/utils.kt +++ b/sources/hv-collector-ssl/src/main/kotlin/org/onap/dcae/collectors/veshv/ssl/boundary/utils.kt @@ -20,60 +20,49 @@ package org.onap.dcae.collectors.veshv.ssl.boundary import arrow.core.None -import arrow.core.Option import arrow.core.Some -import arrow.core.fix -import arrow.instances.option.monad.monad -import arrow.typeclasses.binding +import arrow.core.Try +import arrow.core.getOrElse import org.apache.commons.cli.CommandLine -import org.onap.dcae.collectors.veshv.domain.JdkKeys import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption import org.onap.dcae.collectors.veshv.utils.commandline.hasOption import org.onap.dcae.collectors.veshv.utils.commandline.stringValue -import java.io.File +import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeys +import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeysStore +import org.onap.dcaegen2.services.sdk.security.ssl.Passwords +import java.nio.file.Paths /** * @author Piotr Jaszczyk * @since September 2018 */ - const val KEY_STORE_FILE = "/etc/ves-hv/server.p12" const val TRUST_STORE_FILE = "/etc/ves-hv/trust.p12" -fun createSecurityConfiguration(cmdLine: CommandLine): Option { - val sslDisable = cmdLine.hasOption(CommandLineOption.SSL_DISABLE) - - return if (sslDisable) disabledSecurityConfiguration(sslDisable) else enabledSecurityConfiguration(cmdLine) -} +fun createSecurityConfiguration(cmdLine: CommandLine): Try = + if (cmdLine.hasOption(CommandLineOption.SSL_DISABLE)) + Try { disabledSecurityConfiguration() } + else + enabledSecurityConfiguration(cmdLine) -private fun disabledSecurityConfiguration(sslDisable: Boolean): Some { - return Some(SecurityConfiguration( - sslDisable = sslDisable, - keys = None - )) -} +private fun disabledSecurityConfiguration() = SecurityConfiguration(keys = None) -private fun enabledSecurityConfiguration(cmdLine: CommandLine): Option { - return Option.monad().binding { - val ksFile = cmdLine.stringValue(CommandLineOption.KEY_STORE_FILE, KEY_STORE_FILE) - val ksPass = cmdLine.stringValue(CommandLineOption.KEY_STORE_PASSWORD).bind() - val tsFile = cmdLine.stringValue(CommandLineOption.TRUST_STORE_FILE, TRUST_STORE_FILE) - val tsPass = cmdLine.stringValue(CommandLineOption.TRUST_STORE_PASSWORD).bind() +private fun enabledSecurityConfiguration(cmdLine: CommandLine) = Try { + val ksFile = cmdLine.stringValue(CommandLineOption.KEY_STORE_FILE, KEY_STORE_FILE) + val ksPass = cmdLine.stringValue(CommandLineOption.KEY_STORE_PASSWORD).getOrElse { "" } + val tsFile = cmdLine.stringValue(CommandLineOption.TRUST_STORE_FILE, TRUST_STORE_FILE) + val tsPass = cmdLine.stringValue(CommandLineOption.TRUST_STORE_PASSWORD).getOrElse { "" } - val keys = JdkKeys( - keyStore = streamFromFile(ksFile), - keyStorePassword = ksPass.toCharArray(), - trustStore = streamFromFile(tsFile), - trustStorePassword = tsPass.toCharArray() - ) + val keys = ImmutableSecurityKeys.builder() + .keyStore(ImmutableSecurityKeysStore.of(pathFromFile(ksFile))) + .keyStorePassword(Passwords.fromString(ksPass)) + .trustStore(ImmutableSecurityKeysStore.of(pathFromFile(tsFile))) + .trustStorePassword(Passwords.fromString(tsPass)) + .build() - SecurityConfiguration( - sslDisable = false, - keys = Some(keys) - ) - }.fix() + SecurityConfiguration(keys = Some(keys)) } -private fun streamFromFile(file: String) = { File(file).inputStream() } +private fun pathFromFile(file: String) = Paths.get(file)