smengcl commented on code in PR #12114:
URL: https://github.com/apache/ozone/pull/12114#discussion_r4674258737


##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestDnCertificateClientInit.java:
##########
@@ -145,6 +148,23 @@ public void testInitDatanode(boolean pvtKeyPresent, 
boolean pubKeyPresent,
     }
   }
 
+  /** A user@FQDN longer than 64 characters, as on Kubernetes, is replaced by 
dn@UUID in the CSR. */
+  @Test
+  public void testCsrCommonNameForLongHostName() throws Exception {

Review Comment:
   Only the datanode client has a long hostname test. These have none:
   - OM: `om@<UUID>`
   - Recon: `recon@<Recon ID>`
   - SCM sub CA: `scm-sub@<SCM ID>`
   - SCM root CA at init: `scm@<SCM ID>`
   
   `TestOmCertificateClientInit` can take the OM case the same way.



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/SCMSecurityProtocolServer.java:
##########
@@ -331,31 +332,36 @@ public String getSCMCertificate(ScmNodeDetailsProto 
scmNodeDetails,
     LOGGER.info("Processing CSR for scm {}, nodeId: {}",
         scmNodeDetails.getHostName(), scmNodeDetails.getScmNodeId());
 
-    return getEncodedCertToString(certSignReq, NodeType.SCM);
+    return getEncodedCertToString(certSignReq, NodeType.SCM, null);
   }
 
   /**
    *  Request certificate for the specified role.
    * @param certSignReq - Certificate signing request.
    * @param nodeType - role OM/SCM/DATANODE
+   * @param nodeId - UUID of the requester, for its commonName if the CSR's is 
too long; ignored for SCM, whose SCM ID
+   *               is in the CSR subject.
    * @return String         - SCM signed pem encoded certificate.
    * @throws IOException
    */
   private synchronized String getEncodedCertToString(String certSignReq,
-      NodeType nodeType) throws IOException {
+      NodeType nodeType, String nodeId) throws IOException {
     if (nodeType == NodeType.SCM) {
       // SCM role certificates are CA certificates and are issued to SCM 
admins only, the check addSCM applies.
       storageContainerManager.checkAdminAccess(getRpcRemoteUser(), false);
     }
     checkRequesterIsService(nodeType);
     Future<CertPath> future;
     PKCS10CertificationRequest csr = getCertificationRequest(certSignReq);
+    // The same commonName as CertificateSignRequest.commonName gives a client 
of this version.
+    String commonNameIfTooLong = 
CertificateSignRequest.commonNamePrefix(nodeType) + "@"

Review Comment:
   No test covers this mapping, i.e. which ID ends up in the replacement CN for 
each RPC:
   - `getDataNodeCertificate`: datanode UUID
   - `getOMCertificate`: OM UUID
   - `getCertificate`: `nodeDetails.getUuid()` (Recon)
   - `getSCMCertificate`: SCM ID from the CSR OU
   
   A swapped prefix or ID would go unnoticed. Also, `getScmId(csr)` returns 
null for a CSR without an OU, which gives `scm-sub@null`.
   
   There is no unit test class for `SCMSecurityProtocolServer` yet. A test that 
sends a long CN CSR through each RPC and checks the issued CN would cover this, 
since this is the only path that fixes CSRs from older clients.



##########
hadoop-ozone/cli-repair/src/main/java/org/apache/hadoop/ozone/repair/scm/cert/RecoverSCMCertificate.java:
##########
@@ -149,12 +154,23 @@ private static X509Certificate getRootCertificate(
     return cert.get();
   }
 
+  /** This SCM's ID, or null if its storage cannot be read: then certificates 
are only matched by host name. */
+  private String getScmId() {

Review Comment:
   The new SCM ID matching, and the fallback when `SCMStorageConfig` can't be 
read, have no test. `public-key-cert-recovery-test.sh` runs with short 
hostnames, so it only exercises the hostname match.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/security/OMCertificateClient.java:
##########
@@ -94,6 +94,8 @@ public CertificateSignRequest.Builder configureCSRBuilder()
       // isn't a hostname either, so omit username.
       subject = hostname;
     }
+    subject = CertificateSignRequest.commonName(subject,

Review Comment:
   When the CSR has no DNS SAN, the CN is the bare hostname so hostname 
verification can fall back to it (comment above). With a hostname over 64 chars 
it now becomes `om@<UUID>`, so that fallback no longer matches.
   
   This only happens when the canonical hostname is not a valid DNS name, and 
`addInetAddresses` already warns that by-name TLS fails then, so likely minor. 
It is one case where the CN is used for identity, though, and it is untested 
either way.



##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/authority/TestDefaultCAServer.java:
##########
@@ -203,6 +205,78 @@ public void testRequestCertificate() throws Exception {
 
   }
 
+  /**
+   * Tests that a CSR whose subject is longer than the RFC 5280 ub-common-name 
(64), like user@FQDN of a
+   * Kubernetes pod, is signed and the CN is kept.
+   */
+  @Test
+  public void testRequestCertificateWithLongSubject() throws Exception {

Review Comment:
   Missing case: a CA whose own CN is over 64 chars signing a new cert, i.e. a 
cluster whose root CA was created before BC 1.85. `DefaultApprover` copies the 
issuer from the CA cert's encoded subject, so it should work, but a test with a 
long CA subject plus chain verification would pin it.



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