Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SecretManagerPropertySource does not use configured GcpProjectIdProvider #3714

Open
mcantrell opened this issue Apr 2, 2025 · 0 comments
Open

Comments

@mcantrell
Copy link

Declaring a custom GcpProjectIdProvider does not appear to be effective for the SecretManagerPropertySource.

The following test shows that the Spring context is configured for a custom GcpProjectIdProvider but the DefaultGcpProjectIdProvider is used in the SecretManagerPropertySource.

@SpringBootTest
@Slf4j
public class GcpConfigIntegrationTest {

    @Autowired
    GcpProjectIdProvider provider;

    @Autowired
    private Environment environment;

    @Test
    void checkWiredGcpProjectIdProvider() throws Exception {
        assertInstanceOf(TestGcpProjectIdProvider.class, provider);
        var configurableEnvironment = (ConfigurableEnvironment) environment;
        for (var propertySource : configurableEnvironment.getPropertySources()) {
            if (propertySource instanceof SecretManagerPropertySource smps) {
                var wiredProvider = reflectProvider(smps);
                assertInstanceOf(TestGcpProjectIdProvider.class, wiredProvider);
            }
        }
    }

    private GcpProjectIdProvider reflectProvider(SecretManagerPropertySource source) throws Exception {
        var projectIdProviderField = SecretManagerPropertySource.class.getDeclaredField("projectIdProvider");
        projectIdProviderField.setAccessible(true);
        var providerObject = projectIdProviderField.get(source);
        return (GcpProjectIdProvider) providerObject;
    }

}
@Component
@Primary
public class TestGcpProjectIdProvider implements GcpProjectIdProvider {
    @Override
    public String getProjectId() {
        return "test-project-id";
    }
}
Expected :class com.acme.config.TestGcpProjectIdProvider
Actual   :class com.google.cloud.spring.core.DefaultGcpProjectIdProvider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant