mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(macie2): add organization administration operations (#3105)
* feat(macie2): add organization administration operations * test(macie2): validate organization administration with sdk * fix(macie2): harden delegated administration routing
This commit is contained in:
1 parent
85180a4d88
commit
3927c66040
23 files changed
+566
-21
No files matched your search
@@ -710,6 +710,7 @@ public interface EmulatorConfig {
|
||||
NetworkFirewallServiceConfig networkfirewall();
|
||||
ServiceCatalogServiceConfig servicecatalog();
|
||||
SsoAdminServiceConfig ssoadmin();
|
||||
Macie2ServiceConfig macie2();
|
||||
ServiceQuotasServiceConfig servicequotas();
|
||||
RamServiceConfig ram();
|
||||
ControlTowerServiceConfig controltower();
|
||||
@@ -738,6 +739,11 @@ public interface EmulatorConfig {
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface Macie2ServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface ApsServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@@ -32,6 +32,7 @@ import io.github.hectorvent.floci.services.ses.SesController;
|
||||
import io.github.hectorvent.floci.services.appsync.AppSyncController;
|
||||
import io.github.hectorvent.floci.services.rdsdata.RdsDataController;
|
||||
import io.github.hectorvent.floci.services.guardduty.GuardDutyController;
|
||||
import io.github.hectorvent.floci.services.macie2.MacieController;
|
||||
import io.github.hectorvent.floci.services.aps.ApsController;
|
||||
import io.github.hectorvent.floci.services.controltower.ControlTowerControlController;
|
||||
import io.github.hectorvent.floci.services.controltower.ControlTowerController;
|
||||
@@ -408,6 +409,9 @@ public class ResolvedServiceCatalog {
|
||||
null, null, 5000L, null, ServiceProtocol.JSON,
|
||||
protocols(ServiceProtocol.JSON),
|
||||
Set.of("SWBExternalService."), Set.of("sso"), Set.of(), Set.of()),
|
||||
descriptor("macie2", "macie2", config.services().macie2().enabled(), true,
|
||||
"macie2", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON), Set.of(), Set.of("macie2"), Set.of(), Set.of(MacieController.class)),
|
||||
descriptor("autoscaling", "autoscaling", config.services().autoscaling().enabled(), true,
|
||||
"autoscaling", config.storage().mode(), 5000L, AwsNamespaces.AUTOSCALING, ServiceProtocol.QUERY,
|
||||
protocols(ServiceProtocol.QUERY),
|
||||
|
||||
@@ -59,6 +59,8 @@ public class ServiceEnabledFilter implements ContainerRequestFilter {
|
||||
}
|
||||
|
||||
return SigV4CredentialScope.serviceName(ctx.getHeaderString("Authorization"))
|
||||
.or(() -> SigV4CredentialScope.serviceNameFromCredential(
|
||||
ctx.getUriInfo().getQueryParameters().getFirst("X-Amz-Credential")))
|
||||
.flatMap(catalog::byCredentialScope)
|
||||
.map(descriptor -> new ResolvedRequest(
|
||||
descriptor.externalKey(), protocolFor(claim, descriptor)))
|
||||
|
||||
@@ -26,4 +26,11 @@ public final class SigV4CredentialScope {
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public static Optional<String> serviceNameFromCredential(String credential) {
|
||||
if (credential == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return serviceName("Credential=" + credential);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.AdminAccount;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.MemberAccount;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.Detector;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.DetectorFeature;
|
||||
@@ -216,23 +215,4 @@ public class GuardDutyController {
|
||||
service.disableOrganizationAdminAccount(regionResolver.resolveRegion(headers), parse(body));
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/admin")
|
||||
public Response listOrganizationAdminAccounts(
|
||||
@Context HttpHeaders headers,
|
||||
@QueryParam("maxResults") String maxResults,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
GuardDutyService.Page<AdminAccount> page = service.listOrganizationAdminAccounts(
|
||||
regionResolver.resolveRegion(headers), maxResults, nextToken);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode accounts = response.putArray("adminAccounts");
|
||||
for (AdminAccount account : page.items()) {
|
||||
accounts.add(objectMapper.valueToTree(account));
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("nextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package io.github.hectorvent.floci.services.macie2;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.macie2.model.MacieState;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.PATCH;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
@Path("/")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class MacieController {
|
||||
private final MacieService macieService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public MacieController(MacieService macieService, RegionResolver regionResolver, ObjectMapper objectMapper) {
|
||||
this.macieService = macieService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public Response listAdminInternal(String region) {
|
||||
MacieState state = macieService.state(region);
|
||||
var response = objectMapper.createObjectNode();
|
||||
var accounts = response.putArray("adminAccounts");
|
||||
if (state.getAdminAccountId() != null) {
|
||||
accounts.addObject().put("accountId", state.getAdminAccountId()).put("status", "ENABLED");
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@POST @Path("/admin")
|
||||
public Response enableOrganizationAdminAccount(@Context HttpHeaders headers, String body) {
|
||||
macieService.enableOrganizationAdminAccount(region(headers), readTree(body).path("adminAccountId").asText(null));
|
||||
return empty();
|
||||
}
|
||||
|
||||
@GET @Path("/macie")
|
||||
public Response getMacieSession(@Context HttpHeaders headers) {
|
||||
macieService.requireSession(region(headers));
|
||||
var response = objectMapper.createObjectNode();
|
||||
response.put("status", "ENABLED");
|
||||
response.put("serviceRole", "arn:aws:iam::" + regionResolver.getAccountId() + ":role/aws-service-role/macie.amazonaws.com/AWSServiceRoleForAmazonMacie");
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@POST @Path("/macie")
|
||||
public Response enableMacie(@Context HttpHeaders headers, String body) {
|
||||
macieService.enableMacie(region(headers));
|
||||
return empty();
|
||||
}
|
||||
|
||||
@PATCH @Path("/admin/configuration")
|
||||
public Response updateOrganizationConfiguration(@Context HttpHeaders headers, String body) {
|
||||
JsonNode request = readTree(body);
|
||||
if (!request.has("autoEnable") || !request.get("autoEnable").isBoolean()) {
|
||||
throw new io.github.hectorvent.floci.core.common.AwsException("ValidationException", "autoEnable is required.", 400);
|
||||
}
|
||||
macieService.updateOrganizationConfiguration(
|
||||
region(headers), regionResolver.getAccountId(), request.path("autoEnable").asBoolean());
|
||||
return empty();
|
||||
}
|
||||
|
||||
@GET @Path("/admin/configuration")
|
||||
public Response describeOrganizationConfiguration(@Context HttpHeaders headers) {
|
||||
MacieState state = macieService.requireAdministratorSession(region(headers), regionResolver.getAccountId());
|
||||
var response = objectMapper.createObjectNode();
|
||||
response.put("autoEnable", state.isAutoEnable());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private String region(HttpHeaders headers) { return regionResolver.resolveRegion(headers); }
|
||||
private Response empty() { return Response.ok(objectMapper.createObjectNode()).build(); }
|
||||
private JsonNode readTree(String body) { try { return objectMapper.readTree(body == null || body.isBlank() ? "{}" : body); } catch (Exception e) { throw new WebApplicationException(JsonErrorResponseUtils.createSerializationErrorResponse()); } }
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package io.github.hectorvent.floci.services.macie2;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.macie2.model.MacieState;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ApplicationScoped
|
||||
public class MacieService implements Resettable {
|
||||
private final AccountAwareStorageBackend<MacieState> states;
|
||||
|
||||
@Inject
|
||||
public MacieService(StorageFactory storageFactory) {
|
||||
this(storageFactory.create("macie2", "macie2-state.json",
|
||||
new TypeReference<Map<String, MacieState>>() {}));
|
||||
}
|
||||
|
||||
MacieService(AccountAwareStorageBackend<MacieState> states) {
|
||||
this.states = states;
|
||||
}
|
||||
|
||||
public MacieState state(String region) {
|
||||
return states.get(region).orElseGet(MacieState::new);
|
||||
}
|
||||
|
||||
public synchronized void enableOrganizationAdminAccount(String region, String accountId) {
|
||||
requireAccountId(accountId);
|
||||
MacieState state = state(region);
|
||||
if (state.getAdminAccountId() != null && !state.getAdminAccountId().equals(accountId)) {
|
||||
throw conflict("A different Macie administrator account is already configured.");
|
||||
}
|
||||
state.setAdminAccountId(accountId);
|
||||
states.put(region, state);
|
||||
|
||||
MacieState delegated = states.getForAccount(accountId, region).orElseGet(MacieState::new);
|
||||
delegated.setAdminAccountId(accountId);
|
||||
delegated.setEnabled(true);
|
||||
states.putForAccount(accountId, region, delegated);
|
||||
}
|
||||
|
||||
public synchronized void enableMacie(String region) {
|
||||
MacieState state = state(region);
|
||||
if (state.isEnabled()) {
|
||||
throw conflict("Macie is already enabled for this account.");
|
||||
}
|
||||
state.setEnabled(true);
|
||||
states.put(region, state);
|
||||
}
|
||||
|
||||
public MacieState requireSession(String region) {
|
||||
MacieState state = state(region);
|
||||
if (!state.isEnabled()) {
|
||||
throw notFound("Macie is not enabled for this account.");
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
public MacieState requireAdministratorSession(String region, String callerAccountId) {
|
||||
MacieState state = states.getForAccount(callerAccountId, region).orElseGet(MacieState::new);
|
||||
if (state.getAdminAccountId() == null && !state.isEnabled()) {
|
||||
throw notFound("Macie is not enabled for this account.");
|
||||
}
|
||||
if (!callerAccountId.equals(state.getAdminAccountId())) {
|
||||
throw accessDenied();
|
||||
}
|
||||
if (!state.isEnabled()) {
|
||||
throw notFound("Macie is not enabled for this account.");
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
public synchronized void updateOrganizationConfiguration(
|
||||
String region, String callerAccountId, boolean autoEnable) {
|
||||
MacieState state = requireAdministratorSession(region, callerAccountId);
|
||||
state.setAutoEnable(autoEnable);
|
||||
states.putForAccount(callerAccountId, region, state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
states.clear();
|
||||
}
|
||||
|
||||
private static void requireAccountId(String accountId) {
|
||||
if (accountId == null || !accountId.matches("\\d{12}")) {
|
||||
throw new AwsException("ValidationException", "adminAccountId must be a 12 digit account ID.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException conflict(String message) {
|
||||
return new AwsException("ConflictException", message, 409);
|
||||
}
|
||||
|
||||
private static AwsException notFound(String message) {
|
||||
return new AwsException("ResourceNotFoundException", message, 404);
|
||||
}
|
||||
|
||||
private static AwsException accessDenied() {
|
||||
return new AwsException("AccessDeniedException",
|
||||
"Only the delegated Macie administrator account can manage organization configuration.", 403);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.github.hectorvent.floci.services.macie2.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class MacieState {
|
||||
private String adminAccountId;
|
||||
private boolean enabled;
|
||||
private boolean autoEnable;
|
||||
|
||||
public MacieState() {}
|
||||
public String getAdminAccountId() { return adminAccountId; }
|
||||
public void setAdminAccountId(String adminAccountId) { this.adminAccountId = adminAccountId; }
|
||||
public boolean isEnabled() { return enabled; }
|
||||
public void setEnabled(boolean enabled) { this.enabled = enabled; }
|
||||
public boolean isAutoEnable() { return autoEnable; }
|
||||
public void setAutoEnable(boolean autoEnable) { this.autoEnable = autoEnable; }
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package io.github.hectorvent.floci.services.securityadmin;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.SigV4CredentialScope;
|
||||
import io.github.hectorvent.floci.services.guardduty.GuardDutyService;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.AdminAccount;
|
||||
import io.github.hectorvent.floci.services.macie2.MacieController;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
|
||||
@Path("/")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class SecurityAdminController {
|
||||
private final GuardDutyService guardDutyService;
|
||||
private final MacieController macieController;
|
||||
private final ObjectMapper mapper;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public SecurityAdminController(GuardDutyService guardDutyService, MacieController macieController,
|
||||
ObjectMapper mapper, RegionResolver regionResolver) {
|
||||
this.guardDutyService = guardDutyService;
|
||||
this.macieController = macieController;
|
||||
this.mapper = mapper;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/admin")
|
||||
public Response listAdmin(@Context HttpHeaders headers,
|
||||
@Context UriInfo uriInfo,
|
||||
@QueryParam("maxResults") String maxResults,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
String service = SigV4CredentialScope.serviceName(headers.getHeaderString("Authorization"))
|
||||
.or(() -> SigV4CredentialScope.serviceNameFromCredential(
|
||||
uriInfo.getQueryParameters().getFirst("X-Amz-Credential")))
|
||||
.orElse("");
|
||||
if ("macie2".equals(service)) {
|
||||
String region = headers.getHeaderString("Authorization") != null
|
||||
? regionResolver.resolveRegion(headers)
|
||||
: regionResolver.resolveRegionFromPresignedCredential(
|
||||
uriInfo.getQueryParameters().getFirst("X-Amz-Credential"));
|
||||
return macieController.listAdminInternal(region);
|
||||
}
|
||||
if ("guardduty".equals(service) || service.isBlank()) {
|
||||
GuardDutyService.Page<AdminAccount> page = guardDutyService.listOrganizationAdminAccounts(
|
||||
regionResolver.resolveRegion(headers), maxResults, nextToken);
|
||||
var response = mapper.createObjectNode();
|
||||
var accounts = response.putArray("adminAccounts");
|
||||
for (AdminAccount account : page.items()) {
|
||||
accounts.add(mapper.valueToTree(account));
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("nextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
throw new AwsException("UnknownOperationException", "GET /admin is not supported for " + service + ".", 400);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user