Skip to content
Permalink
Browse files

common config chnages for selsigned certificate

  • Loading branch information...
shaileshp0110 committed Jun 12, 2019
1 parent da426af commit 5ab5933ca47cfc69127b1c0904f0bfc5baab67d6
@@ -39,7 +39,8 @@ import scala.collection.JavaConverters._
* @param codec a decco codec for reading writing messages to NIO ByteBuffer.
* @tparam M the message type.
*/
class TLSPeerGroup[M](val config: Config)(implicit codec: Codec[M],bufferInstantiator: BufferInstantiator[ByteBuffer]) extends TerminalPeerGroup[InetMultiAddress, M]() {
class TLSPeerGroup[M](val config: Config)(implicit codec: Codec[M], bufferInstantiator: BufferInstantiator[ByteBuffer])
extends TerminalPeerGroup[InetMultiAddress, M]() {

private val log = LoggerFactory.getLogger(getClass)

@@ -115,7 +115,11 @@ object NetUtils {
Await.result(pg.initialize().runAsync, 10 seconds)
pg
}
def randomTLSPeerGroup[M](implicit scheduler: Scheduler, codec: Codec[M],bufferInstantiator: BufferInstantiator[ByteBuffer]): TLSPeerGroup[M] = {
def randomTLSPeerGroup[M](
implicit scheduler: Scheduler,
codec: Codec[M],
bufferInstantiator: BufferInstantiator[ByteBuffer]
): TLSPeerGroup[M] = {
val sc1 = new SelfSignedCertificate()
val pg = new TLSPeerGroup(TLSPeerGroup.Config(aRandomAddress(), sc1.key(), List(sc1.cert()), Nil))
Await.result(pg.initialize().runAsync, 10 seconds)
@@ -146,8 +150,8 @@ object NetUtils {

def withTwoRandomTLSPeerGroups[M](clientAuth: Boolean = false)(
testCode: (TLSPeerGroup[M], TLSPeerGroup[M]) => Any
)(implicit scheduler: Scheduler, codec: Codec[M],bufferInstantiator: BufferInstantiator[ByteBuffer]): Unit = {
val (pg1, pg2) = random2TLSPPeerGroup(clientAuth)(scheduler, codec,bufferInstantiator)
)(implicit scheduler: Scheduler, codec: Codec[M], bufferInstantiator: BufferInstantiator[ByteBuffer]): Unit = {
val (pg1, pg2) = random2TLSPPeerGroup(clientAuth)(scheduler, codec, bufferInstantiator)
try {
testCode(pg1, pg2)
} finally {
@@ -158,7 +162,11 @@ object NetUtils {

def random2TLSPPeerGroup[M](
clientAuth: Boolean
)(implicit scheduler: Scheduler, codec: Codec[M],bufferInstantiator: BufferInstantiator[ByteBuffer]): (TLSPeerGroup[M], TLSPeerGroup[M]) = {
)(
implicit scheduler: Scheduler,
codec: Codec[M],
bufferInstantiator: BufferInstantiator[ByteBuffer]
): (TLSPeerGroup[M], TLSPeerGroup[M]) = {
val address1 = aRandomAddress()
val address2 = aRandomAddress()
val sc1 = new SelfSignedCertificate()
@@ -107,8 +107,6 @@ object DTLSPeerGroupSpec {
}
}



def dtlsPeerGroup[M](
config: Config
)(implicit codec: Codec[M], bufferInstantiator: BufferInstantiator[ByteBuffer]): DTLSPeerGroup[M] = {
@@ -88,10 +88,9 @@ class TLSPeerGroupSpec extends FlatSpec with BeforeAndAfterAll {
secondInbound.futureValue.to shouldBe alice.processAddress
}


}

object TLSPeerGroupSpec{
object TLSPeerGroupSpec {

def signedCertConfig(alias: String): Config = {
import scala.collection.JavaConverters._
@@ -101,21 +100,20 @@ object TLSPeerGroupSpec{
.asScala
.toList

Config(aRandomAddress(),keyAt(alias),certChain, NetUtils.trustedCerts.toList)
Config(aRandomAddress(), keyAt(alias), certChain, NetUtils.trustedCerts.toList)
}

def selfSignedCertConfig(alias: String): Config = {

val key = keyStore.getKey(alias,"password".toCharArray).asInstanceOf[PrivateKey]
val key = keyStore.getKey(alias, "password".toCharArray).asInstanceOf[PrivateKey]
val certChain = keyStore.getCertificateChain(alias).toList
val trustStore = List(keyStore.getCertificate("bob"))
Config(aRandomAddress(),key ,certChain, trustStore)
val trustStore = List(keyStore.getCertificate("bob"))
Config(aRandomAddress(), key, certChain, trustStore)

}


def withTwoTLSPeerGroups[M](cgens: (String => Config)*)(
testCode: (TLSPeerGroup[M], TLSPeerGroup[M]) => Any
testCode: (TLSPeerGroup[M], TLSPeerGroup[M]) => Any
)(implicit codec: Codec[M], bufferInstantiator: BufferInstantiator[ByteBuffer]): Unit = cgens.foreach { cgen =>
val pg1 = tlsPeerGroup[M](cgen("alice"))
val pg2 = tlsPeerGroup[M](cgen("bob"))
@@ -129,7 +127,9 @@ object TLSPeerGroupSpec{
}
}

def tlsPeerGroup[M](config: Config)(implicit codec: Codec[M], bufferInstantiator: BufferInstantiator[ByteBuffer]): TLSPeerGroup[M] = {
def tlsPeerGroup[M](
config: Config
)(implicit codec: Codec[M], bufferInstantiator: BufferInstantiator[ByteBuffer]): TLSPeerGroup[M] = {
val pg = new TLSPeerGroup[M](config)
Await.result(pg.initialize().runAsync, Duration.Inf)
pg
@@ -142,15 +142,14 @@ object TLSPeerGroupSpec{
val bobSc = new SelfSignedCertificate()

keyStore.setCertificateEntry("alice", bobSc.cert())
keyStore.setKeyEntry("alice",aliceSc.key(),"password".toCharArray,Array(aliceSc.cert()))
keyStore.setKeyEntry("alice", aliceSc.key(), "password".toCharArray, Array(aliceSc.cert()))

keyStore.setCertificateEntry("bob", aliceSc.cert())
keyStore.setKeyEntry("bob",bobSc.key(),"password".toCharArray,Array(bobSc.cert()))
keyStore.setKeyEntry("bob", bobSc.key(), "password".toCharArray, Array(bobSc.cert()))

keyStore
}


def keyAt(alias: String): PrivateKey = {
NetUtils.keyStore.getKey(alias, "password".toCharArray).asInstanceOf[PrivateKey]
}

0 comments on commit 5ab5933

Please sign in to comment.
You can’t perform that action at this time.