neils-dev commented on code in PR #4427:
URL: https://github.com/apache/ozone/pull/4427#discussion_r1145626684


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/client/DefaultCertificateClient.java:
##########
@@ -503,6 +505,56 @@ public void storeCertificate(String pemEncodedCert,
         certificateCodec, true);
   }
 
+  /**
+   * Stores all Certificates in this response on local disk.
+   * @param response - response from SCM
+   * @return first certificate ID
+   * @throws CertificateException - on Error
+   */
+  public String storeCertificate(SCMGetCertResponseProto response)
+      throws CertificateException {
+    return storeCertificate(response,
+        getSecurityConfig().getCertificateLocation(getComponentName()));
+  }
+
+  protected String storeCertificate(SCMGetCertResponseProto response,
+      Path codecPath) throws CertificateException {
+    CertificateCodec certCodec = new CertificateCodec(securityConfig,
+        codecPath);
+    try {
+      String pemEncodedCert = response.getX509Certificate();
+      if (!response.hasX509RootCACertificate()) {
+        throw new CertificateException("Unable to find SCM root certificate");
+      }
+      String pemEncodedRootCert = response.getX509RootCACertificate();
+      X509Certificate rootCert =
+          CertificateCodec.getX509Certificate(pemEncodedRootCert);
+      CertPath path =
+          CertificateCodec.getCertPathFromPemEncodedString(pemEncodedCert);
+      for (Certificate c: path.getCertificates()) {
+        X509Certificate cert = (X509Certificate) c;
+        CAType caType = CAType.NONE;
+        if (cert.getBasicConstraints() != -1) {
+          if (rootCert.getSerialNumber().toString().equals(
+              cert.getSerialNumber().toString())) {
+            caType = CAType.ROOT;
+          } else {
+            caType = CAType.SUBORDINATE;
+          }
+        }
+        String certName = String.format(CERT_FILE_NAME_FORMAT,
+            caType.getFileNamePrefix() + cert.getSerialNumber().toString());
+        certCodec.writeCertificate(
+            CertificateCodec.getCertificateHolder(cert), certName);
+      }
+      return ((X509Certificate)path.getCertificates().get(0))

Review Comment:
   Makes sense.  Thanks.



-- 
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