mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
refactor(iam): share what a public-access condition must say across S3, Lambda and DSQL (#6134)
* refactor(iam): share what a public-access condition must say across S3, Lambda and DSQL S3's Block Public Access, Lambda's function URL and resource policies, and Aurora DSQL's cluster policies each decide whether a condition keeps an Allow to everyone from being public, and each carried its own copy of the same reads: a value that names one thing (no wildcard, no policy variable), an aws:SourceIp range of /8 or narrower for IPv4 and /32 for IPv6, an ARN whose account has no wildcard, and the trusted condition keys Lambda and DSQL both accept. Those now live in services.iam.PublicAccessConditions. Each service keeps what it was measured to do differently: S3 its own key list (aws:SourceOwner and the s3: access point keys) and operators, Lambda the extra lambda:SourceFunctionArn and exact-case keys, DSQL aws:SourceIp, case-insensitive keys and its operator rules. No answer changes; every existing test of the three services passes unchanged. Asked for on #6048, as its own PR. * refactor(iam): keep the shared-keys-plus-one helper on PublicAccessConditions DsqlClusterPolicies and LambdaWebService each added a private with(keys, key) to extend the shared trusted keys by the one key each accepts on its own; the review asked for one copy, so it lives on PublicAccessConditions.
This commit is contained in:
1 parent
9a7c62b8f1
commit
f4a9b15ced
5 files changed
+152
-89
No files matched your search
@@ -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<String> 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<String> ARN_KEYS = lowercase(Set.of("aws:PrincipalArn", "aws:SourceArn", "aws:SourceVpcArn"));
|
||||
private static final Set<String> RESTRICTING_KEYS =
|
||||
lowercase(PublicAccessConditions.with(PublicAccessConditions.TRUSTED_KEYS, "aws:SourceIp"));
|
||||
private static final Set<String> ARN_KEYS = lowercase(PublicAccessConditions.ARN_KEYS);
|
||||
|
||||
private DsqlClusterPolicies() {
|
||||
}
|
||||
@@ -267,45 +264,20 @@ final class DsqlClusterPolicies {
|
||||
|
||||
private static boolean restricts(String operator, String key, List<String> 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("*");
|
||||
}
|
||||
|
||||
@@ -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<String> 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<String> 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<String> with(Set<String> keys, String key) {
|
||||
Set<String> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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<String> 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<String> PINNING_OPERATORS = Set.of(
|
||||
"StringEquals", "StringEqualsIgnoreCase", "StringLike", "ArnEquals", "ArnLike", "Bool");
|
||||
private static final Set<String> ARN_CONDITION_KEYS = Set.of(
|
||||
"aws:PrincipalArn", "aws:SourceArn", "aws:SourceVpcArn", "lambda:SourceFunctionArn");
|
||||
private static final Set<String> ARN_CONDITION_KEYS =
|
||||
PublicAccessConditions.with(PublicAccessConditions.ARN_KEYS, "lambda:SourceFunctionArn");
|
||||
private static final Set<String> STATEMENT_KEYS = Set.of(
|
||||
"Sid", "Effect", "Principal", "NotPrincipal", "Action", "NotAction", "Resource", "NotResource", "Condition");
|
||||
private static final Set<String> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user