From 5bf83481ecd30e40887a5cb23c73e0da1a35bcf3 Mon Sep 17 00:00:00 2001 From: elisa lee Date: Wed, 29 Jan 2025 10:22:17 -0600 Subject: [PATCH] Add production client --- .../api/devicetype/DeviceTypeController.java | 11 +++++++++ .../SRProductionClientConfiguration.java | 20 ++++++++++++++++ .../service/DeviceTypeProdSyncService.java | 10 ++++++++ .../srprodclient/LiveSRProductionClient.java | 23 +++++++++++++++++++ .../srprodclient/MockSRProductionClient.java | 20 ++++++++++++++++ .../srprodclient/SRProductionClient.java | 8 +++++++ .../resources/application-azure-prod.yaml | 2 ++ backend/src/main/resources/application.yaml | 1 + .../MockSRProductionClientTest.java | 21 +++++++++++++++++ .../src/test/resources/application-test.yaml | 4 ++++ 10 files changed, 120 insertions(+) create mode 100644 backend/src/main/java/gov/cdc/usds/simplereport/config/SRProductionClientConfiguration.java create mode 100644 backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/LiveSRProductionClient.java create mode 100644 backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/MockSRProductionClient.java create mode 100644 backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/SRProductionClient.java create mode 100644 backend/src/test/java/gov/cdc/usds/simplereport/service/srproductionclient/MockSRProductionClientTest.java diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/api/devicetype/DeviceTypeController.java b/backend/src/main/java/gov/cdc/usds/simplereport/api/devicetype/DeviceTypeController.java index 55a68a5a67..4d68391942 100644 --- a/backend/src/main/java/gov/cdc/usds/simplereport/api/devicetype/DeviceTypeController.java +++ b/backend/src/main/java/gov/cdc/usds/simplereport/api/devicetype/DeviceTypeController.java @@ -1,6 +1,7 @@ package gov.cdc.usds.simplereport.api.devicetype; import com.fasterxml.jackson.annotation.JsonView; +import gov.cdc.usds.simplereport.api.model.errors.BadRequestException; import gov.cdc.usds.simplereport.api.model.errors.DryRunException; import gov.cdc.usds.simplereport.db.model.DeviceType; import gov.cdc.usds.simplereport.service.DeviceTypeLIVDSyncService; @@ -33,6 +34,16 @@ public void syncDevices(@RequestParam boolean dryRun) { } } + @GetMapping("/devices/prod-sync") + public List syncDevicesFromProd() { + try { + return deviceTypeProdSyncService.syncDevicesFromProd(); + } catch (BadRequestException e) { + log.error(String.valueOf(e)); + } + return null; + } + @GetMapping("/devices") @JsonView(PublicDeviceType.class) public ResponseEntity getDevices(HttpServletRequest request) { diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/config/SRProductionClientConfiguration.java b/backend/src/main/java/gov/cdc/usds/simplereport/config/SRProductionClientConfiguration.java new file mode 100644 index 0000000000..d06dabbe2a --- /dev/null +++ b/backend/src/main/java/gov/cdc/usds/simplereport/config/SRProductionClientConfiguration.java @@ -0,0 +1,20 @@ +package gov.cdc.usds.simplereport.config; + +import feign.RequestInterceptor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SRProductionClientConfiguration { + + @Value("${simple-report.production.devices-token}") + private String token; + + @Bean + public RequestInterceptor headerRequestInterceptor() { + return template -> { + template.header("Sr-Prod-Devices-Token", token); + }; + } +} diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/service/DeviceTypeProdSyncService.java b/backend/src/main/java/gov/cdc/usds/simplereport/service/DeviceTypeProdSyncService.java index 5da18a30ed..bc624f22b0 100644 --- a/backend/src/main/java/gov/cdc/usds/simplereport/service/DeviceTypeProdSyncService.java +++ b/backend/src/main/java/gov/cdc/usds/simplereport/service/DeviceTypeProdSyncService.java @@ -1,6 +1,10 @@ package gov.cdc.usds.simplereport.service; +import gov.cdc.usds.simplereport.db.model.DeviceType; +import gov.cdc.usds.simplereport.service.srprodclient.SRProductionClient; +import java.util.List; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.access.AccessDeniedException; import org.springframework.stereotype.Service; @@ -14,6 +18,12 @@ public class DeviceTypeProdSyncService { @Value("${simple-report.production.devices-token}") private String token; + @Autowired private SRProductionClient _client; + + public List syncDevicesFromProd() { + return _client.getProdDeviceTypes(); + } + public boolean validateToken(String headerToken) throws AccessDeniedException { if (token.equals(headerToken)) { return true; diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/LiveSRProductionClient.java b/backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/LiveSRProductionClient.java new file mode 100644 index 0000000000..e91e14fc82 --- /dev/null +++ b/backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/LiveSRProductionClient.java @@ -0,0 +1,23 @@ +package gov.cdc.usds.simplereport.service.srprodclient; + +import gov.cdc.usds.simplereport.config.SRProductionClientConfiguration; +import gov.cdc.usds.simplereport.db.model.DeviceType; +import java.util.List; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * This is intended to run in our lower envs NOT on PROD. This is the client used to make HTTP + * requests to PROD's backend from our non-PROD envs. + */ +@ConditionalOnProperty(value = "simple-report.production.client-enabled", havingValue = "true") +@FeignClient( + name = "sr-prod", + url = "${simple-report.production.backend-url}", + configuration = SRProductionClientConfiguration.class) +public interface LiveSRProductionClient extends SRProductionClient { + @Override + @GetMapping(value = "/devices") + List getProdDeviceTypes(); +} diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/MockSRProductionClient.java b/backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/MockSRProductionClient.java new file mode 100644 index 0000000000..0b2ff8537e --- /dev/null +++ b/backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/MockSRProductionClient.java @@ -0,0 +1,20 @@ +package gov.cdc.usds.simplereport.service.srprodclient; + +import gov.cdc.usds.simplereport.api.model.errors.BadRequestException; +import gov.cdc.usds.simplereport.db.model.DeviceType; +import java.util.List; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Service; + +/** + * This is intended to run in our PROD env as an alternative to the LiveSRProductionClient, which + * does not need to be run against PROD from PROD. + */ +@Service +@ConditionalOnProperty(value = "simple-report.production.client-enabled", havingValue = "false") +public class MockSRProductionClient implements SRProductionClient { + @Override + public List getProdDeviceTypes() { + throw new BadRequestException("SRProductionClient not enabled for this environment"); + } +} diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/SRProductionClient.java b/backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/SRProductionClient.java new file mode 100644 index 0000000000..17e2fa6bf8 --- /dev/null +++ b/backend/src/main/java/gov/cdc/usds/simplereport/service/srprodclient/SRProductionClient.java @@ -0,0 +1,8 @@ +package gov.cdc.usds.simplereport.service.srprodclient; + +import gov.cdc.usds.simplereport.db.model.DeviceType; +import java.util.List; + +public interface SRProductionClient { + List getProdDeviceTypes(); +} diff --git a/backend/src/main/resources/application-azure-prod.yaml b/backend/src/main/resources/application-azure-prod.yaml index 610b6be3d4..d5ed067f1b 100644 --- a/backend/src/main/resources/application-azure-prod.yaml +++ b/backend/src/main/resources/application-azure-prod.yaml @@ -19,6 +19,8 @@ simple-report: enabled: true support-escalation: enabled: true + production: + client-enabled: false twilio: enabled: true from-number: "+14045312484" diff --git a/backend/src/main/resources/application.yaml b/backend/src/main/resources/application.yaml index 7e4e7ca489..6b767e282e 100644 --- a/backend/src/main/resources/application.yaml +++ b/backend/src/main/resources/application.yaml @@ -162,6 +162,7 @@ simple-report: production: backend-url: ${SR_PROD_BACKEND_URL:http://localhost:8080} devices-token: ${SR_PROD_DEVICES_TOKEN:sr-prod-devices-fake-token} + client-enabled: true twilio: messaging-service-sid: ${TWILIO_MESSAGING_SID} logging: diff --git a/backend/src/test/java/gov/cdc/usds/simplereport/service/srproductionclient/MockSRProductionClientTest.java b/backend/src/test/java/gov/cdc/usds/simplereport/service/srproductionclient/MockSRProductionClientTest.java new file mode 100644 index 0000000000..b4aac51c86 --- /dev/null +++ b/backend/src/test/java/gov/cdc/usds/simplereport/service/srproductionclient/MockSRProductionClientTest.java @@ -0,0 +1,21 @@ +package gov.cdc.usds.simplereport.service.srproductionclient; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import gov.cdc.usds.simplereport.api.model.errors.BadRequestException; +import gov.cdc.usds.simplereport.service.BaseServiceTest; +import gov.cdc.usds.simplereport.service.srprodclient.SRProductionClient; +import org.junit.jupiter.api.Test; +import org.springframework.test.context.TestPropertySource; + +@TestPropertySource(properties = {"simple-report.production.client-enabled=false"}) +public class MockSRProductionClientTest extends BaseServiceTest { + + @Test + void getProdDeviceTypes_throwsException() { + BadRequestException caught = + assertThrows(BadRequestException.class, () -> _service.getProdDeviceTypes()); + assertEquals("SRProductionClient not enabled for this environment", caught.getMessage()); + } +} diff --git a/backend/src/test/resources/application-test.yaml b/backend/src/test/resources/application-test.yaml index a16fad3904..4d0d7f552e 100644 --- a/backend/src/test/resources/application-test.yaml +++ b/backend/src/test/resources/application-test.yaml @@ -69,6 +69,10 @@ simple-report: azure-reporting-queue: exception-webhook-enabled: true exception-webhook-token: WATERMELON + production: + backend-url: http://localhost:8080 + devices-token: sr-prod-devices-fake-token + client-enabled: false batch-size: 10 demo-users: site-admin-emails: