You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "pjfanning (via GitHub)" <gi...@apache.org> on 2026/09/04 13:41:10 UTC

[PR] feat: load every certificate in the rotating keys engine ca-cert-file [pekko]

pjfanning opened a new pull request, #3528:
URL: https://github.com/apache/pekko/pull/3528

   ### Motivation
   `RotatingKeysSSLEngineProvider` reads `ca-cert-file` with `CertificateFactory.generateCertificate`, which stops at the first PEM block. A CA file that bundles more than one certificate — a root plus the intermediates it delegates to, or two roots while a CA is being rotated — was therefore only half trusted, and peers presenting a certificate issued by any of the other CAs in the bundle were rejected.
   
   ### Modification
   - Add `PemManagersProvider.loadCertificates`, which uses `generateCertificates` and closes the stream (the singular `loadCertificate` never did).
   - `buildTrustManagers` and `buildKeyManagers` take the whole `Seq[Certificate]`; every CA certificate is stored under its own alias, so all of them become trust anchors.
   - The chain attached to the key entry is built by walking issuer → subject from the node certificate upwards, so a CA certificate that did not issue it (an unrelated root kept around for a rotation, say) is trusted without being sent to the peer. When none of them matches the issuer, the previous behaviour of sending them all is kept.
   - `readFiles` fails with `SslTransportException` when the CA file yields no certificate, instead of passing a null certificate on.
   - Document the bundle support on `ca-cert-file` in `reference.conf`.
   
   ### Result
   `ca-cert-file` may bundle any number of certificates and all of them are trusted. Single-certificate configurations behave exactly as before.
   
   ### Tests
   - `sbt "remote/testOnly org.apache.pekko.remote.artery.tcp.ssl.PemManagersProviderSpec org.apache.pekko.remote.artery.tcp.ssl.TlsResourcesSpec org.apache.pekko.remote.artery.tcp.ssl.*Rotating*"` — passed
   - `sbt "remote/mimaReportBinaryIssues"` — passed
   - Both were run before this change was merged with the `keystore-password` change on `main`; verification of the merged state is left to CI.
   - New cases in `PemManagersProviderSpec`, backed by a new `ssl/exampleca-bundle.crt` resource (`exampleca.crt` + `pem/selfsigned-certificate.pem`): the trust manager's accepted issuers contain both certificates, and the key manager's chain for the node certificate contains only the CA that issued it.
   
   ### References
   None — follow-up on multi-certificate `ca-cert-file` support


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: load every certificate in the rotating keys engine ca-cert-file [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3528:
URL: https://github.com/apache/pekko/pull/3528#discussion_r3943880267


##########
remote/src/main/scala/org/apache/pekko/remote/artery/tcp/ssl/PemManagersProvider.scala:
##########
@@ -90,4 +93,46 @@ private[ssl] object PemManagersProvider {
     certFactory.generateCertificate(Files.newInputStream(new File(filename).toPath))
   }
 
+  /**
+   * Loads every certificate found in the given PEM file. A file may bundle more than one
+   * certificate, e.g. a root CA together with the intermediates it delegates to, or several
+   * roots while a CA is being rotated.
+   *
+   * INTERNAL API
+   */
+  @InternalApi
+  private[ssl] def loadCertificates(filename: String): Seq[Certificate] = blocking {
+    val stream = Files.newInputStream(new File(filename).toPath)
+    try certFactory.generateCertificates(stream).asScala.toList
+    finally stream.close()
+  }
+
+  private def caCertAlias(idx: Int): String = if (idx == 0) "cacert" else s"cacert-$idx"
+
+  /**
+   * Builds the certificate chain to present during the handshake: `cert` followed by the
+   * certificates from `cacerts` that actually issued it, ordered from the issuer of `cert` up
+   * to the root. Certificates in `cacerts` that are not part of that chain (an unrelated root
+   * kept around for a CA rotation, say) are trusted but not sent to the peer.
+   *
+   * Falls back to sending all of `cacerts` when none of them matches the issuer of `cert`, which
+   * keeps the behaviour of a single, non-matching CA certificate unchanged.
+   */
+  private def buildCertificateChain(cert: X509Certificate, cacerts: Seq[Certificate]): Array[Certificate] = {
+    val bySubject: Map[X500Principal, X509Certificate] =

Review Comment:
   The duplicate-subject case was real, the chain is now picked by signature. There is now the exampleca-rotated-bundle.crt test.
   
   These cert files are expected to be carefully created by trusted people involved in the deployment. There is no expectation that someone would load a badly formatted cert file. Still no harm in trying to make the code deterministic.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: load every certificate in the rotating keys engine ca-cert-file [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning merged PR #3528:
URL: https://github.com/apache/pekko/pull/3528


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: load every certificate in the rotating keys engine ca-cert-file [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on code in PR #3528:
URL: https://github.com/apache/pekko/pull/3528#discussion_r3947121774


##########
remote/src/main/scala/org/apache/pekko/remote/artery/tcp/ssl/PemManagersProvider.scala:
##########
@@ -90,4 +93,46 @@ private[ssl] object PemManagersProvider {
     certFactory.generateCertificate(Files.newInputStream(new File(filename).toPath))

Review Comment:
   `loadCertificates` closes its stream, but `loadCertificate` here still doesn't, and `CertificateFactory.generateCertificate` doesn't close the stream either — so every call leaks a file descriptor. `RotatingKeysSSLEngineProvider.readFiles` calls this for `SSLCertFile` on every rotation, so a long-lived rotating-keys system accumulates leaked handles over time. This PR is already editing this file; worth wrapping it in `try`/`finally` the same way.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: load every certificate in the rotating keys engine ca-cert-file [pekko]

Posted by "samueleresca (via GitHub)" <gi...@apache.org>.
samueleresca commented on code in PR #3528:
URL: https://github.com/apache/pekko/pull/3528#discussion_r3943818862


##########
remote/src/main/scala/org/apache/pekko/remote/artery/tcp/ssl/PemManagersProvider.scala:
##########
@@ -90,4 +93,46 @@ private[ssl] object PemManagersProvider {
     certFactory.generateCertificate(Files.newInputStream(new File(filename).toPath))
   }
 
+  /**
+   * Loads every certificate found in the given PEM file. A file may bundle more than one
+   * certificate, e.g. a root CA together with the intermediates it delegates to, or several
+   * roots while a CA is being rotated.
+   *
+   * INTERNAL API
+   */
+  @InternalApi
+  private[ssl] def loadCertificates(filename: String): Seq[Certificate] = blocking {
+    val stream = Files.newInputStream(new File(filename).toPath)
+    try certFactory.generateCertificates(stream).asScala.toList
+    finally stream.close()
+  }
+
+  private def caCertAlias(idx: Int): String = if (idx == 0) "cacert" else s"cacert-$idx"
+
+  /**
+   * Builds the certificate chain to present during the handshake: `cert` followed by the
+   * certificates from `cacerts` that actually issued it, ordered from the issuer of `cert` up
+   * to the root. Certificates in `cacerts` that are not part of that chain (an unrelated root
+   * kept around for a CA rotation, say) are trusted but not sent to the peer.
+   *
+   * Falls back to sending all of `cacerts` when none of them matches the issuer of `cert`, which
+   * keeps the behaviour of a single, non-matching CA certificate unchanged.
+   */
+  private def buildCertificateChain(cert: X509Certificate, cacerts: Seq[Certificate]): Array[Certificate] = {
+    val bySubject: Map[X500Principal, X509Certificate] =

Review Comment:
   Given 2 certificates with the same subject, only 1 will be returned from `buildCertificateChain`. Does this have implications?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: load every certificate in the rotating keys engine ca-cert-file [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3528:
URL: https://github.com/apache/pekko/pull/3528#discussion_r3943857106


##########
remote/src/main/scala/org/apache/pekko/remote/artery/tcp/ssl/PemManagersProvider.scala:
##########
@@ -90,4 +93,46 @@ private[ssl] object PemManagersProvider {
     certFactory.generateCertificate(Files.newInputStream(new File(filename).toPath))
   }
 
+  /**
+   * Loads every certificate found in the given PEM file. A file may bundle more than one
+   * certificate, e.g. a root CA together with the intermediates it delegates to, or several
+   * roots while a CA is being rotated.
+   *
+   * INTERNAL API
+   */
+  @InternalApi
+  private[ssl] def loadCertificates(filename: String): Seq[Certificate] = blocking {
+    val stream = Files.newInputStream(new File(filename).toPath)
+    try certFactory.generateCertificates(stream).asScala.toList
+    finally stream.close()
+  }
+
+  private def caCertAlias(idx: Int): String = if (idx == 0) "cacert" else s"cacert-$idx"
+
+  /**
+   * Builds the certificate chain to present during the handshake: `cert` followed by the
+   * certificates from `cacerts` that actually issued it, ordered from the issuer of `cert` up
+   * to the root. Certificates in `cacerts` that are not part of that chain (an unrelated root
+   * kept around for a CA rotation, say) are trusted but not sent to the peer.
+   *
+   * Falls back to sending all of `cacerts` when none of them matches the issuer of `cert`, which
+   * keeps the behaviour of a single, non-matching CA certificate unchanged.
+   */
+  private def buildCertificateChain(cert: X509Certificate, cacerts: Seq[Certificate]): Array[Certificate] = {
+    val bySubject: Map[X500Principal, X509Certificate] =

Review Comment:
   I'll look into adding test coverage in this area and seeing which solution makes most sense



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: load every certificate in the rotating keys engine ca-cert-file [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3528:
URL: https://github.com/apache/pekko/pull/3528#discussion_r3947692986


##########
remote/src/main/scala/org/apache/pekko/remote/artery/tcp/ssl/PemManagersProvider.scala:
##########
@@ -90,4 +93,46 @@ private[ssl] object PemManagersProvider {
     certFactory.generateCertificate(Files.newInputStream(new File(filename).toPath))

Review Comment:
   https://github.com/pjfanning/incubator-pekko/blob/56f9467d32f388336a8abcb735cbf5c671b8294a/remote/src/main/scala/org/apache/pekko/remote/artery/tcp/ssl/PemManagersProvider.scala seems to show this is closed in this PR



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org