diff --git a/src/main/java/org/italiangrid/storm/webdav/oauth/utils/DefaultOidcConfigurationFetcher.java b/src/main/java/org/italiangrid/storm/webdav/oauth/utils/DefaultOidcConfigurationFetcher.java index f484a285..4515bf3c 100644 --- a/src/main/java/org/italiangrid/storm/webdav/oauth/utils/DefaultOidcConfigurationFetcher.java +++ b/src/main/java/org/italiangrid/storm/webdav/oauth/utils/DefaultOidcConfigurationFetcher.java @@ -21,6 +21,7 @@ import java.time.Duration; import java.util.Arrays; import java.util.Map; +import java.util.Objects; import org.italiangrid.storm.webdav.config.OAuthProperties; import org.slf4j.Logger; @@ -99,6 +100,9 @@ public Map loadConfigurationForIssuer(String issuer) { throw new OidcConfigurationResolutionError( format("Received status code: %s", response.getStatusCodeValue())); } + if (Objects.isNull(response.getBody())) { + throw new OidcConfigurationResolutionError("Received null body"); + } metadataChecks(issuer, response.getBody()); return response.getBody(); } diff --git a/src/main/java/org/italiangrid/storm/webdav/oauth/utils/NoExpirationStringCache.java b/src/main/java/org/italiangrid/storm/webdav/oauth/utils/NoExpirationStringCache.java index 7f7a70ee..4bc9e253 100644 --- a/src/main/java/org/italiangrid/storm/webdav/oauth/utils/NoExpirationStringCache.java +++ b/src/main/java/org/italiangrid/storm/webdav/oauth/utils/NoExpirationStringCache.java @@ -43,19 +43,22 @@ public Object getNativeCache() { @Override @Nullable protected Object lookup(Object key) { - return value; + return value; } @Override public void put(Object key, Object value) { + // Nothing to do } @Override public void evict(Object key) { + // Nothing to do } @Override public void clear() { + // Nothing to do } @SuppressWarnings("unchecked") @@ -63,4 +66,4 @@ public void clear() { public T get(Object key, Callable valueLoader) { return (T) fromStoreValue(value); } -} \ No newline at end of file +} diff --git a/src/test/java/org/italiangrid/storm/webdav/server/TLSConnectorBuilderTest.java b/src/test/java/org/italiangrid/storm/webdav/server/TLSConnectorBuilderTest.java index 70bbcb27..b7fcbba9 100644 --- a/src/test/java/org/italiangrid/storm/webdav/server/TLSConnectorBuilderTest.java +++ b/src/test/java/org/italiangrid/storm/webdav/server/TLSConnectorBuilderTest.java @@ -41,7 +41,7 @@ import eu.emi.security.authn.x509.X509CertChainValidatorExt; @ExtendWith(MockitoExtension.class) -public class TLSConnectorBuilderTest { +class TLSConnectorBuilderTest { @Test void tlsConnectorBuilderErrorTests() { diff --git a/src/test/java/org/italiangrid/storm/webdav/test/oauth/jwk/OidcConfigurationFetcherTest.java b/src/test/java/org/italiangrid/storm/webdav/test/oauth/jwk/OidcConfigurationFetcherTest.java index 5b8162ea..8630609b 100644 --- a/src/test/java/org/italiangrid/storm/webdav/test/oauth/jwk/OidcConfigurationFetcherTest.java +++ b/src/test/java/org/italiangrid/storm/webdav/test/oauth/jwk/OidcConfigurationFetcherTest.java @@ -55,6 +55,7 @@ import com.google.common.collect.Maps; import com.nimbusds.jose.KeySourceException; +import com.nimbusds.jose.RemoteKeySourceException; import com.nimbusds.jose.jwk.JWKSet; import com.nimbusds.jose.jwk.KeyType; @@ -62,7 +63,7 @@ class OidcConfigurationFetcherTest { static final String ISSUER = "https://iam-dev.cloud.cnaf.infn.it/"; - final static String JWK_URI = ISSUER + "jwk"; + static final String JWK_URI = ISSUER + "jwk"; static final String ANOTHER_ISSUER = "https://iam.cloud.infn.it/"; static final String ANOTHER_JWK_URI = ANOTHER_ISSUER + "jwk"; @@ -122,6 +123,18 @@ private OidcConfigurationFetcher getFetcher(ResponseEntity> lenient().when(restTemplate.exchange(any(), eq(typeReference))).thenReturn(wellKnownResponse); lenient().when(restTemplate.exchange(any(), eq(String.class))).thenReturn(jwkResponse); + return getFetcher(restTemplate); + } + + private OidcConfigurationFetcher getFetcherWithException(ResponseEntity> wellKnownResponse) { + + lenient().when(restTemplate.exchange(any(), eq(typeReference))).thenReturn(wellKnownResponse); + lenient().when(restTemplate.exchange(any(), eq(String.class))).thenThrow(new RuntimeException("ERROR")); + return getFetcher(restTemplate); + } + + private OidcConfigurationFetcher getFetcher(RestTemplate restTemplate) { + lenient().when(restBuilder.build()).thenReturn(restTemplate); lenient().when(restBuilder.setConnectTimeout(any())).thenReturn(restBuilder); lenient().when(restBuilder.setReadTimeout(any())).thenReturn(restBuilder); @@ -174,8 +187,7 @@ private OidcConfigurationFetcher getFetcherWithErrorOnFetch() throws RestClientE return getFetcher(mockedResponseMapEntity, null); } - private OidcConfigurationFetcher getFetcherWithErrorOnGetJwk() - throws RestClientException, IOException { + private OidcConfigurationFetcher getFetcherWithErrorOnGetJwk() throws RestClientException { ResponseEntity> mockedResponseMapEntity = getWellKnownResponse(OK, getMapWithIssuerAndJwkUri(ISSUER, JWK_URI)); @@ -183,6 +195,13 @@ private OidcConfigurationFetcher getFetcherWithErrorOnGetJwk() return getFetcher(mockedResponseMapEntity, mockedResponseStringEntity); } + private OidcConfigurationFetcher getFetcherWithRuntimeExceptionOnGetJwk() throws RestClientException { + + ResponseEntity> mockedResponseMapEntity = + getWellKnownResponse(OK, getMapWithIssuerAndJwkUri(ISSUER, JWK_URI)); + return getFetcherWithException(mockedResponseMapEntity); + } + @BeforeEach public void setDebugLevel() { System.setProperty("logging.level.org.italiangrid.storm", "DEBUG"); @@ -199,7 +218,7 @@ void fetchWellKnownEndpointWithSuccessTests() throws RestClientException, IOExce } @Test - void fetchWellKnownEndpointWithErrorTests() throws RestClientException, IOException { + void fetchWellKnownEndpointWithErrorTests() throws RestClientException { OidcConfigurationFetcher fetcher = getFetcherWithErrorOnFetch(); RuntimeException exception = assertThrows(RuntimeException.class, () -> { @@ -271,4 +290,18 @@ void fetchJWKEndpointWithErrorTests() throws RestClientException, IOException { assertEquals(expectedMessage, actualMessage); } + + @Test + void fetchJWKEndpointWithRuntimeException() throws RestClientException, IOException { + + OidcConfigurationFetcher fetcher = getFetcherWithRuntimeExceptionOnGetJwk(); + final URI jwkUri = URI.create(JWK_URI); + RemoteKeySourceException exception = assertThrows(RemoteKeySourceException.class, () -> { + fetcher.loadJWKSourceForURL(jwkUri); + }); + String expectedMessage = "Unable to get JWK from 'https://iam-dev.cloud.cnaf.infn.it/jwk'"; + String actualMessage = exception.getMessage(); + + assertEquals(expectedMessage, actualMessage); + } }