Expand admin tools and harden storage operations
This commit is contained in:
1 parent
fbec6694df
commit
5304dfc047
86 files changed
+8111
-1102
No files matched your search
@@ -4,9 +4,11 @@ import java.io.IOException;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
final class S3Access {
|
||||
record Decision(boolean allowed, String reason) { }
|
||||
private final ObjectStorage store;
|
||||
private final String owner;
|
||||
private final java.util.Map<String, BucketPolicy> identityPolicies;
|
||||
private final java.util.Map<String, BucketPolicy> configuredIdentityPolicies;
|
||||
private volatile java.util.Map<String, BucketPolicy> identityPolicies;
|
||||
private final ConcurrentHashMap<String, CachedPolicy> policies = new ConcurrentHashMap<>();
|
||||
private record CachedPolicy(String source, BucketPolicy parsed) { }
|
||||
|
||||
@@ -17,7 +19,16 @@ final class S3Access {
|
||||
S3Access(ObjectStorage store, String owner, java.util.Map<String, BucketPolicy> identityPolicies) {
|
||||
this.store = store;
|
||||
this.owner = owner;
|
||||
this.identityPolicies = java.util.Map.copyOf(identityPolicies);
|
||||
this.configuredIdentityPolicies = java.util.Map.copyOf(identityPolicies);
|
||||
this.identityPolicies = configuredIdentityPolicies;
|
||||
}
|
||||
|
||||
synchronized void setManagedPolicies(java.util.Map<String, BucketPolicy> managed) {
|
||||
var combined = new java.util.HashMap<>(configuredIdentityPolicies);
|
||||
for (var entry : managed.entrySet())
|
||||
if (combined.putIfAbsent(entry.getKey(), entry.getValue()) != null)
|
||||
throw new IllegalArgumentException("Managed policy conflicts with a configured identity");
|
||||
identityPolicies = java.util.Map.copyOf(combined);
|
||||
}
|
||||
|
||||
String owner() {
|
||||
@@ -46,19 +57,43 @@ final class S3Access {
|
||||
require(store.bucket(bucket), metadata.acl(), principal, permission, action, key);
|
||||
}
|
||||
|
||||
Decision inspectBucket(String bucket, String principal) throws IOException {
|
||||
ObjectStorage.Bucket current = store.bucket(bucket);
|
||||
return decide(current, current.acl(), principal, Acl.READ, "s3:ListBucket", null);
|
||||
}
|
||||
|
||||
Decision inspectWrite(String bucket, String key, String principal, String action) throws IOException {
|
||||
ObjectStorage.Bucket current = store.bucket(bucket);
|
||||
return decide(current, current.acl(), principal, Acl.WRITE, action, key);
|
||||
}
|
||||
|
||||
Decision inspectObject(String bucket, String key, ObjectStorage.Metadata metadata,
|
||||
String principal) throws IOException {
|
||||
return decide(store.bucket(bucket), metadata.acl(), principal, Acl.READ, "s3:GetObject", key);
|
||||
}
|
||||
|
||||
private void require(ObjectStorage.Bucket bucket, java.util.Map<String, String> acl,
|
||||
String principal, int permission, String action, String key) {
|
||||
if (!decide(bucket, acl, principal, permission, action, key).allowed())
|
||||
throw new StoreException(403, "AccessDenied", "Access denied");
|
||||
}
|
||||
|
||||
private Decision decide(ObjectStorage.Bucket bucket, java.util.Map<String, String> acl,
|
||||
String principal, int permission, String action, String key) {
|
||||
String resource = "arn:aws:s3:::" + bucket.name() + (key == null ? "" : "/" + key);
|
||||
BucketPolicy policy = policy(bucket);
|
||||
int decision = policy.decide(principal, action, resource);
|
||||
int identityDecision = principal == null ? 0 :
|
||||
identityPolicies.getOrDefault(principal, BucketPolicy.empty()).decide(principal, action, resource);
|
||||
if (decision < 0 || identityDecision < 0 ||
|
||||
!BucketSecurity.allowsAcl(bucket, acl, principal, owner, permission) &&
|
||||
identityDecision <= 0 &&
|
||||
!(decision > 0 && (principal != null || !bucket.publicAccessBlock().restrictPublicBuckets() ||
|
||||
!policy.publicAllow())))
|
||||
throw new StoreException(403, "AccessDenied", "Access denied");
|
||||
if (decision < 0) return new Decision(false, "Explicit bucket policy deny");
|
||||
if (identityDecision < 0) return new Decision(false, "Explicit identity policy deny");
|
||||
if (BucketSecurity.allowsAcl(bucket, acl, principal, owner, permission))
|
||||
return new Decision(true, "Owner or ACL grants this action");
|
||||
if (identityDecision > 0) return new Decision(true, "Identity policy allows this action");
|
||||
if (decision > 0 && (principal != null || !bucket.publicAccessBlock().restrictPublicBuckets() ||
|
||||
!policy.publicAllow()))
|
||||
return new Decision(true, "Bucket policy allows this action");
|
||||
return new Decision(false, "No ACL or policy grants this action");
|
||||
}
|
||||
|
||||
private BucketPolicy policy(ObjectStorage.Bucket bucket) {
|
||||
|
||||
Reference in new issue
Block a user