feat(securityhub): standards subscriptions (#6979)

* feat(securityhub): standards subscriptions

BatchEnableStandards, BatchDisableStandards, GetEnabledStandards and
DescribeStandards, so Terraform's aws_securityhub_standards_subscription
(and cloudposse's security-hub module) can enable CIS, AFSBP and NIST
800-53. EnableSecurityHub now subscribes AWS's default standards (AFSBP
and CIS 1.2.0) unless EnableDefaultStandards is false, as AWS does.
Subscriptions are READY at once; controls and findings are not evaluated.

Found applying kthw-on-floci's account-baseline: every standards
subscription failed with UnknownOperationException (POST
/standards/register).

BatchDisableStandards checks every ARN before removing any, so a batch
naming one unknown subscription changes nothing (before, the removals ahead
of it stuck in memory without being saved, and came back after a
restart). GetEnabledStandards reads the subscriptions under the same lock
the writers hold.

* fix(securityhub): reject a non-boolean EnableDefaultStandards

EnableSecurityHub coerced EnableDefaultStandards with asBoolean(true), so a
string "false" skipped the defaults and an object enabled them, and the
hub was enabled either way. The botocore model types the member boolean.
Reject it with InvalidInputException before any state changes, as
UpdateSecurityHubConfiguration does for AutoEnableControls.

* fix(securityhub): offer CIS v5.0.0 and take ARN partitions from the region resolver

Add CIS AWS Foundations Benchmark v5.0.0 (standards/cis-aws-foundations-benchmark/v/5.0.0,
regional, not enabled by default) to the standards Security Hub offers.

Mint every Security Hub ARN through RegionResolver: the global CIS 1.2.0 ARN takes
partitionForRegion and the regional ARNs use buildArn, so a region the catalog cannot
place keeps the deployment's partition instead of falling back to aws.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-29 23:37:48 -04:00
1 parent c8143b9c38
commit 0a3efd65fe
6 files changed
+293 -10

No files matched your search

@@ -10,13 +10,13 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class SecurityHubServiceTest {
private static final String REGION = "us-east-1";
@@ -27,10 +27,8 @@ class SecurityHubServiceTest {
@BeforeEach
void setUp() {
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
when(regionResolver.getPartition()).thenReturn("aws");
service = new SecurityHubService(AccountAwareStorageBackend.inMemory(ACCOUNT_ID), regionResolver,
service = new SecurityHubService(AccountAwareStorageBackend.inMemory(ACCOUNT_ID),
new RegionResolver(REGION, ACCOUNT_ID),
mock(OrganizationsService.class));
}
@@ -49,6 +47,116 @@ class SecurityHubServiceTest {
assertEquals(Map.of("env", "test"), service.tagsForResource(REGION, service.hubArn(REGION)));
}
@Test
void enablingTheHubSubscribesTheDefaultStandardsUnlessToldNotTo() throws Exception {
service.enableSecurityHub(REGION, objectMapper.readTree("{}"));
assertEquals(Set.of(
"arn:aws:securityhub:::ruleset/cis-aws-foundations-benchmark/v/1.2.0",
"arn:aws:securityhub:us-east-1::standards/aws-foundational-security-best-practices/v/1.0.0"),
Set.copyOf(service.enabledStandards(REGION, null).values()));
}
@Test
void aNonBooleanEnableDefaultStandardsIsRejectedAndLeavesTheHubDisabled() throws Exception {
for (String value : new String[] {"\"false\"", "{}", "0"}) {
AwsException error = assertThrows(AwsException.class, () -> service.enableSecurityHub(REGION,
objectMapper.readTree("{\"EnableDefaultStandards\": " + value + "}")), value);
assertEquals("InvalidInputException", error.getErrorCode(), value);
assertFalse(service.state(REGION).isEnabled(), value);
}
}
@Test
void anExplicitlyTrueOrNullEnableDefaultStandardsSubscribesTheDefaults() throws Exception {
for (String value : new String[] {"true", "null"}) {
service.clear();
service.enableSecurityHub(REGION, objectMapper.readTree("{\"EnableDefaultStandards\": " + value + "}"));
assertEquals(2, service.enabledStandards(REGION, null).size(), value);
}
}
@Test
void aBatchDisableWithAnUnknownArnRemovesNothing() throws Exception {
service.enableSecurityHub(REGION, objectMapper.readTree("{}"));
Map<String, String> before = service.enabledStandards(REGION, null);
String valid = before.keySet().iterator().next();
assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, () -> service.disableStandards(
REGION, objectMapper.readTree("{\"StandardsSubscriptionArns\": [\"" + valid + "\", \"arn:aws:"
+ "securityhub:us-east-1:" + ACCOUNT_ID + ":subscription/made-up/v/1.0.0\"]}")))
.getErrorCode());
assertEquals(before, service.enabledStandards(REGION, null), "the valid subscription is still there");
}
@Test
void standardsSubscriptionsCanBeEnabledListedAndDisabled() throws Exception {
assertEquals("InvalidAccessException", assertThrows(AwsException.class,
() -> service.enabledStandards(REGION, null)).getErrorCode(), "the hub must be enabled first");
service.enableSecurityHub(REGION, objectMapper.readTree("{\"EnableDefaultStandards\": false}"));
assertTrue(service.enabledStandards(REGION, null).isEmpty());
String nist = "arn:aws:securityhub:us-east-1::standards/nist-800-53/v/5.0.0";
Map<String, String> enabled = service.enableStandards(REGION, objectMapper.readTree(
"{\"StandardsSubscriptionRequests\": [{\"StandardsArn\": \"" + nist + "\"}]}"));
String subscription = "arn:aws:securityhub:us-east-1:" + ACCOUNT_ID + ":subscription/nist-800-53/v/5.0.0";
assertEquals(Map.of(subscription, nist), enabled);
assertEquals(Map.of(subscription, nist), service.enabledStandards(REGION, objectMapper.readTree(
"{\"StandardsSubscriptionArns\": [\"" + subscription + "\"]}")));
assertEquals("InvalidInputException", assertThrows(AwsException.class, () -> service.enableStandards(REGION,
objectMapper.readTree("{\"StandardsSubscriptionRequests\": [{\"StandardsArn\": \"arn:aws:securityhub:"
+ "us-east-1::standards/made-up/v/1.0.0\"}]}"))).getErrorCode());
assertEquals(Map.of(subscription, nist), service.disableStandards(REGION, objectMapper.readTree(
"{\"StandardsSubscriptionArns\": [\"" + subscription + "\"]}")));
assertTrue(service.enabledStandards(REGION, null).isEmpty());
assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, () -> service.disableStandards(
REGION, objectMapper.readTree("{\"StandardsSubscriptionArns\": [\"" + subscription + "\"]}")))
.getErrorCode());
}
@Test
void cisFiveCanBeEnabledListedAndDisabledButIsNotADefault() throws Exception {
service.enableSecurityHub(REGION, objectMapper.readTree("{}"));
String cis5 = "arn:aws:securityhub:us-east-1::standards/cis-aws-foundations-benchmark/v/5.0.0";
assertFalse(service.enabledStandards(REGION, null).containsValue(cis5));
Map<String, String> enabled = service.enableStandards(REGION, objectMapper.readTree(
"{\"StandardsSubscriptionRequests\": [{\"StandardsArn\": \"" + cis5 + "\"}]}"));
String subscription = "arn:aws:securityhub:us-east-1:" + ACCOUNT_ID
+ ":subscription/cis-aws-foundations-benchmark/v/5.0.0";
assertEquals(Map.of(subscription, cis5), enabled);
assertEquals(cis5, service.enabledStandards(REGION, null).get(subscription));
assertEquals(Map.of(subscription, cis5), service.disableStandards(REGION, objectMapper.readTree(
"{\"StandardsSubscriptionArns\": [\"" + subscription + "\"]}")));
assertFalse(service.enabledStandards(REGION, null).containsKey(subscription));
}
/** A region the catalog cannot place keeps the deployment's partition, not {@code aws}. */
@Test
void arnsTakeTheDeploymentPartitionForAnUnknownRegion() throws Exception {
SecurityHubService china = new SecurityHubService(AccountAwareStorageBackend.inMemory(ACCOUNT_ID),
new RegionResolver("cn-north-1", ACCOUNT_ID), mock(OrganizationsService.class));
String region = "local-1";
china.enableSecurityHub(region, objectMapper.readTree("{\"Tags\": {\"env\": \"test\"}}"));
String cis12 = "arn:aws-cn:securityhub:::ruleset/cis-aws-foundations-benchmark/v/1.2.0";
assertEquals(Map.of(
"arn:aws-cn:securityhub:local-1:" + ACCOUNT_ID + ":subscription/cis-aws-foundations-benchmark/v/1.2.0",
cis12,
"arn:aws-cn:securityhub:local-1:" + ACCOUNT_ID
+ ":subscription/aws-foundational-security-best-practices/v/1.0.0",
"arn:aws-cn:securityhub:local-1::standards/aws-foundational-security-best-practices/v/1.0.0"),
china.enabledStandards(region, null));
String hub = "arn:aws-cn:securityhub:local-1:" + ACCOUNT_ID + ":hub/default";
assertEquals(hub, china.hubArn(region));
assertEquals(Map.of("env", "test"), china.tagsForResource(region, hub), "the hub's own ARN resolves");
assertEquals(1, china.enableStandards(region, objectMapper.readTree(
"{\"StandardsSubscriptionRequests\": [{\"StandardsArn\": \"" + cis12 + "\"}]}")).size());
}
/** An account id is scoped to its partition: a China ARN of the same account names nothing here. */
@Test
void aResourceArnFromAnotherPartitionIsNotFound() throws Exception {