mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 10:17:48 -05:00
fix(s3): report the owner and ACL grantees as a canonical user id (#6549)
* fix(s3): report the owner and ACL grantees as a canonical user id S3 reported the 12-digit account id where AWS reports the account's canonical user id: the bucket owner in ListBuckets, the owner and the multipart initiator in ListParts, and every CanonicalUser grantee and Owner the ACL responses name. All of them now answer the canonical id, a 64-character lowercase hex string Floci derives as the SHA-256 of the account id, so the same account answers the same id across calls, restarts and storage modes and two accounts never share one. The account id stays the internal identity; only the emitted value changes, and ACLs persisted by an older Floci keep serving the form they store. Owner evaluations, the public-access checks and bucket-policy CanonicalUser principals accept the canonical id and the account id alike, so stored policies and scripts written against the old form keep working. Closes #6242 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * test(s3): say why the cleanup catches stay silent The two new compatibility cleanup catches swallowed without a word, which the guide allows only for a named, explained tolerance. Both now name the reason: cleanup must not mask the assertion. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * test(s3): expect the canonical owner in the namespace ACL assertions The cross-account object metadata test still expected the raw account id in the ACLs, which the canonical owner id broke, and its ACCOUNT_B assertFalse checks passed whatever the ACL held. All four assertions now name the accounts the way the ACLs do, through AwsCanonicalUserId. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * test(s3): share the compatibility cleanup between the two tests The explained cleanup catches landed on the wrong method, a helper that was already on main, leaving the new canonical owner test with bare catches. One deleteObjectAndBucketQuietly helper now holds the commented catches and serves both tests. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> --------- Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
89be5c1742
commit
56196fec1c
15 files changed
+331
-45
No files matched your search
@@ -0,0 +1,18 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
/**
|
||||
* The S3 canonical user ID an account is identified by in bucket and object ownership, ACL
|
||||
* grantees and bucket-policy principals: a 64-character lowercase hex string. AWS derives it
|
||||
* from the account and never publishes how, so Floci derives its own value as the SHA-256 hex
|
||||
* digest of the account ID: the same account answers the same ID across calls, restarts and
|
||||
* storage modes, and two accounts never share one.
|
||||
*/
|
||||
public final class AwsCanonicalUserId {
|
||||
|
||||
private AwsCanonicalUserId() {
|
||||
}
|
||||
|
||||
public static String forAccount(String accountId) {
|
||||
return Digests.sha256Hex(accountId);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.iam;
|
||||
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.core.common.AwsCanonicalUserId;
|
||||
import io.github.hectorvent.floci.core.common.AwsServiceNamespaces;
|
||||
import io.github.hectorvent.floci.core.common.CidrCanonicalizer;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
@@ -1005,9 +1006,10 @@ public class IamPolicyEvaluator {
|
||||
* role session matches the role it assumed through that role's own ARN, path included.
|
||||
* {@code Service} names one service exactly; {@code {"Service": "*"}} is not a form AWS accepts
|
||||
* and matches nothing. {@code CanonicalUser}, which S3 bucket policies accept, names an account
|
||||
* by its S3 canonical user ID, and matches the IAM identities of that account as an account
|
||||
* principal does; Floci's canonical ID for an account is the account id, as its S3 ACLs report
|
||||
* it. {@code Federated} and any other type name callers this evaluator never sees, and so match
|
||||
* by its S3 canonical user ID, the 64-hex value S3 reports as owner and ACL grantee, and matches
|
||||
* the IAM identities of that account as an account principal does. The account ID itself still
|
||||
* matches, for policies written when Floci reported the account as the canonical ID.
|
||||
* {@code Federated} and any other type name callers this evaluator never sees, and so match
|
||||
* nothing, rather than being compared with an IAM ARN. An entry naming the caller itself wins
|
||||
* over one naming only its account.
|
||||
*/
|
||||
@@ -1054,7 +1056,8 @@ public class IamPolicyEvaluator {
|
||||
} else if ("CanonicalUser".equalsIgnoreCase(type)) {
|
||||
if (principal.type() == RequestPrincipal.Type.IAM && principal.arn() != null) {
|
||||
String accountId = extractAccountId(principal.arn());
|
||||
if (accountId != null && patterns.contains(accountId)) {
|
||||
if (accountId != null && (patterns.contains(AwsCanonicalUserId.forAccount(accountId))
|
||||
|| patterns.contains(accountId))) {
|
||||
strongest = PrincipalMatch.ACCOUNT;
|
||||
}
|
||||
}
|
||||
|
||||
+15
-3
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsCanonicalUserId;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
final class S3AclPublicAccessEvaluator {
|
||||
@@ -55,14 +56,25 @@ final class S3AclPublicAccessEvaluator {
|
||||
}
|
||||
}
|
||||
|
||||
static boolean aclGrantsNonOwner(String acl, String ownerId) {
|
||||
/**
|
||||
* Whether an ACL grants anyone other than the owner account: every grant must name the
|
||||
* owner, whose canonical user ID is what generated ACLs carry and whose account ID is
|
||||
* accepted for ACLs written before Floci derived one. An ACL with no grants names nobody,
|
||||
* so ownership enforcement rejects it too.
|
||||
*/
|
||||
static boolean aclGrantsNonOwner(String acl, String ownerAccount) {
|
||||
if (acl == null || acl.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
S3AclPolicy policy = S3AclPolicy.parse(acl);
|
||||
return policy.grants().isEmpty() || policy.grants().stream()
|
||||
.anyMatch(grant -> !grant.grantee().isCanonicalUser(ownerId));
|
||||
if (policy.grants().isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
String canonicalId = AwsCanonicalUserId.forAccount(ownerAccount);
|
||||
return policy.grants().stream()
|
||||
.anyMatch(grant -> !grant.grantee().isCanonicalUser(canonicalId)
|
||||
&& !grant.grantee().isCanonicalUser(ownerAccount));
|
||||
} catch (S3AclPolicy.AclParseException e) {
|
||||
LOG.debugv(e, "Failed to parse S3 ACL for ownership enforcement");
|
||||
return true;
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AccountResolver;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsCanonicalUserId;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsNamespaces;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
@@ -252,7 +253,7 @@ public class S3Controller {
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
.start("ListAllMyBucketsResult", AwsNamespaces.S3)
|
||||
.start("Owner")
|
||||
.elem("ID", regionResolver.getAccountId())
|
||||
.elem("ID", AwsCanonicalUserId.forAccount(regionResolver.getAccountId()))
|
||||
.elem("DisplayName", S3Service.DEFAULT_OWNER_DISPLAY_NAME)
|
||||
.end("Owner")
|
||||
.start("Buckets");
|
||||
@@ -1987,11 +1988,11 @@ public class S3Controller {
|
||||
String initiatorAccountId = upload.getInitiatorAccountId() != null
|
||||
? upload.getInitiatorAccountId() : ownerAccountId;
|
||||
xml.start("Initiator")
|
||||
.elem("ID", initiatorAccountId)
|
||||
.elem("ID", AwsCanonicalUserId.forAccount(initiatorAccountId))
|
||||
.elem("DisplayName", S3Service.DEFAULT_OWNER_DISPLAY_NAME)
|
||||
.end("Initiator")
|
||||
.start("Owner")
|
||||
.elem("ID", ownerAccountId)
|
||||
.elem("ID", AwsCanonicalUserId.forAccount(ownerAccountId))
|
||||
.elem("DisplayName", S3Service.DEFAULT_OWNER_DISPLAY_NAME)
|
||||
.end("Owner")
|
||||
.elem("StorageClass", upload.getStorageClass());
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnShapes;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsCanonicalUserId;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsNamespaces;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
@@ -6042,12 +6043,21 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
throw new AwsException("AccessControlListNotSupported", "The bucket does not allow ACLs", 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an {@code x-amz-grant-full-control} header grants the owner alone, which ownership
|
||||
* enforcement tolerates. The owner is named by its canonical user ID, the form S3 reports
|
||||
* and clients echo back; the account ID form is still accepted for grants written when
|
||||
* Floci reported the account itself.
|
||||
*/
|
||||
private static boolean isOnlyOwnerFullControlGrant(String ownerAccount, String header) {
|
||||
if (isBlank(header)) {
|
||||
return false;
|
||||
}
|
||||
String canonicalId = AwsCanonicalUserId.forAccount(ownerAccount);
|
||||
for (String grantee : header.split(",", -1)) {
|
||||
if (!("id=\"" + ownerAccount + "\"").equals(grantee.trim())) {
|
||||
String token = grantee.trim();
|
||||
if (!("id=\"" + canonicalId + "\"").equals(token)
|
||||
&& !("id=\"" + ownerAccount + "\"").equals(token)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -6448,17 +6458,18 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
LOG.infov("Restored object: {0}/{1} (stub)", bucketName, key);
|
||||
}
|
||||
|
||||
private static String defaultAclXml(String id, String displayName) {
|
||||
private static String defaultAclXml(String ownerAccount, String displayName) {
|
||||
String canonicalId = AwsCanonicalUserId.forAccount(ownerAccount);
|
||||
return new XmlBuilder()
|
||||
.start("AccessControlPolicy")
|
||||
.start("Owner")
|
||||
.elem("ID", id)
|
||||
.elem("ID", canonicalId)
|
||||
.elem("DisplayName", displayName)
|
||||
.end("Owner")
|
||||
.start("AccessControlList")
|
||||
.start("Grant")
|
||||
.raw("<Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"CanonicalUser\">")
|
||||
.elem("ID", id)
|
||||
.elem("ID", canonicalId)
|
||||
.elem("DisplayName", displayName)
|
||||
.raw("</Grantee>")
|
||||
.elem("Permission", "FULL_CONTROL")
|
||||
@@ -6688,7 +6699,8 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
private static String ownerFullControlGrant(String ownerAccount) {
|
||||
return canonicalUserGrant(ownerAccount, DEFAULT_OWNER_DISPLAY_NAME, "FULL_CONTROL");
|
||||
return canonicalUserGrant(AwsCanonicalUserId.forAccount(ownerAccount),
|
||||
DEFAULT_OWNER_DISPLAY_NAME, "FULL_CONTROL");
|
||||
}
|
||||
|
||||
private static String canonicalUserGrant(String id, String displayName, String permission) {
|
||||
@@ -6718,7 +6730,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("AccessControlPolicy")
|
||||
.start("Owner")
|
||||
.elem("ID", ownerAccount)
|
||||
.elem("ID", AwsCanonicalUserId.forAccount(ownerAccount))
|
||||
.elem("DisplayName", DEFAULT_OWNER_DISPLAY_NAME)
|
||||
.end("Owner")
|
||||
.start("AccessControlList");
|
||||
|
||||
Reference in new issue
Block a user