From 52601777837bf205fee8a45e5d4e05e8420972e6 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 13 May 2018 23:39:52 +0200 Subject: [PATCH] modified Store DSL, type is part of constructor --- build.gradle.kts | 4 +-- .../de/swirtz/sekurity/api/SocketCreator.kt | 8 ------ .../sekurity/core/TLSSocketFactoryDSL.kt | 12 +++------ .../sekurity/samples/SocketFactorySample.kt | 13 +++++---- .../de/swirtz/sekurity/samples/TLSServer.kt | 27 +++++++++++-------- .../sekurity/HttpsClientConnectionTest.kt | 2 +- .../de/swirtz/sekurity/TlsLibraryTest.kt | 18 +++++-------- 7 files changed, 38 insertions(+), 46 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index d50a84d..a0f8614 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,11 +11,11 @@ val kotlinVersion = plugins.getPlugin(KotlinPluginWrapper::class.java).kotlinPlu val kotlinxCoroutinesVersion = "0.22.2" project.group = "de.swirtz" -project.version = "0.0.2" +project.version = "0.0.3" val artifactID = "sekurity" plugins { - kotlin("jvm") version "1.2.40" + kotlin("jvm") version "1.2.41" `maven-publish` id("com.jfrog.bintray") version "1.8.0" id("com.github.johnrengelman.shadow") version "2.0.2" diff --git a/src/main/kotlin/de/swirtz/sekurity/api/SocketCreator.kt b/src/main/kotlin/de/swirtz/sekurity/api/SocketCreator.kt index 4cc5643..559d763 100755 --- a/src/main/kotlin/de/swirtz/sekurity/api/SocketCreator.kt +++ b/src/main/kotlin/de/swirtz/sekurity/api/SocketCreator.kt @@ -21,11 +21,3 @@ fun socketFactory(protocols: List = defaultTLSProtocols, configuration: with(TLSSocketFactoryProvider(configuration)) { this.createSocketFactory(protocols) } - -fun main(args: Array) { - var charAr = Array(5) { ' ' } - for (i in 0..4) { - println("Please Enter the character Number ${i + 1}") - charAr[i] = readLine()?.get(0) ?: throw IllegalArgumentException() - } -} diff --git a/src/main/kotlin/de/swirtz/sekurity/core/TLSSocketFactoryDSL.kt b/src/main/kotlin/de/swirtz/sekurity/core/TLSSocketFactoryDSL.kt index 347ce22..c5df2d8 100755 --- a/src/main/kotlin/de/swirtz/sekurity/core/TLSSocketFactoryDSL.kt +++ b/src/main/kotlin/de/swirtz/sekurity/core/TLSSocketFactoryDSL.kt @@ -76,20 +76,16 @@ annotation class TlsDSLMarker data class SocketConfiguration(var cipherSuites: List? = null, var timeout: Int? = null, var clientAuth: Boolean = false) @TlsDSLMarker -class Store(val name: String) { +class Store(val name: String, val fileType: String = "JKS") { var algorithm: String? = null var password: CharArray? = null - var fileType: String = "JKS" + infix fun withPass(pass: String) = apply { password = pass.toCharArray() } - infix fun ofType(type: String) = apply { - fileType = type - } - - infix fun using(algo: String) = apply { + infix fun algorithm(algo: String) = apply { algorithm = algo } @@ -102,7 +98,7 @@ class ProviderConfiguration { var tmConfig: Store? = null var socketConfig: SocketConfiguration? = null - fun open(name: String) = Store(name) + fun open(name: String, type: String = "JKS") = Store(name, type) fun sockets(configInit: SocketConfiguration.() -> Unit) { this.socketConfig = SocketConfiguration().apply(configInit) diff --git a/src/main/kotlin/de/swirtz/sekurity/samples/SocketFactorySample.kt b/src/main/kotlin/de/swirtz/sekurity/samples/SocketFactorySample.kt index 594ee66..ba1444a 100755 --- a/src/main/kotlin/de/swirtz/sekurity/samples/SocketFactorySample.kt +++ b/src/main/kotlin/de/swirtz/sekurity/samples/SocketFactorySample.kt @@ -5,17 +5,20 @@ import de.swirtz.sekurity.api.socketFactory fun main(args: Array) { - "Hello World".groupingBy { it }.eachCount() val fac = socketFactory { keyManager { - open("certsandstores/clientkeystore") withPass "123456" ofType "jks" + open("certsandstores/clientkeystore", "jks") withPass "123456" } trustManager { - open("certsandstores/myTruststore") withPass "123456" ofType "jks" + open("certsandstores/myTruststore", "jks") withPass "123456" } sockets { - cipherSuites = listOf("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", - "TLS_DHE_RSA_WITH_AES_128_CBC_SHA", "TLS_DHE_RSA_WITH_AES_256_CBC_SHA") + cipherSuites = listOf( + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_DHE_RSA_WITH_AES_128_CBC_SHA", + "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" + ) timeout = 10_000 } } diff --git a/src/main/kotlin/de/swirtz/sekurity/samples/TLSServer.kt b/src/main/kotlin/de/swirtz/sekurity/samples/TLSServer.kt index 5e063f3..c33e2db 100755 --- a/src/main/kotlin/de/swirtz/sekurity/samples/TLSServer.kt +++ b/src/main/kotlin/de/swirtz/sekurity/samples/TLSServer.kt @@ -25,17 +25,21 @@ class TLSServer(private val port: Int, private val socketFactory: ServerSocketFa LOG.debug("started server on $port") launch { while (running.get()) { - LOG.debug("wait for client to connect") - with(socket.accept()) { - LOG.debug("accepted socket $this") - DataInputStream(getInputStream()).use { d -> - var readUTF = d.readUTF() - while (readUTF != null) { - LOG.debug("Read: '$readUTF'") - read.append(readUTF) - readUTF = d.readUTF() - } - } + waitForClientAndRead() + } + } + } + + private fun waitForClientAndRead() { + LOG.debug("wait for client to connect") + with(socket.accept()) { + LOG.debug("accepted socket $this") + DataInputStream(getInputStream()).use { d -> + var readUTF = d.readUTF() + while (readUTF != null) { + LOG.debug("Read: '$readUTF'") + read.append(readUTF) + readUTF = d.readUTF() } } } @@ -44,6 +48,7 @@ class TLSServer(private val port: Int, private val socketFactory: ServerSocketFa fun stop() { running.set(false) socket.close() + read = StringBuilder() } diff --git a/src/test/kotlin/de/swirtz/sekurity/HttpsClientConnectionTest.kt b/src/test/kotlin/de/swirtz/sekurity/HttpsClientConnectionTest.kt index a49616c..52e8222 100755 --- a/src/test/kotlin/de/swirtz/sekurity/HttpsClientConnectionTest.kt +++ b/src/test/kotlin/de/swirtz/sekurity/HttpsClientConnectionTest.kt @@ -54,7 +54,7 @@ class HttpsClientConnectionTest { fun testCreateConnection() { val sf = socketFactory { trustManager { - open("src/test/resources/myTrustStore") withPass "123456" ofType "jks" + open("src/test/resources/myTrustStore", "jks") withPass "123456" } sockets { timeout = 10_000 diff --git a/src/test/kotlin/de/swirtz/sekurity/TlsLibraryTest.kt b/src/test/kotlin/de/swirtz/sekurity/TlsLibraryTest.kt index 9d679c2..162cc67 100755 --- a/src/test/kotlin/de/swirtz/sekurity/TlsLibraryTest.kt +++ b/src/test/kotlin/de/swirtz/sekurity/TlsLibraryTest.kt @@ -7,7 +7,6 @@ import org.junit.After import org.junit.Before import org.junit.Test import java.io.DataOutputStream -import javax.net.ssl.SSLSocketFactory import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -35,22 +34,19 @@ class TlsLibraryTest { assertTrue { socket.isClosed } } - private fun createClientSocketFactory(): SSLSocketFactory { - val fac = socketFactory { - trustManager { - open("src/test/resources/myTrustStore") withPass "123456" ofType "jks" - } - sockets { - timeout = 10_000 - } + private fun createClientSocketFactory() = socketFactory { + trustManager { + open("src/test/resources/myTrustStore") withPass "123456" + } + sockets { + timeout = 10_000 } - return fac } private fun startServer() { val fac = serverSocketFactory { keyManager { - open("src/test/resources/clientkeystore") withPass "123456" ofType "jks" + open("src/test/resources/clientkeystore") withPass "123456" } sockets { clientAuth = true