Skip to content

Commit 8b598e3

Browse files
committed
AUT-2758 Refactor creating RevocationInfo objects in ResilientOcspCertificateRevocationChecker
1 parent d657506 commit 8b598e3

2 files changed

Lines changed: 28 additions & 29 deletions

File tree

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

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -291,33 +291,22 @@ private RevocationInfo request(OcspService ocspService, X509Certificate subjectC
291291
responseTime = Instant.now();
292292
requestDuration = Duration.between(requestTime, responseTime);
293293
RevocationInfo revocationInfo = getRevocationInfo(ocspResponderUri, e, request, null, requestDuration, responseTime);
294-
revocationInfo.ocspResponseAttributes().put(RevocationInfo.KEY_OCSP_RESPONSE, e.getResponseBody());
295-
revocationInfo.ocspResponseAttributes().put(RevocationInfo.KEY_HTTP_STATUS_CODE, e.getStatusCode());
294+
revocationInfo = revocationInfo
295+
.withOcspResponse(e.getResponseBody())
296+
.withHttpStatusCode(e.getStatusCode());
296297
throw new ResilientUserCertificateOCSPCheckFailedException(new ValidationInfo(subjectCertificate, List.of(revocationInfo)));
297298
}
298299
if (response.getStatus() != OCSPResponseStatus.SUCCESSFUL) {
299300
ResilientUserCertificateOCSPCheckFailedException exception = new ResilientUserCertificateOCSPCheckFailedException("Response status: " + ocspStatusToString(response.getStatus()));
300-
RevocationInfo revocationInfo = new RevocationInfo(ocspService.getAccessLocation(), new HashMap<>(Map.ofEntries(
301-
Map.entry(RevocationInfo.KEY_OCSP_ERROR, exception),
302-
Map.entry(RevocationInfo.KEY_OCSP_REQUEST, request),
303-
Map.entry(RevocationInfo.KEY_OCSP_RESPONSE, response),
304-
Map.entry(RevocationInfo.KEY_REQUEST_DURATION, requestDuration),
305-
Map.entry(RevocationInfo.KEY_OCSP_RESPONSE_TIME, responseTime)
306-
)));
301+
RevocationInfo revocationInfo = getRevocationInfo(ocspResponderUri, exception, request, response, requestDuration, responseTime);
307302
exception.setValidationInfo(new ValidationInfo(subjectCertificate, List.of(revocationInfo)));
308303
throw exception;
309304
}
310305

311306
final BasicOCSPResp basicResponse = (BasicOCSPResp) response.getResponseObject();
312307
if (basicResponse == null) {
313308
ResilientUserCertificateOCSPCheckFailedException exception = new ResilientUserCertificateOCSPCheckFailedException("Missing Basic OCSP Response");
314-
RevocationInfo revocationInfo = new RevocationInfo(ocspService.getAccessLocation(), new HashMap<>(Map.ofEntries(
315-
Map.entry(RevocationInfo.KEY_OCSP_ERROR, exception),
316-
Map.entry(RevocationInfo.KEY_OCSP_REQUEST, request),
317-
Map.entry(RevocationInfo.KEY_OCSP_RESPONSE, response),
318-
Map.entry(RevocationInfo.KEY_REQUEST_DURATION, requestDuration),
319-
Map.entry(RevocationInfo.KEY_OCSP_RESPONSE_TIME, responseTime)
320-
)));
309+
RevocationInfo revocationInfo = getRevocationInfo(ocspResponderUri, exception, request, response, requestDuration, responseTime);
321310
exception.setValidationInfo(new ValidationInfo(subjectCertificate, List.of(revocationInfo)));
322311
throw exception;
323312
}
@@ -329,12 +318,7 @@ private RevocationInfo request(OcspService ocspService, X509Certificate subjectC
329318
}
330319
LOG.debug("OCSP response verified successfully");
331320

