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

Implement retry mechanism for SocketExceptions #29

Merged
merged 5 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 24 additions & 0 deletions src/main/java/no/ssb/guardian/RetryListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package no.ssb.guardian;

import io.micronaut.core.type.MutableArgumentValue;
import io.micronaut.retry.event.RetryEvent;
import io.micronaut.retry.event.RetryEventListener;
import jakarta.inject.Singleton;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Singleton
public class RetryListener implements RetryEventListener {
@Override
public void onApplicationEvent(RetryEvent event) {
final MutableArgumentValue<?> request = event.getSource().getParameters().get("request");
log.error("Request failed {} time(s) for {}:", event.getRetryState().currentAttempt(),
request != null ? request.getValue(): "<empty request>",
event.getThrowable());
}

@Override
public boolean supports(RetryEvent event) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.micronaut.http.annotation.Post;
import io.micronaut.http.hateoas.JsonError;
import io.micronaut.http.hateoas.Link;
import io.micronaut.retry.annotation.RetryPredicate;
import io.micronaut.retry.annotation.Retryable;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import lombok.AllArgsConstructor;
Expand All @@ -23,6 +25,7 @@
import no.ssb.guardian.maskinporten.config.MaskinportenClientConfig;
import no.ssb.guardian.maskinporten.config.MaskinportenConfig;

import java.net.SocketException;
import java.security.Principal;
import java.util.Set;

Expand All @@ -37,6 +40,7 @@ public class MaskinportenAccessTokenController {
private final MaskinportenConfig maskinportenConfig;

@Post("/maskinporten/access-token")
@Retryable(predicate = WrappedSocketExceptionRetryPredicate.class)
public HttpResponse<AccessTokenResponse> fetchMaskinportenAccessToken(Principal principal, FetchMaskinportenAccessTokenRequest request) {
log.info("Request: {}", request);
log.info("AUDIT {}", PrincipalUtil.auditInfoOf(principal));
Expand Down Expand Up @@ -130,6 +134,17 @@ public ClientRequestException(HttpStatus httpStatus, String errorTag, String mes

}

/**
* The {@link io.micronaut.retry.annotation.DefaultRetryPredicate} does not support wrapped exceptions, so, this
* prediate checks the exception {@code cause} for a {@link SocketException}.
*/
public static class WrappedSocketExceptionRetryPredicate implements RetryPredicate {
@Override
public boolean test(Throwable throwable) {
return throwable.getCause() != null && throwable.getCause().getClass().equals(SocketException.class);
}
}

@Data
@NoArgsConstructor @AllArgsConstructor
@Builder
Expand Down Expand Up @@ -168,6 +183,11 @@ public HttpResponse<JsonError> clientRequestError(HttpRequest request, Maskinpor
return error(request, e, e.getHttpStatus(), e.getMessage());
}

@Error
public HttpResponse<JsonError> runtimeError(HttpRequest request, RuntimeException e) {
metrics.incrementClientError("runtime-exception");
bjornandre marked this conversation as resolved.
Show resolved Hide resolved
return error(request, e, HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
private static HttpResponse<JsonError> error(HttpRequest request, Exception e, HttpStatus httpStatus, String httpStatusReason) {
JsonError error = new JsonError(e.getMessage())
.link(Link.SELF, Link.of(request.getUri()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package no.ssb.guardian.maskinporten;

import io.micronaut.http.HttpStatus;
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.test.annotation.MockBean;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import jakarta.inject.Inject;
import no.ssb.guardian.maskinporten.MaskinportenAccessTokenController.FetchMaskinportenAccessTokenRequest;
import no.ssb.guardian.maskinporten.client.MaskinportenClient;
import no.ssb.guardian.maskinporten.client.MaskinportenClientRegistry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.SocketException;
import java.util.Set;

import static io.restassured.RestAssured.*;
import static no.ssb.guardian.testsupport.KeycloakDevTokenIssuer.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

@MicronautTest
public class MaskinportenAccessTokenControllerRetryTest {

private final static String MASKINPORTEN_ACCESS_TOKEN_ENDPOINT = "/maskinporten/access-token";
private final static String MASKINPORTEN_DUMMY_ACCESS_TOKEN = "maskinporten-dummy-token";
private final static String MASKINPORTEN_CLIENT_ID_1 = "7ea43b76-6b7d-49e8-af2b-4114ebb66c80";
private final static Set<String> REQUESTED_SCOPES = Set.of("some:scope1");

@Inject
private EmbeddedServer embeddedServer;

@Inject
MaskinportenClientRegistry maskinportenClientRegistry;

@MockBean(MaskinportenClientRegistry.class)
MaskinportenClientRegistry maskinportenklientRegistry() {
return mock(MaskinportenClientRegistry.class);
}

private MaskinportenClient maskinportenClientMock = mock(MaskinportenClient.class);
@BeforeEach
void setUp() {
RestAssured.port = embeddedServer.getPort();
when(maskinportenClientRegistry.get(any())).thenReturn(maskinportenClientMock);
}

@Test
void validRequest_shouldRetryOnConnectionError() {
when(maskinportenClientMock.getAccessToken(anySet()))
.thenThrow(new RuntimeException(new SocketException(("Connection reset"))))
.thenReturn(MASKINPORTEN_DUMMY_ACCESS_TOKEN);

given()
.auth().oauth2(personalAccessToken("Kjell", "Fjell"))
.contentType(ContentType.JSON)
.when()
.body(FetchMaskinportenAccessTokenRequest.builder()
.maskinportenClientId(MASKINPORTEN_CLIENT_ID_1)
.scopes(REQUESTED_SCOPES)
.build()
)
.post(MASKINPORTEN_ACCESS_TOKEN_ENDPOINT)
.then()
.statusCode(HttpStatus.OK.getCode())
.body(
"accessToken", equalTo(MASKINPORTEN_DUMMY_ACCESS_TOKEN)
);
verify(maskinportenClientMock, times(2)).getAccessToken(anySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import static org.hamcrest.Matchers.startsWith;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anySet;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

@MicronautTest
public class MaskinportenAccessTokenControllerTest {
Expand All @@ -33,14 +32,15 @@ public class MaskinportenAccessTokenControllerTest {
private final static String MASKINPORTEN_DUMMY_ACCESS_TOKEN = "maskinporten-dummy-token";
private final static String MASKINPORTEN_CLIENT_ID_1 = "7ea43b76-6b7d-49e8-af2b-4114ebb66c80";
private final static String MASKINPORTEN_CLIENT_ID_2 = "675c0111-2035-4d15-9cce-037f55439e80";
private final static Set<String> DEFAULT_SCOPES = Set.of("some:scope1", "some:scope2");
private final static Set<String> REQUESTED_SCOPES = Set.of("some:scope1");

@Inject
private EmbeddedServer embeddedServer;

@Inject
MaskinportenClientRegistry maskinportenClientRegistry;
private MaskinportenClient maskinportenClientMock = mock(MaskinportenClient.class);


@MockBean(MaskinportenClientRegistry.class)
MaskinportenClientRegistry maskinportenklientRegistry() {
Expand All @@ -50,10 +50,8 @@ MaskinportenClientRegistry maskinportenklientRegistry() {
@BeforeEach
void setUp() {
RestAssured.port = embeddedServer.getPort();

MaskinportenClient maskinportenClient = mock(MaskinportenClient.class);
when(maskinportenClient.getAccessToken(anySet())).thenReturn(MASKINPORTEN_DUMMY_ACCESS_TOKEN);
when(maskinportenClientRegistry.get(any())).thenReturn(maskinportenClient);
when(maskinportenClientMock.getAccessToken(anySet())).thenReturn(MASKINPORTEN_DUMMY_ACCESS_TOKEN);
when(maskinportenClientRegistry.get(any())).thenReturn(maskinportenClientMock);
}

private String serviceAccountKeycloakToken() {
Expand All @@ -76,6 +74,7 @@ void validServiceAccount_getAccessToken_shouldReturnToken() {
.body(
"accessToken", equalTo(MASKINPORTEN_DUMMY_ACCESS_TOKEN)
);
verify(maskinportenClientMock, times(1)).getAccessToken(anySet());
}

@Test
Expand All @@ -91,6 +90,7 @@ void serviceAccount_getAccessTokenWithEmptyRequestBody_shouldReturnToken() {
.body(
"accessToken", equalTo(MASKINPORTEN_DUMMY_ACCESS_TOKEN)
);
verify(maskinportenClientMock, times(1)).getAccessToken(anySet());
}

@Test
Expand All @@ -109,6 +109,7 @@ void serviceAccount_getAccessTokenWithExplicitlySpecifiedMaskinportenClientId_sh
.body(
"message", equalTo("maskinportenClientId cannot be explicitly specified for service account users")
);
verifyNoInteractions(maskinportenClientMock);
}

@Test
Expand Down