Skip to content

Commit 93fa0b4

Browse files
committed
AUT-2677 Use Optional for getFallbackService
1 parent 612ad2d commit 93fa0b4

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

src/main/java/eu/webeid/ocsp/service/AiaOcspService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.security.cert.X509Certificate;
3939
import java.util.Date;
4040
import java.util.Objects;
41+
import java.util.Optional;
4142
import java.util.Set;
4243

4344
import static eu.webeid.ocsp.protocol.IssuerCommonName.getIssuerCommonName;
@@ -77,8 +78,8 @@ public URI getAccessLocation() {
7778
}
7879

7980
@Override
80-
public FallbackOcspService getFallbackService() {
81-
return fallbackOcspService;
81+
public Optional<FallbackOcspService> getFallbackService() {
82+
return Optional.of(fallbackOcspService);
8283
}
8384

8485
@Override

src/main/java/eu/webeid/ocsp/service/OcspService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.net.URI;
2929
import java.util.Date;
30+
import java.util.Optional;
3031

3132
public interface OcspService {
3233

@@ -36,8 +37,8 @@ public interface OcspService {
3637

3738
void validateResponderCertificate(X509CertificateHolder cert, Date now) throws AuthTokenException;
3839

39-
default FallbackOcspService getFallbackService() {
40-
return null;
40+
default Optional<FallbackOcspService> getFallbackService() {
41+
return Optional.empty();
4142
}
4243

4344
}

src/main/java/eu/webeid/resilientocsp/ResilientOcspCertificateRevocationChecker.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.HashMap;
6363
import java.util.List;
6464
import java.util.Map;
65+
import java.util.Optional;
6566

6667
import static java.util.Objects.requireNonNull;
6768

@@ -105,12 +106,13 @@ public List<RevocationInfo> validateCertificateNotRevoked(X509Certificate subjec
105106
OcspService primaryService = resolvePrimaryOcspService(subjectCertificate);
106107
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(primaryService.getAccessLocation().toASCIIString());
107108

108-
if (primaryService.getFallbackService() == null) {
109+
Optional<FallbackOcspService> firstFallbackServiceOpt = primaryService.getFallbackService();
110+
if (firstFallbackServiceOpt.isEmpty()) {
109111
return List.of(request(primaryService, subjectCertificate, issuerCertificate, false));
110112
}
111113

112114
List<RevocationInfo> revocationInfoList = new ArrayList<>();
113-
CheckedSupplier<RevocationInfo> fallbackSupplier = buildFallbackSupplier(primaryService, subjectCertificate,
115+
CheckedSupplier<RevocationInfo> fallbackSupplier = buildFallbackSupplier(firstFallbackServiceOpt.get(), subjectCertificate,
114116
issuerCertificate, revocationInfoList);
115117
CheckedSupplier<RevocationInfo> decoratedSupplier = decorateWithResilience(primaryService, subjectCertificate,
116118
issuerCertificate, revocationInfoList, fallbackSupplier, circuitBreaker);
@@ -146,11 +148,10 @@ private CircuitBreakerStatistics createCircuitBreakerStatistics(CircuitBreaker c
146148
);
147149
}
148150

149-
private CheckedSupplier<RevocationInfo> buildFallbackSupplier(OcspService primaryService,
151+
private CheckedSupplier<RevocationInfo> buildFallbackSupplier(FallbackOcspService firstFallbackService,
150152
X509Certificate subjectCertificate,
151153
X509Certificate issuerCertificate,
152154
List<RevocationInfo> revocationInfoList) {
153-
final FallbackOcspService firstFallbackService = primaryService.getFallbackService();
154155
CheckedSupplier<RevocationInfo> firstFallbackSupplier = () -> {
155156
try {
156157
return request(firstFallbackService, subjectCertificate, issuerCertificate, true);
@@ -329,7 +330,7 @@ private RevocationInfo request(OcspService ocspService, X509Certificate subjectC
329330
RevocationInfo revocationInfo = getRevocationInfo(ocspResponderUri, e, request, response, requestDuration, responseTime);
330331
throw new ResilientUserCertificateRevokedException(new ValidationInfo(subjectCertificate, List.of(revocationInfo)));
331332
} catch (OCSPClientException e) {
332-
RevocationInfo revocationInfo = getRevocationInfo(ocspResponderUri, e, request, response, requestDuration, responseTime);
333+
RevocationInfo revocationInfo = getRevocationInfo(ocspResponderUri, e, request, null, null, null);
333334
revocationInfo.ocspResponseAttributes().put(RevocationInfo.KEY_OCSP_RESPONSE, e.getResponseBody());
334335
revocationInfo.ocspResponseAttributes().put(RevocationInfo.KEY_HTTP_STATUS_CODE, e.getStatusCode());
335336
throw new ResilientUserCertificateOCSPCheckFailedException(new ValidationInfo(subjectCertificate, List.of(revocationInfo)));

src/test/java/eu/webeid/resilientocsp/ResilientOcspCertificateRevocationCheckerTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.security.cert.X509Certificate;
4949
import java.util.List;
5050
import java.util.Map;
51+
import java.util.Optional;
5152

5253
import static eu.webeid.ocsp.OcspCertificateRevocationCheckerTest.getOcspResponseBytesFromResources;
5354
import static eu.webeid.security.testutil.AbstractTestWithValidator.VALID_AUTH_TOKEN;
@@ -284,7 +285,7 @@ void whenOneFallbackIsConfiguredAndPrimaryFails_thenRevocationInfoListShouldHave
284285
OcspService primaryService = mock(OcspService.class);
285286
when(primaryService.getAccessLocation()).thenReturn(PRIMARY_URI);
286287
when(primaryService.doesSupportNonce()).thenReturn(false);
287-
when(primaryService.getFallbackService()).thenReturn(fallbackService);
288+
when(primaryService.getFallbackService()).thenReturn(Optional.of(fallbackService));
288289

289290
OcspServiceProvider ocspServiceProvider = mock(OcspServiceProvider.class);
290291
when(ocspServiceProvider.getService(any())).thenReturn(primaryService);
@@ -315,7 +316,7 @@ void whenNoFallbacksAreConfigured_thenRevocationInfoListShouldHaveOneElement() t
315316
OcspService primaryService = mock(OcspService.class);
316317
when(primaryService.getAccessLocation()).thenReturn(PRIMARY_URI);
317318
when(primaryService.doesSupportNonce()).thenReturn(false);
318-
when(primaryService.getFallbackService()).thenReturn(null);
319+
when(primaryService.getFallbackService()).thenReturn(Optional.empty());
319320

320321
OcspServiceProvider ocspServiceProvider = mock(OcspServiceProvider.class);
321322
when(ocspServiceProvider.getService(any())).thenReturn(primaryService);
@@ -364,7 +365,7 @@ private ResilientOcspCertificateRevocationChecker buildChecker(OcspClient ocspCl
364365
OcspService primaryService = mock(OcspService.class);
365366
when(primaryService.getAccessLocation()).thenReturn(PRIMARY_URI);
366367
when(primaryService.doesSupportNonce()).thenReturn(false);
367-
when(primaryService.getFallbackService()).thenReturn(fallbackService);
368+
when(primaryService.getFallbackService()).thenReturn(Optional.of(fallbackService));
368369

369370
OcspServiceProvider ocspServiceProvider = mock(OcspServiceProvider.class);
370371
when(ocspServiceProvider.getService(any())).thenReturn(primaryService);

0 commit comments

Comments
 (0)