mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(inspector2): add organization configuration operations (#3162)
* feat(inspector2): add organization configuration operations * fix(inspector2): complete organization contracts * fix(inspector2): scope organization account state * test(inspector2): log tolerated organization state * test(inspector2): isolate organization cleanup
This commit is contained in:
1 parent
e313ab5b1d
commit
4944764fa3
17 files changed
+1137
-1
No files matched your search
@@ -729,6 +729,7 @@ public interface EmulatorConfig {
|
||||
AccessAnalyzerServiceConfig accessanalyzer();
|
||||
IdentityStoreServiceConfig identitystore();
|
||||
BudgetsServiceConfig budgets();
|
||||
Inspector2ServiceConfig inspector2();
|
||||
DetectiveServiceConfig detective();
|
||||
ServiceQuotasServiceConfig servicequotas();
|
||||
RamServiceConfig ram();
|
||||
@@ -784,6 +785,11 @@ public interface EmulatorConfig {
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface Inspector2ServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface DetectiveServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@@ -35,6 +35,7 @@ import io.github.hectorvent.floci.services.guardduty.GuardDutyController;
|
||||
import io.github.hectorvent.floci.services.macie2.MacieController;
|
||||
import io.github.hectorvent.floci.services.account.AccountController;
|
||||
import io.github.hectorvent.floci.services.accessanalyzer.AccessAnalyzerController;
|
||||
import io.github.hectorvent.floci.services.inspector2.Inspector2Controller;
|
||||
import io.github.hectorvent.floci.services.detective.DetectiveController;
|
||||
import io.github.hectorvent.floci.services.aps.ApsController;
|
||||
import io.github.hectorvent.floci.services.controlcatalog.ControlCatalogController;
|
||||
@@ -436,6 +437,9 @@ public class ResolvedServiceCatalog {
|
||||
descriptor("detective", "detective", config.services().detective().enabled(), true,
|
||||
"detective", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON), Set.of(), Set.of("detective"), Set.of(), Set.of(DetectiveController.class)),
|
||||
descriptor("inspector2", "inspector2", config.services().inspector2().enabled(), true,
|
||||
"inspector2", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON), Set.of(), Set.of("inspector2"), Set.of(), Set.of(Inspector2Controller.class)),
|
||||
descriptor("autoscaling", "autoscaling", config.services().autoscaling().enabled(), true,
|
||||
"autoscaling", config.storage().mode(), 5000L, AwsNamespaces.AUTOSCALING, ServiceProtocol.QUERY,
|
||||
protocols(ServiceProtocol.QUERY),
|
||||
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
package io.github.hectorvent.floci.services.inspector2;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.services.inspector2.model.InspectorState;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
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 Inspector2Controller {
|
||||
private final Inspector2Service service;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final RequestContext requestContext;
|
||||
|
||||
@Inject
|
||||
public Inspector2Controller(Inspector2Service service, RegionResolver regionResolver, ObjectMapper objectMapper,
|
||||
RequestContext requestContext) {
|
||||
this.service = service;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
this.requestContext = requestContext;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/delegatedadminaccounts/list")
|
||||
public Response listDelegatedAdminAccounts(@Context HttpHeaders headers, String body) {
|
||||
JsonNode request = parse(body);
|
||||
if (request.has("maxResults")) {
|
||||
JsonNode maxResults = request.get("maxResults");
|
||||
if (!maxResults.canConvertToInt() || maxResults.intValue() < 1 || maxResults.intValue() > 5) {
|
||||
throw new AwsException("ValidationException", "maxResults must be between 1 and 5.", 400);
|
||||
}
|
||||
}
|
||||
if (request.hasNonNull("nextToken") && !request.path("nextToken").asText().isBlank()) {
|
||||
throw new AwsException("ValidationException", "nextToken is invalid.", 400);
|
||||
}
|
||||
InspectorState state = service.delegatedAdminState(region(headers), requestContext.getAccountId());
|
||||
var response = objectMapper.createObjectNode();
|
||||
var accounts = response.putArray("delegatedAdminAccounts");
|
||||
if (state.getAdminAccountId() != null) {
|
||||
accounts.addObject().put("accountId", state.getAdminAccountId()).put("status", "ENABLED");
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/delegatedadminaccounts/enable")
|
||||
public Response enableDelegatedAdminAccount(@Context HttpHeaders headers, String body) {
|
||||
JsonNode request = parse(body);
|
||||
String accountId = request.path("delegatedAdminAccountId").asText(null);
|
||||
service.enableDelegatedAdmin(region(headers), requestContext.getAccountId(), accountId);
|
||||
var response = objectMapper.createObjectNode();
|
||||
response.put("delegatedAdminAccountId", accountId);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/delegatedadminaccounts/disable")
|
||||
public Response disableDelegatedAdminAccount(@Context HttpHeaders headers, String body) {
|
||||
JsonNode request = parse(body);
|
||||
String accountId = request.path("delegatedAdminAccountId").asText(null);
|
||||
service.disableDelegatedAdmin(region(headers), requestContext.getAccountId(), accountId);
|
||||
var response = objectMapper.createObjectNode();
|
||||
response.put("delegatedAdminAccountId", accountId);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/status/batch/get")
|
||||
public Response batchGetAccountStatus(@Context HttpHeaders headers, String body) {
|
||||
JsonNode request = parse(body);
|
||||
JsonNode accountIds = request.get("accountIds");
|
||||
if (accountIds != null && (!accountIds.isArray() || accountIds.size() > 100)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"accountIds must contain at most 100 account IDs.", 400);
|
||||
}
|
||||
java.util.List<String> requestedAccounts = requestedAccounts(accountIds);
|
||||
var response = objectMapper.createObjectNode();
|
||||
var accounts = response.putArray("accounts");
|
||||
for (String accountId : requestedAccounts) {
|
||||
InspectorState state = service.accountStatus(
|
||||
region(headers), requestContext.getAccountId(), accountId);
|
||||
var account = accounts.addObject();
|
||||
account.put("accountId", accountId);
|
||||
account.set("state", stateNode(state.getStatus()));
|
||||
account.set("resourceState", resourceState(state));
|
||||
}
|
||||
response.putArray("failedAccounts");
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/enable")
|
||||
public Response enable(@Context HttpHeaders headers, String body) {
|
||||
JsonNode request = parse(body);
|
||||
var enabled = service.enable(region(headers), requestContext.getAccountId(), request);
|
||||
var response = objectMapper.createObjectNode();
|
||||
var accounts = response.putArray("accounts");
|
||||
enabled.forEach((accountId, state) -> {
|
||||
var account = accounts.addObject();
|
||||
account.put("accountId", accountId);
|
||||
account.put("status", state.getStatus());
|
||||
account.set("resourceStatus", resourceStatus(state));
|
||||
});
|
||||
response.putArray("failedAccounts");
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/organizationconfiguration/update")
|
||||
public Response updateOrganizationConfiguration(@Context HttpHeaders headers, String body) {
|
||||
InspectorState state = service.updateOrganizationConfiguration(
|
||||
region(headers), requestContext.getAccountId(), parse(body));
|
||||
return Response.ok(organizationConfigurationResponse(state)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/organizationconfiguration/describe")
|
||||
public Response describeOrganizationConfiguration(@Context HttpHeaders headers, String body) {
|
||||
InspectorState state = service.organizationConfiguration(region(headers), requestContext.getAccountId());
|
||||
return Response.ok(organizationConfigurationResponse(state)).build();
|
||||
}
|
||||
|
||||
private com.fasterxml.jackson.databind.node.ObjectNode organizationConfigurationResponse(InspectorState state) {
|
||||
var response = objectMapper.createObjectNode();
|
||||
var autoEnable = response.putObject("autoEnable");
|
||||
autoEnable.put("ec2", state.isAutoEnableEc2());
|
||||
autoEnable.put("ecr", state.isAutoEnableEcr());
|
||||
autoEnable.put("lambda", state.isAutoEnableLambda());
|
||||
autoEnable.put("lambdaCode", state.isAutoEnableLambdaCode());
|
||||
autoEnable.put("codeRepository", state.isAutoEnableCodeRepository());
|
||||
response.put("maxAccountLimitReached", false);
|
||||
return response;
|
||||
}
|
||||
|
||||
private java.util.List<String> requestedAccounts(JsonNode accountIds) {
|
||||
if (accountIds == null || accountIds.isEmpty()) {
|
||||
return java.util.List.of(requestContext.getAccountId());
|
||||
}
|
||||
java.util.List<String> result = new java.util.ArrayList<>(accountIds.size());
|
||||
for (JsonNode accountId : accountIds) {
|
||||
Inspector2Service.requireAccountId(accountId.asText(null));
|
||||
result.add(accountId.asText());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private com.fasterxml.jackson.databind.node.ObjectNode resourceState(InspectorState state) {
|
||||
var resources = objectMapper.createObjectNode();
|
||||
resources.set("ec2", stateNode(state.getEc2Status()));
|
||||
resources.set("ecr", stateNode(state.getEcrStatus()));
|
||||
resources.set("lambda", stateNode(state.getLambdaStatus()));
|
||||
resources.set("lambdaCode", stateNode(state.getLambdaCodeStatus()));
|
||||
resources.set("codeRepository", stateNode(state.getCodeRepositoryStatus()));
|
||||
return resources;
|
||||
}
|
||||
|
||||
private com.fasterxml.jackson.databind.node.ObjectNode resourceStatus(InspectorState state) {
|
||||
var resources = objectMapper.createObjectNode();
|
||||
resources.put("ec2", state.getEc2Status());
|
||||
resources.put("ecr", state.getEcrStatus());
|
||||
resources.put("lambda", state.getLambdaStatus());
|
||||
resources.put("lambdaCode", state.getLambdaCodeStatus());
|
||||
resources.put("codeRepository", state.getCodeRepositoryStatus());
|
||||
return resources;
|
||||
}
|
||||
|
||||
private com.fasterxml.jackson.databind.node.ObjectNode stateNode(String status) {
|
||||
var state = objectMapper.createObjectNode();
|
||||
state.put("status", status);
|
||||
return state;
|
||||
}
|
||||
|
||||
private String region(HttpHeaders headers) {
|
||||
return regionResolver.resolveRegion(headers);
|
||||
}
|
||||
|
||||
private JsonNode parse(String body) {
|
||||
try {
|
||||
return objectMapper.readTree(body == null || body.isBlank() ? "{}" : body);
|
||||
} catch (Exception e) {
|
||||
throw new WebApplicationException(JsonErrorResponseUtils.createSerializationErrorResponse());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
package io.github.hectorvent.floci.services.inspector2;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
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.inspector2.model.InspectorState;
|
||||
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationScoped
|
||||
public class Inspector2Service implements Resettable {
|
||||
private static final Set<String> RESOURCE_TYPES = Set.of(
|
||||
"EC2", "ECR", "LAMBDA", "LAMBDA_CODE", "CODE_REPOSITORY");
|
||||
|
||||
private final AccountAwareStorageBackend<InspectorState> states;
|
||||
private final OrganizationsService organizationsService;
|
||||
|
||||
@Inject
|
||||
public Inspector2Service(StorageFactory storageFactory, OrganizationsService organizationsService) {
|
||||
this(storageFactory.create("inspector2", "inspector2-state.json",
|
||||
new TypeReference<Map<String, InspectorState>>() {}), organizationsService);
|
||||
}
|
||||
|
||||
Inspector2Service(AccountAwareStorageBackend<InspectorState> states, OrganizationsService organizationsService) {
|
||||
this.states = states;
|
||||
this.organizationsService = organizationsService;
|
||||
}
|
||||
|
||||
public InspectorState state(String region) {
|
||||
return states.get(region).orElseGet(InspectorState::new);
|
||||
}
|
||||
|
||||
public InspectorState delegatedAdminState(String region, String callerAccountId) {
|
||||
requireManagementAccount(callerAccountId);
|
||||
return stateForAccount(callerAccountId, region);
|
||||
}
|
||||
|
||||
public synchronized void enableDelegatedAdmin(String region, String callerAccountId, String accountId) {
|
||||
requireAccountId(accountId);
|
||||
String managementAccountId = requireManagementAccount(callerAccountId);
|
||||
requireSameOrganization(managementAccountId, accountId);
|
||||
|
||||
InspectorState management = stateForAccount(managementAccountId, region);
|
||||
if (management.getAdminAccountId() != null && !management.getAdminAccountId().equals(accountId)) {
|
||||
throw new AwsException("ConflictException",
|
||||
"A different delegated administrator is already configured.", 409);
|
||||
}
|
||||
management.setAdminAccountId(accountId);
|
||||
states.putForAccount(managementAccountId, region, management);
|
||||
|
||||
InspectorState delegated = stateForAccount(accountId, region);
|
||||
delegated.setAdminAccountId(accountId);
|
||||
for (String resourceType : RESOURCE_TYPES) {
|
||||
delegated.setResourceStatus(resourceType, "ENABLED");
|
||||
}
|
||||
delegated.setStatus("ENABLED");
|
||||
delegated.setEnablingPollsRemaining(0);
|
||||
states.putForAccount(accountId, region, delegated);
|
||||
}
|
||||
|
||||
public synchronized void disableDelegatedAdmin(String region, String callerAccountId, String accountId) {
|
||||
requireAccountId(accountId);
|
||||
String managementAccountId = requireManagementAccount(callerAccountId);
|
||||
requireSameOrganization(managementAccountId, accountId);
|
||||
InspectorState management = stateForAccount(managementAccountId, region);
|
||||
if (!accountId.equals(management.getAdminAccountId())) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"The specified delegated administrator was not found.", 404);
|
||||
}
|
||||
management.setAdminAccountId(null);
|
||||
states.putForAccount(managementAccountId, region, management);
|
||||
InspectorState delegated = stateForAccount(accountId, region);
|
||||
delegated.setAdminAccountId(null);
|
||||
states.putForAccount(accountId, region, delegated);
|
||||
}
|
||||
|
||||
public synchronized InspectorState accountStatus(String region, String callerAccountId, String accountId) {
|
||||
requireAccountId(accountId);
|
||||
authorizeAccountAccess(region, callerAccountId, accountId);
|
||||
InspectorState state = stateForAccount(accountId, region);
|
||||
if ("ENABLING".equals(state.getStatus()) && state.getEnablingPollsRemaining() > 0) {
|
||||
InspectorState response = copyState(state);
|
||||
state.setEnablingPollsRemaining(state.getEnablingPollsRemaining() - 1);
|
||||
states.putForAccount(accountId, region, state);
|
||||
return response;
|
||||
}
|
||||
if ("ENABLING".equals(state.getStatus())) {
|
||||
for (String resourceType : RESOURCE_TYPES) {
|
||||
if ("ENABLING".equals(state.resourceStatus(resourceType))) {
|
||||
state.setResourceStatus(resourceType, "ENABLED");
|
||||
}
|
||||
}
|
||||
state.setStatus(overallStatus(state));
|
||||
states.putForAccount(accountId, region, state);
|
||||
}
|
||||
return copyState(state);
|
||||
}
|
||||
|
||||
public synchronized Map<String, InspectorState> enable(
|
||||
String region, String callerAccountId, JsonNode request) {
|
||||
List<String> resourceTypes = resourceTypes(request);
|
||||
List<String> accountIds = accountIds(request, callerAccountId);
|
||||
|
||||
for (String accountId : accountIds) {
|
||||
authorizeAccountAccess(region, callerAccountId, accountId);
|
||||
}
|
||||
|
||||
Map<String, InspectorState> result = new LinkedHashMap<>();
|
||||
for (String accountId : accountIds) {
|
||||
InspectorState state = stateForAccount(accountId, region);
|
||||
boolean changed = false;
|
||||
for (String resourceType : resourceTypes) {
|
||||
String current = state.resourceStatus(resourceType);
|
||||
if (!"ENABLED".equals(current) && !"ENABLING".equals(current)) {
|
||||
state.setResourceStatus(resourceType, "ENABLING");
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
state.setStatus("ENABLING");
|
||||
state.setEnablingPollsRemaining(1);
|
||||
states.putForAccount(accountId, region, state);
|
||||
}
|
||||
result.put(accountId, copyState(state));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public synchronized InspectorState updateOrganizationConfiguration(
|
||||
String region, String callerAccountId, JsonNode request) {
|
||||
InspectorState state = requireAdministrator(region, callerAccountId);
|
||||
JsonNode autoEnable = request.get("autoEnable");
|
||||
if (autoEnable == null || !autoEnable.isObject()) {
|
||||
throw new AwsException("ValidationException", "autoEnable is required.", 400);
|
||||
}
|
||||
|
||||
boolean ec2 = requiredBoolean(autoEnable, "ec2");
|
||||
boolean ecr = requiredBoolean(autoEnable, "ecr");
|
||||
boolean lambda = optionalBoolean(autoEnable, "lambda");
|
||||
boolean lambdaCode = optionalBoolean(autoEnable, "lambdaCode");
|
||||
boolean codeRepository = optionalBoolean(autoEnable, "codeRepository");
|
||||
|
||||
state.setAutoEnableEc2(ec2);
|
||||
state.setAutoEnableEcr(ecr);
|
||||
state.setAutoEnableLambda(lambda);
|
||||
state.setAutoEnableLambdaCode(lambdaCode);
|
||||
state.setAutoEnableCodeRepository(codeRepository);
|
||||
states.putForAccount(callerAccountId, region, state);
|
||||
return copyState(state);
|
||||
}
|
||||
|
||||
public InspectorState organizationConfiguration(String region, String callerAccountId) {
|
||||
return copyState(requireAdministrator(region, callerAccountId));
|
||||
}
|
||||
|
||||
private InspectorState requireAdministrator(String region, String callerAccountId) {
|
||||
String managementAccountId = managementAccountFor(callerAccountId)
|
||||
.orElseThrow(() -> new AwsException("AccessDeniedException",
|
||||
"Only the delegated Amazon Inspector administrator can manage organization configuration.", 403));
|
||||
InspectorState management = stateForAccount(managementAccountId, region);
|
||||
if (management.getAdminAccountId() == null || !callerAccountId.equals(management.getAdminAccountId())) {
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"Only the delegated Amazon Inspector administrator can manage organization configuration.", 403);
|
||||
}
|
||||
return stateForAccount(callerAccountId, region);
|
||||
}
|
||||
|
||||
private void authorizeAccountAccess(String region, String callerAccountId, String targetAccountId) {
|
||||
if (callerAccountId.equals(targetAccountId)) {
|
||||
return;
|
||||
}
|
||||
String managementAccountId = managementAccountFor(callerAccountId)
|
||||
.orElseThrow(() -> accessDenied("The caller cannot manage the requested Amazon Inspector account."));
|
||||
requireSameOrganization(managementAccountId, targetAccountId);
|
||||
InspectorState management = stateForAccount(managementAccountId, region);
|
||||
if (!callerAccountId.equals(management.getAdminAccountId())) {
|
||||
throw accessDenied("Only the delegated Amazon Inspector administrator can manage member accounts.");
|
||||
}
|
||||
}
|
||||
|
||||
private String requireManagementAccount(String callerAccountId) {
|
||||
String managementAccountId = managementAccountFor(callerAccountId)
|
||||
.orElseThrow(() -> accessDenied("Only the AWS Organizations management account can perform this operation."));
|
||||
if (!callerAccountId.equals(managementAccountId)) {
|
||||
throw accessDenied("Only the AWS Organizations management account can perform this operation.");
|
||||
}
|
||||
return managementAccountId;
|
||||
}
|
||||
|
||||
private void requireSameOrganization(String managementAccountId, String accountId) {
|
||||
String targetManagementAccount = managementAccountFor(accountId)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"The specified AWS account is not a member of the organization.", 404));
|
||||
if (!managementAccountId.equals(targetManagementAccount)) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"The specified AWS account is not a member of the organization.", 404);
|
||||
}
|
||||
}
|
||||
|
||||
private java.util.Optional<String> managementAccountFor(String accountId) {
|
||||
return organizationsService.findManagementAccountForResource(accountId);
|
||||
}
|
||||
|
||||
private InspectorState stateForAccount(String accountId, String region) {
|
||||
return states.getForAccount(accountId, region).orElseGet(InspectorState::new);
|
||||
}
|
||||
|
||||
private static List<String> resourceTypes(JsonNode request) {
|
||||
JsonNode resourceTypes = request.get("resourceTypes");
|
||||
if (resourceTypes == null || !resourceTypes.isArray() || resourceTypes.isEmpty() || resourceTypes.size() > 5) {
|
||||
throw new AwsException("ValidationException",
|
||||
"resourceTypes must contain between 1 and 5 resource types.", 400);
|
||||
}
|
||||
List<String> result = new ArrayList<>(resourceTypes.size());
|
||||
for (JsonNode resourceType : resourceTypes) {
|
||||
if (!resourceType.isTextual() || !RESOURCE_TYPES.contains(resourceType.textValue())) {
|
||||
throw new AwsException("ValidationException", "resourceTypes contains an invalid resource type.", 400);
|
||||
}
|
||||
if (!result.contains(resourceType.textValue())) {
|
||||
result.add(resourceType.textValue());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<String> accountIds(JsonNode request, String callerAccountId) {
|
||||
JsonNode accountIds = request.get("accountIds");
|
||||
if (accountIds != null && (!accountIds.isArray() || accountIds.size() > 100)) {
|
||||
throw new AwsException("ValidationException", "accountIds must contain at most 100 account IDs.", 400);
|
||||
}
|
||||
if (accountIds == null || accountIds.isEmpty()) {
|
||||
return List.of(callerAccountId);
|
||||
}
|
||||
List<String> result = new ArrayList<>(accountIds.size());
|
||||
for (JsonNode accountId : accountIds) {
|
||||
requireAccountId(accountId.asText(null));
|
||||
result.add(accountId.asText());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String overallStatus(InspectorState state) {
|
||||
boolean enabling = RESOURCE_TYPES.stream().anyMatch(type -> "ENABLING".equals(state.resourceStatus(type)));
|
||||
if (enabling) {
|
||||
return "ENABLING";
|
||||
}
|
||||
boolean enabled = RESOURCE_TYPES.stream().anyMatch(type -> "ENABLED".equals(state.resourceStatus(type)));
|
||||
return enabled ? "ENABLED" : "DISABLED";
|
||||
}
|
||||
|
||||
private static InspectorState copyState(InspectorState source) {
|
||||
InspectorState copy = new InspectorState();
|
||||
copy.setAdminAccountId(source.getAdminAccountId());
|
||||
copy.setStatus(source.getStatus());
|
||||
copy.setEnablingPollsRemaining(source.getEnablingPollsRemaining());
|
||||
copy.setEc2Status(source.getEc2Status());
|
||||
copy.setEcrStatus(source.getEcrStatus());
|
||||
copy.setLambdaStatus(source.getLambdaStatus());
|
||||
copy.setLambdaCodeStatus(source.getLambdaCodeStatus());
|
||||
copy.setCodeRepositoryStatus(source.getCodeRepositoryStatus());
|
||||
copy.setAutoEnableEc2(source.isAutoEnableEc2());
|
||||
copy.setAutoEnableEcr(source.isAutoEnableEcr());
|
||||
copy.setAutoEnableLambda(source.isAutoEnableLambda());
|
||||
copy.setAutoEnableLambdaCode(source.isAutoEnableLambdaCode());
|
||||
copy.setAutoEnableCodeRepository(source.isAutoEnableCodeRepository());
|
||||
return copy;
|
||||
}
|
||||
|
||||
private static boolean requiredBoolean(JsonNode node, String field) {
|
||||
JsonNode value = node.get(field);
|
||||
if (value == null || !value.isBoolean()) {
|
||||
throw new AwsException("ValidationException", "autoEnable." + field + " is required.", 400);
|
||||
}
|
||||
return value.booleanValue();
|
||||
}
|
||||
|
||||
private static boolean optionalBoolean(JsonNode node, String field) {
|
||||
JsonNode value = node.get(field);
|
||||
if (value == null || value.isNull()) {
|
||||
return false;
|
||||
}
|
||||
if (!value.isBoolean()) {
|
||||
throw new AwsException("ValidationException", "autoEnable." + field + " must be a boolean.", 400);
|
||||
}
|
||||
return value.booleanValue();
|
||||
}
|
||||
|
||||
private static AwsException accessDenied(String message) {
|
||||
return new AwsException("AccessDeniedException", message, 403);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
states.clear();
|
||||
}
|
||||
|
||||
static void requireAccountId(String accountId) {
|
||||
if (accountId == null || !accountId.matches("\\d{12}")) {
|
||||
throw new AwsException("ValidationException",
|
||||
"accountId must be a 12 digit AWS account ID.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package io.github.hectorvent.floci.services.inspector2.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class InspectorState {
|
||||
private String adminAccountId;
|
||||
private String status = "DISABLED";
|
||||
private int enablingPollsRemaining;
|
||||
private String ec2Status = "DISABLED";
|
||||
private String ecrStatus = "DISABLED";
|
||||
private String lambdaStatus = "DISABLED";
|
||||
private String lambdaCodeStatus = "DISABLED";
|
||||
private String codeRepositoryStatus = "DISABLED";
|
||||
private boolean autoEnableEc2;
|
||||
private boolean autoEnableEcr;
|
||||
private boolean autoEnableLambda;
|
||||
private boolean autoEnableLambdaCode;
|
||||
private boolean autoEnableCodeRepository;
|
||||
|
||||
public InspectorState() {
|
||||
}
|
||||
|
||||
public String getAdminAccountId() { return adminAccountId; }
|
||||
public void setAdminAccountId(String adminAccountId) { this.adminAccountId = adminAccountId; }
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
public int getEnablingPollsRemaining() { return enablingPollsRemaining; }
|
||||
public void setEnablingPollsRemaining(int enablingPollsRemaining) { this.enablingPollsRemaining = enablingPollsRemaining; }
|
||||
public String getEc2Status() { return ec2Status; }
|
||||
public void setEc2Status(String value) { ec2Status = value; }
|
||||
public String getEcrStatus() { return ecrStatus; }
|
||||
public void setEcrStatus(String value) { ecrStatus = value; }
|
||||
public String getLambdaStatus() { return lambdaStatus; }
|
||||
public void setLambdaStatus(String value) { lambdaStatus = value; }
|
||||
public String getLambdaCodeStatus() { return lambdaCodeStatus; }
|
||||
public void setLambdaCodeStatus(String value) { lambdaCodeStatus = value; }
|
||||
public String getCodeRepositoryStatus() { return codeRepositoryStatus; }
|
||||
public void setCodeRepositoryStatus(String value) { codeRepositoryStatus = value; }
|
||||
public boolean isAutoEnableEc2() { return autoEnableEc2; }
|
||||
public void setAutoEnableEc2(boolean value) { autoEnableEc2 = value; }
|
||||
public boolean isAutoEnableEcr() { return autoEnableEcr; }
|
||||
public void setAutoEnableEcr(boolean value) { autoEnableEcr = value; }
|
||||
public boolean isAutoEnableLambda() { return autoEnableLambda; }
|
||||
public void setAutoEnableLambda(boolean value) { autoEnableLambda = value; }
|
||||
public boolean isAutoEnableLambdaCode() { return autoEnableLambdaCode; }
|
||||
public void setAutoEnableLambdaCode(boolean value) { autoEnableLambdaCode = value; }
|
||||
public boolean isAutoEnableCodeRepository() { return autoEnableCodeRepository; }
|
||||
public void setAutoEnableCodeRepository(boolean value) { autoEnableCodeRepository = value; }
|
||||
|
||||
public String resourceStatus(String resourceType) {
|
||||
return switch (resourceType) {
|
||||
case "EC2" -> ec2Status;
|
||||
case "ECR" -> ecrStatus;
|
||||
case "LAMBDA" -> lambdaStatus;
|
||||
case "LAMBDA_CODE" -> lambdaCodeStatus;
|
||||
case "CODE_REPOSITORY" -> codeRepositoryStatus;
|
||||
default -> throw new IllegalArgumentException("Unknown Inspector resource type: " + resourceType);
|
||||
};
|
||||
}
|
||||
|
||||
public void setResourceStatus(String resourceType, String value) {
|
||||
switch (resourceType) {
|
||||
case "EC2" -> ec2Status = value;
|
||||
case "ECR" -> ecrStatus = value;
|
||||
case "LAMBDA" -> lambdaStatus = value;
|
||||
case "LAMBDA_CODE" -> lambdaCodeStatus = value;
|
||||
case "CODE_REPOSITORY" -> codeRepositoryStatus = value;
|
||||
default -> throw new IllegalArgumentException("Unknown Inspector resource type: " + resourceType);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user