Skip to content

Commit

Permalink
ValidateUri configured in CosmosProperties (Azure#23641)
Browse files Browse the repository at this point in the history
  • Loading branch information
backwind1233 authored Aug 30, 2021
1 parent 45d4787 commit bad2166
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
import com.azure.spring.data.cosmos.core.ResponseDiagnosticsProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotEmpty;
import java.util.regex.Pattern;

/**
* Configuration properties for Cosmos database, consistency, telemetry, connection, query metrics and diagnostics.
*/
@Validated
@ConfigurationProperties("azure.cosmos")
public class CosmosProperties {
public class CosmosProperties implements InitializingBean {

private static final Logger LOGGER = LoggerFactory.getLogger(CosmosProperties.class);

public static final String URI_REGEX = "http[s]{0,1}://.*.documents.azure.com.*";

/**
* Document DB URI.
*/
Expand Down Expand Up @@ -71,6 +76,11 @@ public class CosmosProperties {
}
};

@Override
public void afterPropertiesSet() {
validateUri();
}

public String getUri() {
return uri;
}
Expand Down Expand Up @@ -137,4 +147,12 @@ public ConnectionMode getConnectionMode() {
public void setConnectionMode(ConnectionMode connectionMode) {
this.connectionMode = connectionMode;
}

private void validateUri() {
if (!Pattern.matches(URI_REGEX, uri)) {
throw new IllegalArgumentException("the uri's pattern specified in 'azure.cosmos.uri' is not supported, "
+ "only sql/core api is supported, please check https://docs.microsoft.com/en-us/azure/cosmos-db/ "
+ "for more info.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
package com.azure.spring.autoconfigure.cosmos;


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindException;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
Expand All @@ -17,12 +19,62 @@
import java.util.List;
import java.util.stream.Collectors;

import static com.azure.spring.autoconfigure.cosmos.PropertySettingUtil.PROPERTY_URI;
import static com.azure.spring.autoconfigure.cosmos.PropertySettingUtil.TEST_URI_FAIL;
import static com.azure.spring.autoconfigure.cosmos.PropertySettingUtil.TEST_URI_HTTP;
import static com.azure.spring.autoconfigure.cosmos.PropertySettingUtil.TEST_URI_HTTPS;
import static com.azure.spring.autoconfigure.cosmos.PropertySettingUtil.configureCosmosProperties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.context.support.TestPropertySourceUtils.addInlinedPropertiesToEnvironment;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CosmosPropertiesTest {

@Test
public void uriPatternWithException() {
Assertions.assertThrows(BeanCreationException.class, () -> {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
configureCosmosProperties(context);
addInlinedPropertiesToEnvironment(
context,
PROPERTY_URI + "=" + TEST_URI_FAIL
);
context.register(Config.class);
context.refresh();
}
});
}

@Test
public void uriPatternHttp() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
configureCosmosProperties(context);
addInlinedPropertiesToEnvironment(
context,
PROPERTY_URI + "=" + TEST_URI_HTTP
);
context.register(Config.class);
context.refresh();
final CosmosProperties properties = context.getBean(CosmosProperties.class);
assertThat(properties.getUri()).matches(CosmosProperties.URI_REGEX);
}
}

@Test
public void uriPatternHttps() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
configureCosmosProperties(context);
addInlinedPropertiesToEnvironment(
context,
PROPERTY_URI + "=" + TEST_URI_HTTPS
);
context.register(Config.class);
context.refresh();
final CosmosProperties properties = context.getBean(CosmosProperties.class);
assertThat(properties.getUri()).matches(CosmosProperties.URI_REGEX);
}
}

@Test
public void canSetAllProperties() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class PropertySettingUtil {
public static final String PROPERTY_CONSISTENCY_LEVEL = "azure.cosmos.consistency-level";
public static final String PROPERTY_POPULATE_QUERY_METRICS = "azure.cosmos.populateQueryMetrics";
public static final String PROPERTY_CONNECTION_MODE = "azure.cosmos.connection-mode";
public static final String TEST_URI_HTTPS = "https://test.https.documents.azure.com:443/";
public static final String TEST_URI_HTTP = "http://test.http.documents.azure.com:443/";
public static final String TEST_URI_FAIL = "http://test.fail.documentsfail.azure.com:443/";

public static void configureCosmosProperties(AnnotationConfigApplicationContext context) {
addInlinedPropertiesToEnvironment(
Expand Down

0 comments on commit bad2166

Please sign in to comment.