332-
return new RevocationInfo(ocspResponderUri,new HashMap<>( Map.ofEntries(
333-
Map.entry(RevocationInfo.KEY_OCSP_REQUEST, request),
334-
Map.entry(RevocationInfo.KEY_OCSP_RESPONSE, response),
335-
Map.entry(RevocationInfo.KEY_REQUEST_DURATION, requestDuration),
336-
Map.entry(RevocationInfo.KEY_OCSP_RESPONSE_TIME, responseTime)
337-
)));
321+
return getRevocationInfo(ocspResponderUri, null, request, response, requestDuration, responseTime);
338322
} catch (ResilientUserCertificateOCSPCheckFailedException e) {
339323
throw e;
340324
} catch (UserCertificateRevokedException e) {
@@ -355,20 +339,23 @@ private RevocationInfo request(OcspService ocspService, X509Certificate subjectC
355339

356340
private RevocationInfo getRevocationInfo(URI ocspResponderUri, Exception e, OCSPReq request, OCSPResp response,
357341
Duration requestDuration, Instant end) {
358-
RevocationInfo revocationInfo = new RevocationInfo(ocspResponderUri, new HashMap<>(Map.of(RevocationInfo.KEY_OCSP_ERROR, e)));
342+
Map<String, Object> ocspResponseAttributes = new HashMap<>();
343+
if (e != null) {
344+
ocspResponseAttributes.put(RevocationInfo.KEY_OCSP_ERROR, e);
345+
}
359346
if (request != null) {
360-
revocationInfo.ocspResponseAttributes().put(RevocationInfo.KEY_OCSP_REQUEST, request);
347+
ocspResponseAttributes.put(RevocationInfo.KEY_OCSP_REQUEST, request);
361348
}
362349
if (response != null) {
363-
revocationInfo.ocspResponseAttributes().put(RevocationInfo.KEY_OCSP_RESPONSE, response);
350+
ocspResponseAttributes.put(RevocationInfo.KEY_OCSP_RESPONSE, response);
364351
}
365352
if (requestDuration != null) {
366-
revocationInfo.ocspResponseAttributes().put(RevocationInfo.KEY_REQUEST_DURATION, requestDuration);
353+
ocspResponseAttributes.put(RevocationInfo.KEY_REQUEST_DURATION, requestDuration);
367354
}
368355
if (end != null) {
369-
revocationInfo.ocspResponseAttributes().put(RevocationInfo.KEY_OCSP_RESPONSE_TIME, end);
356+
ocspResponseAttributes.put(RevocationInfo.KEY_OCSP_RESPONSE_TIME, end);
370357
}
371-
return revocationInfo;
358+
return new RevocationInfo(ocspResponderUri, ocspResponseAttributes);
372359
}
373360

374361
private static CircuitBreakerConfig getCircuitBreakerConfig(CircuitBreakerConfig circuitBreakerConfig) {

src/main/java/eu/webeid/security/validator/revocationcheck/RevocationInfo.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,20 @@ public record RevocationInfo(URI ocspResponderUri, Map<String, Object> ocspRespo
3838
public static final String KEY_OCSP_RESPONSE_TIME = "OCSP_RESPONSE_TIME";
3939

4040
public RevocationInfo withCircuitBreakerStatistics(ResilientOcspCertificateRevocationChecker.CircuitBreakerStatistics circuitBreakerStatistics) {
41+
return withAdditionalField(KEY_CIRCUIT_BREAKER_STATISTICS, circuitBreakerStatistics);
42+
}
43+
44+
public RevocationInfo withOcspResponse(byte[] ocspResponse) {
45+
return withAdditionalField(KEY_OCSP_RESPONSE, ocspResponse);
46+
}
47+
48+
public RevocationInfo withHttpStatusCode(Integer statusCode) {
49+
return withAdditionalField(KEY_HTTP_STATUS_CODE, statusCode);
50+
}
51+
52+
private RevocationInfo withAdditionalField(String key, Object value) {
4153
Map<String, Object> newOcspResponseAttributes = new HashMap<>(ocspResponseAttributes);
42-
newOcspResponseAttributes.put(KEY_CIRCUIT_BREAKER_STATISTICS, circuitBreakerStatistics);
54+
newOcspResponseAttributes.put(key, value);
4355
return new RevocationInfo(ocspResponderUri, newOcspResponseAttributes);
4456
}
4557
}

0 commit comments

Comments
 (0)