Skip to content
Snippets Groups Projects
Commit 8fb8365c authored by OZGCloud's avatar OZGCloud
Browse files

OZG-2887 Verwende usermanager health check um zu prüfen ob der

Usermanager verfügbar ist
parent 10c14d58
No related branches found
No related tags found
No related merge requests found
...@@ -3,16 +3,19 @@ package de.itvsh.goofy; ...@@ -3,16 +3,19 @@ package de.itvsh.goofy;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import java.time.Instant; import java.time.Instant;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.info.BuildProperties; import org.springframework.boot.info.BuildProperties;
import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link; import org.springframework.hateoas.Link;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import de.itvsh.goofy.common.ModelBuilder; import de.itvsh.goofy.common.ModelBuilder;
import de.itvsh.goofy.common.downloadtoken.DownloadTokenController; import de.itvsh.goofy.common.downloadtoken.DownloadTokenController;
...@@ -21,7 +24,9 @@ import de.itvsh.goofy.common.user.UserId; ...@@ -21,7 +24,9 @@ import de.itvsh.goofy.common.user.UserId;
import de.itvsh.goofy.common.user.UserProfileController; import de.itvsh.goofy.common.user.UserProfileController;
import de.itvsh.goofy.common.user.UserRole; import de.itvsh.goofy.common.user.UserRole;
import de.itvsh.goofy.vorgang.VorgangController; import de.itvsh.goofy.vorgang.VorgangController;
import lombok.extern.log4j.Log4j2;
@Log4j2
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api")
public class RootController { public class RootController {
...@@ -34,10 +39,17 @@ public class RootController { ...@@ -34,10 +39,17 @@ public class RootController {
static final String REL_DOWNLOAD_TOKEN = "downloadToken"; static final String REL_DOWNLOAD_TOKEN = "downloadToken";
static final String REL_CURRENT_USER = "currentUser"; static final String REL_CURRENT_USER = "currentUser";
private static final String userManagerHealthPath = "/q/health";
@Autowired(required = false) @Autowired(required = false)
public BuildProperties buildProperties; public BuildProperties buildProperties;
@Autowired @Autowired
private CurrentUserService currentUserService; private CurrentUserService currentUserService;
@Autowired
private RestTemplate restTemplate;
@Value("${goofy.user-manager.url}")
private String userManagerUrl;
@GetMapping @GetMapping
public EntityModel<RootResource> getRootResource() { public EntityModel<RootResource> getRootResource() {
...@@ -51,7 +63,7 @@ public class RootController { ...@@ -51,7 +63,7 @@ public class RootController {
.ifMatch(this::hasVerwaltungRole).addLinks( .ifMatch(this::hasVerwaltungRole).addLinks(
buildVorgangListByPageLink(REL_MY_VORGAENGE, Optional.of(currentUserService.getUserId())), buildVorgangListByPageLink(REL_MY_VORGAENGE, Optional.of(currentUserService.getUserId())),
buildVorgangListByPageLink(REL_SEARCH_MY_VORGAENGE, Optional.of(currentUserService.getUserId()))) buildVorgangListByPageLink(REL_SEARCH_MY_VORGAENGE, Optional.of(currentUserService.getUserId())))
.ifMatch(this::hasCurrentUser).addLinks( .ifMatch(this::hasUserManager).addLinks(
linkTo(methodOn(UserProfileController.class).getUser(currentUserService.getUserId())).withRel(REL_CURRENT_USER)) linkTo(methodOn(UserProfileController.class).getUser(currentUserService.getUserId())).withRel(REL_CURRENT_USER))
.buildModel(); .buildModel();
} }
...@@ -65,8 +77,30 @@ public class RootController { ...@@ -65,8 +77,30 @@ public class RootController {
|| currentUserService.hasRole(UserRole.VERWALTUNG_POSTSTELLE); || currentUserService.hasRole(UserRole.VERWALTUNG_POSTSTELLE);
} }
boolean hasCurrentUser() { boolean hasUserManager() {
return Objects.nonNull(currentUserService.getUser()); String url = getUsermanagerHeathUrl();
if (StringUtils.isNotEmpty(url)) {
return isServiceAvailable(url);
}
return false;
}
private boolean isServiceAvailable(String url) {
try {
String status = restTemplate.getForObject(url, String.class);
return StringUtils.isNotEmpty(status) && status.contains("\"status\": \"UP\"");
} catch (RestClientException e) {
LOG.error("Error retrieving usermanager health status", e);
return false;
}
}
String getUsermanagerHeathUrl() {
if (StringUtils.isNotEmpty(userManagerUrl)) {
return userManagerUrl + userManagerHealthPath;
}
return null;
} }
private Link buildVorgangListByPageLink(String linkRel, Optional<UserId> assignedTo) { private Link buildVorgangListByPageLink(String linkRel, Optional<UserId> assignedTo) {
......
...@@ -5,6 +5,8 @@ logging: ...@@ -5,6 +5,8 @@ logging:
goofy: goofy:
production: false production: false
user-manager:
url: http://localhost:9092
keycloak: keycloak:
auth-server-url: http://localhost:8088/auth auth-server-url: http://localhost:8088/auth
......
...@@ -22,6 +22,8 @@ import org.springframework.hateoas.Link; ...@@ -22,6 +22,8 @@ import org.springframework.hateoas.Link;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import de.itvsh.goofy.common.user.CurrentUserService; import de.itvsh.goofy.common.user.CurrentUserService;
import de.itvsh.goofy.common.user.GoofyUserTestFactory; import de.itvsh.goofy.common.user.GoofyUserTestFactory;
...@@ -39,6 +41,8 @@ class RootControllerTest { ...@@ -39,6 +41,8 @@ class RootControllerTest {
private BuildProperties properties; private BuildProperties properties;
@Mock @Mock
private CurrentUserService currentUserService; private CurrentUserService currentUserService;
@Mock
private RestTemplate restTemplate;
private MockMvc mockMvc; private MockMvc mockMvc;
...@@ -166,8 +170,9 @@ class RootControllerTest { ...@@ -166,8 +170,9 @@ class RootControllerTest {
class CurrentUser { class CurrentUser {
@Test @Test
void shouldHaveCurrentUserLink() { void shouldHaveCurrentUserLink() {
when(currentUserService.getUser()).thenReturn(GoofyUserTestFactory.create()); when(controller.getUsermanagerHeathUrl()).thenReturn("http://localhost:9092/q/health");
when(currentUserService.getUserId()).thenReturn(GoofyUserTestFactory.ID); when(currentUserService.getUserId()).thenReturn(GoofyUserTestFactory.ID);
when(restTemplate.getForObject(anyString(), eq(String.class))).thenReturn("{\"status\": \"UP\"}");
var model = controller.getRootResource(); var model = controller.getRootResource();
...@@ -176,7 +181,29 @@ class RootControllerTest { ...@@ -176,7 +181,29 @@ class RootControllerTest {
} }
@Test @Test
void shouldNotHaveCurrentUserLink() { void shouldNotHaveCurrentUserLinkWhenDown() {
when(controller.getUsermanagerHeathUrl()).thenReturn("http://localhost:9092/q/health");
when(currentUserService.getUserId()).thenReturn(GoofyUserTestFactory.ID);
when(restTemplate.getForObject(anyString(), eq(String.class))).thenReturn("{\"status\": \"DOWN\"}");
var model = controller.getRootResource();
assertThat(model.getLink(RootController.REL_CURRENT_USER)).isNotPresent();
}
@Test
void shouldNotHaveCurrentUserLinkWhenError() {
when(controller.getUsermanagerHeathUrl()).thenReturn("http://localhost:9092/q/health");
when(currentUserService.getUserId()).thenReturn(GoofyUserTestFactory.ID);
when(restTemplate.getForObject(anyString(), eq(String.class))).thenThrow(new RestClientException("Test error"));
var model = controller.getRootResource();
assertThat(model.getLink(RootController.REL_CURRENT_USER)).isNotPresent();
}
@Test
void shouldNotHaveCurrentUserLinkWhenNotConfigured() {
var model = controller.getRootResource(); var model = controller.getRootResource();
assertThat(model.getLink(RootController.REL_CURRENT_USER)).isNotPresent(); assertThat(model.getLink(RootController.REL_CURRENT_USER)).isNotPresent();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment