-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
35e28ad
commit 9df57ba
Showing
11 changed files
with
248 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...icipant/src/main/java/bio/terra/pearl/api/participant/config/B2CConfigurationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package bio.terra.pearl.api.participant.config; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.stereotype.Service; | ||
import org.yaml.snakeyaml.LoaderOptions; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.constructor.Constructor; | ||
|
||
@Slf4j | ||
@Service | ||
public class B2CConfigurationService { | ||
private final B2CConfiguration b2cConfiguration; | ||
@Getter private B2CPortalConfiguration portalConfiguration; | ||
private final Map<String, Map<String, String>> portalToConfig = new HashMap<>(); | ||
|
||
public B2CConfigurationService(B2CConfiguration b2cConfiguration) { | ||
this.b2cConfiguration = b2cConfiguration; | ||
portalConfiguration = new B2CPortalConfiguration(); | ||
} | ||
|
||
/** Get the B2C configuration for a portal. Returns null if B2C is not configured for portal */ | ||
public Map<String, String> getB2CForPortal(String portalShortcode) { | ||
return portalToConfig.get(portalShortcode); | ||
} | ||
|
||
/** | ||
* Initialize B2C configuration from a yaml file. The file can be specified as an absolute path or | ||
* a relative path. If relative, it is loaded from the classpath. | ||
*/ | ||
@EventListener(ApplicationReadyEvent.class) | ||
public void initB2CConfig() { | ||
String b2cConfigFile = b2cConfiguration.configFile(); | ||
if (StringUtils.isBlank(b2cConfigFile)) { | ||
log.error("b2c-config-file property is not set"); | ||
return; | ||
} | ||
|
||
log.info("b2c-config-file = '{}'", b2cConfigFile); | ||
Yaml yaml = new Yaml(new Constructor(B2CPortalConfiguration.class, new LoaderOptions())); | ||
|
||
// for deployments into k8s, the config file is mounted into the container as a volume | ||
// for local deployments, the config file is on the classpath, at least for now | ||
File file = new File(b2cConfigFile); | ||
if (file.isAbsolute()) { | ||
// absolute path, load from file system | ||
if (!file.exists()) { | ||
log.error( | ||
"b2c-config-file property is set to an absolute path that does not exist: {}", | ||
b2cConfigFile); | ||
return; | ||
} | ||
|
||
try (InputStream str = new FileInputStream(file)) { | ||
portalConfiguration = yaml.load(str); | ||
} catch (Exception e) { | ||
log.error("Error loading b2c config file: {}", b2cConfigFile, e); | ||
return; | ||
} | ||
} else { | ||
// relative path, load from classpath | ||
ClassPathResource cpr = new ClassPathResource(b2cConfigFile); | ||
try (InputStream str = cpr.getInputStream()) { | ||
portalConfiguration = yaml.load(str); | ||
} catch (Exception e) { | ||
log.error("Error loading b2c config file: {}", b2cConfigFile, e); | ||
return; | ||
} | ||
} | ||
buildPortalToConfig(); | ||
} | ||
|
||
protected void buildPortalToConfig() { | ||
portalToConfig.clear(); | ||
for (String portal : portalConfiguration.getB2CProperties().keySet()) { | ||
portalToConfig.put(portal, buildConfigMap(portal)); | ||
} | ||
} | ||
|
||
protected Map<String, String> buildConfigMap(String portalShortcode) { | ||
B2CPortalConfiguration.B2CProperties b2cConfig = | ||
getPortalConfiguration().getPortalProperties(portalShortcode); | ||
if (b2cConfig == null) { | ||
return Collections.emptyMap(); | ||
} | ||
Map<String, String> config = | ||
Map.of( | ||
"b2cTenantName", | ||
StringUtils.defaultIfEmpty(b2cConfig.getTenantName(), ""), | ||
"b2cClientId", | ||
StringUtils.defaultIfEmpty(b2cConfig.getClientId(), ""), | ||
"b2cPolicyName", | ||
StringUtils.defaultIfEmpty(b2cConfig.getPolicyName(), ""), | ||
"b2cChangePasswordPolicyName", | ||
StringUtils.defaultIfEmpty(b2cConfig.getChangePasswordPolicyName(), "")); | ||
portalToConfig.put(portalShortcode, config); | ||
return config; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ticipant/src/main/java/bio/terra/pearl/api/participant/config/B2CPortalConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package bio.terra.pearl.api.participant.config; | ||
|
||
import java.util.Map; | ||
import lombok.Data; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
public class B2CPortalConfiguration { | ||
private Map<String, B2CProperties> b2c; | ||
|
||
public B2CPortalConfiguration() { | ||
createDefaultProperties(); | ||
} | ||
|
||
protected void createDefaultProperties() { | ||
b2c = Map.of("missingB2CProperties", new B2CProperties()); | ||
} | ||
|
||
public B2CPortalConfiguration.B2CProperties getPortalProperties(String portal) { | ||
return b2c.get(portal); | ||
} | ||
|
||
Map<String, B2CPortalConfiguration.B2CProperties> getB2CProperties() { | ||
return b2c; | ||
} | ||
|
||
@Data | ||
public static class B2CProperties { | ||
private String tenantName; | ||
private String clientId; | ||
private String policyName; | ||
private String changePasswordPolicyName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,7 @@ env: | |
# if true, the swagger UI page will be made available at swagger-ui.html -- should be false for production | ||
enabled: ${SWAGGER_ENABLED:false} | ||
b2c: | ||
tenantName: ${B2C_TENANT_NAME:missing_tenant_name} | ||
clientId: ${B2C_CLIENT_ID:missing_client_id} | ||
policyName: ${B2C_POLICY_NAME:missing_policy_name} | ||
changePasswordPolicyName: ${B2C_CHANGE_PASSWORD_POLICY_NAME:missing_policy_name} | ||
config-file: ${B2C_CONFIG_FILE:b2c-config.yml} | ||
email: | ||
sendgridApiKey: ${SENDGRID_API_KEY:} | ||
supportEmailAddress: ${SUPPORT_EMAIL_ADDRESS:[email protected]} | ||
|
@@ -85,10 +82,7 @@ hibernate: | |
packages-to-scan: bio.terra.pearl.core.model | ||
|
||
b2c: | ||
tenantName: ${env.b2c.tenantName} | ||
clientId: ${env.b2c.clientId} | ||
policyName: ${env.b2c.policyName} | ||
changePasswordPolicyName: ${env.b2c.changePasswordPolicyName} | ||
config-file: ${env.b2c.config-file} | ||
|
||
javatemplate: | ||
ingress: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
b2c: | ||
ourhealth: | ||
tenantName: ddpdevb2c | ||
clientId: 705c09dc-5cca-43d3-ae06-07de78bad29a | ||
policyName: B2C_1A_ddp_participant_signup_signin_dev | ||
changePasswordPolicyName: B2C_1A_ddp_participant_change_password_dev | ||
hearthive: | ||
tenantName: hearthivedev | ||
clientId: 8c778931-b7f6-4503-b30e-e975ab8ea615 | ||
policyName: B2C_1A_ddp_participant_signup_signin_hearthive-dev | ||
changePasswordPolicyName: B2C_1A_ddp_participant_change_password_hearthive-dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ant/src/test/java/bio/terra/pearl/api/participant/config/B2CConfigurationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package bio.terra.pearl.api.participant.config; | ||
|
||
import static io.jsonwebtoken.lang.Assert.notNull; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.util.Map; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
public class B2CConfigurationServiceTest { | ||
|
||
@TempDir File tempDir; | ||
|
||
@Test | ||
void testInitB2CConfigRelativePath() { | ||
B2CConfigurationService b2CService = | ||
new B2CConfigurationService(new B2CConfiguration("b2c-config.yml")); | ||
b2CService.initB2CConfig(); | ||
B2CPortalConfiguration b2cConfig = b2CService.getPortalConfiguration(); | ||
notNull(b2cConfig, "b2cConfig should not be null"); | ||
B2CPortalConfiguration.B2CProperties b2cProperties = b2cConfig.getPortalProperties("ourhealth"); | ||
assertThat(b2cProperties.getTenantName(), equalTo("ddpdevb2c")); | ||
} | ||
|
||
@Test | ||
void testInitB2CConfigAbsPath() { | ||
// create a file that is not on the resource path | ||
File tempFile = null; | ||
try { | ||
tempFile = new File(tempDir, "b2c-config.yml"); | ||
URL url = Thread.currentThread().getContextClassLoader().getResource("test-b2c-config.yml"); | ||
FileUtils.copyFile(new File(url.getPath()), tempFile); | ||
} catch (Exception e) { | ||
fail("Failed to create b2c-config.yml", e); | ||
} | ||
|
||
// ensure initialization | ||
B2CConfigurationService b2CService = | ||
new B2CConfigurationService(new B2CConfiguration(tempFile.getPath())); | ||
b2CService.initB2CConfig(); | ||
B2CPortalConfiguration b2cConfig = b2CService.getPortalConfiguration(); | ||
notNull(b2cConfig, "b2cConfig should not be null"); | ||
B2CPortalConfiguration.B2CProperties b2cProperties = b2cConfig.getPortalProperties("portal2"); | ||
assertThat(b2cProperties.getTenantName(), equalTo("def")); | ||
|
||
// ensure config map building | ||
Map<String, String> configMap = b2CService.getB2CForPortal("portal2"); | ||
assertThat(configMap.size(), equalTo(4)); | ||
assertThat(configMap.get("b2cTenantName"), equalTo("def")); | ||
assertThat( | ||
configMap.get("b2cPolicyName"), equalTo("B2C_1B_ddp_participant_signup_signin_test")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
b2c: | ||
portal1: | ||
tenantName: "abc" | ||
clientId: "705c09dc-a" | ||
policyName: "B2C_1A_ddp_participant_signup_signin_test" | ||
changePasswordPolicyName: "B2C_1A_ddp_participant_change_password_test" | ||
portal2: | ||
tenantName: "def" | ||
clientId: "705c09dc-b" | ||
policyName: "B2C_1B_ddp_participant_signup_signin_test" | ||
changePasswordPolicyName: "B2C_1B_ddp_participant_change_password_test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters