use of org.apache.hadoop.hdds.security.x509.certificates.utils.SelfSignedCertificate in project ozone by apache.
the class DefaultCAServer method generateRootCertificate.
/**
* Generates a self-signed Root Certificate for CA.
*
* @param securityConfig - SecurityConfig
* @param key - KeyPair.
* @throws IOException - on Error.
* @throws SCMSecurityException - on Error.
*/
private void generateRootCertificate(SecurityConfig securityConfig, KeyPair key) throws IOException, SCMSecurityException {
Preconditions.checkNotNull(this.config);
LocalDate beginDate = LocalDate.now().atStartOfDay().toLocalDate();
LocalDateTime temp = LocalDateTime.of(beginDate, LocalTime.MIDNIGHT);
LocalDate endDate = temp.plus(securityConfig.getMaxCertificateDuration()).toLocalDate();
SelfSignedCertificate.Builder builder = SelfSignedCertificate.newBuilder().setSubject(this.subject).setScmID(this.scmID).setClusterID(this.clusterID).setBeginDate(beginDate).setEndDate(endDate).makeCA().setConfiguration(securityConfig.getConfiguration()).setKey(key);
try {
DomainValidator validator = DomainValidator.getInstance();
// Add all valid ips.
OzoneSecurityUtil.getValidInetsForCurrentHost().forEach(ip -> {
builder.addIpAddress(ip.getHostAddress());
if (validator.isValid(ip.getCanonicalHostName())) {
builder.addDnsName(ip.getCanonicalHostName());
}
});
} catch (IOException e) {
throw new org.apache.hadoop.hdds.security.x509.exceptions.CertificateException("Error while adding ip to CA self signed certificate", e, CSR_ERROR);
}
X509CertificateHolder selfSignedCertificate = builder.build();
CertificateCodec certCodec = new CertificateCodec(config, componentName);
certCodec.writeCertificate(selfSignedCertificate);
}
Aggregations