Skip to content

Commit

Permalink
Check connection string env var before loading from a file (#3794)
Browse files Browse the repository at this point in the history
  • Loading branch information
heyams authored Jul 24, 2024
1 parent a54829c commit 4e231e5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -713,21 +713,12 @@ static void overlayFromEnv(
StringLookup stringLookup =
StringLookupFactory.INSTANCE.interpolatorStringLookup(stringLookupMap, null, false);
StringSubstitutor stringSubstitutor = new StringSubstitutor(stringLookup);
String replacedConnectionString = stringSubstitutor.replace(config.connectionString);
if (replacedConnectionString != null
&& !replacedConnectionString.startsWith("InstrumentationKey=")
&& config.connectionString.equals(replacedConnectionString)) {
throw new FriendlyException(
"Your connection string seems to have a wrong format: \""
+ config.connectionString
+ "\").\n"
+ "If you want to load the connection string from a file, please use this format:"
+ "\n{ \"connectionString\": \"${file:connection-string-file.txt}\" }\n",
"Learn more about configuration options here: " + CONFIGURATION_OPTIONS_LINK);
}
config.connectionString =
overlayConnectionStringFromEnv(
replacedConnectionString, envVarsFunction, systemPropertiesFunction);
stringSubstitutor.replace(config.connectionString),
envVarsFunction,
systemPropertiesFunction);

if (isTrimEmpty(config.role.name)) {
// only use WEBSITE_SITE_NAME as a fallback
config.role.name = getWebsiteSiteNameEnvVar(envVarsFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,43 +172,9 @@ void testOverlayWithEnvVarWithGoodFileStringLookupFormat() throws Exception {
assertThat(configuration.connectionString).isEqualTo(CONNECTION_STRING);
}

@Test
void testOverlayWithEnvVarWithBadFileStringLookupFormat() throws Exception {
Configuration configuration = new Configuration();
String filename = "${file:" + connectionStringFile.getAbsolutePath();
configuration.connectionString = filename;
assertFriendlyExceptionThrown(configuration, filename);

filename = "${xyz:" + connectionStringFile.getAbsolutePath() + "}";
configuration.connectionString = filename;
assertFriendlyExceptionThrown(configuration, filename);

filename = "file:" + connectionStringFile.getAbsolutePath() + "}";
configuration.connectionString = filename;
assertFriendlyExceptionThrown(configuration, filename);

filename = "file:" + connectionStringFile.getAbsolutePath();
configuration.connectionString = filename;
assertFriendlyExceptionThrown(configuration, filename);

configuration.connectionString = CONNECTION_STRING;
ConfigurationBuilder.overlayFromEnv(
configuration, Paths.get("."), System::getenv, System::getProperty);
assertThat(configuration.connectionString).isEqualTo(CONNECTION_STRING);
}

private static void assertFriendlyExceptionThrown(Configuration configuration, String filename) {
assertThatThrownBy(
() ->
ConfigurationBuilder.overlayFromEnv(
configuration, Paths.get("."), System::getenv, System::getProperty))
.isInstanceOf(FriendlyException.class);
assertThat(configuration.connectionString).isEqualTo(filename);
}

@Test
void testConnectionStringEnvVarHasHigherPrecedenceOverFileLookup() throws Exception {
String testConnectionString = "test-connection-string";
String testConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000";
envVars.put("APPLICATIONINSIGHTS_CONNECTION_STRING", testConnectionString);

Configuration configuration = new Configuration();
Expand Down

0 comments on commit 4e231e5

Please sign in to comment.