Skip to content

Commit 8485b53

Browse files
Only suggest the env var fix for missing tenantId when auth is from APPLICATIONINSIGHTS_AUTHENTICATION_STRING
The environment variable replaces the json authentication configuration, so adding tenantId to the json can't fix the error in that case. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent af0a98f commit 8485b53

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import com.azure.monitor.opentelemetry.autoconfigure.implementation.utils.PropertyHelper;
1111
import com.fasterxml.jackson.annotation.JsonCreator;
12+
import com.fasterxml.jackson.annotation.JsonIgnore;
1213
import com.fasterxml.jackson.annotation.JsonProperty;
1314
import com.microsoft.applicationinsights.agent.internal.common.FriendlyException;
1415
import com.microsoft.applicationinsights.agent.internal.diagnostics.DiagnosticsHelper;
@@ -1590,6 +1591,10 @@ public static class AadAuthentication {
15901591
@Deprecated public String clientSecret;
15911592
@Deprecated public String authorityHost;
15921593

1594+
// set when configured using APPLICATIONINSIGHTS_AUTHENTICATION_STRING, which replaces the
1595+
// "authentication" json configuration
1596+
@JsonIgnore public boolean fromAuthenticationStringEnvVar;
1597+
15931598
public void validate() {
15941599
if (!enabled) {
15951600
return;
@@ -1619,8 +1624,9 @@ public void validate() {
16191624
if (isEmpty(tenantId)) {
16201625
throw new FriendlyException(
16211626
"AAD Authentication configuration of type Client Secret Identity is missing \"tenantId\".",
1622-
"Please provide a valid \"tenantId\" under the \"authentication\" configuration, "
1623-
+ "or add \"TenantId={TENANT_ID}\" to the APPLICATIONINSIGHTS_AUTHENTICATION_STRING environment variable. "
1627+
(fromAuthenticationStringEnvVar
1628+
? "Please add \"TenantId={TENANT_ID}\" to the APPLICATIONINSIGHTS_AUTHENTICATION_STRING environment variable. "
1629+
: "Please provide a valid \"tenantId\" under the \"authentication\" configuration. ")
16241630
+ "Learn more about authentication configuration here: https://docs.microsoft.com/en-us/azure/azure-monitor/app/java-standalone-config#authentication");
16251631
}
16261632

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ private static void overlayAadEnvVars(
506506
if (authorization != null && authorization.equals("AAD")) {
507507
// Override any configuration from json
508508
config.authentication = new Configuration.AadAuthentication();
509+
config.authentication.fromAuthenticationStringEnvVar = true;
509510
config.authentication.enabled = true;
510511
config.authentication.type = Configuration.AuthenticationType.SAMI;
511512
String clientId = keyValueMap.get("ClientId");

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,21 @@ void shouldOverrideAadAuthenticationConfig() throws IOException {
709709
assertThat(configuration3.authentication.tenantId).isNull();
710710
assertThatThrownBy(configuration3.authentication::validate)
711711
.isInstanceOf(FriendlyException.class)
712-
.hasMessageContaining("missing \"tenantId\"");
712+
.hasMessageContaining("missing \"tenantId\"")
713+
.hasMessageContaining(
714+
"\"TenantId={TENANT_ID}\" to the APPLICATIONINSIGHTS_AUTHENTICATION_STRING")
715+
.hasMessageNotContaining("under the \"authentication\" configuration");
716+
717+
Configuration.AadAuthentication jsonAuthentication = new Configuration.AadAuthentication();
718+
jsonAuthentication.enabled = true;
719+
jsonAuthentication.type = Configuration.AuthenticationType.CLIENTSECRET;
720+
jsonAuthentication.clientId = "12345678";
721+
jsonAuthentication.clientSecret = "clientsecret123";
722+
assertThatThrownBy(jsonAuthentication::validate)
723+
.isInstanceOf(FriendlyException.class)
724+
.hasMessageContaining("missing \"tenantId\"")
725+
.hasMessageContaining("under the \"authentication\" configuration")
726+
.hasMessageNotContaining("APPLICATIONINSIGHTS_AUTHENTICATION_STRING");
713727

714728
envVars.put(
715729
"APPLICATIONINSIGHTS_AUTHENTICATION_STRING",

0 commit comments

Comments
 (0)