Skip to content

Commit

Permalink
modified Store DSL, type is part of constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon committed May 13, 2018
1 parent 4213f1d commit 5260177
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 46 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 0 additions & 8 deletions src/main/kotlin/de/swirtz/sekurity/api/SocketCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,3 @@ fun socketFactory(protocols: List<String> = defaultTLSProtocols, configuration:
with(TLSSocketFactoryProvider(configuration)) {
this.createSocketFactory(protocols)
}

fun main(args: Array<String>) {
var charAr = Array(5) { ' ' }
for (i in 0..4) {
println("Please Enter the character Number ${i + 1}")
charAr[i] = readLine()?.get(0) ?: throw IllegalArgumentException()
}
}
12 changes: 4 additions & 8 deletions src/main/kotlin/de/swirtz/sekurity/core/TLSSocketFactoryDSL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,16 @@ annotation class TlsDSLMarker
data class SocketConfiguration(var cipherSuites: List<String>? = 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
}

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import de.swirtz.sekurity.api.socketFactory

fun main(args: Array<String>) {

"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
}
}
Expand Down
27 changes: 16 additions & 11 deletions src/main/kotlin/de/swirtz/sekurity/samples/TLSServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand All @@ -44,6 +48,7 @@ class TLSServer(private val port: Int, private val socketFactory: ServerSocketFa
fun stop() {
running.set(false)
socket.close()
read = StringBuilder()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 7 additions & 11 deletions src/test/kotlin/de/swirtz/sekurity/TlsLibraryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5260177

Please sign in to comment.