mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(securityhub): give resource-tag conditions the tags of the hub or configuration policy a request names (#8889)
The generated request resources give Security Hub's hub and configuration-policy types the aws:ResourceTag/ key, but no service answered the securityhub namespace's stored tags, so a condition on a hub's or a configuration policy's tags saw none. SecurityHubService now answers it: storedTags reads the account's state for the Region the ARN names, in the request's account, where the tag handlers read, in place, and matches the ARN hubArn or policyArn gives; a hub reads its tags only while Security Hub is enabled there. A policy's tags change in place under the service's lock, so the lookup takes that lock too.
This commit is contained in:
1 parent
ae856867bf
commit
4453ca9fe9
5 files changed
+146
-9
No files matched your search
@@ -3479,8 +3479,9 @@ floci populates:
|
||||
actions, AWS FIS for its experiment templates, experiments and actions, CodePipeline for its pipelines, webhooks and
|
||||
custom action types, DataSync for its agents, locations and tasks, Global Accelerator for its accelerators, listeners
|
||||
and endpoint groups, EMR Serverless for its applications, job runs and sessions, EMR on EKS for its virtual clusters,
|
||||
job runs, managed endpoints, job templates and security configurations, and Cost Explorer for its anomaly monitors and
|
||||
subscriptions. Tag keys differing only in case are one condition key holding every value.
|
||||
job runs, managed endpoints, job templates and security configurations, Cost Explorer for its anomaly monitors and
|
||||
subscriptions, and Security Hub for its hubs and configuration policies. Tag keys differing only in case are one
|
||||
condition key holding every value.
|
||||
- EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and
|
||||
`ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the
|
||||
key names, an ARN for a key the Service Reference types as one. `resourceKeys` in
|
||||
|
||||
@@ -54,9 +54,9 @@ import java.util.Optional;
|
||||
* location or a task; Global Accelerator, for the tags of an accelerator, a listener or an endpoint group; EMR
|
||||
* Serverless, for the tags of an application, a job run or a session; EMR on EKS, for the tags of a virtual cluster, a
|
||||
* job run, a managed endpoint, a job template or a security configuration; Cost Explorer, for the tags of an anomaly
|
||||
* monitor or subscription); looked up lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver}
|
||||
* through {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on
|
||||
* that service directly.</p>
|
||||
* monitor or subscription; Security Hub, for the tags of a hub or a configuration policy); looked up lazily by
|
||||
* {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for
|
||||
* the namespace a request or an ARN is of, so IAM never depends on that service directly.</p>
|
||||
*
|
||||
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
|
||||
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.Resettable;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.organizations.OrganizationsService;
|
||||
@@ -25,10 +26,11 @@ import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@ApplicationScoped
|
||||
public class SecurityHubService implements Resettable {
|
||||
public class SecurityHubService implements Resettable, StoredResourceArns {
|
||||
private static final String SELF_MANAGED = "SELF_MANAGED_SECURITY_HUB";
|
||||
private static final String MEMBER_CREATED = "Created";
|
||||
private static final String MEMBER_ENABLED = "Enabled";
|
||||
@@ -36,6 +38,9 @@ public class SecurityHubService implements Resettable {
|
||||
private static final List<String> LINKING_MODES = List.of(
|
||||
"ALL_REGIONS", "ALL_REGIONS_EXCEPT_SPECIFIED", "SPECIFIED_REGIONS", "NO_REGIONS");
|
||||
|
||||
/** What a configuration policy's ARN resource starts with, its id following. */
|
||||
private static final String POLICY_RESOURCE = "configuration-policy/";
|
||||
|
||||
private final AccountAwareStorageBackend<SecurityHubState> states;
|
||||
private final RegionResolver regionResolver;
|
||||
private final OrganizationsService organizationsService;
|
||||
@@ -795,8 +800,12 @@ public class SecurityHubService implements Resettable {
|
||||
}
|
||||
return Map.copyOf(state.getHubTags());
|
||||
}
|
||||
JsonNode resource = taggedPolicy(state, region, arn);
|
||||
JsonNode tags = resource.get("Tags");
|
||||
return policyTags(taggedPolicy(state, region, arn));
|
||||
}
|
||||
|
||||
/** The tags a configuration policy holds, its {@code Tags} object's string members, by key. */
|
||||
private static Map<String, String> policyTags(JsonNode policy) {
|
||||
JsonNode tags = policy.get("Tags");
|
||||
if (tags == null || !tags.isObject()) {
|
||||
return Map.of();
|
||||
}
|
||||
@@ -809,6 +818,53 @@ public class SecurityHubService implements Resettable {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "securityhub";
|
||||
}
|
||||
|
||||
/**
|
||||
* A request names its hub by the Region it is signed for, and a configuration policy by the id Security Hub minted,
|
||||
* which its ARN ends in: nothing to find.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the hub or the configuration policy whose ARN is {@code arn}, in the request's account, where the tag
|
||||
* handlers read: an ARN of another account names none. Read in place from the account's state for the Region the
|
||||
* ARN names, a state stored before storage was account-aware included, and matched on the whole ARN, the one
|
||||
* {@link #hubArn} or {@link #policyArn} gives; a hub reads its tags only while Security Hub is enabled there, as
|
||||
* the tag handlers do. A policy's tags change in place under this service's lock, so the read takes that lock.
|
||||
*/
|
||||
@Override
|
||||
public synchronized Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "securityhub")) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String account = regionResolver.getAccountId();
|
||||
if (!account.equals(parsed.accountId()) || parsed.region().isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
String region = parsed.region();
|
||||
Optional<SecurityHubState> state = AccountAwareStorageBackend.readInAccount(states, account, region);
|
||||
if (state.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
if (arn.equals(hubArn(region))) {
|
||||
return state.get().isEnabled() ? new LinkedHashMap<>(state.get().getHubTags()) : Map.of();
|
||||
}
|
||||
if (!parsed.resource().startsWith(POLICY_RESOURCE)) {
|
||||
return Map.of();
|
||||
}
|
||||
String id = parsed.resource().substring(POLICY_RESOURCE.length());
|
||||
JsonNode policy = state.get().getPolicies().get(id);
|
||||
return policy == null || !arn.equals(policyArn(region, id)) ? Map.of() : policyTags(policy);
|
||||
}
|
||||
|
||||
public synchronized void tagResource(String region, String arn, Map<String, String> tags) {
|
||||
SecurityHubState state = state(region);
|
||||
validateResourceArn(region, arn);
|
||||
@@ -886,7 +942,7 @@ public class SecurityHubService implements Resettable {
|
||||
}
|
||||
|
||||
public String policyArn(String region, String id) {
|
||||
return regionResolver.buildArn("securityhub", region, "configuration-policy/" + id);
|
||||
return regionResolver.buildArn("securityhub", region, POLICY_RESOURCE + id);
|
||||
}
|
||||
|
||||
private static SecurityHubState copyConfigurationState(SecurityHubState source) {
|
||||
|
||||
@@ -933,6 +933,38 @@ class IamRestRouteEnforcementIntegrationTest {
|
||||
as(user, "us-west-2", "securityhub").get("/standards").then().statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* An {@code aws:ResourceTag} condition reads the tags Security Hub holds on the hub a request names, as TagResource
|
||||
* leaves them, so an allow on one team's tag lets the caller describe that team's hub and not another's. An
|
||||
* account has one hub a Region, so the two are in two Regions.
|
||||
*/
|
||||
@Test
|
||||
void aSecurityHubResourceTagConditionReadsTheTagsOfTheHub() {
|
||||
String taggedRegion = "ca-central-1";
|
||||
String otherRegion = "sa-east-1";
|
||||
securityHub(taggedRegion, "web");
|
||||
securityHub(otherRegion, "web");
|
||||
String taggedHub = "arn:aws:securityhub:" + taggedRegion + ":" + ACCOUNT + ":hub/default";
|
||||
given().header("Authorization", auth(ADMIN, taggedRegion, "securityhub")).contentType("application/json")
|
||||
.body("{\"Tags\":{\"team\":\"data\"}}")
|
||||
.post("/tags/{arn}", taggedHub).then().statusCode(200);
|
||||
String user = user("route-hub-tags");
|
||||
|
||||
policy(user, onTeam("Allow", "securityhub:DescribeHub", "data"));
|
||||
as(user, taggedRegion, "securityhub").get("/accounts").then().statusCode(200);
|
||||
refused(as(user, otherRegion, "securityhub").get("/accounts").then(), "securityhub:DescribeHub");
|
||||
}
|
||||
|
||||
/**
|
||||
* Security Hub enabled for the administrator in {@code region}, its hub tagged with the team. Floci serves no
|
||||
* DisableSecurityHub, so each Region a test enables it in is one no other test uses.
|
||||
*/
|
||||
private void securityHub(String region, String team) {
|
||||
given().header("Authorization", auth(ADMIN, region, "securityhub")).contentType("application/json")
|
||||
.body("{\"EnableDefaultStandards\":false,\"Tags\":{\"team\":\"" + team + "\"}}").post("/accounts")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* A GuardDuty request naming its detector in its path is checked on that detector, the ARN GuardDuty gives it,
|
||||
* and one naming none as its action on {@code *}: a caller with no grant is refused, a grant scoped to the
|
||||
|
||||
@@ -4,11 +4,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.organizations.OrganizationsService;
|
||||
import io.floci.aws.services.securityhub.model.SecurityHubState;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -47,6 +49,52 @@ class SecurityHubServiceTest {
|
||||
assertEquals(Map.of("env", "test"), service.tagsForResource(REGION, service.hubArn(REGION)));
|
||||
}
|
||||
|
||||
/**
|
||||
* IAM's lookup reads the tags of the hub or the configuration policy the ARN names, as the tag calls leave them,
|
||||
* and nothing for another account's, another Region's or an unknown policy, or an ARN of another service.
|
||||
*/
|
||||
@Test
|
||||
void storedTagsAreTheTagsOfTheHubOrThePolicyTheArnNames() throws Exception {
|
||||
AccountAwareStorageBackend<SecurityHubState> store = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
|
||||
SecurityHubService administrator = new SecurityHubService(store, new RegionResolver(REGION, ACCOUNT_ID),
|
||||
mock(OrganizationsService.class));
|
||||
administrator.enableSecurityHub(REGION, objectMapper.readTree("{\"Tags\": {\"team\": \"web\"}}"));
|
||||
SecurityHubState state = store.get(REGION).orElseThrow();
|
||||
state.setAdminAccountId(ACCOUNT_ID);
|
||||
store.put(REGION, state);
|
||||
String policyId = administrator.createConfigurationPolicy(REGION, objectMapper.readTree("""
|
||||
{"Name": "tagged", "ConfigurationPolicy": {"SecurityHub": {"ServiceEnabled": true}},
|
||||
"Tags": {"team": "web", "env": "dev"}}
|
||||
"""));
|
||||
String hub = administrator.hubArn(REGION);
|
||||
String policy = administrator.policyArn(REGION, policyId);
|
||||
administrator.tagResource(REGION, hub, Map.of("team", "data"));
|
||||
administrator.tagResource(REGION, policy, Map.of("team", "ops"));
|
||||
administrator.untagResource(REGION, policy, List.of("env"));
|
||||
|
||||
assertEquals("securityhub", administrator.namespace());
|
||||
assertEquals(Map.of("team", "data"), administrator.storedTags(hub));
|
||||
assertEquals(Map.of("team", "ops"), administrator.storedTags(policy));
|
||||
assertTrue(administrator.storedTags(hub.replace(ACCOUNT_ID, "111122223333")).isEmpty());
|
||||
assertTrue(administrator.storedTags(hub.replace(REGION, "us-west-2")).isEmpty());
|
||||
assertTrue(administrator.storedTags(policy.replace(policyId, "missing")).isEmpty());
|
||||
assertTrue(administrator.storedTags("arn:aws:guardduty:us-east-1:222222222222:detector/abc").isEmpty());
|
||||
}
|
||||
|
||||
/** A hub stored before storage was account-aware is read in place: the lookup migrates nothing. */
|
||||
@Test
|
||||
void storedTagsReadAPreAccountAwareHubInPlace() throws Exception {
|
||||
InMemoryStorage<String, SecurityHubState> raw = new InMemoryStorage<>();
|
||||
SecurityHubService legacyState = new SecurityHubService(new AccountAwareStorageBackend<>(raw, null, ACCOUNT_ID),
|
||||
new RegionResolver(REGION, ACCOUNT_ID), mock(OrganizationsService.class));
|
||||
legacyState.enableSecurityHub(REGION, objectMapper.readTree("{\"Tags\": {\"team\": \"data\"}}"));
|
||||
raw.put(REGION, raw.get(ACCOUNT_ID + "/" + REGION).orElseThrow());
|
||||
raw.delete(ACCOUNT_ID + "/" + REGION);
|
||||
|
||||
assertEquals(Map.of("team", "data"), legacyState.storedTags(legacyState.hubArn(REGION)));
|
||||
assertEquals(Set.of(REGION), raw.keys());
|
||||
}
|
||||
|
||||
@Test
|
||||
void enablingTheHubSubscribesTheDefaultStandardsUnlessToldNotTo() throws Exception {
|
||||
service.enableSecurityHub(REGION, objectMapper.readTree("{}"));
|
||||
|
||||
Reference in new issue
Block a user