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


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/client/SCMCertificateClient.java:
##########
@@ -386,8 +388,8 @@ public CertificateServer initializeRootCertificateServer(
       SecurityConfig config, CertificateStore scmCertStore,
       BigInteger rootCertId, PKIProfile pkiProfile, String component)
       throws IOException {
-    String subject = SCM_ROOT_CA_PREFIX +
-        InetAddress.getLocalHost().getHostName();
+    String subject = CertificateSignRequest.commonName(SCM_ROOT_CA_PREFIX +

Review Comment:
   Root CA rotation doesn't go through this path. `RootCARotationManager` 
creates the new root CA with `HASecurityUtils.initializeRootCertificateServer`, 
which still builds `SCM_ROOT_CA_PREFIX + 
InetAddress.getLocalHost().getHostName()`. Since `SelfSignedCertificate` now 
uses the typed builder, rotation on an SCM with a long hostname issues a root 
CA with a CN over 64 chars (on master it throws and the rotation manager shuts 
SCM down).
   
   Needs the same `commonName(..., SCM_ROOT_CA, scmStorageConfig.getScmId())` 
there, plus a rotation test with a long hostname.
   
   This also matters for existing clusters: rotation is the only way they 
replace a long root CA CN, which is copied into the issuer of every cert it 
signs. Those certs still validate (SUN and BC providers and `openssl verify` 
accept a long CN on read), so this is about conformance only.



##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/utils/TestCertificateSignRequest.java:
##########
@@ -270,6 +283,66 @@ public void testCsrSerialization() throws Exception {
     assertEquals(csr, dsCsr);
   }
 
+  @Test
+  public void testDistinguishedNameEncodingUnchanged() throws Exception {
+    String clusterID = UUID.randomUUID().toString();
+    String scmID = UUID.randomUUID().toString();
+    String subject = "[email protected]";
+
+    assertArrayEquals(
+        new X500Name(String.format(DISTINGUISHED_NAME_FORMAT, subject, scmID, 
clusterID)).getEncoded(),
+        getDistinguishedName(subject, scmID, clusterID).getEncoded());
+    assertArrayEquals(
+        new X500Name(String.format(DISTINGUISHED_NAME_WITH_SN_FORMAT, subject, 
scmID, clusterID, "12345"))
+            .getEncoded(),
+        getDistinguishedNameWithSN(subject, scmID, clusterID, 
"12345").getEncoded());
+    // A datanode sends null SCM and cluster IDs before registration.
+    assertArrayEquals(
+        new X500Name(String.format(DISTINGUISHED_NAME_FORMAT, subject, null, 
null)).getEncoded(),
+        getDistinguishedName(subject, null, null).getEncoded());
+  }
+
+  @Test
+  public void testCommonName() {
+    String id = UUID.randomUUID().toString();
+    String fits = "dn@" + StringUtils.repeat('a', 61);
+    assertEquals(64, fits.length());
+    assertEquals(fits, CertificateSignRequest.commonName(fits, "dn", id));
+    // one character too long, and a long user@FQDN: the same prefix@id, 
whatever the host names have in common
+    assertEquals("dn@" + id, CertificateSignRequest.commonName(fits + "b", 
"dn", id));
+    assertEquals("dn@" + id, CertificateSignRequest.commonName(LONG_SUBJECT, 
"dn", id));
+
+    assertEquals("dn", 
CertificateSignRequest.commonNamePrefix(NodeType.DATANODE));
+    assertEquals("om", CertificateSignRequest.commonNamePrefix(NodeType.OM));
+    assertEquals("recon", 
CertificateSignRequest.commonNamePrefix(NodeType.RECON));
+    assertEquals(OzoneConsts.SCM_SUB_CA, 
CertificateSignRequest.commonNamePrefix(NodeType.SCM));
+  }
+
+  @Test
+  public void testGenerateCSRWithLongSubject() throws Exception {
+    String clusterID = UUID.randomUUID().toString();
+    String scmID = UUID.randomUUID().toString();
+    assertThat(LONG_SUBJECT.length()).isGreaterThan(64);

Review Comment:
   nit: this checks that the subject is long, not that the string path still 
rejects it. An assertion here documents why the typed builder is needed and 
shows when it can go:
   
   ```java
   assertThrows(IllegalArgumentException.class,
       () -> new X500Name(String.format(DISTINGUISHED_NAME_FORMAT, 
LONG_SUBJECT, scmID, clusterID)));
   ```
   
   Bouncy Castle 1.86 accepts a 64 char CN and rejects 65, so a 65 char subject 
would also pin the boundary.



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/utils/CertificateSignRequest.java:
##########
@@ -106,26 +110,61 @@ private CertificateSignRequest(String subject, String 
scmID, String clusterID,
     this.extensions = extensions;
   }
 
-  public static String getDistinguishedNameFormat() {
-    return DISTINGUISHED_NAME_FORMAT;
-  }
-
-  public static String getDistinguishedNameFormatWithSN() {
-    return DISTINGUISHED_NAME_WITH_SN_FORMAT;
-  }
-
   // used by server side DN regeneration
   public static X500Name getDistinguishedNameWithSN(String subject,
       String scmID, String clusterID, String serialID) {
-    return new X500Name(String.format(DISTINGUISHED_NAME_WITH_SN_FORMAT,
-        subject, scmID, clusterID, serialID));
+    return distinguishedNameBuilder(subject, scmID, clusterID)
+        .addRDN(BCStyle.SERIALNUMBER, new 
DERPrintableString(String.valueOf(serialID)))
+        .build();
   }
 
   // used by client side DN generation
   public static X500Name getDistinguishedName(String subject, String scmID,
       String clusterID) {
-    return new X500Name(String.format(getDistinguishedNameFormat(), subject,
-        scmID, clusterID));
+    return distinguishedNameBuilder(subject, scmID, clusterID).build();
+  }
+
+  /**
+   * The commonName of a certificate subject: {@code preferred} when it fits 
RFC 5280 ub-common-name
+   * ({@value #MAX_COMMON_NAME_LENGTH}), else {@code prefix@id}, e.g. {@code 
dn@<datanode UUID>} for a user@FQDN on
+   * Kubernetes. Truncating {@code preferred} instead could give hosts whose 
names differ only at the end the same
+   * commonName; {@code id} is unique to the requester and does not change 
with its host name.
+   */
+  public static String commonName(String preferred, String prefix, String id) {

Review Comment:
   A potential simpler alternative: apply the length rule inside 
`getDistinguishedName` and `getDistinguishedNameWithSN`. Every subject already 
goes through them (client CSRs, `DefaultApprover` re-signing, 
`SelfSignedCertificate`, rotation), so a fallback derived from the CN itself, 
e.g. `user@<first DNS label>`, then truncated to 64, needs no IDs.
   
   Trade-off: a shortened CN is not guaranteed unique across domains. Tho 
nothing relies on CN uniqueness (TLS checks the SAN, the CA is per cluster). 
For StatefulSet pods the ordinal is at the end of the first label 
(`ozone-dn-0.ozone-dn.ns.svc.cluster.local`), so `-0` and `-1` stay distinct.



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