adoroszlai commented on a change in pull request #3027:
URL: https://github.com/apache/ozone/pull/3027#discussion_r796885940



##########
File path: 
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/security/exception/SCMSecurityException.java
##########
@@ -109,6 +109,7 @@ public ErrorCode getErrorCode() {
     MISSING_BLOCK_TOKEN,
     BLOCK_TOKEN_VERIFICATION_FAILED,
     GET_ROOT_CA_CERT_FAILED,
-    NOT_A_PRIMARY_SCM
+    NOT_A_PRIMARY_SCM,
+    GET_RECON_CERTIFICATE_FAILED

Review comment:
       I think we can reuse `GET_CERTIFICATE_FAILED` instead of adding new 
error code.

##########
File path: 
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/client/ReconCertificateClient.java
##########
@@ -0,0 +1,119 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hdds.security.x509.certificate.client;
+
+import org.apache.hadoop.hdds.security.x509.SecurityConfig;
+import 
org.apache.hadoop.hdds.security.x509.certificates.utils.CertificateSignRequest;
+import org.apache.hadoop.hdds.security.x509.exceptions.CertificateException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static 
org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient.InitResponse.FAILURE;
+import static 
org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient.InitResponse.GETCERT;
+import static 
org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient.InitResponse.RECOVER;
+import static 
org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient.InitResponse.SUCCESS;
+
+/**
+ * Certificate client for Recon.
+ */
+public class ReconCertificateClient  extends DefaultCertificateClient{

Review comment:
       This class has lot in common with OMCertificateClient.  Can you please 
extract a common parent?

##########
File path: hadoop-hdds/interface-client/src/main/proto/hdds.proto
##########
@@ -84,6 +84,12 @@ message ScmNodeDetailsProto {
     required string hostName = 3;      // Hostname of SCM.
 }
 
+message ReconDetailsProto {
+    required string uuid = 1;
+    required string clusterId = 2;
+    required string hostName = 3;
+}

Review comment:
       Can you please make it more generic by including a `NodeType` field (and 
renaming the message)?  This way it will be easier to add certificate for any 
other component (e.g. S3 Gateway) in the future.
   
   (Also please rename `SCMGetReconCertRequestProto` and other Recon-specific 
items.)

##########
File path: 
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/ReconServer.java
##########
@@ -129,6 +166,108 @@ public Void call() throws Exception {
     return null;
   }
 
+  /**
+   * Initializes secure Recon.
+   * */
+  private void initializeCertificateClient(OzoneConfiguration conf)
+      throws IOException {
+    LOG.info("Initializing secure Recon.");
+    certClient = new ReconCertificateClient(
+        new SecurityConfig(configuration),
+        reconStorage.getReconCertSerialId());
+
+    CertificateClient.InitResponse response = certClient.init();
+    LOG.info("Init response: {}", response);
+    switch (response) {
+    case SUCCESS:
+      LOG.info("Initialization successful, case:{}.", response);
+      break;
+    case GETCERT:
+      getSCMSignedCert(conf);
+      LOG.info("Successfully stored SCM signed certificate, case:{}.",
+          response);
+      break;
+    case FAILURE:
+      LOG.error("Recon security initialization failed, case:{}.", response);
+      throw new RuntimeException("Recon security initialization failed.");
+    case RECOVER:
+      LOG.error("Recon security initialization failed. Recon certificate is " +
+          "missing.");
+      throw new RuntimeException("Recon security initialization failed.");
+    default:
+      LOG.error("Recon security initialization failed. Init response: {}",
+          response);
+      throw new RuntimeException("Recon security initialization failed.");
+    }
+  }
+
+  /**
+   * Get SCM signed certificate and store it using certificate client.
+   * @param config
+   * */
+  private void getSCMSignedCert(OzoneConfiguration config) {
+    try {
+      PKCS10CertificationRequest csr = getCSR(config);
+      SCMSecurityProtocolClientSideTranslatorPB secureScmClient =
+          HddsServerUtil.getScmSecurityClientWithMaxRetry(config);
+      HddsProtos.ReconDetailsProto.Builder reconDetailsProtoBuilder =
+          HddsProtos.ReconDetailsProto.newBuilder()
+              .setHostName(InetAddress.getLocalHost().getHostName())
+              .setClusterId(reconStorage.getClusterID())
+              .setUuid(reconStorage.getReconId());
+
+      SCMSecurityProtocolProtos.SCMGetCertResponseProto response =
+          secureScmClient.getReconCertificateChain(
+              reconDetailsProtoBuilder.build(),
+              getEncodedString(csr));
+      // Persist certificates.
+      if(response.hasX509CACertificate()) {
+        String pemEncodedCert = response.getX509Certificate();
+        certClient.storeCertificate(pemEncodedCert, true);
+        certClient.storeCertificate(response.getX509CACertificate(), true,
+            true);
+
+        // Store Root CA certificate.
+        if (response.hasX509RootCACertificate()) {
+          certClient.storeRootCACertificate(
+              response.getX509RootCACertificate(), true);
+        }
+        String reconCertSerialId = getX509Certificate(pemEncodedCert).
+            getSerialNumber().toString();
+        reconStorage.setReconCertSerialId(reconCertSerialId);
+      } else {
+        throw new RuntimeException("Unable to retrieve recon certificate " +
+            "chain");
+      }
+    } catch (IOException | CertificateException e) {
+      LOG.error("Error while storing SCM signed certificate.", e);
+      throw new RuntimeException(e);
+    }
+  }
+
+  /**
+   * Creates CSR for Recon.
+   * @param config
+   * */
+  private PKCS10CertificationRequest getCSR(OzoneConfiguration config)

Review comment:
       This method is also very similar to existing code.  Can you please 
create a utility method building the CSR?




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to