mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(accessanalyzer): add analyzer lifecycle (#3117)
* feat(accessanalyzer): add analyzer lifecycle * fix(accessanalyzer): complete service contracts * test(accessanalyzer): make SDK cleanup best effort * test(accessanalyzer): use project logging for cleanup
This commit is contained in:
1 parent
9b28388d89
commit
aebed6e15c
17 files changed
+619
-1
No files matched your search
@@ -711,6 +711,7 @@ public interface EmulatorConfig {
|
||||
ServiceCatalogServiceConfig servicecatalog();
|
||||
SsoAdminServiceConfig ssoadmin();
|
||||
Macie2ServiceConfig macie2();
|
||||
AccessAnalyzerServiceConfig accessanalyzer();
|
||||
ServiceQuotasServiceConfig servicequotas();
|
||||
RamServiceConfig ram();
|
||||
ControlTowerServiceConfig controltower();
|
||||
@@ -744,6 +745,11 @@ public interface EmulatorConfig {
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface AccessAnalyzerServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface ApsServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@@ -33,6 +33,7 @@ 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.accessanalyzer.AccessAnalyzerController;
|
||||
import io.github.hectorvent.floci.services.aps.ApsController;
|
||||
import io.github.hectorvent.floci.services.controltower.ControlTowerControlController;
|
||||
import io.github.hectorvent.floci.services.controltower.ControlTowerController;
|
||||
@@ -412,6 +413,9 @@ public class ResolvedServiceCatalog {
|
||||
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("access-analyzer", "accessanalyzer", config.services().accessanalyzer().enabled(), true,
|
||||
"accessanalyzer", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON), Set.of(), Set.of("access-analyzer"), Set.of(), Set.of(AccessAnalyzerController.class)),
|
||||
descriptor("autoscaling", "autoscaling", config.services().autoscaling().enabled(), true,
|
||||
"autoscaling", config.storage().mode(), 5000L, AwsNamespaces.AUTOSCALING, ServiceProtocol.QUERY,
|
||||
protocols(ServiceProtocol.QUERY),
|
||||
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package io.github.hectorvent.floci.services.accessanalyzer;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.accessanalyzer.model.Analyzer;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.PUT;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
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 AccessAnalyzerController {
|
||||
private final AccessAnalyzerService accessAnalyzerService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public AccessAnalyzerController(AccessAnalyzerService accessAnalyzerService,
|
||||
RegionResolver regionResolver, ObjectMapper objectMapper) {
|
||||
this.accessAnalyzerService = accessAnalyzerService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/analyzer")
|
||||
public Response listAnalyzers(@Context HttpHeaders headers,
|
||||
@QueryParam("type") String type,
|
||||
@QueryParam("maxResults") String maxResults,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<Analyzer> page = accessAnalyzerService.listAnalyzers(
|
||||
region, type, Pagination.parseMaxResults(maxResults, "ValidationException"), nextToken);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode items = response.putArray("analyzers");
|
||||
page.items().forEach(analyzer -> items.add(analyzerSummary(analyzer)));
|
||||
if (page.nextToken() != null) {
|
||||
response.put("nextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/analyzer")
|
||||
public Response createAnalyzer(@Context HttpHeaders headers, String body) {
|
||||
Analyzer analyzer = accessAnalyzerService.createAnalyzer(readTree(body), regionResolver.resolveRegion(headers));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("arn", analyzer.getArn());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/analyzer/{analyzerName}")
|
||||
public Response deleteAnalyzer(@Context HttpHeaders headers, @PathParam("analyzerName") String analyzerName) {
|
||||
accessAnalyzerService.deleteAnalyzer(regionResolver.resolveRegion(headers), analyzerName);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private ObjectNode analyzerSummary(Analyzer analyzer) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("arn", analyzer.getArn());
|
||||
node.put("name", analyzer.getName());
|
||||
node.put("type", analyzer.getType());
|
||||
node.put("status", analyzer.getStatus());
|
||||
node.put("createdAt", analyzer.getCreatedAt());
|
||||
if (analyzer.getConfiguration() != null) {
|
||||
node.set("configuration", analyzer.getConfiguration());
|
||||
}
|
||||
node.set("tags", objectMapper.valueToTree(analyzer.getTags()));
|
||||
return node;
|
||||
}
|
||||
|
||||
private JsonNode readTree(String body) {
|
||||
try {
|
||||
return objectMapper.reader()
|
||||
.with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
|
||||
.readTree(body == null || body.isBlank() ? "{}" : body);
|
||||
} catch (Exception e) {
|
||||
throw new WebApplicationException(JsonErrorResponseUtils.createSerializationErrorResponse());
|
||||
}
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
package io.github.hectorvent.floci.services.accessanalyzer;
|
||||
|
||||
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.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
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.accessanalyzer.model.Analyzer;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class AccessAnalyzerService implements Resettable {
|
||||
private static final Pattern ANALYZER_NAME = Pattern.compile("[A-Za-z][A-Za-z0-9_.-]*");
|
||||
private static final Set<String> ANALYZER_TYPES = Set.of(
|
||||
"ACCOUNT",
|
||||
"ORGANIZATION",
|
||||
"ACCOUNT_UNUSED_ACCESS",
|
||||
"ORGANIZATION_UNUSED_ACCESS",
|
||||
"ACCOUNT_INTERNAL_ACCESS",
|
||||
"ORGANIZATION_INTERNAL_ACCESS");
|
||||
|
||||
private final AccountAwareStorageBackend<Analyzer> analyzers;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public AccessAnalyzerService(StorageFactory storageFactory, RegionResolver regionResolver) {
|
||||
this.analyzers = storageFactory.create("accessanalyzer", "accessanalyzer-analyzers.json",
|
||||
new TypeReference<Map<String, Analyzer>>() {});
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
public synchronized Analyzer createAnalyzer(JsonNode request, String region) {
|
||||
String name = requireAnalyzerName(request);
|
||||
String type = requireAnalyzerType(text(request, "type"));
|
||||
String key = storageKey(region, name);
|
||||
if (analyzers.get(key).isPresent()) {
|
||||
throw new AwsException("ConflictException",
|
||||
"An analyzer with the specified name already exists.", 409);
|
||||
}
|
||||
|
||||
List<Analyzer> inRegion = analyzers.scan(candidate -> candidate.startsWith(region + "::"));
|
||||
long sameType = inRegion.stream().filter(analyzer -> type.equals(analyzer.getType())).count();
|
||||
if (sameType >= analyzerLimit(type)) {
|
||||
throw new AwsException("ServiceQuotaExceededException",
|
||||
"The analyzer quota for this account or organization in the Region has been exceeded.", 402);
|
||||
}
|
||||
|
||||
Analyzer analyzer = new Analyzer();
|
||||
analyzer.setName(name);
|
||||
analyzer.setType(type);
|
||||
analyzer.setStatus("ACTIVE");
|
||||
analyzer.setCreatedAt(Instant.now().toString());
|
||||
analyzer.setArn(regionResolver.buildArn("access-analyzer", region, "analyzer/" + name));
|
||||
analyzer.setTags(readTags(request.get("tags")));
|
||||
if (request.hasNonNull("configuration")) {
|
||||
if (!request.get("configuration").isObject()) {
|
||||
throw validation("configuration must be an object.");
|
||||
}
|
||||
analyzer.setConfiguration(request.get("configuration").deepCopy());
|
||||
}
|
||||
analyzers.put(key, analyzer);
|
||||
return analyzer;
|
||||
}
|
||||
|
||||
public PaginatedResult<Analyzer> listAnalyzers(String region, String type, Integer maxResults, String nextToken) {
|
||||
String requestedType = type == null || type.isBlank() ? null : requireAnalyzerType(type);
|
||||
List<Analyzer> matching = analyzers.scan(key -> key.startsWith(region + "::")).stream()
|
||||
.filter(analyzer -> requestedType == null || requestedType.equals(analyzer.getType()))
|
||||
.toList();
|
||||
return Pagination.paginate(matching, Analyzer::getName, maxResults, nextToken,
|
||||
100, 1000, "ValidationException");
|
||||
}
|
||||
|
||||
public synchronized void deleteAnalyzer(String region, String analyzerName) {
|
||||
validateAnalyzerName(analyzerName);
|
||||
String key = storageKey(region, analyzerName);
|
||||
if (analyzers.get(key).isEmpty()) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"The specified analyzer could not be found.", 404);
|
||||
}
|
||||
analyzers.delete(key);
|
||||
}
|
||||
|
||||
private static int analyzerLimit(String type) {
|
||||
return switch (type) {
|
||||
case "ORGANIZATION", "ORGANIZATION_UNUSED_ACCESS" -> 5;
|
||||
case "ACCOUNT", "ACCOUNT_UNUSED_ACCESS", "ACCOUNT_INTERNAL_ACCESS",
|
||||
"ORGANIZATION_INTERNAL_ACCESS" -> 1;
|
||||
default -> throw new IllegalArgumentException("Unsupported analyzer type: " + type);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
analyzers.clear();
|
||||
}
|
||||
|
||||
private static String requireAnalyzerName(JsonNode request) {
|
||||
String value = text(request, "analyzerName");
|
||||
validateAnalyzerName(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
private static void validateAnalyzerName(String value) {
|
||||
if (value == null || value.length() < 1 || value.length() > 255 || !ANALYZER_NAME.matcher(value).matches()) {
|
||||
throw validation("analyzerName must be 1-255 characters and match [A-Za-z][A-Za-z0-9_.-]*.");
|
||||
}
|
||||
}
|
||||
|
||||
private static String requireAnalyzerType(String value) {
|
||||
if (value == null || !ANALYZER_TYPES.contains(value)) {
|
||||
throw validation("type must be a valid analyzer type.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static Map<String, String> readTags(JsonNode node) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
if (node == null || node.isNull()) {
|
||||
return tags;
|
||||
}
|
||||
if (!node.isObject()) {
|
||||
throw validation("tags must be an object.");
|
||||
}
|
||||
node.fields().forEachRemaining(entry -> {
|
||||
String key = entry.getKey();
|
||||
JsonNode value = entry.getValue();
|
||||
if (key.length() < 1 || key.length() > 128 || key.startsWith("aws:") || !value.isTextual()
|
||||
|| value.textValue().length() > 256) {
|
||||
throw validation("tags contain an invalid key or value.");
|
||||
}
|
||||
tags.put(key, value.textValue());
|
||||
});
|
||||
return tags;
|
||||
}
|
||||
|
||||
private static String text(JsonNode request, String field) {
|
||||
JsonNode node = request == null ? null : request.get(field);
|
||||
return node != null && node.isTextual() ? node.textValue() : null;
|
||||
}
|
||||
|
||||
private static String storageKey(String region, String analyzerName) {
|
||||
return region + "::" + analyzerName;
|
||||
}
|
||||
|
||||
private static AwsException validation(String message) {
|
||||
return new AwsException("ValidationException", message, 400);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package io.github.hectorvent.floci.services.accessanalyzer.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Analyzer {
|
||||
private String arn;
|
||||
private String name;
|
||||
private String type;
|
||||
private String status;
|
||||
private String createdAt;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
private JsonNode configuration;
|
||||
|
||||
public Analyzer() {
|
||||
}
|
||||
|
||||
public String getArn() {
|
||||
return arn;
|
||||
}
|
||||
|
||||
public void setArn(String arn) {
|
||||
this.arn = arn;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(String createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public JsonNode getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public void setConfiguration(JsonNode configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user