title: Akka https ssl localhost testing
body: <p>I am trying to setup akka https locally, in a way that certificate does not need to be installed on every server ( I want that it can be run on jenkins build, or other machines ).</p>
  <p>This way I am creating certs:</p>
  <pre><code>openssl genrsa -des3 -out server.key 2048
  openssl rsa -in server.key -out server.key
  openssl req -sha256 -new -key server.key -out server.csr -subj '/CN=localhost'
  openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  cat server.crt server.key &gt; cert.pem
  openssl pkcs12 -export -in cert.pem -out mykeystore.pkcs12  -name akka-http-test -noiter -nomaciter
  </code></pre>
  <p>( tried also using mkcert tool )</p>
  <p>Now scala code:</p>
  <pre><code>def createHttpsContext(): HttpsConnectionContext = {
      val password: Array[Char] = &quot;changeit&quot;.toCharArray
      val ks: KeyStore = KeyStore.getInstance(&quot;PKCS12&quot;)
      val keystore: InputStream = {
        getClass.getClassLoader.getResourceAsStream(&quot;mykeystore.pkcs12&quot;)
      }
  
      require(keystore != null, &quot;Keystore required!&quot;)
      ks.load(keystore, password)
  
      val keyManagerFactory: KeyManagerFactory = KeyManagerFactory.getInstance(&quot;SunX509&quot;)
      keyManagerFactory.init(ks, password)
  
      val tmf: TrustManagerFactory = TrustManagerFactory.getInstance(&quot;SunX509&quot;)
      tmf.init(ks)
  
      val sslContext: SSLContext = SSLContext.getInstance(&quot;TLS&quot;)
      sslContext.init(keyManagerFactory.getKeyManagers, tmf.getTrustManagers, new SecureRandom)
      val https: HttpsConnectionContext = akka.http.scaladsl.ConnectionContext.httpsServer(sslContext)
      https
    }
  
  def createHttpsApiServer()(implicit system: ActorSystem) = {
      val routes: Route = get { complete(&quot;Hello world!&quot;) }
      val httpsContext = createHttpsContext()
      Http().newServerAt(&quot;127.0.0.1&quot;, 2001).enableHttps(httpsContext).bind(routes)
  }
  </code></pre>
  <p>and test code:</p>
  <pre><code>private def runStopServerHttpsRequest()(implicit system: ActorSystem): Unit = {
      val trustfulSslContext: HttpsConnectionContext = {
        object NoCheckX509TrustManager extends X509TrustManager {
          override def checkClientTrusted(chain: Array[X509Certificate], authType: String): Unit = ()
          override def checkServerTrusted(chain: Array[X509Certificate], authType: String): Unit = ()
          override def getAcceptedIssuers: Array[X509Certificate] = Array[X509Certificate]()
        }
        val context = SSLContext.getInstance(&quot;TLS&quot;)
        context.init(Array[KeyManager](), Array(NoCheckX509TrustManager), null)
        context
        ConnectionContext.httpsClient(context)
      }
  
      val request = HttpRequest(HttpMethods.GET, &quot;https://127.0.0.1:2001/&quot;)
      val connectionFlow = Http().outgoingConnectionHttps(&quot;127.0.0.1&quot;, 2001, connectionContext = trustfulSslContext)
  
      Source.single(request).via(connectionFlow).runWith(Sink.head).map(x =&gt; (x.status, x.entity)).futureValue
    }
  </code></pre>
  <p>But I got errors:</p>
  <pre><code>The future returned an exception of type: javax.net.ssl.SSLHandshakeException, with message: General SSLEngine problem.
  ScalaTestFailureLocation: me.archdev.restapi.HttpSpec at (HttpSpec.scala:38)
  org.scalatest.exceptions.TestFailedException: The future returned an exception of type: javax.net.ssl.SSLHandshakeException, with message: General SSLEngine problem.
  </code></pre>
  <p>Any idea how to solve this ? thanks!</p>
  <p>EDIT:
  Ive tried also adding config:</p>
  <pre><code>ssl-config {
    trustManager = {
        stores = [
          { type = &quot;PKCS12&quot;, path = &quot;src/main/resources/mykeystore.pkcs12&quot;, password = &quot;changeit&quot; }
        ]
    }
  }
  </code></pre>
  <p>with code change a bit:</p>
  <pre><code>l badSslConfig = AkkaSSLConfig().mapSettings(s =&gt; s.withLoose(s.loose
        .withAcceptAnyCertificate(true)
        .withDisableHostnameVerification(true)
      ))
  
      val badCtx = Http().createClientHttpsContext(badSslConfig)
      val connectionFlow = Http().outgoingConnectionHttps(&quot;127.0.0.1&quot;, 2001, connectionContext = badCtx)
  </code></pre>
  <p>but then I have an error:</p>
  <blockquote>
  <p>the trustAnchors parameter must be non-empty
  java.security.InvalidAlgorithmParameterException: the trustAnchors
  parameter must be non-empty</p>
  </blockquote>
  <p>Tried also this code: <a href="https://gist.github.com/iRevive/4a3c7cb96374da5da80d4538f3da17cb" rel="nofollow noreferrer">https://gist.github.com/iRevive/4a3c7cb96374da5da80d4538f3da17cb</a></p>
  <p>but then:</p>
  <pre><code>[ERROR] [09/10/2020 08:51:38.088] [default-akka.actor.default-dispatcher-5] [akka://default/system/Materializers/StreamSupervisor-0/TLS-for-flow-3-1] the trustAnchors parameter must be non-empty
  akka.actor.ActorInitializationException: akka://default/system/Materializers/StreamSupervisor-0/TLS-for-flow-3-1: exception during creation
  </code></pre>
  <p>again the same..</p>