mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 17:58:05 -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
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user