Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/java/frameworks/azure_application_insights_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ func (a *AzureApplicationInsightsAgentFramework) Finalize() error {

// Set connection string if available
if credentials.ConnectionString != "" {
opts = append(opts, fmt.Sprintf("-Dapplicationinsights.connection.string=%s", credentials.ConnectionString))
// Single-quote the value so eval exec java $JAVA_OPTS does not split on ; in the connection string
opts = append(opts, fmt.Sprintf("-Dapplicationinsights.connection.string='%s'", credentials.ConnectionString))
} else if credentials.InstrumentationKey != "" {
// Fallback to instrumentation key
opts = append(opts, fmt.Sprintf("-Dapplicationinsights.instrumentation-key=%s", credentials.InstrumentationKey))
Expand Down
25 changes: 14 additions & 11 deletions src/java/frameworks/azure_application_insights_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,12 @@ var _ = Describe("Azure Application Insights Agent", func() {
os.Setenv("APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=abc123;IngestionEndpoint=https://eastus.in.applicationinsights.azure.com/")
})

It("opts file contains -Dapplicationinsights.connection.string", func() {
It("opts file contains -Dapplicationinsights.connection.string with shell-safe single-quoted value", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "13_azure_application_insights_agent.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Dapplicationinsights.connection.string=InstrumentationKey=abc123;IngestionEndpoint=https://eastus.in.applicationinsights.azure.com/"))
// Value must be single-quoted so that eval exec java $JAVA_OPTS does not split on ; in the connection string
Expect(string(content)).To(ContainSubstring("-Dapplicationinsights.connection.string='InstrumentationKey=abc123;IngestionEndpoint=https://eastus.in.applicationinsights.azure.com/'"))
})
})

Expand All @@ -353,29 +354,31 @@ var _ = Describe("Azure Application Insights Agent", func() {
BeforeEach(func() {
installAzureAgent(depsDir, "3.4.0")
os.Setenv("VCAP_SERVICES", azureVCAPServices("azure-application-insights", "my-ai", nil,
`"connection_string":"InstrumentationKey=binding-key"`))
`"connection_string":"InstrumentationKey=binding-key;IngestionEndpoint=https://westeurope.in.applicationinsights.azure.com/"`))
})

It("opts file contains -Dapplicationinsights.connection.string from binding", func() {
It("opts file contains -Dapplicationinsights.connection.string with shell-safe single-quoted value", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "13_azure_application_insights_agent.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Dapplicationinsights.connection.string=InstrumentationKey=binding-key"))
// Value must be single-quoted so that eval exec java $JAVA_OPTS does not split on ;
Expect(string(content)).To(ContainSubstring("-Dapplicationinsights.connection.string='InstrumentationKey=binding-key;IngestionEndpoint=https://westeurope.in.applicationinsights.azure.com/'"))
})
})

Context("with connectionString credential in service binding (camelCase)", func() {
BeforeEach(func() {
installAzureAgent(depsDir, "3.4.0")
os.Setenv("VCAP_SERVICES", azureVCAPServices("azure-application-insights", "my-ai", nil,
`"connectionString":"InstrumentationKey=camel-key"`))
`"connectionString":"InstrumentationKey=camel-key;IngestionEndpoint=https://westeurope.in.applicationinsights.azure.com/"`))
})

It("opts file contains -Dapplicationinsights.connection.string from camelCase binding", func() {
It("opts file contains -Dapplicationinsights.connection.string with shell-safe single-quoted value", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "13_azure_application_insights_agent.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Dapplicationinsights.connection.string=InstrumentationKey=camel-key"))
// Value must be single-quoted so that eval exec java $JAVA_OPTS does not split on ;
Expect(string(content)).To(ContainSubstring("-Dapplicationinsights.connection.string='InstrumentationKey=camel-key;IngestionEndpoint=https://westeurope.in.applicationinsights.azure.com/'"))
})
})

Expand Down Expand Up @@ -412,15 +415,15 @@ var _ = Describe("Azure Application Insights Agent", func() {
Context("with connection string taking precedence over instrumentation key when both present in env", func() {
BeforeEach(func() {
installAzureAgent(depsDir, "3.4.0")
os.Setenv("APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=conn-str-key")
os.Setenv("APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=conn-str-key;IngestionEndpoint=https://eastus.in.applicationinsights.azure.com/")
os.Setenv("APPINSIGHTS_INSTRUMENTATIONKEY", "plain-ikey")
})

It("opts file uses connection string and not instrumentation key", func() {
It("opts file uses single-quoted connection string and not instrumentation key", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "13_azure_application_insights_agent.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Dapplicationinsights.connection.string=InstrumentationKey=conn-str-key"))
Expect(string(content)).To(ContainSubstring("-Dapplicationinsights.connection.string='InstrumentationKey=conn-str-key;IngestionEndpoint=https://eastus.in.applicationinsights.azure.com/'"))
Expect(string(content)).NotTo(ContainSubstring("applicationinsights.instrumentation-key"))
})
})
Expand Down