Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,25 @@ public class EurekaInstanceConfigValidator {
private static final String UNSET_VALUE_STRING = "{apiml.";
private static final char[] UNSET_VALUE_CHAR_ARRAY = UNSET_VALUE_STRING.toCharArray();

private List<String> missingSslParameters = new ArrayList<>();
private List<String> missingRoutesParameters = new ArrayList<>();
private final List<String> missingSslParameters = new ArrayList<>();
private final List<String> missingRoutesParameters = new ArrayList<>();
private final List<String> poorlyFormedRelativeUrlParameters = new ArrayList<>();

/**
* Validation method that validates mandatory and non-mandatory parameters
*
* @param config
* @throws MetadataValidationException if the validation fails
*/
public void validate(ApiMediationServiceConfig config) {
validateRoutes(config.getRoutes());
validateSsl(config.getSsl());
validateUrls(config);

if (config.getCatalog() == null) {
log.warn("The API Catalog UI tile configuration is not provided. Try to add apiml.service.catalog.tile section.");
}

if (isInvalid(config.getHomePageRelativeUrl())) {
log.warn("The home page URL is not provided. Try to add apiml.service.homePageRelativeUrl property or check its value.");
}

if (config.getApiInfo() == null || config.getApiInfo().isEmpty()) {
log.warn("The API info configuration is not provided. Try to add apiml.service.apiInfo section.");
}
Expand All @@ -60,10 +59,10 @@ private void validateRoutes(List<Route> routes) {
}
routes.forEach(route -> {
if (isInvalid(route.getGatewayUrl())) {
createListOfMissingRoutesParameters("gatewayUrl", missingRoutesParameters);
addParameterToProblemsList("gatewayUrl", missingRoutesParameters);
}
if (isInvalid(route.getServiceUrl())) {
createListOfMissingRoutesParameters("serviceUrl", missingRoutesParameters);
addParameterToProblemsList("serviceUrl", missingRoutesParameters);
}
if (!missingRoutesParameters.isEmpty()) {
throw new MetadataValidationException(String.format("Routes parameters ** %s ** are missing or were not replaced by the system properties.", String.join(", ", missingRoutesParameters)));
Expand All @@ -76,46 +75,90 @@ private void validateSsl(Ssl ssl) {
throw new MetadataValidationException("SSL configuration was not provided. Try add apiml.service.ssl section.");
}
if (isInvalid(ssl.getProtocol())) {
createListOfMissingSslParameters("protocol", missingSslParameters);
addParameterToProblemsList("protocol", missingSslParameters);
}
if (isInvalid(ssl.getTrustStore())) {
createListOfMissingSslParameters("trustStore", missingSslParameters);
addParameterToProblemsList("trustStore", missingSslParameters);
}
if (isInvalid(ssl.getKeyStore())) {
createListOfMissingSslParameters("keyStore", missingSslParameters);
addParameterToProblemsList("keyStore", missingSslParameters);
}
if (isInvalid(ssl.getKeyAlias())) {
createListOfMissingSslParameters("keyAlias", missingSslParameters);
addParameterToProblemsList("keyAlias", missingSslParameters);
}
if (isInvalid(ssl.getKeyStoreType())) {
createListOfMissingSslParameters("keyStoreType", missingSslParameters);
addParameterToProblemsList("keyStoreType", missingSslParameters);
}
if (isInvalid(ssl.getTrustStoreType())) {
createListOfMissingSslParameters("trustStoreType", missingSslParameters);
addParameterToProblemsList("trustStoreType", missingSslParameters);
}
if (isInvalid(ssl.getTrustStorePassword())) {
if (isInvalid(ssl.getTrustStoreType()) ||
(!isInvalid(ssl.getTrustStoreType()) && !ssl.getTrustStoreType().equals("JCERACFKS"))) {
createListOfMissingSslParameters("trustStorePassword", missingSslParameters);
addParameterToProblemsList("trustStorePassword", missingSslParameters);
}
}
if (isInvalid(ssl.getKeyStorePassword())) {
if (isInvalid(ssl.getKeyStoreType()) ||
(!isInvalid(ssl.getKeyStoreType()) && !ssl.getKeyStoreType().equals("JCERACFKS"))) {
createListOfMissingSslParameters("keyStorePassword", missingSslParameters);
addParameterToProblemsList("keyStorePassword", missingSslParameters);
}
}
if (isInvalid(ssl.getKeyPassword())) {
createListOfMissingSslParameters("keyPassword", missingSslParameters);
addParameterToProblemsList("keyPassword", missingSslParameters);
}
if (ssl.getEnabled() == null) {
createListOfMissingSslParameters("enabled", missingSslParameters);
addParameterToProblemsList("enabled", missingSslParameters);
}
if (!missingSslParameters.isEmpty()) {
throw new MetadataValidationException(String.format("SSL parameters ** %s ** are missing or were not replaced by the system properties.", String.join(", ", missingSslParameters)));
}
}

private void validateUrls(ApiMediationServiceConfig config) {
validateHomePageRelativeUrl(config);

if (isPoorlyFormedRelativeUrl(config.getHealthCheckRelativeUrl())) {
addParameterToProblemsList("healthCheckRelativeUrl", poorlyFormedRelativeUrlParameters);
}

if (isPoorlyFormedRelativeUrl(config.getStatusPageRelativeUrl())) {
addParameterToProblemsList("statusPageRelativeUrl", poorlyFormedRelativeUrlParameters);
}

if (isPoorlyFormedRelativeUrl(config.getContextPath())) {
addParameterToProblemsList("contextPath", poorlyFormedRelativeUrlParameters);
}

if (!poorlyFormedRelativeUrlParameters.isEmpty()) {
log.warn(String.format("Relative URL parameters ** %s ** don't begin with '/' which often causes malformed URLs.", String.join(", ", poorlyFormedRelativeUrlParameters)));
}

if (config.getBaseUrl() != null && config.getBaseUrl().endsWith("/")) {
log.warn("The baseUrl parameter ends with a trailing '/'. This often causes malformed URLs when relative URLs are used.");
}

if (config.getContextPath() != null && config.getContextPath().endsWith("/")) {
log.warn("The contextPath parameter ends with a trailing '/'. This often causes malformed URLs when relative URLs are used.");
}
}

private void validateHomePageRelativeUrl(ApiMediationServiceConfig config) {
String homePageUrl = config.getHomePageRelativeUrl();
if (isInvalid(homePageUrl) && config.getRoutes().stream().noneMatch(route -> route.getGatewayUrl().toLowerCase().startsWith("ui"))) {
// Some applications may not require a home page, so don't log a warning that the home page URL doesn't exist
// unless there is a gateway UI URL, which indicates there should have been a home page URL.
log.warn("The home page URL is not provided. Try to add apiml.service.homePageRelativeUrl property or check its value.");
} else if (isPoorlyFormedRelativeUrl(homePageUrl)) {
addParameterToProblemsList("homePageRelativeUrl", poorlyFormedRelativeUrlParameters);
}
}

private boolean isPoorlyFormedRelativeUrl(String url) {
// URL not existing **is not** poorly formed. A check for it existing should be done elsewhere.
return url != null && !url.startsWith("/");
}

private boolean isInvalid(String value) {
return value == null || value.isEmpty() || value.contains(UNSET_VALUE_STRING);
}
Expand All @@ -124,12 +167,7 @@ private boolean isInvalid(char[] value) {
return value == null || value.length == 0 || Chars.indexOf(value, UNSET_VALUE_CHAR_ARRAY) >= 0;
}

private void createListOfMissingSslParameters(String parameter, List parameters) {
parameters.add(parameter);
private void addParameterToProblemsList(String parameter, List<String> problemParameters) {
problemParameters.add(parameter);
}

private void createListOfMissingRoutesParameters(String parameter, List parameters) {
parameters.add(parameter);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

class EurekaInstanceConfigValidatorTest {

private EurekaInstanceConfigValidator validator = new EurekaInstanceConfigValidator();
private ApiMediationServiceConfigReader configReader = new ApiMediationServiceConfigReader();
private final EurekaInstanceConfigValidator validator = new EurekaInstanceConfigValidator();
private final ApiMediationServiceConfigReader configReader = new ApiMediationServiceConfigReader();

@Test
void givenServiceConfiguration_whenConfigurationIsValid_thenValidate() throws ServiceDefinitionException {
Expand Down Expand Up @@ -104,15 +104,15 @@ void givenConfigurationEmptyCatalog_whenValidate_thenLog() throws Exception {
ApiMediationServiceConfig testConfig = configReader.loadConfiguration("empty-catalog-service-configuration.yml");
validator.validate(testConfig);

assertEquals(null, testConfig.getCatalog());
assertNull(testConfig.getCatalog());
}

@Test
void givenConfigurationEmptyApiInfo_whenValidate_thenLog() throws Exception {
ApiMediationServiceConfig testConfig = configReader.loadConfiguration("empty-apiinfo-service-configuration.yml");
validator.validate(testConfig);

assertEquals(null, testConfig.getApiInfo());
assertNull(testConfig.getApiInfo());
}

@Test
Expand Down