Skip to content

Commit c0a4a03

Browse files
IGDD-2010 make healthcheck public (#142) (#143)
* IGDD-2010 Adding new endpoint /healthy and allowing new role of 'any' to allow public access. * Changing role name to be more obvious that the endpoint is publicly accessible. * Setting pom to 0.7.1 Co-authored-by: pcahillai <[email protected]>
1 parent 2ee1da6 commit c0a4a03

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>gov.cdc.izgateway</groupId>
1212
<artifactId>xform</artifactId>
13-
<version>0.8.0</version>
13+
<version>0.7.1</version>
1414
<name>xform</name>
1515
<description>IZ Gateway Xform Service</description>
1616
<properties>

src/main/java/gov/cdc/izgateway/xform/XformApplicationController.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import gov.cdc.izgateway.common.HealthService;
44
import gov.cdc.izgateway.security.AccessControlRegistry;
5-
import gov.cdc.izgateway.xform.services.*;
5+
import gov.cdc.izgateway.xform.security.Roles;
66
import io.swagger.v3.oas.annotations.Operation;
77
import io.swagger.v3.oas.annotations.media.Content;
88
import io.swagger.v3.oas.annotations.responses.ApiResponse;
99
import jakarta.annotation.security.RolesAllowed;
1010
import lombok.extern.java.Log;
1111
import org.springframework.beans.factory.annotation.Autowired;
1212
import org.springframework.context.annotation.Lazy;
13+
import org.springframework.http.ResponseEntity;
1314
import org.springframework.web.bind.annotation.*;
1415

1516
@Log
@@ -24,7 +25,17 @@ public XformApplicationController(
2425
registry.register(this);
2526
}
2627

27-
@RolesAllowed({"admin"})
28+
@RolesAllowed({Roles.PUBLIC_ACCESS})
29+
@GetMapping("/healthy")
30+
public ResponseEntity<Boolean> isHealthy() {
31+
boolean healthy = HealthService.getHealth().isHealthy();
32+
if (!healthy) {
33+
return ResponseEntity.status(503).body(false);
34+
}
35+
return ResponseEntity.ok(true);
36+
}
37+
38+
@RolesAllowed({Roles.ADMIN})
2839
@GetMapping("/health")
2940
public gov.cdc.izgateway.logging.event.Health getHealth() {
3041
return HealthService.getHealth();
@@ -35,7 +46,7 @@ public gov.cdc.izgateway.logging.event.Health getHealth() {
3546
@ApiResponse(responseCode = "200", description = "Success",
3647
content = @Content(mediaType = "text/plain")
3748
)
38-
@RolesAllowed({"admin"})
49+
@RolesAllowed({Roles.ADMIN})
3950
@GetMapping({"/build", "/build.txt"})
4051
public String getBuild() {
4152
return Application.getPage(Application.BUILD);

src/main/java/gov/cdc/izgateway/xform/security/Roles.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public class Roles {
2424
/** Header to indicate that request from localhost should not be treated as an admin */
2525
public static final String NOT_ADMIN_HEADER = "X-Not-Admin";
2626

27-
/**
27+
// Special role for public access to an API endpoint. Health check endpoints use this role.
28+
public static final String PUBLIC_ACCESS = "public-access";
29+
30+
/**
2831
* A list of all roles to enable validation of role names
2932
* in model elements.
3033
*/
@@ -33,7 +36,7 @@ public class Roles {
3336
ADMIN, XFORM_SENDING_SYSTEM,
3437
PIPELINE_READER, PIPELINE_WRITER, PIPELINE_DELETER,
3538
ORGANIZATION_READER, ORGANIZATION_WRITER, ORGANIZATION_DELETER,
36-
SOLUTION_READER, SOLUTION_WRITER, SOLUTION_DELETER
39+
SOLUTION_READER, SOLUTION_WRITER, SOLUTION_DELETER, PUBLIC_ACCESS
3740
)
3841
);
3942
private Roles() {}

src/main/java/gov/cdc/izgateway/xform/services/AccessControlService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public Boolean checkSwaggerAccess(String method, String path) {
5757
public Boolean checkXformAccess(String method, String path) {
5858
List<String> allowedRoles = getAllowedRoles(RequestMethod.valueOf(method), path);
5959

60+
// Check for public access
61+
if (allowedRoles.contains(Roles.PUBLIC_ACCESS)) {
62+
return true;
63+
}
64+
6065
// If RequestContext.getRoles() has one role that matches the roles list, return true
6166
return RequestContext.getPrincipal().getRoles().stream().anyMatch(allowedRoles::contains);
6267
}

0 commit comments

Comments
 (0)