mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(account): add alternate contact operations (#3118)
* feat(account): add alternate contact operations * fix(account): complete alternate contact contracts
This commit is contained in:
1 parent
6345c37bad
commit
d80734e6de
18 files changed
+509
-3
No files matched your search
@@ -711,6 +711,7 @@ public interface EmulatorConfig {
|
||||
ServiceCatalogServiceConfig servicecatalog();
|
||||
SsoAdminServiceConfig ssoadmin();
|
||||
Macie2ServiceConfig macie2();
|
||||
AccountServiceConfig account();
|
||||
AccessAnalyzerServiceConfig accessanalyzer();
|
||||
ServiceQuotasServiceConfig servicequotas();
|
||||
RamServiceConfig ram();
|
||||
@@ -745,6 +746,11 @@ public interface EmulatorConfig {
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface AccountServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface AccessAnalyzerServiceConfig {
|
||||
@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.account.AccountController;
|
||||
import io.github.hectorvent.floci.services.accessanalyzer.AccessAnalyzerController;
|
||||
import io.github.hectorvent.floci.services.aps.ApsController;
|
||||
import io.github.hectorvent.floci.services.controltower.ControlTowerControlController;
|
||||
@@ -413,6 +414,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("account", "account", config.services().account().enabled(), true,
|
||||
"account", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON), Set.of(), Set.of("account"), Set.of(), Set.of(AccountController.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)),
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package io.github.hectorvent.floci.services.account;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.services.account.model.AlternateContact;
|
||||
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.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
@Path("/")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class AccountController {
|
||||
private final AccountService accountService;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final RequestContext requestContext;
|
||||
|
||||
@Inject
|
||||
public AccountController(AccountService accountService, ObjectMapper objectMapper, RequestContext requestContext) {
|
||||
this.accountService = accountService;
|
||||
this.objectMapper = objectMapper;
|
||||
this.requestContext = requestContext;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/putAlternateContact")
|
||||
public Response putAlternateContact(String body) {
|
||||
accountService.putAlternateContact(requestContext.getAccountId(), readTree(body));
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/getAlternateContact")
|
||||
public Response getAlternateContact(String body) {
|
||||
AlternateContact contact = accountService.getAlternateContact(requestContext.getAccountId(), readTree(body));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("AlternateContact", objectMapper.valueToTree(contact));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package io.github.hectorvent.floci.services.account;
|
||||
|
||||
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.account.model.AlternateContact;
|
||||
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
|
||||
import io.github.hectorvent.floci.services.organizations.model.Organization;
|
||||
import io.github.hectorvent.floci.services.organizations.model.OrganizationAccount;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class AccountService implements Resettable {
|
||||
private static final String ACCOUNT_MANAGEMENT_SERVICE_PRINCIPAL = "account.amazonaws.com";
|
||||
private static final Pattern ACCOUNT_ID = Pattern.compile("\\d{12}");
|
||||
private static final Pattern EMAIL = Pattern.compile("\\s*[\\w+=.#|!&-]+@[\\w.-]+\\.[\\w]+\\s*");
|
||||
private static final Pattern PHONE = Pattern.compile("[\\s0-9()+-]+");
|
||||
private static final Set<String> CONTACT_TYPES = Set.of("BILLING", "OPERATIONS", "SECURITY");
|
||||
|
||||
private final AccountAwareStorageBackend<AlternateContact> contacts;
|
||||
private final OrganizationsService organizationsService;
|
||||
|
||||
@Inject
|
||||
public AccountService(StorageFactory storageFactory, OrganizationsService organizationsService) {
|
||||
this.contacts = storageFactory.create("account", "account-alternate-contacts.json",
|
||||
new TypeReference<Map<String, AlternateContact>>() {});
|
||||
this.organizationsService = organizationsService;
|
||||
}
|
||||
|
||||
public void putAlternateContact(String callerAccountId, JsonNode request) {
|
||||
String targetAccountId = resolveTargetAccount(callerAccountId, request);
|
||||
String type = requireContactType(request);
|
||||
AlternateContact contact = new AlternateContact(
|
||||
type,
|
||||
requirePattern(request, "EmailAddress", 1, 254, EMAIL),
|
||||
requireLength(request, "Name", 1, 64),
|
||||
requirePattern(request, "PhoneNumber", 1, 25, PHONE),
|
||||
requireLength(request, "Title", 1, 50));
|
||||
contacts.putForAccount(targetAccountId, type, contact);
|
||||
}
|
||||
|
||||
public AlternateContact getAlternateContact(String callerAccountId, JsonNode request) {
|
||||
String targetAccountId = resolveTargetAccount(callerAccountId, request);
|
||||
String type = requireContactType(request);
|
||||
return contacts.getForAccount(targetAccountId, type)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"The alternate contact does not exist for the specified account and contact type.", 404));
|
||||
}
|
||||
|
||||
private String resolveTargetAccount(String callerAccountId, JsonNode request) {
|
||||
JsonNode accountIdNode = request == null ? null : request.get("AccountId");
|
||||
if (accountIdNode == null || accountIdNode.isNull()) {
|
||||
return callerAccountId;
|
||||
}
|
||||
if (!accountIdNode.isTextual()) {
|
||||
throw new AwsException("SerializationException",
|
||||
"AccountId must be a string.", 400);
|
||||
}
|
||||
String requestedAccountId = accountIdNode.textValue();
|
||||
if (!ACCOUNT_ID.matcher(requestedAccountId).matches()) {
|
||||
throw validation("AccountId must be a 12 digit account ID.");
|
||||
}
|
||||
|
||||
Organization organization;
|
||||
OrganizationAccount caller;
|
||||
try {
|
||||
organization = organizationsService.describeOrganization(callerAccountId);
|
||||
caller = organizationsService.describeAccount(callerAccountId, callerAccountId);
|
||||
organizationsService.describeAccount(callerAccountId, requestedAccountId);
|
||||
} catch (AwsException e) {
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"The specified account cannot be accessed by the calling account.", 403);
|
||||
}
|
||||
|
||||
if (!"ALL".equals(organization.getFeatureSet())) {
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"The organization must have all features enabled.", 403);
|
||||
}
|
||||
if (!organization.getEnabledServicePrincipals().containsKey(ACCOUNT_MANAGEMENT_SERVICE_PRINCIPAL)) {
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"Trusted access for AWS Account Management is not enabled for the organization.", 403);
|
||||
}
|
||||
|
||||
boolean managementAccount = callerAccountId.equals(organization.getMasterAccountId());
|
||||
boolean delegatedAdministrator = caller.getDelegatedServices()
|
||||
.containsKey(ACCOUNT_MANAGEMENT_SERVICE_PRINCIPAL);
|
||||
if (!managementAccount && !delegatedAdministrator) {
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"The calling account is not the management account or a delegated administrator.", 403);
|
||||
}
|
||||
if (managementAccount && requestedAccountId.equals(callerAccountId)) {
|
||||
throw validation("The management account cannot specify its own AccountId.");
|
||||
}
|
||||
return requestedAccountId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
contacts.clear();
|
||||
}
|
||||
|
||||
private static String requireContactType(JsonNode request) {
|
||||
String value = requireLength(request, "AlternateContactType", 1, 32);
|
||||
if (!CONTACT_TYPES.contains(value)) {
|
||||
throw validation("AlternateContactType must be BILLING, OPERATIONS, or SECURITY.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static String requirePattern(JsonNode request, String field, int min, int max, Pattern pattern) {
|
||||
String value = requireLength(request, field, min, max);
|
||||
if (!pattern.matcher(value).matches()) {
|
||||
throw validation(field + " does not satisfy the required pattern.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static String requireLength(JsonNode request, String field, int min, int max) {
|
||||
String value = text(request, field);
|
||||
if (value == null || value.length() < min || value.length() > max) {
|
||||
throw validation(field + " must be between " + min + " and " + max + " characters.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static String text(JsonNode request, String field) {
|
||||
JsonNode value = request == null ? null : request.get(field);
|
||||
return value != null && value.isTextual() ? value.textValue() : null;
|
||||
}
|
||||
|
||||
private static AwsException validation(String message) {
|
||||
return new AwsException("ValidationException", message, 400);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package io.github.hectorvent.floci.services.account.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class AlternateContact {
|
||||
@JsonProperty("AlternateContactType")
|
||||
private String alternateContactType;
|
||||
@JsonProperty("EmailAddress")
|
||||
private String emailAddress;
|
||||
@JsonProperty("Name")
|
||||
private String name;
|
||||
@JsonProperty("PhoneNumber")
|
||||
private String phoneNumber;
|
||||
@JsonProperty("Title")
|
||||
private String title;
|
||||
|
||||
public AlternateContact() {
|
||||
}
|
||||
|
||||
public AlternateContact(String alternateContactType, String emailAddress, String name,
|
||||
String phoneNumber, String title) {
|
||||
this.alternateContactType = alternateContactType;
|
||||
this.emailAddress = emailAddress;
|
||||
this.name = name;
|
||||
this.phoneNumber = phoneNumber;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAlternateContactType() {
|
||||
return alternateContactType;
|
||||
}
|
||||
|
||||
public void setAlternateContactType(String alternateContactType) {
|
||||
this.alternateContactType = alternateContactType;
|
||||
}
|
||||
|
||||
public String getEmailAddress() {
|
||||
return emailAddress;
|
||||
}
|
||||
|
||||
public void setEmailAddress(String emailAddress) {
|
||||
this.emailAddress = emailAddress;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPhoneNumber() {
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
public void setPhoneNumber(String phoneNumber) {
|
||||
this.phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user