Skip to content

Commit 7254bf0

Browse files
author
Ajay Kumar
committed
HDDS-1065. OM and DN should persist SCM certificate as the trust root. Contributed by Ajay Kumar.
1 parent df76cdc commit 7254bf0

File tree

7 files changed

+35
-14
lines changed

7 files changed

+35
-14
lines changed

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/client/CertificateClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ boolean verifySignature(byte[] data, byte[] signature,
135135
*
136136
* @param pemEncodedCert - pem encoded X509 Certificate
137137
* @param force - override any existing file
138+
* @param caCert - Is CA certificate.
138139
* @throws CertificateException - on Error.
139140
*
140141
*/
141-
void storeCertificate(String pemEncodedCert, boolean force)
142+
void storeCertificate(String pemEncodedCert, boolean force, boolean caCert)
142143
throws CertificateException;
143144

144145
/**

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/client/DefaultCertificateClient.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
public abstract class DefaultCertificateClient implements CertificateClient {
8181

8282
private static final String CERT_FILE_NAME_FORMAT = "%s.crt";
83+
private static final String CA_CERT_PREFIX = "CA-";
8384
private final Logger logger;
8485
private final SecurityConfig securityConfig;
8586
private final KeyCodec keyCodec;
@@ -251,7 +252,7 @@ private X509Certificate getCertificateFromScm(String certId)
251252
(OzoneConfiguration) securityConfig.getConfiguration());
252253
String pemEncodedCert =
253254
scmSecurityProtocolClient.getCertificate(certId);
254-
this.storeCertificate(pemEncodedCert, true);
255+
this.storeCertificate(pemEncodedCert, true, false);
255256
return CertificateCodec.getX509Certificate(pemEncodedCert);
256257
} catch (Exception e) {
257258
getLogger().error("Error while getting Certificate with " +
@@ -452,14 +453,15 @@ public X509Certificate queryCertificate(String query) {
452453
* Stores the Certificate for this client. Don't use this api to add trusted
453454
* certificates of others.
454455
*
455-
* @param pemEncodedCert - pem encoded X509 Certificate
456-
* @param force - override any existing file
456+
* @param pemEncodedCert - pem encoded X509 Certificate
457+
* @param force - override any existing file
458+
* @param caCert - Is CA certificate.
457459
* @throws CertificateException - on Error.
458460
*
459461
*/
460462
@Override
461-
public void storeCertificate(String pemEncodedCert, boolean force)
462-
throws CertificateException {
463+
public void storeCertificate(String pemEncodedCert, boolean force,
464+
boolean caCert) throws CertificateException {
463465
CertificateCodec certificateCodec = new CertificateCodec(securityConfig);
464466
try {
465467
Path basePath = securityConfig.getCertificateLocation();
@@ -469,6 +471,10 @@ public void storeCertificate(String pemEncodedCert, boolean force)
469471
String certName = String.format(CERT_FILE_NAME_FORMAT,
470472
cert.getSerialNumber().toString());
471473

474+
if(caCert) {
475+
certName = CA_CERT_PREFIX + certName;
476+
}
477+
472478
certificateCodec.writeCertificate(basePath, certName,
473479
pemEncodedCert, force);
474480
certificateMap.putIfAbsent(cert.getSerialNumber().toString(), cert);

hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestDefaultCertificateClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void testCertificateOps() throws Exception {
174174
X509Certificate cert = omCertClient.getCertificate();
175175
assertNull(cert);
176176
omCertClient.storeCertificate(getPEMEncodedString(x509Certificate),
177-
true);
177+
true, false);
178178

179179
cert = omCertClient.getCertificate(
180180
x509Certificate.getSerialNumber().toString());
@@ -327,9 +327,9 @@ public void testStoreCertificate() throws Exception {
327327
X509Certificate cert2 = generateX509Cert(keyPair);
328328
X509Certificate cert3 = generateX509Cert(keyPair);
329329

330-
dnCertClient.storeCertificate(getPEMEncodedString(cert1), true);
331-
dnCertClient.storeCertificate(getPEMEncodedString(cert2), true);
332-
dnCertClient.storeCertificate(getPEMEncodedString(cert3), true);
330+
dnCertClient.storeCertificate(getPEMEncodedString(cert1), true, false);
331+
dnCertClient.storeCertificate(getPEMEncodedString(cert2), true, false);
332+
dnCertClient.storeCertificate(getPEMEncodedString(cert3), true, false);
333333

334334
assertNotNull(dnCertClient.getCertificate(cert1.getSerialNumber()
335335
.toString()));

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/HddsDatanodeService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,13 @@ private void getSCMSignedCert(OzoneConfiguration config) {
268268

269269
String pemEncodedCert = secureScmClient.getDataNodeCertificate(
270270
datanodeDetails.getProtoBufMessage(), getEncodedString(csr));
271-
dnCertClient.storeCertificate(pemEncodedCert, true);
271+
dnCertClient.storeCertificate(pemEncodedCert, true, false);
272272
datanodeDetails.setCertSerialId(getX509Certificate(pemEncodedCert).
273273
getSerialNumber().toString());
274274
persistDatanodeDetails(datanodeDetails);
275+
// Get SCM CA certificate and store it in filesystem.
276+
String pemEncodedRootCert = secureScmClient.getCACertificate();
277+
dnCertClient.storeCertificate(pemEncodedRootCert, true, true);
275278
} catch (IOException | CertificateException e) {
276279
LOG.error("Error while storing SCM signed certificate.", e);
277280
throw new RuntimeException(e);

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/TestSecureOzoneCluster.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
4141
import org.apache.hadoop.hdds.scm.server.SCMStorageConfig;
4242
import org.apache.hadoop.hdds.scm.server.StorageContainerManager;
43+
import org.apache.hadoop.hdds.security.x509.certificate.utils.CertificateCodec;
4344
import org.apache.hadoop.hdds.security.x509.keys.HDDSKeyGenerator;
4445
import org.apache.hadoop.hdds.security.x509.keys.KeyCodec;
4546
import org.apache.hadoop.io.Text;
@@ -98,6 +99,7 @@
9899
import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.TOKEN_EXPIRED;
99100
import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.VOLUME_NOT_FOUND;
100101
import static org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod.KERBEROS;
102+
import static org.junit.Assert.assertEquals;
101103
import static org.junit.Assert.assertFalse;
102104
import static org.junit.Assert.assertNull;
103105
import static org.junit.Assert.assertTrue;
@@ -780,6 +782,12 @@ public void testSecureOmInitSuccess() throws Exception {
780782
"SCM signed certificate"));
781783
X509Certificate certificate = om.getCertificateClient().getCertificate();
782784
validateCertificate(certificate);
785+
String pemEncodedCACert =
786+
scm.getSecurityProtocolServer().getCACertificate();
787+
X509Certificate caCert = CertificateCodec.getX509Cert(pemEncodedCACert);
788+
X509Certificate caCertStored = om.getCertificateClient().getCertificate(caCert.getSerialNumber().
789+
toString());
790+
assertEquals(caCert, caCertStored);
783791
} finally {
784792
if (scm != null) {
785793
scm.stop();

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/client/CertificateClientTestImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ public X509Certificate queryCertificate(String query) {
137137
}
138138

139139
@Override
140-
public void storeCertificate(String cert, boolean force)
140+
public void storeCertificate(String cert, boolean force, boolean caCert)
141141
throws CertificateException {
142-
143142
}
144143

145144
/**

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,10 +1461,14 @@ private static void getSCMSignedCert(CertificateClient client,
14611461
getEncodedString(csr));
14621462

14631463
try {
1464-
client.storeCertificate(pemEncodedCert, true);
1464+
client.storeCertificate(pemEncodedCert, true, false);
14651465
// Persist om cert serial id.
14661466
omStore.setOmCertSerialId(CertificateCodec.
14671467
getX509Certificate(pemEncodedCert).getSerialNumber().toString());
1468+
1469+
// Get SCM CA certificate and store it in filesystem.
1470+
String pemEncodedRootCert = secureScmClient.getCACertificate();
1471+
client.storeCertificate(pemEncodedRootCert, true, true);
14681472
} catch (IOException | CertificateException e) {
14691473
LOG.error("Error while storing SCM signed certificate.", e);
14701474
throw new RuntimeException(e);

0 commit comments

Comments
 (0)