Skip to content

Commit

Permalink
Add production client
Browse files Browse the repository at this point in the history
  • Loading branch information
emyl3 committed Jan 30, 2025
1 parent 78e008a commit 5bf8348
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,6 +34,16 @@ public void syncDevices(@RequestParam boolean dryRun) {
}
}

@GetMapping("/devices/prod-sync")
public List<DeviceType> syncDevicesFromProd() {
try {
return deviceTypeProdSyncService.syncDevicesFromProd();
} catch (BadRequestException e) {
log.error(String.valueOf(e));
}
return null;
}

@GetMapping("/devices")
@JsonView(PublicDeviceType.class)
public ResponseEntity<Object> getDevices(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
};
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +18,12 @@ public class DeviceTypeProdSyncService {
@Value("${simple-report.production.devices-token}")
private String token;

@Autowired private SRProductionClient _client;

public List<DeviceType> syncDevicesFromProd() {
return _client.getProdDeviceTypes();
}

public boolean validateToken(String headerToken) throws AccessDeniedException {
if (token.equals(headerToken)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DeviceType> getProdDeviceTypes();
}
Original file line number Diff line number Diff line change
@@ -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<DeviceType> getProdDeviceTypes() {
throw new BadRequestException("SRProductionClient not enabled for this environment");
}
}
Original file line number Diff line number Diff line change
@@ -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<DeviceType> getProdDeviceTypes();
}
2 changes: 2 additions & 0 deletions backend/src/main/resources/application-azure-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ simple-report:
enabled: true
support-escalation:
enabled: true
production:
client-enabled: false
twilio:
enabled: true
from-number: "+14045312484"
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SRProductionClient> {

@Test
void getProdDeviceTypes_throwsException() {
BadRequestException caught =
assertThrows(BadRequestException.class, () -> _service.getProdDeviceTypes());
assertEquals("SRProductionClient not enabled for this environment", caught.getMessage());
}
}
4 changes: 4 additions & 0 deletions backend/src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 5bf8348

Please sign in to comment.