Files
floci/src/test/java/io/floci/aws/services/account/AccountServiceTest.java
T
Priamo Germosen 9f1cccf83f feat(iam): check FIS, RAM, OAM and three more services by their REST routes (#8154)
Under enforcement a REST route no rule maps and no vendored model names
runs unchecked. AWS Fault Injection Service, AWS RAM, CloudWatch
Observability Access Manager, AWS Resource Explorer, AWS Account
Management and AWS Control Catalog were among those: none of their 94
served routes had a rule.

Vendors the six models, so each route is checked as its operation's
action, on the resource its request names. A caller holding no grant
for them is now refused where the request used to pass; that is the
behaviour change.

Resource Explorer's five routes that S3 Vectors shares are matched on
the path without the prefix its filter moves them under. The FIS and
Resource Explorer tag routes are the shared controller's and keep its
bare /tags form. OAM's tag routes take the ARN as the rest of the path,
as the controllers serving them do, so an ARN sent with its slash
unencoded is checked too.

The request-resources generator reads these requests with no
hand-written correction, through four rules that hold for any service:
- A bare id after a path segment naming a type names that type:
  /safetyLevers/{id}/state is the lever whose state
  UpdateSafetyLeverState sets.
- A member fills the variables of a resource's own path before the
  account its ARN is in, so Account Management's AccountId is the
  member account of an accountInOrganization ARN.
- A type whose ARN is the account alone applies to a request leaving
  out its optional account member, when that member names another
  type: a request without AccountId acts on the caller's own account.
- A create makes its own type even when a member holds the resources
  the action acts on: CreateResourceShare makes the share, and its
  resourceArns are what the share holds.

Account Management finds that member account's organization and
management account in the caller's organization, through the lookup
the reader asks a service for the ARNs it stores, so a request naming a
member is checked on the ARN AWS checks.

The first rule also reads four CloudFront web ACL association
operations and four OpenSearch data source attachment operations on the
distribution or application their paths name. Floci serves none of
them.

An ARN fitting both a customer's format and one AWS owns is AWS's, so
an AWS managed permission is not also checked in the caller's account.

Regenerating the tables also brings in what AWS has added since the
last sync: eight Amazon Cognito user pool session operations and eight
Kinesis Analytics snapshot operations, none of which Floci serves. The
snapshot create's tags are read once the pinned botocore has it. The
count of tagged operations in iam.md is now the table's, 589, which
also takes 23 that earlier syncs added without moving it.
2026-11-12 18:59:01 -04:00

119 lines
5.4 KiB
Java

package io.floci.aws.services.account;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.account.model.AlternateContact;
import io.floci.aws.services.organizations.OrganizationsService;
import io.floci.aws.services.organizations.model.Organization;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class AccountServiceTest {
private static final String ACCOUNT_ID = "123456789012";
private final ObjectMapper mapper = new ObjectMapper();
private AccountService service;
private OrganizationsService organizations;
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
void setUp() {
StorageFactory storageFactory = mock(StorageFactory.class);
AccountAwareStorageBackend<AlternateContact> store = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
when(storageFactory.create(eq("account"), eq("account-alternate-contacts.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) store);
organizations = mock(OrganizationsService.class);
service = new AccountService(storageFactory, organizations);
}
@Test
void ownAccountPutAndGetRoundTrip() {
ObjectNode request = contactRequest();
service.putAlternateContact(ACCOUNT_ID, request);
AlternateContact contact = service.getAlternateContact(ACCOUNT_ID, request);
assertEquals("security@example.com", contact.getEmailAddress());
assertEquals("SECURITY", contact.getAlternateContactType());
}
@Test
void nonStringAccountIdReturnsSerializationException() {
ObjectNode request = contactRequest();
request.put("AccountId", 123456789012L);
AwsException error = assertThrows(AwsException.class,
() -> service.putAlternateContact(ACCOUNT_ID, request));
assertEquals("SerializationException", error.getErrorCode());
}
@Test
void clearRemovesContacts() {
ObjectNode request = contactRequest();
service.putAlternateContact(ACCOUNT_ID, request);
service.clear();
AwsException error = assertThrows(AwsException.class,
() -> service.getAlternateContact(ACCOUNT_ID, request));
assertEquals("ResourceNotFoundException", error.getErrorCode());
}
@Test
void deleteRemovesContactAndDeletingUnsetThrows() {
ObjectNode request = contactRequest();
service.putAlternateContact(ACCOUNT_ID, request);
service.deleteAlternateContact(ACCOUNT_ID, request);
AwsException getError = assertThrows(AwsException.class,
() -> service.getAlternateContact(ACCOUNT_ID, request));
assertEquals("ResourceNotFoundException", getError.getErrorCode());
AwsException deleteError = assertThrows(AwsException.class,
() -> service.deleteAlternateContact(ACCOUNT_ID, request));
assertEquals("ResourceNotFoundException", deleteError.getErrorCode());
}
@Test
void findsTheMemberAccountOfTheCallersOrganizationARequestNames() {
Organization organization = new Organization();
organization.setId("o-abcdefghij");
organization.setArn("arn:aws:organizations::" + ACCOUNT_ID + ":organization/o-abcdefghij");
organization.setMasterAccountId(ACCOUNT_ID);
when(organizations.describeOrganization(ACCOUNT_ID)).thenReturn(organization);
when(organizations.describeAccount(ACCOUNT_ID, "999988887777"))
.thenThrow(new AwsException("AccountNotFoundException", "not a member", 400));
assertEquals(Optional.of("arn:aws:account::" + ACCOUNT_ID + ":account/o-abcdefghij/111122223333"),
service.find("accountInOrganization", List.of("111122223333"), "us-east-1"));
// An account the organization does not hold, and another type, find nothing.
assertEquals(Optional.empty(), service.find("accountInOrganization", List.of("999988887777"), "us-east-1"));
assertEquals(Optional.empty(), service.find("account", List.of("111122223333"), "us-east-1"));
}
@Test
void findsNoMemberAccountForACallerInNoOrganization() {
when(organizations.describeOrganization(ACCOUNT_ID))
.thenThrow(new AwsException("AWSOrganizationsNotInUseException", "no organization", 400));
assertEquals(Optional.empty(), service.find("accountInOrganization", List.of("111122223333"), "us-east-1"));
}
private ObjectNode contactRequest() {
ObjectNode request = mapper.createObjectNode();
request.put("AlternateContactType", "SECURITY");
request.put("EmailAddress", "security@example.com");
request.put("Name", "Security Team");
request.put("PhoneNumber", "+1 555 0100");
request.put("Title", "Security");
return request;
}
}