Skip to content

[BUG] Passwordless authentication for RDBMS when multiple data sources are registered #41977

Open

Description

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:

  • com.azure.spring:spring-cloud-azure-autoconfigure:5.14.0
  • com.azure.spring:spring-cloud-azure-starter-jdbc-postgresql:5.14.0
    Java version: 17
    Frameworks: Spring Boot

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

  • [+] Bug Description Added
  • [+] Repro Steps Added
  • [+] Setup information Added
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.azure-springAll azure-spring related issuescustomer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

  • Status

    In Progress

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions