diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java index 6ba78b8760..623cec510f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java +++ b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator; +import io.github.hectorvent.floci.services.iam.PublicAccessConditions; import java.util.ArrayList; import java.util.HashMap; @@ -61,17 +62,13 @@ final class DsqlClusterPolicies { private static final String SOURCE_IP = "aws:sourceip"; private static final String PRINCIPAL_IS_AWS_SERVICE = "aws:principalisawsservice"; /** - * The trusted condition keys of DSQL's block public access page, and those it was measured to - * accept too: {@code aws:SourceIp}, {@code aws:SourceVpcArn} and the three {@code aws:Vpce*}. - * Keys are matched in any case (measured). + * The trusted condition keys of DSQL's block public access page and those it was measured to + * accept too, {@code aws:SourceIp}, {@code aws:SourceVpcArn} and the three {@code aws:Vpce*}: + * the keys Lambda shares, and {@code aws:SourceIp}. Keys are matched in any case (measured). */ - private static final Set RESTRICTING_KEYS = lowercase(Set.of( - "aws:PrincipalAccount", "aws:PrincipalArn", "aws:PrincipalOrgID", "aws:PrincipalOrgPaths", - "aws:SourceAccount", "aws:SourceArn", "aws:SourceVpc", "aws:SourceVpce", "aws:UserId", - "aws:PrincipalServiceName", "aws:PrincipalServiceNamesList", "aws:PrincipalIsAWSService", - "aws:Ec2InstanceSourceVpc", "aws:SourceOrgID", "aws:SourceOrgPaths", - "aws:SourceIp", "aws:SourceVpcArn", "aws:VpceAccount", "aws:VpceOrgID", "aws:VpceOrgPaths")); - private static final Set ARN_KEYS = lowercase(Set.of("aws:PrincipalArn", "aws:SourceArn", "aws:SourceVpcArn")); + private static final Set RESTRICTING_KEYS = + lowercase(PublicAccessConditions.with(PublicAccessConditions.TRUSTED_KEYS, "aws:SourceIp")); + private static final Set ARN_KEYS = lowercase(PublicAccessConditions.ARN_KEYS); private DsqlClusterPolicies() { } @@ -267,45 +264,20 @@ final class DsqlClusterPolicies { private static boolean restricts(String operator, String key, List values) { if (SOURCE_IP.equals(key) && "IpAddress".equals(operator)) { - return values.stream().allMatch(DsqlClusterPolicies::isNarrowSourceIp); + return values.stream().allMatch(PublicAccessConditions::isNarrowSourceIp); } return switch (operator) { case "StringEquals", "StringEqualsIgnoreCase" -> values.stream().noneMatch(value -> value.contains("${")); case "StringLike" -> values.stream().allMatch(value -> ARN_KEYS.contains(key) - ? pinsAccount(value) && !value.contains("${") : isFixedValue(value)); + ? PublicAccessConditions.pinsAccount(value) && !value.contains("${") + : PublicAccessConditions.isFixedValue(value)); case "ArnEquals", "ArnLike" -> values.stream().allMatch(value -> value.split(":", 6).length < 6 - || pinsAccount(value) && !value.contains("${")); + || PublicAccessConditions.pinsAccount(value) && !value.contains("${")); case "Bool" -> PRINCIPAL_IS_AWS_SERVICE.equals(key) && values.stream().allMatch("true"::equalsIgnoreCase); default -> false; }; } - /** A value that names one thing: not empty, and with no wildcard or policy variable in it. */ - private static boolean isFixedValue(String value) { - return !value.isEmpty() && value.indexOf('*') < 0 && value.indexOf('?') < 0 && !value.contains("${"); - } - - /** An ARN of six fields whose account has no wildcard. */ - private static boolean pinsAccount(String arn) { - String[] fields = arn.split(":", 6); - return fields.length == 6 && fields[4].indexOf('*') < 0 && fields[4].indexOf('?') < 0; - } - - /** A range of /8 or narrower for IPv4, /32 for IPv6, or one address (measured /8 restricts, /7 does not). */ - private static boolean isNarrowSourceIp(String cidr) { - int slash = cidr.indexOf('/'); - if (slash < 0) { - return isFixedValue(cidr); - } - String address = cidr.substring(0, slash); - try { - int prefixLength = Integer.parseInt(cidr.substring(slash + 1).trim()); - return isFixedValue(address) && prefixLength >= (address.indexOf(':') >= 0 ? 32 : 8); - } catch (NumberFormatException e) { - return false; - } - } - private static boolean grantsToEveryone(JsonNode principal) { return principal.isTextual() ? "*".equals(principal.textValue()) : texts(principal.path("AWS")).contains("*"); } diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/PublicAccessConditions.java b/src/main/java/io/github/hectorvent/floci/services/iam/PublicAccessConditions.java new file mode 100644 index 0000000000..c63018a971 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/iam/PublicAccessConditions.java @@ -0,0 +1,81 @@ +package io.github.hectorvent.floci.services.iam; + +import org.jboss.logging.Logger; + +import java.util.HashSet; +import java.util.Set; + +/** + * What a resource policy's condition has to say for an {@code Allow} to everyone not to count as + * public access, as the services that refuse or report public policies read it: S3's Block Public + * Access, Lambda's function URL and resource policies, and Aurora DSQL's cluster policies. Each + * service keeps the operators and the extra keys it was measured to accept; what they read the + * same way lives here. + */ +public final class PublicAccessConditions { + + private static final Logger LOG = Logger.getLogger(PublicAccessConditions.class); + + /** + * The trusted condition keys of IAM's block public access definition that Lambda and DSQL both + * accept: pinned to a fixed value, one of these keeps a wildcard principal from making a + * statement public. + */ + public static final Set TRUSTED_KEYS = Set.of( + "aws:PrincipalAccount", "aws:PrincipalArn", "aws:PrincipalOrgID", "aws:PrincipalOrgPaths", + "aws:SourceAccount", "aws:SourceArn", "aws:SourceOrgID", "aws:SourceOrgPaths", "aws:SourceVpc", + "aws:SourceVpcArn", "aws:SourceVpce", "aws:VpceAccount", "aws:VpceOrgID", "aws:VpceOrgPaths", + "aws:Ec2InstanceSourceVpc", "aws:UserId", "aws:PrincipalServiceName", "aws:PrincipalServiceNamesList", + "aws:PrincipalIsAWSService"); + + /** The trusted keys whose values are ARNs, which a pattern pins only through their account. */ + public static final Set ARN_KEYS = Set.of("aws:PrincipalArn", "aws:SourceArn", "aws:SourceVpcArn"); + + private PublicAccessConditions() { + } + + /** {@code keys} and one more, for a service that accepts a key of its own on top of the shared ones. */ + public static Set with(Set keys, String key) { + Set all = new HashSet<>(keys); + all.add(key); + return Set.copyOf(all); + } + + /** A value that names one thing: not empty, and with no wildcard or policy variable in it. */ + public static boolean isFixedValue(String value) { + return !value.isEmpty() && !hasWildcard(value) && !value.contains("${"); + } + + public static boolean hasWildcard(String value) { + return value.indexOf('*') >= 0 || value.indexOf('?') >= 0; + } + + /** An ARN of six fields whose account has no wildcard. */ + public static boolean pinsAccount(String arn) { + String[] fields = arn.split(":", 6); + return fields.length == 6 && !hasWildcard(fields[4]); + } + + /** + * An {@code aws:SourceIp} value narrow enough to keep a statement private: one address, or a + * range of /8 or narrower for IPv4 and /32 or narrower for IPv6. AWS still calls a broader + * range public. + */ + public static boolean isNarrowSourceIp(String cidr) { + int slash = cidr.indexOf('/'); + if (slash < 0) { + return isFixedValue(cidr); + } + String address = cidr.substring(0, slash); + if (!isFixedValue(address)) { + return false; + } + try { + int prefixLength = Integer.parseInt(cidr.substring(slash + 1).trim()); + return prefixLength >= (address.indexOf(':') >= 0 ? 32 : 8); + } catch (NumberFormatException e) { + LOG.debugv("Treating aws:SourceIp value with an unparseable prefix as public: {0}", cidr); + return false; + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java index 06e336dbe1..062336cabd 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java @@ -15,6 +15,7 @@ import io.github.hectorvent.floci.core.common.Resettable; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.iam.PublicAccessConditions; import io.github.hectorvent.floci.services.lambdaweb.model.WebFunction; import io.github.hectorvent.floci.services.lambdaweb.model.WebFunctionEndpoint; import io.github.hectorvent.floci.services.lambdaweb.model.WebFunctionRevision; @@ -82,17 +83,13 @@ public class LambdaWebService implements Resettable { "AWS_LAMBDA_HTTP_ENDPOINT", "AWS_LAMBDA_RUNTIME_API", "AWS_LAMBDA_INITIALIZATION_TYPE", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "AWS_LAMBDA_METADATA_API", "AWS_LAMBDA_METADATA_TOKEN"); /** A wildcard principal pinned by one of these condition keys does not make a policy public. */ - static final Set TRUSTED_CONDITION_KEYS = Set.of( - "aws:PrincipalAccount", "aws:PrincipalArn", "aws:PrincipalOrgID", "aws:PrincipalOrgPaths", - "aws:SourceAccount", "aws:SourceArn", "aws:SourceOrgID", "aws:SourceOrgPaths", "aws:SourceVpc", - "aws:SourceVpcArn", "aws:SourceVpce", "aws:VpceAccount", "aws:VpceOrgID", "aws:VpceOrgPaths", - "aws:Ec2InstanceSourceVpc", "aws:UserId", "aws:PrincipalServiceName", "aws:PrincipalServiceNamesList", - "aws:PrincipalIsAWSService", "lambda:SourceFunctionArn"); + static final Set TRUSTED_CONDITION_KEYS = + PublicAccessConditions.with(PublicAccessConditions.TRUSTED_KEYS, "lambda:SourceFunctionArn"); /** The operators whose match pins the caller. Negated and IfExists forms do not. */ private static final Set PINNING_OPERATORS = Set.of( "StringEquals", "StringEqualsIgnoreCase", "StringLike", "ArnEquals", "ArnLike", "Bool"); - private static final Set ARN_CONDITION_KEYS = Set.of( - "aws:PrincipalArn", "aws:SourceArn", "aws:SourceVpcArn", "lambda:SourceFunctionArn"); + private static final Set ARN_CONDITION_KEYS = + PublicAccessConditions.with(PublicAccessConditions.ARN_KEYS, "lambda:SourceFunctionArn"); private static final Set STATEMENT_KEYS = Set.of( "Sid", "Effect", "Principal", "NotPrincipal", "Action", "NotAction", "Resource", "NotResource", "Condition"); private static final Set PRINCIPAL_KEYS = Set.of("AWS", "Service", "Federated", "CanonicalUser"); @@ -1334,15 +1331,9 @@ public class LambdaWebService implements Resettable { each.add(values.asText()); } for (String value : each) { - String checked = value; - if (ARN_CONDITION_KEYS.contains(key)) { - String[] fields = value.split(":", 6); - if (fields.length < 6) { - return false; - } - checked = fields[4]; - } - if (checked.contains("*") || checked.contains("?")) { + boolean pins = ARN_CONDITION_KEYS.contains(key) + ? PublicAccessConditions.pinsAccount(value) : !PublicAccessConditions.hasWildcard(value); + if (!pins) { return false; } } diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluator.java index 24673070ed..c1edc7a1b5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluator.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3PublicAccessEvaluator.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator; +import io.github.hectorvent.floci.services.iam.PublicAccessConditions; import org.jboss.logging.Logger; import java.util.Iterator; @@ -201,7 +202,7 @@ final class S3PublicAccessEvaluator { } if (NON_PUBLIC_CONDITION_KEYS.contains(key) && !operatorName.equalsIgnoreCase("IpAddress") - && allValuesFixed(entry.getValue(), S3PublicAccessEvaluator::isFixedValue)) { + && allValuesFixed(entry.getValue(), PublicAccessConditions::isFixedValue)) { return true; } if (SOURCE_IP_CONDITION_KEY.equals(key) @@ -239,13 +240,6 @@ final class S3PublicAccessEvaluator { return false; } - private static boolean isFixedValue(String value) { - return !value.isEmpty() - && value.indexOf('*') < 0 - && value.indexOf('?') < 0 - && !value.contains("${"); - } - private static boolean isFixedAccountAccessPointArn(String value) { return BUCKET_POLICY_ACCESS_POINT_ARN.matcher(value).matches(); } @@ -255,11 +249,11 @@ final class S3PublicAccessEvaluator { return false; } if (value.isTextual()) { - return isNarrowCidr(value.asText()); + return PublicAccessConditions.isNarrowSourceIp(value.asText()); } if (value.isArray() && !value.isEmpty()) { for (JsonNode item : value) { - if (!item.isTextual() || !isNarrowCidr(item.asText())) { + if (!item.isTextual() || !PublicAccessConditions.isNarrowSourceIp(item.asText())) { return false; } } @@ -268,29 +262,6 @@ final class S3PublicAccessEvaluator { return false; } - /** - * AWS still calls a policy public when it is conditioned on an {@code aws:SourceIp} range - * broader than /8 for IPv4 or /32 for IPv6, so only a prefix at least that long makes the - * statement non-public. A bare address carries no prefix and is as narrow as a range gets. - */ - private static boolean isNarrowCidr(String cidr) { - int slash = cidr.indexOf('/'); - if (slash < 0) { - return isFixedValue(cidr); - } - String address = cidr.substring(0, slash); - if (!isFixedValue(address)) { - return false; - } - try { - int prefixLength = Integer.parseInt(cidr.substring(slash + 1).trim()); - return address.indexOf(':') >= 0 ? prefixLength >= 32 : prefixLength >= 8; - } catch (NumberFormatException e) { - LOG.debugv("Treating aws:SourceIp value with an unparseable prefix as public: {0}", cidr); - return false; - } - } - static String bucketArn(String partition, String bucketName) { return AwsArnUtils.Arn.global(partition, "s3", "", bucketName).toString(); } diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/PublicAccessConditionsTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/PublicAccessConditionsTest.java new file mode 100644 index 0000000000..b5cab942d3 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/iam/PublicAccessConditionsTest.java @@ -0,0 +1,48 @@ +package io.github.hectorvent.floci.services.iam; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PublicAccessConditionsTest { + + @ParameterizedTest + @CsvSource(delimiter = '|', value = { + "123456789012|true", + "o-abc|true", + "''|false", + "1234*|false", + "12?4|false", + "${aws:PrincipalAccount}|false", + }) + void aFixedValueNamesOneThing(String value, boolean fixed) { + assertEquals(fixed, PublicAccessConditions.isFixedValue(value)); + } + + @ParameterizedTest + @CsvSource(delimiter = '|', value = { + "arn:aws:iam::123456789012:role/app|true", + "arn:aws:iam::123456789012:role/*|true", + "arn:aws:iam::*:role/app|false", + "arn:aws:iam::12345678901?:root|false", + "arn:aws:iam::123456789012|false", + }) + void anArnPinsItsAccountWhenTheAccountHasNoWildcard(String arn, boolean pins) { + assertEquals(pins, PublicAccessConditions.pinsAccount(arn)); + } + + @ParameterizedTest + @CsvSource(delimiter = '|', value = { + "203.0.113.7|true", + "10.0.0.0/8|true", + "10.0.0.0/7|false", + "2001:db8::/32|true", + "2001:db8::/31|false", + "10.0.0.0/x|false", + "10.*.0.0/16|false", + }) + void aSourceIpRangeIsNarrowFromSlash8ForIpv4AndSlash32ForIpv6(String cidr, boolean narrow) { + assertEquals(narrow, PublicAccessConditions.isNarrowSourceIp(cidr)); + } +}