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

Handle wildcard parameter in RoleBinding deletion API #457

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public List<RoleBinding> list(String namespace, @QueryValue(defaultValue = "*")
* @param namespace The namespace
* @param name The role binding name
* @return A role binding
* @deprecated use list(String, String name) instead.
* @deprecated use {@link #list(String, String)} instead.
*/
@Get("/{name}")
@Deprecated(since = "1.12.0")
Expand Down Expand Up @@ -107,11 +107,13 @@ public HttpResponse<RoleBinding> apply(String namespace, @Valid @Body RoleBindin
*
* @param namespace The namespace
* @param name The role binding
* @param dryrun Is dry run mode or not ?
* @param dryrun Is dry run mode or not?
* @return An HTTP response
* @deprecated use {@link #bulkDelete(String, String, boolean)} instead.
*/
@Delete("/{name}{?dryrun}")
@Status(HttpStatus.NO_CONTENT)
@Deprecated(since = "1.13.0")
public HttpResponse<Void> delete(String namespace, String name,
@QueryValue(defaultValue = "false") boolean dryrun) {
Optional<RoleBinding> roleBinding = roleBindingService.findByName(namespace, name);
Expand All @@ -136,4 +138,39 @@ public HttpResponse<Void> delete(String namespace, String name,
roleBindingService.delete(roleBindingToDelete);
return HttpResponse.noContent();
}

/**
* Delete role bindings.
*
* @param namespace The namespace
* @param name The name parameter
* @param dryrun Is dry run mode or not?
* @return An HTTP response
*/
@Status(HttpStatus.NO_CONTENT)
@Delete
public HttpResponse<Void> bulkDelete(String namespace, @QueryValue(defaultValue = "*") String name,
@QueryValue(defaultValue = "false") boolean dryrun) {
List<RoleBinding> roleBindings = roleBindingService.findByWildcardName(namespace, name);
if (roleBindings.isEmpty()) {
return HttpResponse.notFound();
}

if (dryrun) {
return HttpResponse.noContent();
}

roleBindings.forEach(roleBinding -> {
sendEventLog(
roleBinding,
ApplyStatus.deleted,
roleBinding.getSpec(),
null,
EMPTY_STRING
);
roleBindingService.delete(roleBinding);
});

return HttpResponse.noContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.michelin.ns4kafka.service.NamespaceService;
import com.michelin.ns4kafka.service.RoleBindingService;
import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.http.HttpStatus;
import io.micronaut.security.utils.SecurityService;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -162,6 +163,7 @@ void shouldCreateRoleBindingInDryRunMode() {
}

@Test
@SuppressWarnings("deprecation")
void shouldDeleteRoleBinding() {
RoleBinding rolebinding = RoleBinding.builder()
.metadata(Metadata.builder()
Expand All @@ -183,6 +185,7 @@ void shouldDeleteRoleBinding() {
}

@Test
@SuppressWarnings("deprecation")
void shouldDeleteRoleBindingInDryRunMode() {
RoleBinding rolebinding = RoleBinding.builder()
.metadata(Metadata.builder()
Expand All @@ -197,6 +200,62 @@ void shouldDeleteRoleBindingInDryRunMode() {
verify(roleBindingService, never()).delete(any());
}

@Test
void shouldDeleteRoleBindings() {
RoleBinding rolebinding1 = RoleBinding.builder()
.metadata(Metadata.builder()
.name("test.rolebinding1")
.build())
.build();
RoleBinding rolebinding2 = RoleBinding.builder()
.metadata(Metadata.builder()
.name("test.rolebinding2")
.build())
.build();

when(roleBindingService.findByWildcardName(any(), any()))
.thenReturn(List.of(rolebinding1, rolebinding2));
when(securityService.username())
.thenReturn(Optional.of("test-user"));
when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN))
.thenReturn(false);
doNothing().when(applicationEventPublisher).publishEvent(any());

assertDoesNotThrow(
() -> roleBindingController.bulkDelete("test", "test.rolebinding*", false)
);
}

@Test
void shouldDeleteRoleBindingsInDryRunMode() {
RoleBinding rolebinding1 = RoleBinding.builder()
.metadata(Metadata.builder()
.name("test.rolebinding1")
.build())
.build();
RoleBinding rolebinding2 = RoleBinding.builder()
.metadata(Metadata.builder()
.name("test.rolebinding2")
.build())
.build();

when(roleBindingService.findByWildcardName(any(), any()))
.thenReturn(List.of(rolebinding1, rolebinding2));

roleBindingController.bulkDelete("test", "test.rolebinding*", true);
verify(roleBindingService, never()).delete(any());
}

@Test
void shouldNotDeleteRoleBindingsWhenNotFound() {
when(roleBindingService.findByWildcardName(any(), any()))
.thenReturn(List.of());

var response = roleBindingController.bulkDelete("test", "test.rolebinding*", false);
verify(roleBindingService, never()).delete(any());
assertEquals(HttpStatus.NOT_FOUND, response.getStatus());
}

@Test
void shouldListRoleBindingsWithNameParameter() {
RoleBinding rb1 = RoleBinding.builder()
Expand Down