Galsza commented on code in PR #4808:
URL: https://github.com/apache/ozone/pull/4808#discussion_r1221268314
##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/client/DefaultCertificateClient.java:
##########
@@ -340,20 +349,52 @@ public synchronized List<X509Certificate> getTrustChain()
{
}
} else {
// case before certificate bundle is supported
- chain.add(getCertificate());
- X509Certificate cert = getCACertificate();
- if (cert != null) {
- chain.add(getCACertificate());
- }
- cert = getRootCACertificate();
- if (cert != null) {
- chain.add(cert);
+ X509Certificate lastInsertedCert = getCertificate();
+ chain.add(lastInsertedCert);
+ List<X509Certificate> caCertList = getCaCertList();
+ while (!isRootCa(lastInsertedCert)) {
+ Optional<X509Certificate> issuerOpt =
+ getIssuerForCert(lastInsertedCert, caCertList);
+ if (issuerOpt.isPresent()) {
+ X509Certificate issuer = issuerOpt.get();
+ chain.add(issuer);
+ lastInsertedCert = issuer;
+ } else {
+ throw new CertificateException("No issuer found for certificate: " +
+ lastInsertedCert);
+ }
}
+ //add self-signed certificate to the chain
+ chain.add(lastInsertedCert);
}
return chain;
}
+ private boolean isRootCa(X509Certificate cert) {
+ return
cert.getSubjectX500Principal().equals(cert.getIssuerX500Principal());
+ }
+
+ private Optional<X509Certificate> getIssuerForCert(X509Certificate cert,
+ Iterable<X509Certificate> issuerCerts) {
+ for (X509Certificate issuer : issuerCerts) {
+ if (cert.getIssuerX500Principal().equals(
+ issuer.getSubjectX500Principal())) {
+ return Optional.of(issuer);
+ }
+ }
+ return Optional.empty();
+ }
+
+ private List<X509Certificate> getCaCertList() throws IOException {
Review Comment:
Thanks, I'll use it here. There are a lot of cert conversation related code
all over the place, maybe we should create a utility class for it. (After your
refactors)
--
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]