azure-spring-boot-starter-keyvault-secrets autoconfiguration of SecretClient does not work with spring-boot 2.7.17

302 Views Asked by At

I set up connection between spring-boot and AKV and everything works fine when @Bean SecretClient is added explicitly. I have looked across various code samples and for azure-spring-boot-starter-keyvault-secrets this should work automatically and set @Bean should not be necessary.

This is version of spring-boot and spring-cloud-azure I use

<spring.boot.version>2.7.17</spring.boot.version>
<spring-cloud-azure.version>4.12.0</spring-cloud-azure.version>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-dependencies</artifactId>
            <version>${spring-cloud-azure.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>spring-cloud-azure-starter-keyvault</artifactId>
</dependency>

and this is my application.yml configuration

spring:
  cloud:
    azure:
      keyvault:
        secret:
          property-sources[0]:
            endpoint: ${ENDPOINT_URL}
            credential:
              client-secret: ${AZURE_CLIENT_SECRET}
              client-id: ${AZURE_CLIENT_ID}
            profile:
              tenant-id: ${AZURE_TENANT_ID}

If I remove @Bean from my config

@Bean
public SecretClient secretClient() {
    return new SecretClientBuilder()
            .vaultUrl("akv-url")
            .credential(new DefaultAzureCredentialBuilder().build())
            .buildClient();
}

I see spring bean exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.azure.security.keyvault.secrets.SecretClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

No idea what the problem is, I checked for version compatibility and they should be compatible. Any ideas?

2

There are 2 best solutions below

0
michalavis On BEST ANSWER

I was able to solve this by looking at the this class -

com.azure.spring.cloud.autoconfigure.keyvault.secrets.AzureKeyVaultSecretAutoConfiguration

there is @ConditionOnAnyProperty annotation which checks the presence of "spring.cloud.azure.keyvault.secret.endpoint" in application.yml

So apparently property-sources[0] is unnecessary for this version, which is quite strange, because it was included in every tutorial and documentation on azure and version 4.12.0 is currently one of the latest for spring-boot below 3.0.0

Anyway - I might have thought earlier to look into the classes that configure this automatically, but at least now it works correctly and maybe it will help someone solve a similar problem more quickly.

0
Paizo On

for version compatibility is better to check spring boot/spring cloud and azure sdk bom version matrix

Spring boot/cloud matrix can be found here https://spring.io/projects/spring-cloud

for azure bom https://github.com/Azure/azure-sdk-for-java/wiki/Spring-Versions-Mapping

you should end up with a similar code snippet in your pom.xml file:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-sdk-bom</artifactId>
            <version>${azure-bom.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

where the version variables depends on your setup and the matrix mentioned above