mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(paymentcryptography): the resource policies of a key (#8702)
Payment Cryptography keys now take a resource-based policy through PutResourcePolicy, GetResourcePolicy and DeleteResourcePolicy. The policy is stored on the key, so it goes when the key goes, and it is not enforced. A document that is not a JSON object or carries trailing content, a policy with no statement, a statement that names no effect, principal, action or resource, a Resource that is neither the wildcard nor the key's own ARN, and a policy on a replica key are refused with ValidationException. An unconditional Allow that reaches everyone, through a wildcard Principal or a NotPrincipal that excludes someone, is refused with PublicPolicyException, and GetResourcePolicy on a key without a policy answers ResourceNotFoundException. Reading a policy's statements and checking for an everyone principal now live in iam.PolicyDocument, which S3, Secrets Manager and Payment Cryptography share.
This commit is contained in:
1 parent
e8735be43b
commit
91943a061c
15 files changed
+947
-122
No files matched your search
@@ -0,0 +1,108 @@
|
||||
package io.floci.aws.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* How a resource policy document reads: the statements it carries, and the principals
|
||||
* those statements reach.
|
||||
*
|
||||
* <p>S3's block public access, Secrets Manager's block public policy and Payment
|
||||
* Cryptography's public policy refusal all read a policy this way, and each of them had a
|
||||
* copy of it. The copies disagreed: one of them did not see a wildcard principal written
|
||||
* as a list, and one of them saw a wildcard in a shape the others did not. The rule is
|
||||
* here now, and the three read it.
|
||||
*/
|
||||
public final class PolicyDocument {
|
||||
|
||||
/** The wildcard that names every principal. */
|
||||
private static final String WILDCARD = "*";
|
||||
|
||||
private PolicyDocument() {
|
||||
}
|
||||
|
||||
/**
|
||||
* The statements of a policy, whether it carries one alone or a list of them.
|
||||
*
|
||||
* @param policy the policy document
|
||||
* @return the statements, empty when the document carries none
|
||||
*/
|
||||
public static List<JsonNode> statementsOf(JsonNode policy) {
|
||||
JsonNode statements = policy.path("Statement");
|
||||
if (statements.isObject()) {
|
||||
return List.of(statements);
|
||||
}
|
||||
List<JsonNode> out = new ArrayList<>();
|
||||
if (statements.isArray()) {
|
||||
statements.forEach(out::add);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a principal names every principal.
|
||||
*
|
||||
* <p>A principal names everyone when it is the wildcard itself, when it is a list that
|
||||
* holds the wildcard, or when it is a map whose values hold it: {@code "*"},
|
||||
* {@code {"AWS": "*"}}, {@code {"AWS": ["*"]}} and {@code ["*"]} all name everyone.
|
||||
*
|
||||
* @param principal the principal of a statement
|
||||
* @return whether the principal names everyone
|
||||
*/
|
||||
public static boolean namesEveryone(JsonNode principal) {
|
||||
if (principal == null || principal.isMissingNode() || principal.isNull()) {
|
||||
return false;
|
||||
}
|
||||
if (principal.isTextual()) {
|
||||
return WILDCARD.equals(principal.asText());
|
||||
}
|
||||
if (principal.isArray()) {
|
||||
return holdsEveryone(principal);
|
||||
}
|
||||
if (principal.isObject()) {
|
||||
for (JsonNode value : principal) {
|
||||
if (holdsEveryone(value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a statement grants to everyone.
|
||||
*
|
||||
* <p>A grant that names everyone as its principal reaches everyone, and a grant that
|
||||
* names the principals it excludes reaches everyone else. A grant that excludes
|
||||
* everyone reaches nobody.
|
||||
*
|
||||
* @param statement the statement of a policy
|
||||
* @return whether the statement reaches everyone
|
||||
*/
|
||||
public static boolean grantsToEveryone(JsonNode statement) {
|
||||
if (statement.hasNonNull("Principal")) {
|
||||
return namesEveryone(statement.path("Principal"));
|
||||
}
|
||||
return statement.hasNonNull("NotPrincipal") && !namesEveryone(statement.path("NotPrincipal"));
|
||||
}
|
||||
|
||||
/** Whether a value holds the wildcard, whether it is the value itself or a list of them. */
|
||||
private static boolean holdsEveryone(JsonNode node) {
|
||||
if (node == null || node.isNull()) {
|
||||
return false;
|
||||
}
|
||||
if (node.isTextual()) {
|
||||
return WILDCARD.equals(node.asText());
|
||||
}
|
||||
if (node.isArray()) {
|
||||
for (JsonNode item : node) {
|
||||
if (WILDCARD.equals(item.asText())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+183
@@ -1,9 +1,13 @@
|
||||
package io.floci.aws.services.paymentcryptography;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.services.iam.PolicyDocument;
|
||||
import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyAlias;
|
||||
import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyKey;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -28,6 +32,15 @@ public class PaymentCryptographyJsonHandler {
|
||||
/** The error a bad page size or a bad token is refused with. */
|
||||
private static final String PAGINATION_ERROR = "ValidationException";
|
||||
|
||||
/**
|
||||
* The reader of a resource policy, which arrives as a JSON document in a string.
|
||||
*
|
||||
* <p>The whole string is what the key keeps, so a string that carries anything after
|
||||
* the document is not one policy and is refused.
|
||||
*/
|
||||
private static final ObjectMapper POLICY_READER = new ObjectMapper()
|
||||
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
|
||||
|
||||
private final PaymentCryptographyService service;
|
||||
|
||||
@Inject
|
||||
@@ -70,6 +83,9 @@ public class PaymentCryptographyJsonHandler {
|
||||
case "TagResource" -> () -> tagResource(request, region);
|
||||
case "UntagResource" -> () -> untagResource(request, region);
|
||||
case "ListTagsForResource" -> () -> listTagsForResource(request, region);
|
||||
case "PutResourcePolicy" -> () -> putResourcePolicy(request, region);
|
||||
case "GetResourcePolicy" -> () -> getResourcePolicy(request, region);
|
||||
case "DeleteResourcePolicy" -> () -> deleteResourcePolicy(request, region);
|
||||
default -> throw new AwsException("UnknownOperationException",
|
||||
"Operation " + action + " is not supported by floci", 400);
|
||||
};
|
||||
@@ -273,6 +289,173 @@ public class PaymentCryptographyJsonHandler {
|
||||
return out;
|
||||
}
|
||||
|
||||
// ---- resource policies ----------------------------------------------------
|
||||
|
||||
/**
|
||||
* Attaches or replaces the resource-based policy of a key.
|
||||
*
|
||||
* <p>The policy is a JSON document in a string, and a document that does not parse is
|
||||
* refused before it is attached. A policy that hands the key to everyone and carries no
|
||||
* condition is refused with the reference's own error, so the key is never left open by
|
||||
* a policy no one bounds.
|
||||
*/
|
||||
private Map<String, Object> putResourcePolicy(JsonNode request, String region) {
|
||||
PaymentCryptographyKey key = requiredKey(request, region, RESOURCE_ARN);
|
||||
String policy = request.path("Policy").asText();
|
||||
JsonNode document = readPolicy(policy);
|
||||
List<JsonNode> statements = PolicyDocument.statementsOf(document);
|
||||
if (grantsPublicAccess(statements)) {
|
||||
throw new AwsException("PublicPolicyException",
|
||||
"The resource policy grants public access to the key", 400);
|
||||
}
|
||||
if (!namesTheKey(grantsOf(statements), key.getArn())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"The resource policy names a resource that is not the key it is attached to", 400);
|
||||
}
|
||||
return policyAnswer(service.putResourcePolicy(key, policy));
|
||||
}
|
||||
|
||||
/**
|
||||
* The policy document a request carries.
|
||||
*
|
||||
* <p>The policy is a JSON document in a string, and a document that does not parse, or
|
||||
* that carries anything after the document, is refused before it is attached: the whole
|
||||
* string is what the key keeps, so a string that is not one document is not a policy.
|
||||
*
|
||||
* @param policy the policy as it was given
|
||||
* @return the policy document
|
||||
*/
|
||||
private static JsonNode readPolicy(String policy) {
|
||||
JsonNode document;
|
||||
try {
|
||||
document = POLICY_READER.readTree(policy);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Policy is not a valid JSON policy document: " + e.getOriginalMessage(), 400);
|
||||
}
|
||||
if (document == null || !document.isObject()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Policy is not a valid JSON policy document", 400);
|
||||
}
|
||||
return document;
|
||||
}
|
||||
|
||||
/** The resource-based policy of a key, which a key without one does not have. */
|
||||
private Map<String, Object> getResourcePolicy(JsonNode request, String region) {
|
||||
PaymentCryptographyKey key = requiredKey(request, region, RESOURCE_ARN);
|
||||
return policyAnswer(service.resourcePolicy(key));
|
||||
}
|
||||
|
||||
/** Removes the resource-based policy of a key. */
|
||||
private Map<String, Object> deleteResourcePolicy(JsonNode request, String region) {
|
||||
PaymentCryptographyKey key = requiredKey(request, region, RESOURCE_ARN);
|
||||
service.deleteResourcePolicy(key);
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
private static Map<String, Object> policyAnswer(PaymentCryptographyService.ResourcePolicy policy) {
|
||||
return Map.of("ResourceArn", policy.arn(), "Policy", policy.policy());
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether every statement of a policy names the key the policy is attached to.
|
||||
*
|
||||
* <p>The reference says the resource of a policy must be every resource or the ARN of
|
||||
* the key the policy is on, so a policy that names another key is refused.
|
||||
*
|
||||
* @param statements the statements of the policy
|
||||
* @param arn the ARN of the key the policy is attached to
|
||||
* @return whether the policy names the key
|
||||
*/
|
||||
private static boolean namesTheKey(List<JsonNode> statements, String arn) {
|
||||
for (JsonNode statement : statements) {
|
||||
if (!statementNamesTheKey(statement, arn)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The statements of a policy that grant something.
|
||||
*
|
||||
* <p>The grammar a resource policy follows names an effect, a principal, an action and
|
||||
* a resource in every statement, and a document that carries no statement, or a
|
||||
* statement that names none of them, grants nothing, so it is refused rather than
|
||||
* stored on the key.
|
||||
*
|
||||
* @param statements the statements of the policy
|
||||
* @return the statements
|
||||
*/
|
||||
private static List<JsonNode> grantsOf(List<JsonNode> statements) {
|
||||
if (statements.isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"The resource policy carries no statement", 400);
|
||||
}
|
||||
for (JsonNode statement : statements) {
|
||||
if (!carriesTheElementsOfAGrant(statement)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"The resource policy carries a statement that names no effect, principal, "
|
||||
+ "action or resource", 400);
|
||||
}
|
||||
}
|
||||
return statements;
|
||||
}
|
||||
|
||||
/** Whether a statement names the elements a grant is made of. */
|
||||
private static boolean carriesTheElementsOfAGrant(JsonNode statement) {
|
||||
String effect = statement.path("Effect").asText();
|
||||
return ("Allow".equalsIgnoreCase(effect) || "Deny".equalsIgnoreCase(effect))
|
||||
&& (statement.hasNonNull("Principal") || statement.hasNonNull("NotPrincipal"))
|
||||
&& (statement.hasNonNull("Action") || statement.hasNonNull("NotAction"))
|
||||
&& statement.hasNonNull("Resource");
|
||||
}
|
||||
|
||||
/** Whether a statement names the key, either alone or in a list of resources. */
|
||||
private static boolean statementNamesTheKey(JsonNode statement, String arn) {
|
||||
JsonNode resource = statement.path("Resource");
|
||||
if (resource.isTextual()) {
|
||||
return "*".equals(resource.asText()) || arn.equals(resource.asText());
|
||||
}
|
||||
if (resource.isArray()) {
|
||||
for (JsonNode entry : resource) {
|
||||
if (!"*".equals(entry.asText()) && !arn.equals(entry.asText())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return resource.size() > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a policy hands the key to every principal without a condition.
|
||||
*
|
||||
* <p>The reference refuses such a policy, so the service does too: an Allow statement
|
||||
* whose principal is everyone, and that carries no condition, is public access.
|
||||
*
|
||||
* @param statements the statements of the policy
|
||||
* @return whether the policy grants public access
|
||||
*/
|
||||
private static boolean grantsPublicAccess(List<JsonNode> statements) {
|
||||
for (JsonNode statement : statements) {
|
||||
if (statementGrantsPublicAccess(statement)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean statementGrantsPublicAccess(JsonNode statement) {
|
||||
if (!"Allow".equalsIgnoreCase(statement.path("Effect").asText())) {
|
||||
return false;
|
||||
}
|
||||
if (!PolicyDocument.grantsToEveryone(statement) || statement.has("Condition")) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** The tags of a request, as the name and the value of each. */
|
||||
private static Map<String, String> tagsOf(JsonNode request) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
+70
@@ -640,6 +640,76 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
maxResults, nextToken, DEFAULT_PAGE_SIZE, MAX_RESULTS, PAGINATION_ERROR);
|
||||
}
|
||||
|
||||
// ---- resource policies ----------------------------------------------------
|
||||
|
||||
/**
|
||||
* A key's resource-based policy as it is at one moment.
|
||||
*
|
||||
* <p>The store holds the key itself, and a key is mutable, so a read takes the two
|
||||
* values the answer carries while it holds the lock. A policy taken away after the
|
||||
* read cannot change the answer.
|
||||
*
|
||||
* @param arn the ARN of the key the policy is on
|
||||
* @param policy the policy document
|
||||
*/
|
||||
public record ResourcePolicy(String arn, String policy) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches or replaces the resource-based policy of a key.
|
||||
*
|
||||
* <p>The policy belongs to the key, so it is written with the key and goes when the key
|
||||
* goes. A policy the service has not seen is replaced, as the reference replaces one.
|
||||
*/
|
||||
public synchronized ResourcePolicy putResourcePolicy(PaymentCryptographyKey key, String policy) {
|
||||
PaymentCryptographyKey current = liveKey(key);
|
||||
// A replica is read-only, and the reference attaches a policy to the primary alone.
|
||||
requireNotReplica(current, "a policy may only be attached to the primary");
|
||||
current.setResourcePolicy(policy);
|
||||
storeKey(current);
|
||||
return new ResourcePolicy(current.getArn(), current.getResourcePolicy());
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource-based policy attached to a key.
|
||||
*
|
||||
* <p>A key without a policy is refused as the resource the reference cannot find, not
|
||||
* answered with an empty policy: the two are different answers.
|
||||
*/
|
||||
public synchronized ResourcePolicy resourcePolicy(PaymentCryptographyKey key) {
|
||||
PaymentCryptographyKey current = keyWithPolicy(key);
|
||||
return new ResourcePolicy(current.getArn(), current.getResourcePolicy());
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the resource-based policy of a key.
|
||||
*
|
||||
* <p>A key without a policy is refused, as it is by the read.
|
||||
*/
|
||||
public synchronized void deleteResourcePolicy(PaymentCryptographyKey key) {
|
||||
PaymentCryptographyKey current = keyWithPolicy(key);
|
||||
current.setResourcePolicy(null);
|
||||
storeKey(current);
|
||||
}
|
||||
|
||||
/**
|
||||
* The key as it is in the store now, with the policy it carries.
|
||||
*
|
||||
* <p>A key without a policy is refused as the resource the reference cannot find, not
|
||||
* answered with an empty policy: the two are different answers.
|
||||
*
|
||||
* @param key the key the request named
|
||||
* @return the key, with its policy
|
||||
*/
|
||||
private PaymentCryptographyKey keyWithPolicy(PaymentCryptographyKey key) {
|
||||
PaymentCryptographyKey current = liveKey(key);
|
||||
if (current.getResourcePolicy() == null) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Resource policy for key " + key.getArn() + " is not found", 400);
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
// ---- the key exchange ----------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
+9
@@ -69,6 +69,11 @@ public class PaymentCryptographyKey {
|
||||
private long deletePendingTimestamp;
|
||||
private long deleteTimestamp;
|
||||
private volatile Map<String, String> tags = new HashMap<>();
|
||||
/**
|
||||
* The resource-based policy attached to the key, in JSON, or null when the key has none.
|
||||
* It is the key's own, so it goes when the key goes.
|
||||
*/
|
||||
private String resourcePolicy;
|
||||
/** The region of the primary of a multi-region key, and its own region otherwise. */
|
||||
private String primaryRegion;
|
||||
/** The regions a multi-region key is in, besides the primary's. */
|
||||
@@ -183,4 +188,8 @@ public class PaymentCryptographyKey {
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) { this.tags = tags; }
|
||||
|
||||
/** The resource-based policy of the key, or null when it has none. */
|
||||
public String getResourcePolicy() { return resourcePolicy; }
|
||||
public void setResourcePolicy(String resourcePolicy) { this.resourcePolicy = resourcePolicy; }
|
||||
}
|
||||
@@ -5,11 +5,11 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.services.iam.IamPolicyEvaluator;
|
||||
import io.floci.aws.services.iam.PolicyDocument;
|
||||
import io.floci.aws.services.iam.PublicAccessConditions;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -118,10 +118,8 @@ final class S3PublicAccessEvaluator {
|
||||
return PublicAccessDecision.NEUTRAL;
|
||||
}
|
||||
try {
|
||||
JsonNode statements = objectMapper.readTree(policy).path("Statement");
|
||||
boolean allowed = false;
|
||||
Iterable<JsonNode> iterable = statements.isArray() ? statements : List.of(statements);
|
||||
for (JsonNode statement : iterable) {
|
||||
for (JsonNode statement : PolicyDocument.statementsOf(objectMapper.readTree(policy))) {
|
||||
String effect = effectOf(statement);
|
||||
if (effect.isEmpty()) {
|
||||
continue;
|
||||
@@ -156,10 +154,8 @@ final class S3PublicAccessEvaluator {
|
||||
return PublicAccessDecision.NEUTRAL;
|
||||
}
|
||||
try {
|
||||
JsonNode statements = objectMapper.readTree(policy).path("Statement");
|
||||
boolean allowed = false;
|
||||
Iterable<JsonNode> iterable = statements.isArray() ? statements : List.of(statements);
|
||||
for (JsonNode statement : iterable) {
|
||||
for (JsonNode statement : PolicyDocument.statementsOf(objectMapper.readTree(policy))) {
|
||||
String effect = effectOf(statement);
|
||||
if (effect.isEmpty()) {
|
||||
continue;
|
||||
@@ -199,9 +195,7 @@ final class S3PublicAccessEvaluator {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
JsonNode statements = objectMapper.readTree(policy).path("Statement");
|
||||
Iterable<JsonNode> iterable = statements.isArray() ? statements : List.of(statements);
|
||||
for (JsonNode statement : iterable) {
|
||||
for (JsonNode statement : PolicyDocument.statementsOf(objectMapper.readTree(policy))) {
|
||||
if (statementIsPublic(statement)) {
|
||||
return true;
|
||||
}
|
||||
@@ -220,27 +214,12 @@ final class S3PublicAccessEvaluator {
|
||||
if (!"Allow".equals(effectOf(statement))) {
|
||||
return false;
|
||||
}
|
||||
if (!grantsToEveryone(statement)) {
|
||||
if (!PolicyDocument.grantsToEveryone(statement)) {
|
||||
return false;
|
||||
}
|
||||
return !conditionPinsToFixedValues(statement.path("Condition"));
|
||||
}
|
||||
|
||||
private static boolean grantsToEveryone(JsonNode statement) {
|
||||
if (statement.hasNonNull("Principal")) {
|
||||
return hasPublicPrincipal(statement.path("Principal"));
|
||||
}
|
||||
// An Allow on NotPrincipal grants to everyone the statement does not name. Block Public
|
||||
// Access asks a different question from the readers that decide a request: S3 "begins by
|
||||
// assuming that the policy is public" and calls it non-public only when it grants "only to
|
||||
// fixed values" of a principal or of one of the recognised condition keys (S3 User Guide,
|
||||
// "The meaning of public"). A NotPrincipal pins no principal to a fixed value whether it
|
||||
// names anybody or not, so the status stays public here even where the grant readers below
|
||||
// decide the statement covers nobody.
|
||||
return statement.hasNonNull("NotPrincipal")
|
||||
&& !hasPublicPrincipal(statement.path("NotPrincipal"));
|
||||
}
|
||||
|
||||
private static boolean conditionPinsToFixedValues(JsonNode conditions) {
|
||||
if (conditions == null || !conditions.isObject()) {
|
||||
return false;
|
||||
@@ -333,10 +312,10 @@ final class S3PublicAccessEvaluator {
|
||||
|
||||
private static boolean principalMatchesPublic(JsonNode statement) {
|
||||
if (statement.hasNonNull("Principal")) {
|
||||
return hasPublicPrincipal(statement.path("Principal"));
|
||||
return PolicyDocument.namesEveryone(statement.path("Principal"));
|
||||
}
|
||||
if (namesAnyPrincipal(statement.path("NotPrincipal"))) {
|
||||
return !hasPublicPrincipal(statement.path("NotPrincipal"));
|
||||
return !PolicyDocument.namesEveryone(statement.path("NotPrincipal"));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -497,49 +476,6 @@ final class S3PublicAccessEvaluator {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean hasPublicPrincipal(JsonNode principal) {
|
||||
if (principal == null || principal.isMissingNode() || principal.isNull()) {
|
||||
return false;
|
||||
}
|
||||
if (principal.isTextual()) {
|
||||
return "*".equals(principal.asText());
|
||||
}
|
||||
if (principal.isArray()) {
|
||||
for (JsonNode item : principal) {
|
||||
if ("*".equals(item.asText())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (principal.isObject()) {
|
||||
Iterator<JsonNode> values = principal.elements();
|
||||
while (values.hasNext()) {
|
||||
if (nodeContainsPublicPrincipal(values.next())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean nodeContainsPublicPrincipal(JsonNode node) {
|
||||
if (node == null || node.isNull()) {
|
||||
return false;
|
||||
}
|
||||
if (node.isTextual()) {
|
||||
return "*".equals(node.asText());
|
||||
}
|
||||
if (node.isArray()) {
|
||||
for (JsonNode item : node) {
|
||||
if ("*".equals(item.asText())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean nodeCanMatch(JsonNode node) {
|
||||
if (node == null || node.isNull()) {
|
||||
return false;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.secretsmanager;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.services.iam.PolicyDocument;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -24,8 +25,6 @@ public final class ResourcePolicyValidator {
|
||||
/** The name AWS reports a broad-principal finding under. */
|
||||
public static final String REFLEXIVE_PRINCIPAL_CHECK = "REFLEXIVE_PRINCIPAL_CHECK";
|
||||
|
||||
private static final String WILDCARD = "*";
|
||||
|
||||
/** A single finding: the check that failed and what it found. */
|
||||
public record ValidationError(String checkName, String errorMessage) {
|
||||
}
|
||||
@@ -40,15 +39,8 @@ public final class ResourcePolicyValidator {
|
||||
*/
|
||||
public static List<ValidationError> validate(JsonNode policy) {
|
||||
List<ValidationError> errors = new ArrayList<>();
|
||||
JsonNode statements = policy.path("Statement");
|
||||
|
||||
// A lone statement object is as valid as an array of them.
|
||||
if (statements.isObject()) {
|
||||
checkStatement(statements, errors);
|
||||
} else if (statements.isArray()) {
|
||||
for (JsonNode statement : statements) {
|
||||
checkStatement(statement, errors);
|
||||
}
|
||||
for (JsonNode statement : PolicyDocument.statementsOf(policy)) {
|
||||
checkStatement(statement, errors);
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
@@ -59,45 +51,10 @@ public final class ResourcePolicyValidator {
|
||||
if (!"Allow".equalsIgnoreCase(statement.path("Effect").asText())) {
|
||||
return;
|
||||
}
|
||||
if (isWildcardPrincipal(statement.path("Principal"))) {
|
||||
if (PolicyDocument.namesEveryone(statement.path("Principal"))) {
|
||||
errors.add(new ValidationError(REFLEXIVE_PRINCIPAL_CHECK,
|
||||
"The resource policy grants broad access to your secret. "
|
||||
+ "Amend the policy so that it does not use a wildcard principal."));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A principal is wildcard either as the bare string {@code "*"} or as a keyed form whose value
|
||||
* is {@code "*"} - {@code {"AWS": "*"}} and {@code {"AWS": ["*"]}} both grant everyone.
|
||||
*/
|
||||
private static boolean isWildcardPrincipal(JsonNode principal) {
|
||||
if (principal.isMissingNode() || principal.isNull()) {
|
||||
return false;
|
||||
}
|
||||
if (principal.isTextual()) {
|
||||
return WILDCARD.equals(principal.asText());
|
||||
}
|
||||
if (principal.isObject()) {
|
||||
for (JsonNode value : principal) {
|
||||
if (containsWildcard(value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean containsWildcard(JsonNode value) {
|
||||
if (value.isTextual()) {
|
||||
return WILDCARD.equals(value.asText());
|
||||
}
|
||||
if (value.isArray()) {
|
||||
for (JsonNode entry : value) {
|
||||
if (entry.isTextual() && WILDCARD.equals(entry.asText())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user