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] Passwordless authentication for RDBMS when multiple data sources are registered #41977

Closed Rapirys closed 2 weeks ago

Rapirys commented 1 month ago

Describe the bug Hello,

When using the Azure SDK for Spring to enable passwordless authentication for PostgreSQL (and potentially other RDBMS as well) with multiple data sources, the application fails to start due to a bean naming conflict. Specifically, the JdbcPropertiesBeanPostProcessor is executed twice and attempts to register the bean named SpringTokenCredentialProvider::PASSWORDLESS_TOKEN_CREDENTIAL_BEAN_NAME several times, resulting in an error.

The line which causes the error can be found here.

Exception or Stack Trace

2024-09-23T12:46:51.710+02:00  WARN 19768 --- [example-svc] [           main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readModelEntityManagerFactory' defined in class path resource [com /example/config/JpaReadModelDatasourceConfiguration.class]: Failed to initialize dependency 'readModelLiquibase' of LoadTimeWeaverAware bean 'readModelEntityManagerFactory': Error creating bean with name 'readModelLiquibase' defined in class path resource [com /example/config/LiquibaseConfiguration.class]: Unsatisfied dependency expressed through method 'readModelLiquibase' parameter 0: Error creating bean with name 'readModelDataSource' defined in class path resource [com /example/config/JpaReadModelDatasourceConfiguration.class]: Unsatisfied dependency expressed through method 'readModelDataSource' parameter 0: Error creating bean with name 'readModelDataSourceProperties' defined in class path resource [com /example/config/JpaReadModelDatasourceConfiguration.class]: Invalid bean definition with name 'passwordlessTokenCredential' defined in null: Cannot register bean definition [Root bean: class [com.azure.core.credential.TokenCredential]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null] for bean 'passwordlessTokenCredential' since there is already [Root bean: class [com.azure.core.credential.TokenCredential]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null] bound.
2024-09-23T12:46:51.756+02:00  INFO 19768 --- [example-svc] [           main] .s.b.a.l.ConditionEvaluationReportLogger :
Disconnected from the target VM, address: 'localhost:59571', transport: 'socket'

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-09-23T12:46:51.801+02:00 ERROR 19768 --- [example-svc] [           main] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'passwordlessTokenCredential' could not be registered. A bean with that name has already been defined and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

To Reproduce

  1. Create spring project.
  2. Define 2 spring beans of type DataSource .
  3. Configure passwordless authentication to azure Azure Database for PostgreSQL(or other RDBMS which supports passwordless authentication) using spring properties.

Code Snippet The spring application.yaml config file

spring:
  datasource:
    azure:
      passwordless-enabled: true
    read-model:
      url: jdbc:postgresql://[placholder]:5432/read_model_db?sslmode=require
      username: [URL to azure database] 
    write-model:
      url: jdbc:postgresql://[placholder]:5432/read_model_db?sslmode=require
      username: [placholder]

The data-source configuration

  @Primary
  @Bean(name = "readModelDataSourceProperties")
  @ConfigurationProperties(prefix = "spring.datasource.read-model")
  public DataSourceProperties dataSourceProperties() {
    return new DataSourceProperties();
  }

  @Primary
  @Bean(name = "readModelDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.read-model")
  public DataSource readModelDataSource(DataSourceProperties dataSourceProperties) {
    return dataSourceProperties.initializeDataSourceBuilder().build();
  }

  @Bean(name = "writeModelDataSourceProperties")
  @ConfigurationProperties(prefix = "spring.datasource.write-model")
  public DataSourceProperties dataSourceProperties() {
    return new DataSourceProperties();
  }

  @Bean(name = "writeModelDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.write-model")
  public DataSource dataSource(
      @Qualifier("writeModelDataSourceProperties") DataSourceProperties dataSourceProperties) {
    return dataSourceProperties.initializeDataSourceBuilder().build();
  }

Expected behavior The application should initialize without errors, with two distinct data sources, and correctly configure passwordless authentication.

Setup (please complete the following information): OS: Windows 11 IDE: [ntelliJ IDEA Library/Libraries:

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 1 month ago

@moarychan @netyyyy @rujche @saragluna

github-actions[bot] commented 1 month ago

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

stijnvanbever commented 1 month ago

We are also impacted by this bug and waiting for a fix.