Skip to content

Commit

Permalink
common config chnages for selsigned certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
mineme0110 committed Jun 12, 2019
1 parent da426af commit 5ab5933
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/io/iohk/scalanet/peergroup/TLSPeerGroup.scala
Expand Up @@ -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)

Expand Down
16 changes: 12 additions & 4 deletions src/io/iohk/scalanet/test/NetUtils.scala
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions src/io/iohk/scalanet/test/peergroup/DTLSPeerGroupSpec.scala
Expand Up @@ -107,8 +107,6 @@ object DTLSPeerGroupSpec {
}
}



def dtlsPeerGroup[M](
config: Config
)(implicit codec: Codec[M], bufferInstantiator: BufferInstantiator[ByteBuffer]): DTLSPeerGroup[M] = {
Expand Down
23 changes: 11 additions & 12 deletions src/io/iohk/scalanet/test/peergroup/TLSPeerGroupSpec.scala
Expand Up @@ -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._
Expand All @@ -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"))
Expand All @@ -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
Expand All @@ -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]
}
Expand Down

0 comments on commit 5ab5933

Please sign in to comment.