Skip to content

Conversation

@ielatif
Copy link
Contributor

@ielatif ielatif commented Nov 21, 2025

Fixes #874

@ielatif ielatif changed the title Allow certificate authentication to specify certificate role Allow TLS certificate to authenticate against the named certificate role Nov 26, 2025
Copy link
Member

@spencergibb spencergibb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for you contribution! Please add a test.

public void contextLoads() {
assertThat(this.configValue).isEqualTo("foo");
void authenticateUsingTlsCertificate() {
ConfigurableApplicationContext context = this.app.run("--spring.cloud.vault.authentication=cert",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the repetitions, would you mind introducing an abstraction that removes duplications?

A possible design could be a context runner utility that accepts reactive, bootstrap and test class details (basically everything that is changing across individual test methods while hiding everything that remains the same) and then invokes app.run(…)?

Copy link
Contributor Author

@ielatif ielatif Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mp911de for your review.

A possible design could be a context runner utility that accepts reactive, bootstrap and test class details (basically everything that is changing across individual test methods while hiding everything that remains the same) and then invokes app.run(…)?

Are you considering a generic solution for all Vault tests or a specific abstraction for TLS authentication tests?

However, i can remove the duplications by configuring the SpringApplication app with shared properties

SpringApplication app = new SpringApplicationBuilder().sources(TestConfig.class)
	.web(WebApplicationType.NONE)
	.properties("spring.cloud.vault.authentication=cert",
		"spring.cloud.vault.ssl.key-store=file:../work/client-cert.jks",
		"spring.cloud.vault.ssl.key-store-password=changeit",
		"spring.cloud.vault.application-name=VaultConfigTlsCertAuthenticationTests",
		"spring.cloud.vault.reactive.enabled=false", "spring.cloud.bootstrap.enabled=true")
	.build();

Then add specific properties when running tests :

ConfigurableApplicationContext context = this.app.run("--spring.cloud.vault.ssl.role=my-role");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you considering a generic solution for all Vault tests or a specific abstraction for TLS authentication tests?

Going forward, I'd like to have a consistent approach for all tests, however in the scope of this ticket, let's only focus on this test class to keep the scope rather focussed. In any case, appreciate your view outside of the box :-)

I think that SpringApplicationBuilder is a fine abstraction for the low-level part. By putting the aspect in front what is relevant for the actual test turns the entire property handling (names, values) rather into noise. I'm thinking about a functional design approach along the lines of:

VaultTestContextRuner.of(this.app).testClass(getClass()).sources(…).property("spring.cloud.vault.ssl.role", "my-role").run()

We would pick it then up for other tests and extend it to:

VaultTestContextRuner.of(this.app).sources(…).config(c -> c.reactive(true/false).boostrap(true/false)).run()

This is mostly an idea for now, when we introduce the broader notion of VaultTestContextRuner, we might refine the API as needed and based on how it makes the most sense.

Copy link
Contributor Author

@ielatif ielatif Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea. I can explore it further in a separate PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mp911de I had some time this weekend, so I gave your idea a try.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot. Let us take this PR from here.

@mp911de mp911de self-assigned this Dec 8, 2025
@ielatif
Copy link
Contributor Author

ielatif commented Dec 8, 2025

@mp911de while working on #751 I discovred that spring-vault have rich testing utilities than spring-cloud-vault like VaultExtension and VaultInitializer. I think it will be very helpfull if we extract these utilities in a seperate module spring-vault-test and use them in spring-cloud-vault tests to avoid boilerplate code for example in @BeforeAll. It will also facilitate maintenance as code will be put in one place.

What do you think?

@mp911de
Copy link
Member

mp911de commented Dec 9, 2025

VaultExtension works well for Spring Vault expecting Vault to run on a specific port assuming an initial key and SSL configuration being provided through a bundle. These are rather specific assumptions that don't hold necessarily true for application out there. I don't have much reason to introduce another module and maintain test support, especially that nowadays testcontainers-driven integration testing is much more common than it has been when creating the project.

@ielatif
Copy link
Contributor Author

ielatif commented Dec 9, 2025

I completely agree with this assessment about Testcontainers.
I haven't proposed using Testcontainers primarily because I assumed that option was off the table for this project's testing approach. Thanks for clarifying it.

@mp911de mp911de added this to the 5.0.1 milestone Dec 10, 2025
mp911de pushed a commit that referenced this pull request Dec 10, 2025
…ole.

Also, introduce VaultTestContextRunner.

Fixes gh-874
Original pull request: gh-875
Signed-off-by: Issam El-atif <[email protected]>
mp911de added a commit that referenced this pull request Dec 10, 2025
Simplify test, use token for assertions for tighter assumptions instead of relying on 403 responses potentially caused by other reasons.

See gh-874
Original pull request: gh-875
mp911de added a commit that referenced this pull request Dec 10, 2025
Also, refine method naming and return a new instance of the runner to avoid shared mutable state.

See gh-874
Original pull request: gh-875
@mp911de
Copy link
Member

mp911de commented Dec 10, 2025

Thank you for your contribution. That's merged and polished now. VaultTestContextRunner is a decent improvement to keep Vault integration testing settings in a common place. In some areas, we're using deliberately ApplicationContextRunner as it uses isolated auto-configuration and some places still make use of @SpringBootTest. We don't have a test module and we don't want to introduce one just for the sake of simplifying some additional tests right now. In any case, this has been a good exercise.

@mp911de mp911de closed this Dec 10, 2025
@ielatif
Copy link
Contributor Author

ielatif commented Dec 10, 2025

In any case, this has been a good exercise.

Indeed!
I enjoyed contributing to this and greatly appreciate your valuable feedback.
Thanks.

@ielatif ielatif deleted the gh-874 branch December 10, 2025 13:10
@mp911de
Copy link
Member

mp911de commented Dec 10, 2025

Likewise, you might want to have a look at the polishing commits that follow your contribution commit to get an idea how the context runner has evolved.

@ielatif
Copy link
Contributor Author

ielatif commented Dec 10, 2025

I Love it! More robust more fluent a master touch! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow TLS certificate to authenticate against the named certificate role

3 participants