Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.35k stars 1.99k forks source link

[BUG] Still use connection string auth even if the connection string property is set as empty string #42880

Open haoozhang opened 2 days ago

haoozhang commented 2 days ago

Describe the bug Even if the connection string property is set as empty, the application still tries to auth via connection string.

The application is a simple spring cloud stream + kafka, refering to the sample project https://github.com/Azure-Samples/azure-spring-boot-samples/tree/main/eventhubs/spring-cloud-azure-starter/spring-cloud-azure-sample-eventhubs-kafka.

In the application.yaml, when we specify the auth as follows, we want to use managed identity auth, instead of connection string (we leave it empty), but the application still try to use connection string for auth, causing it cannot startup.

spring:
  cloud:
        azure:
          eventhubs:
            credential:
              managed-identity-enabled: true
              client-id:  XXX
          connection-string:  # empty string

Exception or Stack Trace

In the following file, when the property is set as empty, it should not enter the method. but now it will execute this method even if empty.

public class AzureEventHubsKafkaAutoConfiguration {

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

    @Bean
    @ConditionalOnProperty("spring.cloud.azure.eventhubs.connection-string")
    @ConditionalOnMissingBean(value = AzureServiceType.EventHubs.class, parameterizedContainer = ServiceConnectionStringProvider.class)
    StaticConnectionStringProvider<AzureServiceType.EventHubs> eventHubsKafkaConnectionString(Environment environment) {
        String connectionString = environment.getProperty("spring.cloud.azure.eventhubs.connection-string");

        try {
            new EventHubsConnectionString(connectionString);
        } catch (Exception e) {
            LOGGER.error("A valid Event Hubs connection string must be provided");
            throw e;
        }

        return new StaticConnectionStringProvider<>(AzureServiceType.EVENT_HUBS, connectionString);
    }

To Reproduce Steps to reproduce the behavior:

  1. Clone the sample project.
  2. As described above, specify the application properties and then run.

Code Snippet Add the code snippet that causes the issue.

Expected behavior

When setting connection string as empty, it should auto-use MI configuration for auth.

Screenshots If applicable, add screenshots to help explain your problem.

Setup (please complete the following information):

If you suspect a dependency version mismatch (e.g. you see NoClassDefFoundError, NoSuchMethodError or similar), please check out Troubleshoot dependency version conflict article first. If it doesn't provide solution for the problem, please provide:

Additional context Add any other context about the problem here.

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

github-actions[bot] commented 2 days ago

@moarychan @netyyyy @rujche @saragluna

github-actions[bot] commented 2 days ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

rujche commented 1 day ago

Another problem is that for spring-cloud-starter-stream-kafka, SPRING_CLOUD_AZURE_EVENTHUBS_CONNECTIONSTRING not work, SPRING_CLOUD_AZURE_EVENTHUBS_CONNECTION_STRING works.

But for spring-cloud-azure-stream-binder-eventhubs, SPRING_CLOUD_AZURE_EVENTHUBS_CONNECTIONSTRING works well. This behavior will make customer confused.

rujche commented 1 day ago

FYI: More information about environment variable used in Spring Boot can be found here: https://github.com/azure-javaee/azure-dev/wiki/Knowledge-Sharing#2-sometimes-the-environment-variable-not-take-effect-in-spring-boot-application