spray-client ssl helloworld

93 views
Skip to first unread message

ldeck

unread,
Oct 11, 2015, 5:56:56 AM10/11/15
to spray.io User List

Hi all,


I'm just getting started with spray-client and am failing to get past the [SSLHandshakeException]. 

Forgive me if this has been answered already. I've tried a few stackoverflow suggestions [1] without success.


I've defined a trustful ssl context (for now):


trait Https {
 
implicit val sslContext = {
    val permissiveTrustManager
: TrustManager = new X509TrustManager() {
     
override def getAcceptedIssuers: Array[X509Certificate] = Array()
     
override def checkClientTrusted(x509Certificates: Array[X509Certificate], s: String): Unit = Array()
     
override def checkServerTrusted(x509Certificates: Array[X509Certificate], s: String): Unit = Array()
   
}

    val initTrustManagers
= Array(permissiveTrustManager)

    val ctx
= SSLContext.getInstance("TLS")
    ctx
.init(null, initTrustManagers, new SecureRandom())
    ctx
 
}
}



I'm looking to extend the spray-client example (but connecting to a json service running at https://localhost:18443/....)

http://spray.io/documentation/1.2.3/spray-client/#usage


I'm not quite sure how to incorporate the following:

http://spray.io/documentation/1.2.2/spray-can/http-client/#ssl-support


So I have:

case class HealthStatus(...)
...
object Health extends App with Https {

 val HEALTH_URL
= "https://localhost:18443/.../health"

 
implicit val system = ActorSystem("health-spray-client")
 
import system.dispatcher // execution context for futures below
 val log
= Logging(system, getClass)

 
import MyJsonProtocol._
 
import SprayJsonSupport._

 val pipeline
: HttpRequest => Future[HealthStatus] = (
 addCredentials
(BasicHttpCredentials("foo", "bar"))
 
~> encode(Gzip)
 
~> sendReceive
 
~> decode(Deflate)
 
~> unmarshal[HealthStatus]
 
)

 val responseFuture
= pipeline {
 
Get(HEALTH_URL)
 
}
 responseFuture onComplete
{
 
case Success(HealthStatus(_, status)) =>
 log
.info(s"woot! Status is $status")
 shutdown
()

 
case Failure(error) =>
   log
.error(error, "Couldn't resolve health")
   shutdown
()
 
}

 
def shutdown(): Unit = {
 IO
(Http).ask(Http.CloseAll)(1.second).await
 system
.shutdown()
 
}
}




What extra steps are needed for this to work?


Thanks!



[1] I've tried a few different stack overflow suggestions

http://stackoverflow.com/questions/28995658/spray-client-sendreceive-throws-sslhandshakeexception

http://stackoverflow.com/questions/28396173/how-to-define-sslcontext-with-spray-https-client


scala.version = 2.11.7

spray.version 1.3.3

dependencies:

- spray-can

- spray-client

- spray-json

- spray-http

- spray-httpx

- spray-util

- spray-routing

- akka-actor

- etc

Ashish Gupta

unread,
Apr 29, 2016, 2:52:49 AM4/29/16
to spray.io User List
Did you get any response to your query, even I am facing the same issue? 
Reply all
Reply to author
Forward
0 new messages