Search in sources :

Example 1 with SelfSignedCertificate

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);
}
Also used : LocalDateTime(java.time.LocalDateTime) SelfSignedCertificate(org.apache.hadoop.hdds.security.x509.certificates.utils.SelfSignedCertificate) CertificateCodec(org.apache.hadoop.hdds.security.x509.certificate.utils.CertificateCodec) IOException(java.io.IOException) LocalDate(java.time.LocalDate) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) DomainValidator(org.apache.commons.validator.routines.DomainValidator)

Aggregations

IOException (java.io.IOException)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 DomainValidator (org.apache.commons.validator.routines.DomainValidator)1 CertificateCodec (org.apache.hadoop.hdds.security.x509.certificate.utils.CertificateCodec)1 SelfSignedCertificate (org.apache.hadoop.hdds.security.x509.certificates.utils.SelfSignedCertificate)1 X509CertificateHolder (org.bouncycastle.cert.X509CertificateHolder)1