Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use java 17 for SonarCloud #49

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/sonarcloud-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: zulu
java-version: 11
java-version: 17
- name: Cache SonarCloud packages
uses: actions/cache@v3
with:
Expand All @@ -28,7 +28,7 @@ jobs:
with:
path: ~/.m2
key: ${{ runner.os }}-m2-v11-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2-v11
restore-keys: ${{ runner.os }}-m2-v17
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<maven-source-plugin.version>3.3.0</maven-source-plugin.version>
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
<jacoco.version>0.8.5</jacoco.version>
<jacoco.version>0.8.8</jacoco.version>
<sonar.coverage.jacoco.xmlReportPaths>
${project.basedir}/../jacoco-coverage-report/target/site/jacoco-aggregate/jacoco.xml
</sonar.coverage.jacoco.xmlReportPaths>
Expand Down Expand Up @@ -113,7 +113,7 @@
</execution>
</executions>
</plugin>
<!-- Build Javacdoc JAR during packaging -->
<!-- Build Javadoc JAR during packaging -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand Down Expand Up @@ -152,7 +152,7 @@

<!-- For publishing the library to GitHub Packages/GitLab Package Repository -->
<distributionManagement>
<!-- Github Packages does not currently support public access, so disabled until it does.
<!-- GitHub Packages does not currently support public access, so disabled until it does.
See https://github.community/t/download-from-github-package-registry-without-authentication/14407
<repository>
<id>github</id>
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/eu/webeid/security/util/DateAndTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public static void requirePositiveDuration(Duration duration, String fieldName)

