Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge branch 'ARUHA-998-admin-endpoint' into ARUHA-1077-admin-endpoints
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/org/zalando/nakadi/controller/SettingsController.java
#	src/test/java/org/zalando/nakadi/service/AdminServiceTest.java
  • Loading branch information
Lionel Montrieux committed Aug 31, 2017
2 parents d460634 + fb90914 commit 9864d05
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.junit.Assert;
import org.junit.Test;
import org.zalando.nakadi.domain.EventType;
import org.zalando.nakadi.domain.EventTypeAuthorization;
import org.zalando.nakadi.domain.EventTypeAuthorizationAttribute;
import org.zalando.nakadi.domain.ResourceAuthorization;
import org.zalando.nakadi.domain.ResourceAuthorizationAttribute;
import org.zalando.nakadi.domain.Timeline;
import org.zalando.nakadi.exceptions.NoSuchEventTypeException;
import org.zalando.nakadi.partitioning.PartitionStrategy;
Expand Down Expand Up @@ -245,10 +245,10 @@ public void whenUpdateRetentionTimeWithNullValueNoChange() throws Exception {
public void whenPOSTEventTypeWithAuthorizationThenOk() throws JsonProcessingException {
final EventType eventType = buildDefaultEventType();

eventType.setAuthorization(new EventTypeAuthorization(
ImmutableList.of(new EventTypeAuthorizationAttribute("type1", "value1")),
ImmutableList.of(new EventTypeAuthorizationAttribute("type2", "value2")),
ImmutableList.of(new EventTypeAuthorizationAttribute("type3", "value3"))));
eventType.setAuthorization(new ResourceAuthorization(
ImmutableList.of(new ResourceAuthorizationAttribute("type1", "value1")),
ImmutableList.of(new ResourceAuthorizationAttribute("type2", "value2")),
ImmutableList.of(new ResourceAuthorizationAttribute("type3", "value3"))));

final String body = MAPPER.writer().writeValueAsString(eventType);
given().body(body)
Expand All @@ -271,10 +271,10 @@ public void whenPOSTEventTypeWithAuthorizationThenOk() throws JsonProcessingExce

@Test
public void whenUpdateETAuthObjectThen422() throws Exception {
final EventTypeAuthorization auth = new EventTypeAuthorization(
Collections.singletonList(new EventTypeAuthorizationAttribute("type1", "value1")),
Collections.singletonList(new EventTypeAuthorizationAttribute("type2", "value2")),
Collections.singletonList(new EventTypeAuthorizationAttribute("type3", "value3")));
final ResourceAuthorization auth = new ResourceAuthorization(
Collections.singletonList(new ResourceAuthorizationAttribute("type1", "value1")),
Collections.singletonList(new ResourceAuthorizationAttribute("type2", "value2")),
Collections.singletonList(new ResourceAuthorizationAttribute("type3", "value3")));
final EventType eventType = EventTypeTestBuilder.builder().authorization(auth).build();
NakadiTestUtils.createEventTypeInNakadi(eventType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;
import org.zalando.nakadi.config.SecuritySettings;
import org.zalando.nakadi.domain.AdminAuthorization;
import org.zalando.nakadi.domain.ItemsWrapper;
import org.zalando.nakadi.domain.ResourceAuthorization;
import org.zalando.nakadi.exceptions.runtime.UnknownOperationException;
import org.zalando.nakadi.plugin.api.authz.AuthorizationService;
import org.zalando.nakadi.security.Client;
Expand Down Expand Up @@ -103,11 +103,11 @@ public ResponseEntity<?> getAdmins() {
if (!adminService.isAdmin(AuthorizationService.Operation.READ)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
return ResponseEntity.ok(AdminAuthorization.fromPermissionsList(adminService.getAdmins()));
return ResponseEntity.ok(ResourceAuthorization.fromPermissionsList(adminService.getAdmins()));
}

@RequestMapping(path = "/admins", method = RequestMethod.POST)
public ResponseEntity<?> updateAdmins(@Valid @RequestBody final AdminAuthorization authz) {
public ResponseEntity<?> updateAdmins(@Valid @RequestBody final ResourceAuthorization authz) {
if (!adminService.isAdmin(AuthorizationService.Operation.ADMIN)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/zalando/nakadi/domain/AdminResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class AdminResource implements Resource {
public static final String ADMIN_RESOURCE = "nakadi";

private final String name;
private final AdminAuthorization etAuthorization;
private final ResourceAuthorization resourceAuthorization;

public AdminResource(final String name, final AdminAuthorization etAuthorization) {
public AdminResource(final String name, final ResourceAuthorization resourceAuthorization) {
this.name = name;
this.etAuthorization = etAuthorization;
this.resourceAuthorization = resourceAuthorization;
}

@Override
Expand All @@ -34,18 +34,18 @@ public Optional<List<AuthorizationAttribute>> getAttributesForOperation(
final AuthorizationService.Operation operation) {
switch (operation) {
case READ:
return Optional.of(etAuthorization.getReaders());
return Optional.of(resourceAuthorization.getReaders());
case WRITE:
return Optional.of(etAuthorization.getWriters());
return Optional.of(resourceAuthorization.getWriters());
case ADMIN:
return Optional.of(etAuthorization.getAdmins());
return Optional.of(resourceAuthorization.getAdmins());
default:
throw new IllegalArgumentException("Operation " + operation + " is not supported");
}
}

public List<Permission> getPermissionsList() {
return etAuthorization.toPermissionsList(name);
return resourceAuthorization.toPermissionsList(name);
}

@Override
Expand Down

This file was deleted.

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/org/zalando/nakadi/domain/EventTypeBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class EventTypeBase {
private EventTypeOptions options;

@Valid
private EventTypeAuthorization authorization;
private ResourceAuthorization authorization;

private CompatibilityMode compatibilityMode;

Expand Down Expand Up @@ -203,11 +203,11 @@ public void setValidationStrategies(final List<ValidationStrategyConfiguration>
}

@Nullable
public EventTypeAuthorization getAuthorization() {
public ResourceAuthorization getAuthorization() {
return authorization;
}

public void setAuthorization(final EventTypeAuthorization authorization) {
public void setAuthorization(final ResourceAuthorization authorization) {
this.authorization = authorization;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
public class EventTypeResource implements Resource {

private final String name;
private final EventTypeAuthorization etAuthorization;
private final ResourceAuthorization etAuthorization;

public EventTypeResource(final String name, final EventTypeAuthorization etAuthorization) {
public EventTypeResource(final String name, final ResourceAuthorization etAuthorization) {
this.name = name;
this.etAuthorization = etAuthorization;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/zalando/nakadi/domain/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.zalando.nakadi.plugin.api.authz.AuthorizationService;

import javax.annotation.concurrent.Immutable;
import java.util.Objects;

@Immutable
public class Permission {
Expand All @@ -29,4 +30,24 @@ public AuthorizationService.Operation getOperation() {
public AuthorizationAttribute getAuthorizationAttribute() {
return authorizationAttribute;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}
final Permission that = (Permission) o;
return Objects.equals(resource, that.resource) &&
Objects.equals(operation, that.operation) &&
Objects.equals(authorizationAttribute, that.authorizationAttribute);
}

@Override
public int hashCode() {
return Objects.hash(resource, operation, authorizationAttribute);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import jdk.nashorn.internal.ir.annotations.Immutable;
import org.zalando.nakadi.exceptions.runtime.UnknownOperationException;
import org.zalando.nakadi.plugin.api.authz.AuthorizationAttribute;
import org.zalando.nakadi.plugin.api.authz.AuthorizationService;

import javax.annotation.concurrent.Immutable;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
Expand All @@ -16,29 +16,29 @@
import java.util.stream.Collectors;

@Immutable
public class AdminAuthorization {
public class ResourceAuthorization {

@NotNull
@Valid
@Size(min = 1, message = "must contain at least one attribute")
@JsonDeserialize(contentAs = AdminAuthorizationAttribute.class)
@JsonDeserialize(contentAs = ResourceAuthorizationAttribute.class)
private final List<AuthorizationAttribute> admins;

@NotNull
@Valid
@Size(min = 1, message = "must contain at least one attribute")
@JsonDeserialize(contentAs = AdminAuthorizationAttribute.class)
@JsonDeserialize(contentAs = ResourceAuthorizationAttribute.class)
private final List<AuthorizationAttribute> readers;

@NotNull
@Valid
@Size(min = 1, message = "must contain at least one attribute")
@JsonDeserialize(contentAs = AdminAuthorizationAttribute.class)
@JsonDeserialize(contentAs = ResourceAuthorizationAttribute.class)
private final List<AuthorizationAttribute> writers;

public AdminAuthorization(@JsonProperty("admins") final List<AuthorizationAttribute> admins,
@JsonProperty("readers") final List<AuthorizationAttribute> readers,
@JsonProperty("writers") final List<AuthorizationAttribute> writers) {
public ResourceAuthorization(@JsonProperty("admins") final List<AuthorizationAttribute> admins,
@JsonProperty("readers") final List<AuthorizationAttribute> readers,
@JsonProperty("writers") final List<AuthorizationAttribute> writers) {
// actually these three properties should never be null but the validation framework first creates an object
// and then uses getters to check if values are null or not, so we need to do this check to avoid exception
this.admins = admins == null ? null : Collections.unmodifiableList(admins);
Expand Down Expand Up @@ -85,7 +85,7 @@ public List<Permission> toPermissionsList(final String resource) {
return permissions;
}

public static AdminAuthorization fromPermissionsList(final List<Permission> permissions) {
public static ResourceAuthorization fromPermissionsList(final List<Permission> permissions) {
final List<AuthorizationAttribute> admins = permissions.stream()
.filter(p -> p.getOperation().equals(AuthorizationService.Operation.ADMIN))
.map(p -> p.getAuthorizationAttribute())
Expand All @@ -99,7 +99,7 @@ public static AdminAuthorization fromPermissionsList(final List<Permission> perm
.map(p -> p.getAuthorizationAttribute())
.collect(Collectors.toList());

return new AdminAuthorization(admins, readers, writers);
return new ResourceAuthorization(admins, readers, writers);
}

@Override
Expand All @@ -111,7 +111,7 @@ public boolean equals(final Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
final AdminAuthorization that = (AdminAuthorization) o;
final ResourceAuthorization that = (ResourceAuthorization) o;
return Objects.equals(admins, that.admins) &&
Objects.equals(readers, that.readers) &&
Objects.equals(writers, that.writers);
Expand Down
Loading

0 comments on commit 9864d05

Please sign in to comment.