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

Add auditing metadata in tests #190

Merged
merged 11 commits into from Apr 3, 2024
Merged

Add auditing metadata in tests #190

merged 11 commits into from Apr 3, 2024

Conversation

NielsCW
Copy link
Contributor

@NielsCW NielsCW commented Feb 29, 2024

No description provided.

Comment on lines 159 to 163
public static void loginUser(String username) {
var user = new User(username, "password", Set.of());
var authentication = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
var context = SecurityContextHolder.getContext();
context.setAuthentication(authentication);
}
Copy link
Member

Choose a reason for hiding this comment

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

can you explain how this is related to @WithMockUser on the class-level ?

Copy link
Contributor Author

@NielsCW NielsCW Mar 1, 2024

Choose a reason for hiding this comment

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

@WithMockUser sets the SecurityContext to contain an authenticated User with the given username. All HTTP requests and repository.save() calls will update lastModifiedBy to contain this mockuser. This loginUser() overrides SecurityContext.authentication to contain a new authenticated User. So that the new user is used in the auditing of the following requests. This is used to test whether lastModifiedBy can be altered when a different user makes a PUT request for example.

Copy link
Member

Choose a reason for hiding this comment

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

so the "problem" is that if you override @WithMockUser("other") on the test-method level, the before-each also runs with user "other" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With @WithMockUser alone, you can only test one user per test:
If a test is marked with @WithMockUser("John") and it calls a function that modifies an object in a repository with @WithMockUser("Bob"), lastModifiedDate of that object will be updated, but lastModifiedBy will contain "John".

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@NielsCW NielsCW Mar 1, 2024

Choose a reason for hiding this comment

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

The "problem" with this is that you can't use mocks and have to add extra org.springframework.security:spring-security-config dependency and implement security filtering in your configuration. I have not yet succeeded in getting it to work in demo application.


@Bean
public AuditorAware<UserMetadata> auditorProvider() {
return new AuditorAwareImpl();
Copy link
Member

Choose a reason for hiding this comment

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

to keep this test-fixture as compact as possible, I would prefer to inline the implementation as an anonymous class

Comment on lines 159 to 163
public static void loginUser(String username) {
var user = new User(username, "password", Set.of());
var authentication = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
var context = SecurityContextHolder.getContext();
context.setAuthentication(authentication);
}
Copy link
Member

Choose a reason for hiding this comment

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

so the "problem" is that if you override @WithMockUser("other") on the test-method level, the before-each also runs with user "other" ?

Comment on lines 159 to 163
public static void loginUser(String username) {
var user = new User(username, "password", Set.of());
var authentication = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
var context = SecurityContextHolder.getContext();
context.setAuthentication(authentication);
}
Copy link
Member

Choose a reason for hiding this comment

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

@tgeens
Copy link
Member

tgeens commented Mar 20, 2024

We need to figure out what the implications are of putting spring-security and spring-boot-starter-oauth2-resource-server on the classpath (in a separate PR)


private static ServerRequestObservationContext createContext() {
var request = new MockHttpServletRequest();
request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, "true");
Copy link
Member

Choose a reason for hiding this comment

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

why is this necessary ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests used to work because the handler in BasicAuditEventExtractor.createEventBuilder is null and it was missing a null check. Since the null-check is added and the requests are crafted manually, we add this attribute to make the handler non-null and the request isn't ignored.

Copy link
Member

Choose a reason for hiding this comment

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

isn't it fishy that all the code expect that it contains a request-handler-method and we're putting the string "true" in there ? 🤔

Copy link

sonarcloud bot commented Apr 3, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@NielsCW NielsCW merged commit 11775f2 into main Apr 3, 2024
9 checks passed
@NielsCW NielsCW deleted the auditing-metadata branch April 3, 2024 11:39
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

Successfully merging this pull request may close these issues.

None yet

2 participants