public static class DefaultClock implements Clock {

public static final Clock INSTANCE = new DefaultClock();
// Allows mocking of time-dependent behavior with Mockito.mockStatic().
private static final Clock instance = new DefaultClock();

public static Clock getInstance() {
return instance;
}

@Override
public Date now() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public SubjectCertificateExpiryValidator(Set<TrustAnchor> trustedCACertificateAn
*/
public void validateCertificateExpiry(X509Certificate subjectCertificate) throws AuthTokenException {
// Use the clock instance so that the date can be mocked in tests.
final Date now = DateAndTime.DefaultClock.INSTANCE.now();
final Date now = DateAndTime.DefaultClock.getInstance().now();
CertificateValidator.trustedCACertificatesAreValidOnDate(trustedCACertificateAnchors, now);
LOG.debug("CA certificates are valid.");
CertificateValidator.certificateIsValidOnDate(subjectCertificate, now, "User");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public SubjectCertificateTrustedValidator(Set<TrustAnchor> trustedCACertificateA
*/
public void validateCertificateTrusted(X509Certificate subjectCertificate) throws AuthTokenException {
// Use the clock instance so that the date can be mocked in tests.
final Date now = DateAndTime.DefaultClock.INSTANCE.now();
final Date now = DateAndTime.DefaultClock.getInstance().now();
subjectCertificateIssuerCertificate = CertificateValidator.validateIsSignedByTrustedCA(
subjectCertificate,
trustedCACertificateAnchors,
Expand Down
35 changes: 6 additions & 29 deletions src/test/java/eu/webeid/security/testutil/Dates.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,19 @@
package eu.webeid.security.testutil;

import com.fasterxml.jackson.databind.util.StdDateFormat;
import io.jsonwebtoken.Clock;
import eu.webeid.security.util.DateAndTime;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.util.Date;

public final class Dates {
private static final StdDateFormat STD_DATE_FORMAT = new StdDateFormat();

public static Date create(String iso8601Date) throws ParseException {
return STD_DATE_FORMAT.parse(iso8601Date);
}

public static void setMockedCertificateValidatorDate(Date mockedDate) throws NoSuchFieldException, IllegalAccessException {
setClockField(DateAndTime.DefaultClock.class, mockedDate);
}

public static void resetMockedCertificateValidatorDate() throws NoSuchFieldException, IllegalAccessException {
setClockField(DateAndTime.DefaultClock.class, new Date());
}

private static void setClockField(Class<? extends Clock> cls, Date date) throws NoSuchFieldException, IllegalAccessException {
final Field clockField = cls.getDeclaredField("INSTANCE");
setFinalStaticField(clockField, (Clock) () -> date);
}

private static void setFinalStaticField(Field field, Object newValue) throws NoSuchFieldException, IllegalAccessException {
field.setAccessible(true);

final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

field.set(null, newValue);
public static Date create(String iso8601Date) {
try {
return STD_DATE_FORMAT.parse(iso8601Date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,32 @@

import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import eu.webeid.security.authtoken.WebEidAuthToken;
import eu.webeid.security.exceptions.*;
import eu.webeid.security.exceptions.AuthTokenException;
import eu.webeid.security.exceptions.AuthTokenParseException;
import eu.webeid.security.exceptions.CertificateDecodingException;
import eu.webeid.security.exceptions.CertificateExpiredException;
import eu.webeid.security.exceptions.CertificateNotTrustedException;
import eu.webeid.security.exceptions.CertificateNotYetValidException;
import eu.webeid.security.exceptions.UserCertificateDisallowedPolicyException;
import eu.webeid.security.exceptions.UserCertificateMissingPurposeException;
import eu.webeid.security.exceptions.UserCertificateRevokedException;
import eu.webeid.security.exceptions.UserCertificateWrongPurposeException;
import eu.webeid.security.testutil.AbstractTestWithValidator;
import eu.webeid.security.testutil.AuthTokenValidators;
import eu.webeid.security.testutil.Dates;
import eu.webeid.security.util.DateAndTime;
import io.jsonwebtoken.Clock;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import java.security.cert.CertificateException;
import java.text.ParseException;
import java.util.Date;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mockStatic;

class AuthTokenCertificateTest extends AbstractTestWithValidator {

Expand All @@ -56,21 +69,20 @@ class AuthTokenCertificateTest extends AbstractTestWithValidator {
private static final String EXPIRED_ECDSA_CERT = "MIIF0TCCA7mgAwIBAgIQMBVFXroEt3hZ8FHcKKE65TANBgkqhkiG9w0BAQsFADBjMQswCQYDVQQGEwJFRTEiMCAGA1UECgwZQVMgU2VydGlmaXRzZWVyaW1pc2tlc2t1czEXMBUGA1UEYQwOTlRSRUUtMTA3NDcwMTMxFzAVBgNVBAMMDkVTVEVJRC1TSyAyMDE1MB4XDTE3MTAyNTA4NTcwMFoXDTIxMDIxMDIxNTk1OVowgYsxCzAJBgNVBAYTAkVFMQ8wDQYDVQQKDAZFU1RFSUQxFzAVBgNVBAsMDmF1dGhlbnRpY2F0aW9uMR4wHAYDVQQDDBVUT09NLE1BUlQsMzc2MDIwNDAzMzQxDTALBgNVBAQMBFRPT00xDTALBgNVBCoMBE1BUlQxFDASBgNVBAUTCzM3NjAyMDQwMzM0MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAExS1YQQBDLVvOi0a2GA5Y34AXODpx0AL8eKDOB7Bj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GCysGAQQBg5F/AQIBMCMwIQYIKwYBBQUHAgEWFWh0dHBzOi8vd3d3LnNrLmVlL0NQUzAIBgYEAI96AQIwHwYDVR0RBBgwFoEUMzgwMDEwODU3MThAZWVzdGkuZWUwHQYDVR0OBBYEFEQA6/1GXJtp+6czUzorhEJ7B95pMGEGCCsGAQUFBwEDBFUwUzBRBgYEAI5GAQUwRzBFFj9odHRwczovL3NrLmVlL2VuL3JlcG9zaXRvcnkvY29uZGl0aW9ucy1mb3ItdXNlLW9mLWNlcnRpZmljYXRlcy8TAkVOMCAGA1UdJQEB/wQWMBQGCCsGAQUFBwMCBggrBgEFBQcDBDAfBgNVHSMEGDAWgBTAhJkpxE6fOwI09pnhClYACCk+ezB/BggrBgEFBQcBAQRzMHEwLAYIKwYBBQUHMAGGIGh0dHA6Ly9haWEuZGVtby5zay5lZS9lc3RlaWQyMDE4MEEGCCsGAQUFBzAChjVodHRwczovL3NrLmVlL3VwbG9hZC9maWxlcy9URVNUX29mX0VTVEVJRDIwMTguZGVyLmNydDAzBgNVHR8ELDAqMCigJqAkhiJodHRwOi8vYy5zay5lZS90ZXN0X2VzdGVpZDIwMTguY3JsMAoGCCqGSM49BAMEA4GLADCBhwJBcmcfLC+HcSJ6BuRrDGL+K+7BAW8BfAiiWWAuBV4ebLkbbAWmkc9dSKgr4BEGEt90xDTQ85yW4SjGulFXu9C3yQsCQgETaXTs3Hp6vDAcQYL8Bx4BO3DwJbDuD4BUJyT0+9HQiFCQmTQ4xrNjeaeOwRWyMOM9z5ORMeJCiQUyil1x4YPIbg==";

private MockedStatic<DateAndTime.DefaultClock> mockedClock;

@Override
@BeforeEach
protected void setup() {
super.setup();
mockedClock = mockStatic(DateAndTime.DefaultClock.class);
// Ensure that the certificates do not expire.
mockDate("2021-08-01");
}

@AfterEach
public void tearDown() {
try {
Dates.resetMockedCertificateValidatorDate();
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
mockedClock.close();
}

@Test
Expand Down Expand Up @@ -275,11 +287,8 @@ void whenCertificateCaIsNotPartOfTrustChain_thenValidationFails() throws Excepti
}

private void mockDate(String date) {
try {
Dates.setMockedCertificateValidatorDate(Dates.create(date));
} catch (ParseException | NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
final Date theDate = Dates.create(date);
mockedClock.when(DateAndTime.DefaultClock::getInstance).thenReturn((Clock) () -> theDate);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ void whenOcspUrlIsInvalid_thenThrows() throws Exception {

@Test
void whenOcspRequestFails_thenThrows() throws Exception {
final OcspServiceProvider ocspServiceProvider = getDesignatedOcspServiceProvider("https://web-eid-test.free.beeceptor.com");
final OcspServiceProvider ocspServiceProvider = getDesignatedOcspServiceProvider("http://demo.sk.ee/ocsps");
final SubjectCertificateNotRevokedValidator validator = new SubjectCertificateNotRevokedValidator(trustedValidator, ocspClient, ocspServiceProvider);
assertThatCode(() ->
validator.validateCertificateNotRevoked(estEid2018Cert))
.isInstanceOf(UserCertificateOCSPCheckFailedException.class)
.cause()
.isInstanceOf(IOException.class)
.hasMessageStartingWith("OCSP request was not successful, response: (POST https://web-eid-test.free.beeceptor.com) 404");
.hasMessageStartingWith("OCSP request was not successful, response: (POST http://demo.sk.ee/ocsps) 404");
}

@Test
Expand Down
Loading