Skip to content

Commit

Permalink
Change AcmeService to return Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo committed Mar 16, 2022
1 parent a33236c commit 6a5dd44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.acme.events;

import io.micronaut.core.annotation.NonNull;
import java.security.KeyPair;
import java.security.cert.X509Certificate;

Expand Down Expand Up @@ -82,6 +83,7 @@ public boolean isValidationCert() {
*
* @return array of certificates in the chain.
*/
@NonNull
public X509Certificate[] getFullCertificateChain() {
return fullCertificateChain;
}
Expand Down
28 changes: 13 additions & 15 deletions acme/src/main/java/io/micronaut/acme/services/AcmeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.micronaut.acme.challenge.http.endpoint.HttpChallengeDetails;
import io.micronaut.acme.events.CertificateEvent;
import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.io.IOUtils;
import io.micronaut.core.io.ResourceResolver;
import io.micronaut.scheduling.TaskScheduler;
Expand Down Expand Up @@ -145,24 +145,22 @@ public X509Certificate getCurrentCertificate() {
*
* @return array of each of the certificates in the chain
*/
@Nullable
protected X509Certificate[] getFullCertificateChain() {
@NonNull
protected Optional<X509Certificate[]> getFullCertificateChain() {
try {
CertificateFactory cf = CertificateFactory.getInstance(X509_CERT);
File certificate = new File(certLocation, DOMAIN_CRT);
if (certificate.exists()) {
return cf.generateCertificates(Files.newInputStream(certificate.toPath())).stream()
return Optional.of(cf.generateCertificates(Files.newInputStream(certificate.toPath())).stream()
.map(X509Certificate.class::cast)
.toArray(X509Certificate[]::new);
} else {
return null;
.toArray(X509Certificate[]::new));
}
} catch (CertificateException | IOException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Could not create certificate from file", e);
}
return null;
}
return Optional.empty();
}

/**
Expand Down Expand Up @@ -305,9 +303,9 @@ private boolean writeCombinedFile(Certificate certificate) {
try (BufferedWriter writer = Files.newBufferedWriter(domainCsr.toPath(), WRITE, CREATE, TRUNCATE_EXISTING)) {
certificate.writeCertificate(writer);
}
X509Certificate[] chain = getFullCertificateChain();
if (chain != null) {
eventPublisher.publishEvent(new CertificateEvent(domainKeyPair, false, chain));
Optional<X509Certificate[]> chainOptional = getFullCertificateChain();
if (chainOptional.isPresent()) {
eventPublisher.publishEvent(new CertificateEvent(domainKeyPair, false, chainOptional.get()));
if (LOG.isInfoEnabled()) {
LOG.info("ACME certificate order success! Certificate URL: {}", certificate.getLocation());
}
Expand Down Expand Up @@ -517,13 +515,13 @@ private void doChallengeSpecificSetup(Authorization auth, Challenge challenge) t
* Setup the certificate that has been saved to disk and configures it for use.
*/
public void setupCurrentCertificate() {
X509Certificate[] fullCertificateChain = getFullCertificateChain();
if (fullCertificateChain == null) {
Optional<X509Certificate[]> fullCertificateChainOptional = getFullCertificateChain();
if (fullCertificateChainOptional.isPresent()) {
eventPublisher.publishEvent(new CertificateEvent(getDomainKeyPair(), false, fullCertificateChainOptional.get()));
} else {
if (LOG.isErrorEnabled()) {
LOG.error("ACME certificate chain could not be loaded from file.");
}
} else {
eventPublisher.publishEvent(new CertificateEvent(getDomainKeyPair(), false, fullCertificateChain));
}
}

Expand Down

0 comments on commit 6a5dd44

Please sign in to comment.