Skip to content

Commit 84baa91

Browse files
authored
Support clientsecret via env var (#3692)
1 parent e387734 commit 84baa91

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/ConfigurationBuilder.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ private static void logConfigurationWarnings(Configuration config) {
265265
}
266266
if (config.authentication.clientSecret != null) {
267267
configurationLogger.warn(
268-
"\"clientsecret\" typed of AAD authentication has been deprecated since 3.5.0 GA. Please use \"user-assigned identity\" or \"system-assigned identity\" instead.");
268+
"\"clientsecret\" json configuration has been deprecated since 3.5.0 GA. Please use \"user-assigned managed identity\" or \"system-assigned managed identity\" instead. "
269+
+ "If you're on premise, you can use APPLICATIONINSIGHTS_AUTHENTICATION_STRING environment variable to pass the client ID and secret, "
270+
+ "e.g. APPLICATIONINSIGHTS_AUTHENTICATION_STRING=Authorization=AAD;ClientId={CLIENT_ID};ClientSecret={CLIENT_SECRET}.");
269271
}
270272
if (config.sampling.percentage != null && config.sampling.requestsPerSecond != null) {
271273
configurationLogger.warn(
@@ -496,9 +498,16 @@ private static void overlayAadEnvVars(
496498
config.authentication.type = Configuration.AuthenticationType.SAMI;
497499
String clientId = keyValueMap.get("ClientId");
498500
if (clientId != null && !clientId.isEmpty()) {
499-
// Override type to User Assigned Managed Identity
500-
config.authentication.type = Configuration.AuthenticationType.UAMI;
501501
config.authentication.clientId = clientId;
502+
String clientSecret = keyValueMap.get("ClientSecret");
503+
if (clientSecret != null && !clientSecret.isEmpty()) {
504+
// Override type to Client Secret
505+
config.authentication.type = Configuration.AuthenticationType.CLIENTSECRET;
506+
config.authentication.clientSecret = clientSecret;
507+
} else {
508+
// Override type to User Assigned Managed Identity
509+
config.authentication.type = Configuration.AuthenticationType.UAMI;
510+
}
502511
}
503512
}
504513
}

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/configuration/ConfigurationTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ void shouldOverrideInstrumentationSpringSchedulingEnabled() throws IOException {
678678
@Test
679679
void shouldOverrideAadAuthenticationConfig() throws IOException {
680680
envVars.put("APPLICATIONINSIGHTS_AUTHENTICATION_STRING", "Authorization=AAD;ClientId=12345678");
681-
682681
Configuration configuration = loadConfiguration("applicationinsights_aadauthenv.json");
683682
ConfigurationBuilder.overlayFromEnv(
684683
configuration, Paths.get("."), this::envVars, this::systemProperties);
@@ -688,8 +687,7 @@ void shouldOverrideAadAuthenticationConfig() throws IOException {
688687
assertThat(configuration.authentication.clientId).isEqualTo("12345678");
689688
assertThat(configuration.authentication.clientSecret).isNull();
690689

691-
envVars.put("APPLICATIONINSIGHTS_AUTHENTICATION_STRING", "Authorization=AAD;ClientId=");
692-
690+
envVars.put("APPLICATIONINSIGHTS_AUTHENTICATION_STRING", "Authorization=AAD");
693691
Configuration configuration2 = loadConfiguration("applicationinsights_aadauthenv.json");
694692
ConfigurationBuilder.overlayFromEnv(
695693
configuration2, Paths.get("."), this::envVars, this::systemProperties);
@@ -698,6 +696,18 @@ void shouldOverrideAadAuthenticationConfig() throws IOException {
698696
assertThat(configuration2.authentication.type).isEqualTo(Configuration.AuthenticationType.SAMI);
699697
assertThat(configuration2.authentication.clientId).isNull();
700698
assertThat(configuration2.authentication.clientSecret).isNull();
699+
700+
envVars.put(
701+
"APPLICATIONINSIGHTS_AUTHENTICATION_STRING",
702+
"Authorization=AAD;ClientId=12345678;ClientSecret=clientsecret123");
703+
Configuration configuration3 = loadConfiguration("applicationinsights_aadauthenv.json");
704+
ConfigurationBuilder.overlayFromEnv(
705+
configuration3, Paths.get("."), this::envVars, this::systemProperties);
706+
assertThat(configuration3.authentication.enabled).isTrue();
707+
assertThat(configuration3.authentication.type)
708+
.isEqualTo(Configuration.AuthenticationType.CLIENTSECRET);
709+
assertThat(configuration3.authentication.clientId).isEqualTo("12345678");
710+
assertThat(configuration3.authentication.clientSecret).isEqualTo("clientsecret123");
701711
}
702712

703713
@Test

0 commit comments

Comments
 (0)