This is an automated email from the ASF dual-hosted git repository. jojochuang pushed a commit to branch ozone-2.2 in repository https://gitbox.apache.org/repos/asf/ozone.git
commit 659a7dad9c2109e8eb4150bd1e47ed5c1d68c66b Author: Siyao Meng <[email protected]> AuthorDate: Wed Oct 21 03:10:22 2026 -0700 HDDS-17235. Accept only SCM certificates on SCM to SCM connections (cherry picked from commit e53f2caf3461d8cdf4f82c62ab774aff4fc4d137) Change-Id: Ie482541ef99060071ac587b01d940f1417141ca3 --- .../apache/hadoop/hdds/scm/ha/HASecurityUtils.java | 54 +++++++++++++++++++++- .../hadoop/hdds/scm/ha/InterSCMGrpcClient.java | 3 +- .../hdds/scm/ha/InterSCMGrpcProtocolService.java | 2 +- .../scm/ha/TestInterSCMGrpcProtocolService.java | 34 ++++++++++++-- .../hadoop/hdds/scm/ha/TestSCMRatisServerImpl.java | 46 ++++++++++++++++++ 5 files changed, 133 insertions(+), 6 deletions(-) diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/HASecurityUtils.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/HASecurityUtils.java index cec412e9310..fafcd53e3be 100644 --- a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/HASecurityUtils.java +++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/HASecurityUtils.java @@ -25,9 +25,11 @@ import java.io.IOException; import java.math.BigInteger; import java.net.InetAddress; +import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; +import javax.net.ssl.X509TrustManager; import org.apache.hadoop.hdds.conf.OzoneConfiguration; import org.apache.hadoop.hdds.protocolPB.SCMSecurityProtocolClientSideTranslatorPB; import org.apache.hadoop.hdds.ratis.RatisHelper; @@ -154,11 +156,61 @@ public static GrpcTlsConfig createSCMRatisTLSConfig(SecurityConfig conf, CertificateClient certificateClient) throws IOException { if (conf.isSecurityEnabled() && conf.isGrpcTlsEnabled()) { return new GrpcTlsConfig(certificateClient.getKeyManager(), - certificateClient.getTrustManager(), true); + new ScmPeerTrustManager(certificateClient.getTrustManager()), true); } return null; } + /** + * {@link X509TrustManager} that accepts only SCM peers on SCM to SCM connections (SCM Ratis and inter-SCM gRPC). + * <p> + * After the delegate validates the chain against the cluster trust store, the peer certificate + * ({@code chain[0]}) must be a CA certificate. {@link X509Certificate#getBasicConstraints()} returns {@code -1} + * for a certificate that is not a CA (no basic constraints extension, or CA false), and the path length + * constraint ({@code 0} or more) for a CA certificate. + * <p> + * SCM certificates are CA certificates: an SCM uses either its sub-CA certificate ({@code SCMCertificateClient} + * requests {@code setCA(true)}) or the self-signed root CA certificate. + * Datanode, OM and Recon certificates are leaf certificates, so they are not accepted here. + */ + static final class ScmPeerTrustManager implements X509TrustManager { + private final X509TrustManager delegate; + + ScmPeerTrustManager(X509TrustManager delegate) { + this.delegate = delegate; + } + + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + delegate.checkClientTrusted(chain, authType); + // The peer must be an SCM (CA certificate), not a Datanode, OM or Recon (leaf certificate). + checkScmCertificate(chain[0]); + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + delegate.checkServerTrusted(chain, authType); + // The peer must be an SCM (CA certificate), not a Datanode, OM or Recon (leaf certificate). + checkScmCertificate(chain[0]); + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return delegate.getAcceptedIssuers(); + } + + /** + * Requires {@code cert} to be a CA certificate, as SCM certificates are. + * + * @throws CertificateException unless {@link HASecurityUtils#isCACertificate} accepts {@code cert} + */ + private static void checkScmCertificate(X509Certificate cert) throws CertificateException { + if (!isCACertificate(cert)) { + throw new CertificateException("Not an SCM certificate: " + cert.getSubjectX500Principal()); + } + } + } + /** * Submit SCM request to ratis using RaftClient. * @param raftGroup diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/InterSCMGrpcClient.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/InterSCMGrpcClient.java index 59aff304f25..995cf54a0cc 100644 --- a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/InterSCMGrpcClient.java +++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/InterSCMGrpcClient.java @@ -72,7 +72,8 @@ public InterSCMGrpcClient(final String host, && securityConfig.isGrpcTlsEnabled()) { SslContextBuilder sslClientContextBuilder = SslContextBuilder.forClient(); sslClientContextBuilder.keyManager(scmCertificateClient.getKeyManager()); - sslClientContextBuilder.trustManager(scmCertificateClient.getTrustManager()); + sslClientContextBuilder.trustManager( + new HASecurityUtils.ScmPeerTrustManager(scmCertificateClient.getTrustManager())); SslContextBuilder sslContextBuilder = GrpcSslContexts.configure( sslClientContextBuilder, securityConfig.getGrpcSslProvider()); channelBuilder.sslContext(sslContextBuilder.build()) diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/InterSCMGrpcProtocolService.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/InterSCMGrpcProtocolService.java index 8b4086a69d9..81cb00ee629 100644 --- a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/InterSCMGrpcProtocolService.java +++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/InterSCMGrpcProtocolService.java @@ -71,7 +71,7 @@ public class InterSCMGrpcProtocolService { CertificateClient certClient = scm.getScmCertificateClient(); SslContextBuilder sslServerContextBuilder = forServer(certClient.getKeyManager()) - .trustManager(certClient.getTrustManager()); + .trustManager(new HASecurityUtils.ScmPeerTrustManager(certClient.getTrustManager())); SslContextBuilder sslContextBuilder = GrpcSslContexts.configure( sslServerContextBuilder, securityConfig.getGrpcSslProvider()); sslContextBuilder.clientAuth(ClientAuth.REQUIRE); diff --git a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestInterSCMGrpcProtocolService.java b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestInterSCMGrpcProtocolService.java index 8697521f075..afafe66ff57 100644 --- a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestInterSCMGrpcProtocolService.java +++ b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestInterSCMGrpcProtocolService.java @@ -20,6 +20,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.hadoop.hdds.security.x509.CertificateTestUtils.createSelfSignedCert; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyBoolean; import static org.mockito.Mockito.doReturn; @@ -41,17 +42,21 @@ import java.security.KeyStore; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.time.ZonedDateTime; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.hadoop.hdds.HddsConfigKeys; import org.apache.hadoop.hdds.conf.OzoneConfiguration; import org.apache.hadoop.hdds.scm.ScmConfigKeys; import org.apache.hadoop.hdds.scm.metadata.SCMMetadataStore; import org.apache.hadoop.hdds.scm.server.StorageContainerManager; +import org.apache.hadoop.hdds.security.SecurityConfig; import org.apache.hadoop.hdds.security.ssl.ReloadingX509KeyManager; import org.apache.hadoop.hdds.security.ssl.ReloadingX509TrustManager; import org.apache.hadoop.hdds.security.x509.CertificateTestUtils; import org.apache.hadoop.hdds.security.x509.certificate.client.SCMCertificateClient; +import org.apache.hadoop.hdds.security.x509.certificate.utils.SelfSignedCertificate; import org.apache.hadoop.hdds.utils.TransactionInfo; import org.apache.hadoop.hdds.utils.db.Codec; import org.apache.hadoop.hdds.utils.db.DBCheckpoint; @@ -90,7 +95,7 @@ void testMTLSOnInterScmGrpcProtocolServiceAccess() throws Exception { int port = PortAllocator.getFreePort(); OzoneConfiguration conf = setupConfiguration(port); SCMCertificateClient - scmCertClient = setupCertificateClientForMTLS(conf); + scmCertClient = setupCertificateClientForMTLS(conf, true); InterSCMGrpcProtocolService service = new InterSCMGrpcProtocolService(conf, scmWith(scmCertClient)); service.start(); @@ -109,6 +114,23 @@ void testMTLSOnInterScmGrpcProtocolServiceAccess() throws Exception { service.stop(); } + @Test + void testInterScmGrpcProtocolServiceRequiresScmCertificate() throws Exception { + int port = PortAllocator.getFreePort(); + OzoneConfiguration conf = setupConfiguration(port); + SCMCertificateClient scmCertClient = setupCertificateClientForMTLS(conf, false); + InterSCMGrpcProtocolService service = new InterSCMGrpcProtocolService(conf, scmWith(scmCertClient)); + service.start(); + InterSCMGrpcClient client = new InterSCMGrpcClient("localhost", port, conf, scmCertClient); + try { + CompletableFuture<Path> res = client.download(temp.resolve(CP_FILE_NAME)); + assertThatThrownBy(res::get).isInstanceOf(ExecutionException.class); + } finally { + client.close(); + service.stop(); + } + } + private void verifyServiceUsedItsCertAndValidatedClientCert() throws CertificateException { ArgumentCaptor<X509Certificate[]> capturedCerts = @@ -195,13 +217,13 @@ private TypedTable<String, TransactionInfo> trInfoTable() } private SCMCertificateClient setupCertificateClientForMTLS( - OzoneConfiguration conf + OzoneConfiguration conf, boolean clientIsScm ) throws Exception { KeyPair serviceKeys = CertificateTestUtils.aKeyPair(conf); KeyPair clientKeys = CertificateTestUtils.aKeyPair(conf); serviceCert = createSelfSignedCert(serviceKeys, "service"); - clientCert = createSelfSignedCert(clientKeys, "client"); + clientCert = clientIsScm ? createSelfSignedCert(clientKeys, "client") : createLeafCert(conf, clientKeys); ReloadingX509TrustManager toSpyServerTrustManager = new ReloadingX509TrustManager(KeyStore.getDefaultType(), ImmutableList.of(clientCert)); @@ -222,6 +244,12 @@ private SCMCertificateClient setupCertificateClientForMTLS( return scmCertClient; } + private static X509Certificate createLeafCert(OzoneConfiguration conf, KeyPair keys) throws Exception { + ZonedDateTime now = ZonedDateTime.now(); + return SelfSignedCertificate.newBuilder().setSubject("client").setClusterID("clusterId").setScmID("scmId") + .setBeginDate(now).setEndDate(now.plusDays(1)).setConfiguration(new SecurityConfig(conf)).setKey(keys).build(); + } + private OzoneConfiguration setupConfiguration(int port) { OzoneConfiguration conf = new OzoneConfiguration(); conf.setInt(ScmConfigKeys.OZONE_SCM_GRPC_PORT_KEY, port); diff --git a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMRatisServerImpl.java b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMRatisServerImpl.java index 6b601868c32..e6176c5e0e2 100644 --- a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMRatisServerImpl.java +++ b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMRatisServerImpl.java @@ -17,20 +17,28 @@ package org.apache.hadoop.hdds.scm.ha; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockConstruction; import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; import java.util.UUID; +import javax.net.ssl.X509TrustManager; import org.apache.hadoop.hdds.conf.ConfigurationSource; import org.apache.hadoop.hdds.scm.server.StorageContainerManager; import org.apache.hadoop.hdds.security.SecurityConfig; +import org.apache.hadoop.hdds.security.ssl.ReloadingX509KeyManager; +import org.apache.hadoop.hdds.security.ssl.ReloadingX509TrustManager; +import org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient; import org.apache.ratis.conf.RaftProperties; import org.apache.ratis.protocol.RaftPeer; import org.apache.ratis.protocol.RaftPeerId; @@ -103,4 +111,42 @@ public void testGetLeaderId() throws Exception { } } + @Test + public void testTlsConfigAcceptsOnlyScmCertificates() throws Exception { + SecurityConfig securityConfig = mock(SecurityConfig.class); + when(securityConfig.isSecurityEnabled()).thenReturn(true); + when(securityConfig.isGrpcTlsEnabled()).thenReturn(true); + ReloadingX509TrustManager clusterTrustManager = mock(ReloadingX509TrustManager.class); + CertificateClient certificateClient = mock(CertificateClient.class); + when(certificateClient.getKeyManager()).thenReturn(mock(ReloadingX509KeyManager.class)); + when(certificateClient.getTrustManager()).thenReturn(clusterTrustManager); + + X509TrustManager trustManager = (X509TrustManager) HASecurityUtils + .createSCMRatisTLSConfig(securityConfig, certificateClient).getTrustManager().getTrustManager(); + + X509Certificate scmCert = mock(X509Certificate.class); + when(scmCert.getBasicConstraints()).thenReturn(Integer.MAX_VALUE); + X509Certificate[] scmChain = {scmCert}; + trustManager.checkClientTrusted(scmChain, "RSA"); + trustManager.checkServerTrusted(scmChain, "RSA"); + + X509Certificate leafCert = mock(X509Certificate.class); + when(leafCert.getBasicConstraints()).thenReturn(-1); + X509Certificate[] leafChain = {leafCert}; + assertThatThrownBy(() -> trustManager.checkClientTrusted(leafChain, "RSA")) + .isInstanceOf(CertificateException.class) + .hasMessageStartingWith("Not an SCM certificate"); + assertThatThrownBy(() -> trustManager.checkServerTrusted(leafChain, "RSA")) + .isInstanceOf(CertificateException.class) + .hasMessageStartingWith("Not an SCM certificate"); + // Only the peer certificate counts, not the CA certificates that follow it in the chain. + X509Certificate[] leafWithIssuerChain = {leafCert, scmCert}; + assertThatThrownBy(() -> trustManager.checkClientTrusted(leafWithIssuerChain, "RSA")) + .isInstanceOf(CertificateException.class) + .hasMessageStartingWith("Not an SCM certificate"); + + // A chain the cluster trust manager rejects stays rejected. + doThrow(new CertificateException("untrusted chain")).when(clusterTrustManager).checkClientTrusted(scmChain, "RSA"); + assertThatThrownBy(() -> trustManager.checkClientTrusted(scmChain, "RSA")).hasMessage("untrusted chain"); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
