Skip to content

Commit ae87041

Browse files
authored
docs: Use the new akka-http cert loading utils in quickstarts (#1948)
1 parent ed4977d commit ae87041

File tree

2 files changed

+20
-94
lines changed

2 files changed

+20
-94
lines changed

samples/akka-grpc-quickstart-java/src/main/java/com/example/helloworld/GreeterServer.java

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import akka.actor.typed.ActorSystem;
66
import akka.actor.typed.javadsl.Behaviors;
77
import akka.http.javadsl.*;
8+
import akka.http.javadsl.common.SSLContextFactory;
89
import akka.http.javadsl.model.HttpRequest;
910
import akka.http.javadsl.model.HttpResponse;
1011
import akka.japi.function.Function;
@@ -16,6 +17,7 @@
1617
import java.io.ByteArrayOutputStream;
1718
import java.io.IOException;
1819
import java.io.InputStream;
20+
import java.nio.file.Paths;
1921
import java.security.KeyFactory;
2022
import java.security.KeyStore;
2123
import java.security.PrivateKey;
@@ -52,10 +54,16 @@ public CompletionStage<ServerBinding> run() throws Exception {
5254
new GreeterServiceImpl(system),
5355
system);
5456

57+
HttpsConnectionContext httpsConnectionContext = ConnectionContext.httpsServer(SSLContextFactory.createSSLContextFromPem(
58+
// Note: filesystem paths, not classpath
59+
Paths.get("src/main/resources/certs/server1.pem"),
60+
Paths.get("src/main/resources/certs/server1.key")
61+
));
62+
5563
CompletionStage<ServerBinding> bound =
5664
Http.get(system)
5765
.newServerAt("127.0.0.1", 8080)
58-
.enableHttps(serverHttpContext())
66+
.enableHttps(httpsConnectionContext)
5967
.bind(service);
6068

6169
bound.thenAccept(binding ->
@@ -64,52 +72,5 @@ public CompletionStage<ServerBinding> run() throws Exception {
6472

6573
return bound;
6674
}
67-
// #server
68-
69-
70-
// FIXME this will be replaced by a more convenient utility, see https://github.com/akka/akka-grpc/issues/89
71-
private static HttpsConnectionContext serverHttpContext() throws Exception {
72-
String keyEncoded = read(GreeterServer.class.getResourceAsStream("/certs/server1.key"))
73-
.replace("-----BEGIN PRIVATE KEY-----\n", "")
74-
.replace("-----END PRIVATE KEY-----\n", "")
75-
.replace("\n", "");
76-
77-
byte[] decodedKey = Base64.getDecoder().decode(keyEncoded);
78-
79-
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decodedKey);
80-
81-
KeyFactory kf = KeyFactory.getInstance("RSA");
82-
PrivateKey privateKey = kf.generatePrivate(spec);
83-
84-
CertificateFactory fact = CertificateFactory.getInstance("X.509");
85-
Certificate cer =
86-
fact.generateCertificate(GreeterServer.class.getResourceAsStream("/certs/server1.pem"));
87-
88-
KeyStore ks = KeyStore.getInstance("PKCS12");
89-
ks.load(null);
90-
ks.setKeyEntry("private", privateKey, new char[0], new Certificate[]{ cer });
91-
92-
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
93-
keyManagerFactory.init(ks, null);
94-
95-
SSLContext context = SSLContext.getInstance("TLS");
96-
context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
97-
98-
return ConnectionContext.httpsServer(context);
99-
}
100-
101-
private static String read(InputStream in) throws IOException {
102-
ByteArrayOutputStream baos = new ByteArrayOutputStream(Math.max(64, in.available()));
103-
byte[] buffer = new byte[32 * 1024];
104-
105-
int bytesRead = in.read(buffer);
106-
while (bytesRead >= 0) {
107-
baos.write(buffer, 0, bytesRead);
108-
bytesRead = in.read(buffer);
109-
}
110-
111-
return new String(baos.toByteArray(), "UTF-8");
112-
}
113-
//#server
11475
}
11576
//#server

