Skip to content

Commit

Permalink
Added conditionals to beans in order to avoid autoconfiguration when …
Browse files Browse the repository at this point in the history
…the necessary requirements are not set.
  • Loading branch information
rfc3092 committed Dec 18, 2024
1 parent ee8ca68 commit cb3200e
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
Expand All @@ -11,9 +12,9 @@
@AutoConfiguration
public class ClientCredentialAutoConfiguration {

private static final String AZURE_MISSING = "AAD_ISSUER_URI, AZURE_APP_CLIENT_ID and AZURE_APP_CLIENT_SECRET must be set";
private static final String TRYGDEETATEN_MISSING = "AZURE_TRYGDEETATEN_OPENID_CONFIG_TOKEN_ENDPOINT, AZURE_TRYGDEETATEN_APP_CLIENT_ID and AZURE_TRYGDEETATEN_APP_CLIENT_SECRET must be set";
private static final String NAV_MISSING = "AZURE_NAV_OPENID_CONFIG_TOKEN_ENDPOINT, AZURE_NAV_APP_CLIENT_ID and AZURE_NAV_APP_CLIENT_SECRET must be set";
private static final String AZURE_MISSING = "AZURE_APP_CLIENT_ID and AZURE_APP_CLIENT_SECRET must be set";
private static final String TRYGDEETATEN_MISSING = "AZURE_TRYGDEETATEN_APP_CLIENT_ID and AZURE_TRYGDEETATEN_APP_CLIENT_SECRET must be set";
private static final String NAV_MISSING = "AZURE_NAV_APP_CLIENT_ID and AZURE_NAV_APP_CLIENT_SECRET must be set";

private static final String TEST_TOKEN_ENDPOINT = "test-token-endpoint";
private static final String TEST_CLIENT_ID = "test-client-id";
Expand All @@ -27,13 +28,13 @@ AzureClientCredential azureClientCredentialTest() {
}

@Bean("azureClientCredential")
@ConditionalOnProperty("AAD_ISSUER_URI")
@ConditionalOnMissingBean(AzureClientCredential.class)
AzureClientCredential azureClientCredential(
@Value("${AAD_ISSUER_URI:#{null}}") String azureTokenEndpoint, // TODO: Not currently used, AAD_ISSUER_URI is hardcoded elsewhere; should be refactored to use AZURE_OPENID_CONFIG_TOKEN_ENDPOINT instead.
@Value("${AZURE_APP_CLIENT_ID:#{null}}") String azureClientId,
@Value("${AZURE_APP_CLIENT_SECRET:#{null}}") String azureClientSecret
) {
Assert.hasLength(azureTokenEndpoint, AZURE_MISSING);
Assert.hasLength(azureClientId, AZURE_MISSING);
Assert.hasLength(azureClientSecret, AZURE_MISSING);
return new AzureClientCredential(azureTokenEndpoint, azureClientId, azureClientSecret);
Expand All @@ -47,13 +48,13 @@ AzureTrygdeetatenClientCredential azureTrygdeetatenClientCredentialTest() {
}

@Bean("azureTrygdeetatenClientCredential")
@ConditionalOnProperty("AZURE_TRYGDEETATEN_OPENID_CONFIG_TOKEN_ENDPOINT")
@ConditionalOnMissingBean(AzureTrygdeetatenClientCredential.class)
AzureTrygdeetatenClientCredential azureTrygdeetatenClientCredential(
@Value("${AZURE_TRYGDEETATEN_OPENID_CONFIG_TOKEN_ENDPOINT:#{null}}") String azureTrygdeetatenTokenEndpoint,
@Value("${AZURE_TRYGDEETATEN_APP_CLIENT_ID:#{null}}") String azureTrygdeetatenClientId,
@Value("${AZURE_TRYGDEETATEN_APP_CLIENT_SECRET:#{null}}") String azureTrygdeetatenClientSecret
) {
Assert.hasLength(azureTrygdeetatenTokenEndpoint, TRYGDEETATEN_MISSING);
Assert.hasLength(azureTrygdeetatenClientId, TRYGDEETATEN_MISSING);
Assert.hasLength(azureTrygdeetatenClientSecret, TRYGDEETATEN_MISSING);
return new AzureTrygdeetatenClientCredential(azureTrygdeetatenTokenEndpoint, azureTrygdeetatenClientId, azureTrygdeetatenClientSecret);
Expand All @@ -67,13 +68,13 @@ AzureNavClientCredential azureNavClientCredentialTest() {
}

@Bean("azureNavClientCredential")
@ConditionalOnProperty("AZURE_NAV_OPENID_CONFIG_TOKEN_ENDPOINT")
@ConditionalOnMissingBean(AzureNavClientCredential.class)
AzureNavClientCredential azureNavClientCredential(
@Value("${AZURE_NAV_OPENID_CONFIG_TOKEN_ENDPOINT:#{null}}") String azureNavTokenEndpoint,
@Value("${AZURE_NAV_APP_CLIENT_ID:#{null}}") String azureNavClientId,
@Value("${AZURE_NAV_APP_CLIENT_SECRET:#{null}}") String azureNavClientSecret
) {
Assert.hasLength(azureNavTokenEndpoint, NAV_MISSING);
Assert.hasLength(azureNavClientId, NAV_MISSING);
Assert.hasLength(azureNavClientSecret, NAV_MISSING);
return new AzureNavClientCredential(azureNavTokenEndpoint, azureNavClientId, azureNavClientSecret);
Expand Down

0 comments on commit cb3200e

Please sign in to comment.