samples/akka-grpc-quickstart-scala/src/main/scala/com/example/helloworld/GreeterServer.scala

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,30 @@ package com.example.helloworld
22

33
//#import
44

5-
6-
import java.security.KeyStore
7-
import java.security.SecureRandom
8-
import java.security.cert.Certificate
9-
import java.security.cert.CertificateFactory
10-
11-
import scala.io.Source
12-
135
import akka.actor.typed.ActorSystem
146
import akka.actor.typed.scaladsl.Behaviors
157
import akka.http.scaladsl.ConnectionContext
168
import akka.http.scaladsl.Http
17-
import akka.http.scaladsl.HttpsConnectionContext
9+
import akka.http.scaladsl.common.SSLContextFactory
1810
import akka.http.scaladsl.model.HttpRequest
1911
import akka.http.scaladsl.model.HttpResponse
20-
import akka.pki.pem.DERPrivateKeyLoader
21-
import akka.pki.pem.PEMDecoder
2212
import com.typesafe.config.ConfigFactory
23-
import javax.net.ssl.KeyManagerFactory
24-
import javax.net.ssl.SSLContext
2513

14+
import java.nio.file.Paths
2615
import scala.concurrent.ExecutionContext
2716
import scala.concurrent.Future
17+
import scala.concurrent.duration._
2818
import scala.util.Failure
2919
import scala.util.Success
30-
import scala.concurrent.duration._
3120
//#import
3221

33-
3422
//#server
3523
object GreeterServer {
3624

3725
def main(args: Array[String]): Unit = {
3826
// important to enable HTTP/2 in ActorSystem's config
39-
val conf = ConfigFactory.parseString("akka.http.server.enable-http2 = on")
40-
.withFallback(ConfigFactory.defaultApplication())
27+
val conf =
28+
ConfigFactory.parseString("akka.http.server.enable-http2 = on").withFallback(ConfigFactory.defaultApplication())
4129
val system = ActorSystem[Nothing](Behaviors.empty[Nothing], "GreeterServer", conf)
4230
new GreeterServer(system).run()
4331
}
@@ -52,6 +40,12 @@ class GreeterServer(system: ActorSystem[_]) {
5240
val service: HttpRequest => Future[HttpResponse] =
5341
GreeterServiceHandler(new GreeterServiceImpl(system))
5442

43+
val serverHttpContext = ConnectionContext.httpsServer(
44+
SSLContextFactory.createSSLContextFromPem(
45+
// Note: filesystem paths, not classpath
46+
Paths.get("src/main/resources/certs/server1.pem"),
47+
Paths.get("src/main/resources/certs/server1.key")))
48+
5549
val bound: Future[Http.ServerBinding] = Http()(system)
5650
.newServerAt(interface = "127.0.0.1", port = 8080)
5751
.enableHttps(serverHttpContext)
@@ -70,34 +64,5 @@ class GreeterServer(system: ActorSystem[_]) {
7064

7165
bound
7266
}
73-
//#server
74-
75-
76-
private def serverHttpContext: HttpsConnectionContext = {
77-
val privateKey =
78-
DERPrivateKeyLoader.load(PEMDecoder.decode(readPrivateKeyPem()))
79-
val fact = CertificateFactory.getInstance("X.509")
80-
val cer = fact.generateCertificate(
81-
classOf[GreeterServer].getResourceAsStream("/certs/server1.pem")
82-
)
83-
val ks = KeyStore.getInstance("PKCS12")
84-
ks.load(null)
85-
ks.setKeyEntry(
86-
"private",
87-
privateKey,
88-
new Array[Char](0),
89-
Array[Certificate](cer)
90-
)
91-
val keyManagerFactory = KeyManagerFactory.getInstance("SunX509")
92-
keyManagerFactory.init(ks, null)
93-
val context = SSLContext.getInstance("TLS")
94-
context.init(keyManagerFactory.getKeyManagers, null, new SecureRandom)
95-
ConnectionContext.httpsServer(context)
96-
}
97-
98-
private def readPrivateKeyPem(): String =
99-
Source.fromResource("certs/server1.key").mkString
100-
//#server
101-
10267
}
10368
//#server

0 commit comments

Comments
 (0)