Simplify S3 request handlers and routing
This commit is contained in:
1 parent
3c739deb09
commit
b724a21a0d
6 files changed
+657
-396
No files matched your search
@@ -38,53 +38,99 @@ final class BucketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handleBucket(HttpExchange exchange, Map<String, String> query, String hash,
|
void handleBucket(HttpExchange exchange, Map<String, String> query, String hash,
|
||||||
String bucketName, String principal) throws IOException {
|
String bucketName, String principal) throws IOException {
|
||||||
String method = exchange.getRequestMethod();
|
String method = exchange.getRequestMethod();
|
||||||
if (query.size() >= 1 && query.size() <= 2 &&
|
if (configurationRequest(query, "ownershipControls")) {
|
||||||
query.keySet().stream().allMatch(java.util.Set.of("ownershipControls", "x-id")::contains) &&
|
|
||||||
query.containsKey("ownershipControls") && query.get("ownershipControls").isEmpty()) {
|
|
||||||
access.requireOwner(principal);
|
access.requireOwner(principal);
|
||||||
if (method.equals("GET")) {
|
handleOwnership(exchange, hash, bucketName);
|
||||||
|
} else if (configurationRequest(query, "publicAccessBlock")) {
|
||||||
|
access.requireOwner(principal);
|
||||||
|
handlePublicAccess(exchange, hash, bucketName);
|
||||||
|
} else if (configurationRequest(query, "policy")) {
|
||||||
|
access.requireOwner(principal);
|
||||||
|
handlePolicy(exchange, hash, bucketName);
|
||||||
|
} else if (aclRequest(query, method)) {
|
||||||
|
handleAcl(exchange, hash, bucketName, principal);
|
||||||
|
} else if (bucketRequest(query, method)) {
|
||||||
|
handleBucketOperation(exchange, hash, bucketName, principal);
|
||||||
|
} else {
|
||||||
|
store.bucket(bucketName);
|
||||||
|
handleExistingBucket(exchange, query, hash, bucketName, principal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean configurationRequest(Map<String, String> query, String configuration) {
|
||||||
|
return query.size() >= 1 && query.size() <= 2 &&
|
||||||
|
query.keySet().stream().allMatch(java.util.Set.of(configuration, "x-id")::contains) &&
|
||||||
|
query.containsKey(configuration) && query.get(configuration).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean aclRequest(Map<String, String> query, String method) {
|
||||||
|
return query.containsKey("acl") && query.get("acl").isEmpty() &&
|
||||||
|
query.keySet().stream().allMatch(java.util.Set.of("acl", "x-id")::contains) &&
|
||||||
|
(!query.containsKey("x-id") ||
|
||||||
|
query.get("x-id").equals(method.equals("GET") ? "GetBucketAcl" : "PutBucketAcl")) &&
|
||||||
|
(method.equals("GET") || method.equals("PUT"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean bucketRequest(Map<String, String> query, String method) {
|
||||||
|
String operation = switch (method) {
|
||||||
|
case "PUT" -> "CreateBucket";
|
||||||
|
case "HEAD" -> "HeadBucket";
|
||||||
|
case "DELETE" -> "DeleteBucket";
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
return operation != null && (query.isEmpty() ||
|
||||||
|
query.size() == 1 && operation.equals(query.get("x-id")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleOwnership(HttpExchange exchange, String hash, String bucketName) throws IOException {
|
||||||
|
switch (exchange.getRequestMethod()) {
|
||||||
|
case "GET" -> {
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
S3Xml.sendXml(exchange, 200, BucketSecurity.ownershipXml(store.bucket(bucketName).ownership()));
|
S3Xml.sendXml(exchange, 200, BucketSecurity.ownershipXml(store.bucket(bucketName).ownership()));
|
||||||
} else if (method.equals("PUT")) {
|
}
|
||||||
|
case "PUT" -> {
|
||||||
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
||||||
S3Request.verifyContentMd5(exchange.getRequestHeaders(), body);
|
S3Request.verifyContentMd5(exchange.getRequestHeaders(), body);
|
||||||
store.setBucketOwnership(bucketName, BucketSecurity.parseOwnership(body));
|
store.setBucketOwnership(bucketName, BucketSecurity.parseOwnership(body));
|
||||||
exchange.sendResponseHeaders(200, -1);
|
exchange.sendResponseHeaders(200, -1);
|
||||||
} else if (method.equals("DELETE")) {
|
}
|
||||||
|
case "DELETE" -> {
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
store.setBucketOwnership(bucketName, ObjectStorage.ObjectOwnership.OBJECT_WRITER);
|
store.setBucketOwnership(bucketName, ObjectStorage.ObjectOwnership.OBJECT_WRITER);
|
||||||
exchange.sendResponseHeaders(204, -1);
|
exchange.sendResponseHeaders(204, -1);
|
||||||
} else S3Request.unsupported("Ownership controls operation");
|
}
|
||||||
return;
|
default -> S3Request.unsupported("Ownership controls operation");
|
||||||
}
|
}
|
||||||
if (query.size() >= 1 && query.size() <= 2 &&
|
}
|
||||||
query.keySet().stream().allMatch(java.util.Set.of("publicAccessBlock", "x-id")::contains) &&
|
|
||||||
query.containsKey("publicAccessBlock") && query.get("publicAccessBlock").isEmpty()) {
|
private void handlePublicAccess(HttpExchange exchange, String hash, String bucketName) throws IOException {
|
||||||
access.requireOwner(principal);
|
switch (exchange.getRequestMethod()) {
|
||||||
if (method.equals("GET")) {
|
case "GET" -> {
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
S3Xml.sendXml(exchange, 200, BucketSecurity.publicAccessXml(
|
S3Xml.sendXml(exchange, 200, BucketSecurity.publicAccessXml(
|
||||||
store.bucket(bucketName).publicAccessBlock()));
|
store.bucket(bucketName).publicAccessBlock()));
|
||||||
} else if (method.equals("PUT")) {
|
}
|
||||||
|
case "PUT" -> {
|
||||||
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
||||||
S3Request.verifyContentMd5(exchange.getRequestHeaders(), body);
|
S3Request.verifyContentMd5(exchange.getRequestHeaders(), body);
|
||||||
store.setPublicAccessBlock(bucketName, BucketSecurity.parsePublicAccess(body));
|
store.setPublicAccessBlock(bucketName, BucketSecurity.parsePublicAccess(body));
|
||||||
exchange.sendResponseHeaders(200, -1);
|
exchange.sendResponseHeaders(200, -1);
|
||||||
} else if (method.equals("DELETE")) {
|
}
|
||||||
|
case "DELETE" -> {
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
store.setPublicAccessBlock(bucketName, ObjectStorage.PublicAccessBlock.open());
|
store.setPublicAccessBlock(bucketName, ObjectStorage.PublicAccessBlock.open());
|
||||||
exchange.sendResponseHeaders(204, -1);
|
exchange.sendResponseHeaders(204, -1);
|
||||||
} else S3Request.unsupported("Public access block operation");
|
}
|
||||||
return;
|
default -> S3Request.unsupported("Public access block operation");
|
||||||
}
|
}
|
||||||
if (query.size() >= 1 && query.size() <= 2 &&
|
}
|
||||||
query.keySet().stream().allMatch(java.util.Set.of("policy", "x-id")::contains) &&
|
|
||||||
query.containsKey("policy") && query.get("policy").isEmpty()) {
|
private void handlePolicy(HttpExchange exchange, String hash, String bucketName) throws IOException {
|
||||||
access.requireOwner(principal);
|
var current = store.bucket(bucketName);
|
||||||
var current = store.bucket(bucketName);
|
switch (exchange.getRequestMethod()) {
|
||||||
if (method.equals("GET")) {
|
case "GET" -> {
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
if (current.policy() == null)
|
if (current.policy() == null)
|
||||||
throw new StoreException(404, "NoSuchBucketPolicy", "Bucket has no policy");
|
throw new StoreException(404, "NoSuchBucketPolicy", "Bucket has no policy");
|
||||||
@@ -92,7 +138,8 @@ final class BucketHandler {
|
|||||||
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
||||||
exchange.sendResponseHeaders(200, body.length);
|
exchange.sendResponseHeaders(200, body.length);
|
||||||
exchange.getResponseBody().write(body);
|
exchange.getResponseBody().write(body);
|
||||||
} else if (method.equals("PUT")) {
|
}
|
||||||
|
case "PUT" -> {
|
||||||
byte[] body = S3Request.signedBody(exchange, hash, 20480);
|
byte[] body = S3Request.signedBody(exchange, hash, 20480);
|
||||||
S3Request.verifyContentMd5(exchange.getRequestHeaders(), body);
|
S3Request.verifyContentMd5(exchange.getRequestHeaders(), body);
|
||||||
String policy = new String(body, StandardCharsets.UTF_8);
|
String policy = new String(body, StandardCharsets.UTF_8);
|
||||||
@@ -101,128 +148,144 @@ final class BucketHandler {
|
|||||||
throw new StoreException(403, "AccessDenied", "Public bucket policies are blocked");
|
throw new StoreException(403, "AccessDenied", "Public bucket policies are blocked");
|
||||||
store.setBucketPolicy(bucketName, policy);
|
store.setBucketPolicy(bucketName, policy);
|
||||||
exchange.sendResponseHeaders(204, -1);
|
exchange.sendResponseHeaders(204, -1);
|
||||||
} else if (method.equals("DELETE")) {
|
}
|
||||||
|
case "DELETE" -> {
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
store.setBucketPolicy(bucketName, null);
|
store.setBucketPolicy(bucketName, null);
|
||||||
exchange.sendResponseHeaders(204, -1);
|
exchange.sendResponseHeaders(204, -1);
|
||||||
} else S3Request.unsupported("Bucket policy operation");
|
}
|
||||||
return;
|
default -> S3Request.unsupported("Bucket policy operation");
|
||||||
}
|
}
|
||||||
if (query.containsKey("acl") && query.get("acl").isEmpty() &&
|
}
|
||||||
query.keySet().stream().allMatch(java.util.Set.of("acl", "x-id")::contains) &&
|
|
||||||
(!query.containsKey("x-id") ||
|
private void handleAcl(HttpExchange exchange, String hash, String bucketName, String principal) throws IOException {
|
||||||
query.get("x-id").equals(method.equals("GET") ? "GetBucketAcl" : "PutBucketAcl")) &&
|
var current = store.bucket(bucketName);
|
||||||
(method.equals("GET") || method.equals("PUT"))) {
|
boolean get = exchange.getRequestMethod().equals("GET");
|
||||||
var current = store.bucket(bucketName);
|
access.requireBucket(bucketName, principal, get ? Acl.READ_ACP : Acl.WRITE_ACP);
|
||||||
access.requireBucket(bucketName, principal,
|
if (get) {
|
||||||
method.equals("GET") ? Acl.READ_ACP : Acl.WRITE_ACP);
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
if (method.equals("GET")) {
|
S3Xml.sendXml(exchange, 200, Acl.xml(
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
current.ownership() == ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED
|
||||||
S3Xml.sendXml(exchange, 200, Acl.xml(
|
? Map.of() : current.acl(), owner));
|
||||||
current.ownership() == ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED
|
} else {
|
||||||
? Map.of() : current.acl(), owner));
|
if (current.ownership() == ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED)
|
||||||
} else {
|
throw new StoreException(400, "AccessControlListNotSupported", "ACLs are disabled for this bucket");
|
||||||
if (current.ownership() == ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED)
|
Map<String, String> acl = readAcl(exchange, hash);
|
||||||
throw new StoreException(400, "AccessControlListNotSupported", "ACLs are disabled for this bucket");
|
BucketSecurity.checkAcl(current, exchange.getRequestHeaders(), acl, true);
|
||||||
if (exchange.getRequestHeaders().containsKey("x-amz-acl") ||
|
store.setBucketAcl(bucketName, acl);
|
||||||
exchange.getRequestHeaders().keySet().stream().anyMatch(
|
exchange.sendResponseHeaders(200, -1);
|
||||||
name -> name.toLowerCase(Locale.ROOT).startsWith("x-amz-grant-"))) {
|
}
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
}
|
||||||
S3Request.verifyContentMd5(exchange.getRequestHeaders(), new byte[0]);
|
|
||||||
Map<String, String> acl = Acl.fromHeaders(exchange.getRequestHeaders(),
|
private Map<String, String> readAcl(HttpExchange exchange, String hash) throws IOException {
|
||||||
authentication.identities());
|
if (exchange.getRequestHeaders().containsKey("x-amz-acl") ||
|
||||||
BucketSecurity.checkAcl(current, exchange.getRequestHeaders(), acl, true);
|
exchange.getRequestHeaders().keySet().stream().anyMatch(
|
||||||
store.setBucketAcl(bucketName, acl);
|
name -> name.toLowerCase(Locale.ROOT).startsWith("x-amz-grant-"))) {
|
||||||
} else {
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
S3Request.verifyContentMd5(exchange.getRequestHeaders(), new byte[0]);
|
||||||
S3Request.verifyContentMd5(exchange.getRequestHeaders(), body);
|
return Acl.fromHeaders(exchange.getRequestHeaders(), authentication.identities());
|
||||||
Map<String, String> acl = Acl.fromXml(body, owner, authentication.identities());
|
}
|
||||||
BucketSecurity.checkAcl(current, exchange.getRequestHeaders(), acl, true);
|
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
||||||
store.setBucketAcl(bucketName, acl);
|
S3Request.verifyContentMd5(exchange.getRequestHeaders(), body);
|
||||||
}
|
return Acl.fromXml(body, owner, authentication.identities());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleBucketOperation(HttpExchange exchange, String hash,
|
||||||
|
String bucketName, String principal) throws IOException {
|
||||||
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
|
switch (exchange.getRequestMethod()) {
|
||||||
|
case "PUT" -> {
|
||||||
|
access.requireOwner(principal);
|
||||||
|
createBucket(exchange, bucketName);
|
||||||
|
}
|
||||||
|
case "HEAD" -> {
|
||||||
|
access.requireBucket(bucketName, principal, Acl.READ);
|
||||||
exchange.sendResponseHeaders(200, -1);
|
exchange.sendResponseHeaders(200, -1);
|
||||||
}
|
}
|
||||||
return;
|
case "DELETE" -> {
|
||||||
}
|
access.requireOwner(principal);
|
||||||
String bucketOperation = switch (method) {
|
deleteBucket(exchange, bucketName);
|
||||||
case "PUT" -> "CreateBucket";
|
|
||||||
case "HEAD" -> "HeadBucket";
|
|
||||||
case "DELETE" -> "DeleteBucket";
|
|
||||||
default -> null;
|
|
||||||
};
|
|
||||||
if (bucketOperation != null && (query.isEmpty() ||
|
|
||||||
query.size() == 1 && bucketOperation.equals(query.get("x-id")))) {
|
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
|
||||||
switch (method) {
|
|
||||||
case "PUT" -> {
|
|
||||||
access.requireOwner(principal);
|
|
||||||
String requestedOwnership = SigV4.single(exchange.getRequestHeaders(), "x-amz-object-ownership");
|
|
||||||
ObjectStorage.ObjectOwnership ownership = requestedOwnership == null
|
|
||||||
? ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED
|
|
||||||
: BucketSecurity.ownership(requestedOwnership);
|
|
||||||
Map<String, String> acl = Acl.fromHeaders(exchange.getRequestHeaders(),
|
|
||||||
authentication.identities());
|
|
||||||
BucketSecurity.checkAcl(new ObjectStorage.Bucket(bucketName, 0,
|
|
||||||
ObjectStorage.VersioningState.NEVER, Map.of(), ownership,
|
|
||||||
ObjectStorage.PublicAccessBlock.secure(), null),
|
|
||||||
exchange.getRequestHeaders(), acl, false);
|
|
||||||
store.createBucket(bucketName);
|
|
||||||
if (requestedOwnership != null &&
|
|
||||||
ownership != ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED)
|
|
||||||
store.setBucketOwnership(bucketName, ownership);
|
|
||||||
if (!acl.isEmpty()) store.setBucketAcl(bucketName, acl);
|
|
||||||
exchange.getResponseHeaders().set("Location", "/" + bucketName);
|
|
||||||
exchange.sendResponseHeaders(200, -1);
|
|
||||||
}
|
|
||||||
case "HEAD" -> {
|
|
||||||
access.requireBucket(bucketName, principal, Acl.READ);
|
|
||||||
exchange.sendResponseHeaders(200, -1);
|
|
||||||
}
|
|
||||||
case "DELETE" -> {
|
|
||||||
access.requireOwner(principal);
|
|
||||||
if (bucketName.equals(bucket))
|
|
||||||
throw new StoreException(409, "BucketNotEmpty", "The configured default bucket cannot be deleted");
|
|
||||||
synchronized (multipart) {
|
|
||||||
if (!multipart.listUploads(bucketName, "").isEmpty())
|
|
||||||
throw new StoreException(409, "BucketNotEmpty", "Bucket has active uploads");
|
|
||||||
store.deleteBucket(bucketName);
|
|
||||||
}
|
|
||||||
exchange.sendResponseHeaders(204, -1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
store.bucket(bucketName);
|
}
|
||||||
String versioningOperation = method.equals("GET") ? "GetBucketVersioning" :
|
|
||||||
method.equals("PUT") ? "PutBucketVersioning" : null;
|
private void createBucket(HttpExchange exchange, String bucketName) throws IOException {
|
||||||
if (query.containsKey("versioning") && query.get("versioning").isEmpty() &&
|
String requestedOwnership = SigV4.single(exchange.getRequestHeaders(), "x-amz-object-ownership");
|
||||||
(query.size() == 1 || query.size() == 2 && versioningOperation != null &&
|
ObjectStorage.ObjectOwnership ownership = requestedOwnership == null
|
||||||
versioningOperation.equals(query.get("x-id")))) {
|
? ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED
|
||||||
|
: BucketSecurity.ownership(requestedOwnership);
|
||||||
|
Map<String, String> acl = Acl.fromHeaders(exchange.getRequestHeaders(), authentication.identities());
|
||||||
|
BucketSecurity.checkAcl(new ObjectStorage.Bucket(bucketName, 0,
|
||||||
|
ObjectStorage.VersioningState.NEVER, Map.of(), ownership,
|
||||||
|
ObjectStorage.PublicAccessBlock.secure(), null),
|
||||||
|
exchange.getRequestHeaders(), acl, false);
|
||||||
|
store.createBucket(bucketName);
|
||||||
|
if (requestedOwnership != null && ownership != ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED)
|
||||||
|
store.setBucketOwnership(bucketName, ownership);
|
||||||
|
if (!acl.isEmpty()) store.setBucketAcl(bucketName, acl);
|
||||||
|
exchange.getResponseHeaders().set("Location", "/" + bucketName);
|
||||||
|
exchange.sendResponseHeaders(200, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteBucket(HttpExchange exchange, String bucketName) throws IOException {
|
||||||
|
if (bucketName.equals(bucket))
|
||||||
|
throw new StoreException(409, "BucketNotEmpty", "The configured default bucket cannot be deleted");
|
||||||
|
synchronized (multipart) {
|
||||||
|
if (!multipart.listUploads(bucketName, "").isEmpty())
|
||||||
|
throw new StoreException(409, "BucketNotEmpty", "Bucket has active uploads");
|
||||||
|
store.deleteBucket(bucketName);
|
||||||
|
}
|
||||||
|
exchange.sendResponseHeaders(204, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleExistingBucket(HttpExchange exchange, Map<String, String> query, String hash,
|
||||||
|
String bucketName, String principal) throws IOException {
|
||||||
|
String method = exchange.getRequestMethod();
|
||||||
|
if (versioningRequest(query, method)) {
|
||||||
access.requireOwner(principal);
|
access.requireOwner(principal);
|
||||||
handleVersioning(exchange, hash, bucketName);
|
handleVersioning(exchange, hash, bucketName);
|
||||||
return;
|
} else if (method.equals("GET") && query.containsKey("versions") && query.get("versions").isEmpty()) {
|
||||||
}
|
|
||||||
if (method.equals("GET") && query.containsKey("versions") && query.get("versions").isEmpty()) {
|
|
||||||
access.requireOwner(principal);
|
access.requireOwner(principal);
|
||||||
if (!query.keySet().stream().allMatch(java.util.Set.of("versions", "prefix", "key-marker",
|
handleListVersions(exchange, query, hash, bucketName);
|
||||||
"version-id-marker", "max-keys", "encoding-type", "x-id")::contains) ||
|
} else if (method.equals("GET") && query.containsKey("uploads")) {
|
||||||
query.containsKey("x-id") && !"ListObjectVersions".equals(query.get("x-id")))
|
|
||||||
S3Request.unsupported("Version listing option");
|
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
|
||||||
listVersions(exchange, query, bucketName);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (exchange.getRequestMethod().equals("GET") && query.containsKey("uploads")) {
|
|
||||||
access.requireOwner(principal);
|
access.requireOwner(principal);
|
||||||
if (!query.get("uploads").isEmpty() ||
|
handleListUploads(exchange, query, hash, bucketName);
|
||||||
!query.keySet().stream().allMatch(java.util.Set.of("uploads", "prefix", "key-marker",
|
} else {
|
||||||
"upload-id-marker", "max-uploads", "x-id")::contains) ||
|
handleListObjects(exchange, query, hash, bucketName, principal);
|
||||||
(query.containsKey("x-id") && !"ListMultipartUploads".equals(query.get("x-id"))))
|
|
||||||
S3Request.unsupported("Bucket operation");
|
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
|
||||||
multipartHandler.listUploads(exchange, query, bucketName);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean versioningRequest(Map<String, String> query, String method) {
|
||||||
|
String operation = method.equals("GET") ? "GetBucketVersioning" :
|
||||||
|
method.equals("PUT") ? "PutBucketVersioning" : null;
|
||||||
|
return query.containsKey("versioning") && query.get("versioning").isEmpty() &&
|
||||||
|
(query.size() == 1 || query.size() == 2 && operation != null &&
|
||||||
|
operation.equals(query.get("x-id")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleListVersions(HttpExchange exchange, Map<String, String> query,
|
||||||
|
String hash, String bucketName) throws IOException {
|
||||||
|
if (!query.keySet().stream().allMatch(java.util.Set.of("versions", "prefix", "key-marker",
|
||||||
|
"version-id-marker", "max-keys", "encoding-type", "x-id")::contains) ||
|
||||||
|
query.containsKey("x-id") && !"ListObjectVersions".equals(query.get("x-id")))
|
||||||
|
S3Request.unsupported("Version listing option");
|
||||||
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
|
listVersions(exchange, query, bucketName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleListUploads(HttpExchange exchange, Map<String, String> query,
|
||||||
|
String hash, String bucketName) throws IOException {
|
||||||
|
if (!query.get("uploads").isEmpty() ||
|
||||||
|
!query.keySet().stream().allMatch(java.util.Set.of("uploads", "prefix", "key-marker",
|
||||||
|
"upload-id-marker", "max-uploads", "x-id")::contains) ||
|
||||||
|
(query.containsKey("x-id") && !"ListMultipartUploads".equals(query.get("x-id"))))
|
||||||
|
S3Request.unsupported("Bucket operation");
|
||||||
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
|
multipartHandler.listUploads(exchange, query, bucketName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleListObjects(HttpExchange exchange, Map<String, String> query, String hash,
|
||||||
|
String bucketName, String principal) throws IOException {
|
||||||
if (!exchange.getRequestMethod().equals("GET") || !"2".equals(query.get("list-type")) ||
|
if (!exchange.getRequestMethod().equals("GET") || !"2".equals(query.get("list-type")) ||
|
||||||
!query.keySet().stream().allMatch(java.util.Set.of("list-type", "prefix", "delimiter", "max-keys",
|
!query.keySet().stream().allMatch(java.util.Set.of("list-type", "prefix", "delimiter", "max-keys",
|
||||||
"continuation-token", "start-after", "encoding-type", "x-id")::contains) ||
|
"continuation-token", "start-after", "encoding-type", "x-id")::contains) ||
|
||||||
|
|||||||
@@ -229,59 +229,85 @@ public final class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void dispatch(HttpExchange exchange, RequestContext context) throws IOException {
|
private void dispatch(HttpExchange exchange, RequestContext context) throws IOException {
|
||||||
SigV4.Verified verified = anonymousRead(exchange)
|
SigV4.Verified verified = verifyRequest(exchange);
|
||||||
? new SigV4.Verified("UNSIGNED-PAYLOAD", exchange.getRequestURI().getRawQuery(),
|
|
||||||
null, null, null, null, null)
|
|
||||||
: authentication.verifyRequest(exchange.getRequestMethod(),
|
|
||||||
exchange.getRequestURI(), exchange.getRequestHeaders());
|
|
||||||
context.principal = verified.principal();
|
context.principal = verified.principal();
|
||||||
String path = SigV4.decode(exchange.getRequestURI().getRawPath());
|
String path = SigV4.decode(exchange.getRequestURI().getRawPath());
|
||||||
Map<String, String> query = S3Request.query(verified.applicationQuery());
|
Map<String, String> query = S3Request.query(verified.applicationQuery());
|
||||||
if (path.equals(CAPABILITIES_PATH)) {
|
if (path.equals(CAPABILITIES_PATH)) {
|
||||||
access.requireOwner(verified.principal());
|
handleCapabilities(exchange, query, verified);
|
||||||
if (!exchange.getRequestMethod().equals("GET")) S3Request.unsupported("Capability operation");
|
|
||||||
if (!query.isEmpty())
|
|
||||||
throw new StoreException(400, "InvalidArgument", "Capability request has unsupported query parameters");
|
|
||||||
S3Request.requireEmptyBody(exchange, verified.payload());
|
|
||||||
capabilities(exchange);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String hostBucket = virtualHostBucket(exchange);
|
String hostBucket = virtualHostBucket(exchange);
|
||||||
if (path.equals("/") && hostBucket == null) {
|
if (path.equals("/") && hostBucket == null) {
|
||||||
access.requireOwner(verified.principal());
|
handleService(exchange, query, verified);
|
||||||
if (!exchange.getRequestMethod().equals("GET") ||
|
|
||||||
!(query.isEmpty() || query.size() == 1 && "ListBuckets".equals(query.get("x-id"))))
|
|
||||||
S3Request.unsupported("Service operation");
|
|
||||||
S3Request.requireEmptyBody(exchange, verified.payload());
|
|
||||||
bucketHandler.listBuckets(exchange);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int slash = path.indexOf('/', 1);
|
int slash = path.indexOf('/', 1);
|
||||||
String requestedBucket = hostBucket == null ?
|
String requestedBucket = hostBucket == null ?
|
||||||
(slash < 0 ? path.substring(1) : path.substring(1, slash)) : hostBucket;
|
(slash < 0 ? path.substring(1) : path.substring(1, slash)) : hostBucket;
|
||||||
|
boolean bucketRequest = hostBucket != null ? path.equals("/") : slash < 0 || slash == path.length() - 1;
|
||||||
context.bucket = requestedBucket;
|
context.bucket = requestedBucket;
|
||||||
context.operation = (hostBucket != null ? path.equals("/") : slash < 0 ||
|
context.operation = (bucketRequest ? "Bucket " : "Object ") + exchange.getRequestMethod();
|
||||||
slash == path.length() - 1) ? "Bucket " + exchange.getRequestMethod() :
|
|
||||||
"Object " + exchange.getRequestMethod();
|
|
||||||
if (requestedBucket.isEmpty()) throw new StoreException(404, "NoSuchBucket", "Bucket not found");
|
if (requestedBucket.isEmpty()) throw new StoreException(404, "NoSuchBucket", "Bucket not found");
|
||||||
if (hostBucket != null ? path.equals("/") : slash < 0 || slash == path.length() - 1) {
|
if (bucketRequest) {
|
||||||
ManagedCredentials managed = managedScope(verified.principal());
|
dispatchBucket(exchange, query, verified, requestedBucket);
|
||||||
if (managed != null)
|
|
||||||
managed.checkBucket(verified.principal(), requestedBucket, exchange.getRequestMethod(), query);
|
|
||||||
else keyScopes.checkBucket(verified.principal(), requestedBucket, exchange.getRequestMethod(), query);
|
|
||||||
bucketHandler.handleBucket(exchange, query, verified.payload(), requestedBucket, verified.principal());
|
|
||||||
} else {
|
} else {
|
||||||
store.bucket(requestedBucket);
|
store.bucket(requestedBucket);
|
||||||
String key = hostBucket == null ? path.substring(slash + 1) : path.substring(1);
|
String key = hostBucket == null ? path.substring(slash + 1) : path.substring(1);
|
||||||
ManagedCredentials managed = managedScope(verified.principal());
|
dispatchObject(exchange, query, verified, requestedBucket, key);
|
||||||
if (managed != null)
|
|
||||||
managed.checkObject(verified.principal(), requestedBucket, key,
|
|
||||||
exchange.getRequestMethod(), query, exchange.getRequestHeaders());
|
|
||||||
else keyScopes.checkObject(verified.principal(), requestedBucket, key,
|
|
||||||
exchange.getRequestMethod(), query, exchange.getRequestHeaders());
|
|
||||||
objectHandler.handleObject(exchange, key,
|
|
||||||
query, verified, requestedBucket);
|
|
||||||
}
|
}
|
||||||
|
recordBucketActivity(exchange, requestedBucket);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SigV4.Verified verifyRequest(HttpExchange exchange) {
|
||||||
|
if (anonymousRead(exchange))
|
||||||
|
return new SigV4.Verified("UNSIGNED-PAYLOAD", exchange.getRequestURI().getRawQuery(),
|
||||||
|
null, null, null, null, null);
|
||||||
|
return authentication.verifyRequest(exchange.getRequestMethod(),
|
||||||
|
exchange.getRequestURI(), exchange.getRequestHeaders());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleCapabilities(HttpExchange exchange, Map<String, String> query,
|
||||||
|
SigV4.Verified verified) throws IOException {
|
||||||
|
access.requireOwner(verified.principal());
|
||||||
|
if (!exchange.getRequestMethod().equals("GET")) S3Request.unsupported("Capability operation");
|
||||||
|
if (!query.isEmpty())
|
||||||
|
throw new StoreException(400, "InvalidArgument", "Capability request has unsupported query parameters");
|
||||||
|
S3Request.requireEmptyBody(exchange, verified.payload());
|
||||||
|
capabilities(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleService(HttpExchange exchange, Map<String, String> query,
|
||||||
|
SigV4.Verified verified) throws IOException {
|
||||||
|
access.requireOwner(verified.principal());
|
||||||
|
if (!exchange.getRequestMethod().equals("GET") ||
|
||||||
|
!(query.isEmpty() || query.size() == 1 && "ListBuckets".equals(query.get("x-id"))))
|
||||||
|
S3Request.unsupported("Service operation");
|
||||||
|
S3Request.requireEmptyBody(exchange, verified.payload());
|
||||||
|
bucketHandler.listBuckets(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dispatchBucket(HttpExchange exchange, Map<String, String> query,
|
||||||
|
SigV4.Verified verified, String bucket) throws IOException {
|
||||||
|
ManagedCredentials managed = managedScope(verified.principal());
|
||||||
|
if (managed != null)
|
||||||
|
managed.checkBucket(verified.principal(), bucket, exchange.getRequestMethod(), query);
|
||||||
|
else keyScopes.checkBucket(verified.principal(), bucket, exchange.getRequestMethod(), query);
|
||||||
|
bucketHandler.handleBucket(exchange, query, verified.payload(), bucket, verified.principal());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dispatchObject(HttpExchange exchange, Map<String, String> query,
|
||||||
|
SigV4.Verified verified, String bucket, String key) throws IOException {
|
||||||
|
ManagedCredentials managed = managedScope(verified.principal());
|
||||||
|
if (managed != null)
|
||||||
|
managed.checkObject(verified.principal(), bucket, key,
|
||||||
|
exchange.getRequestMethod(), query, exchange.getRequestHeaders());
|
||||||
|
else keyScopes.checkObject(verified.principal(), bucket, key,
|
||||||
|
exchange.getRequestMethod(), query, exchange.getRequestHeaders());
|
||||||
|
objectHandler.handleObject(exchange, key, query, verified, bucket);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recordBucketActivity(HttpExchange exchange, String requestedBucket) {
|
||||||
BucketActivity activity = bucketActivity;
|
BucketActivity activity = bucketActivity;
|
||||||
if (activity != null && exchange.getResponseCode() > 0 && exchange.getResponseCode() < 400)
|
if (activity != null && exchange.getResponseCode() > 0 && exchange.getResponseCode() < 400)
|
||||||
activity.record(requestedBucket, exchange.getRequestMethod());
|
activity.record(requestedBucket, exchange.getRequestMethod());
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cloud.lunarsky.store;
|
package cloud.lunarsky.store;
|
||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
|
import com.sun.net.httpserver.Headers;
|
||||||
import com.sun.net.httpserver.HttpsExchange;
|
import com.sun.net.httpserver.HttpsExchange;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
@@ -32,72 +33,16 @@ final class MultipartHandler {
|
|||||||
String key, SigV4.Verified verified, String bucketName) throws IOException {
|
String key, SigV4.Verified verified, String bucketName) throws IOException {
|
||||||
String hash = verified.payload();
|
String hash = verified.payload();
|
||||||
var headers = exchange.getRequestHeaders();
|
var headers = exchange.getRequestHeaders();
|
||||||
if ((headers.containsKey("content-encoding") && !(method.equals("PUT") && verified.streaming())) ||
|
|
||||||
headers.containsKey("if-none-match"))
|
|
||||||
S3Request.unsupported("Multipart request header");
|
|
||||||
if (headers.containsKey("x-amz-copy-source") || headers.containsKey("x-amz-metadata-directive") ||
|
|
||||||
headers.containsKey("x-amz-tagging-directive"))
|
|
||||||
S3Request.unsupported("Multipart copy request");
|
|
||||||
if (!query.containsKey("uploads") &&
|
|
||||||
(headers.containsKey("x-amz-tagging") || !ObjectAttributes.userMetadata(headers).isEmpty() ||
|
|
||||||
headers.containsKey("x-amz-acl") || headers.keySet().stream().anyMatch(
|
|
||||||
name -> name.toLowerCase(Locale.ROOT).startsWith("x-amz-grant-"))))
|
|
||||||
S3Request.unsupported("Multipart object attributes belong on initiation");
|
|
||||||
boolean initiate = query.containsKey("uploads");
|
boolean initiate = query.containsKey("uploads");
|
||||||
if (!method.equals("PUT") && (headers.containsKey("content-md5") ||
|
validateHeaders(headers, method, verified.streaming(), initiate);
|
||||||
headers.containsKey("x-amz-sdk-checksum-algorithm") ||
|
|
||||||
headers.keySet().stream().anyMatch(name -> {
|
|
||||||
String lower = name.toLowerCase(Locale.ROOT);
|
|
||||||
return lower.startsWith("x-amz-checksum-") &&
|
|
||||||
!(initiate && lower.equals("x-amz-checksum-algorithm"));
|
|
||||||
})))
|
|
||||||
S3Request.unsupported("Multipart checksum header");
|
|
||||||
if (initiate) {
|
if (initiate) {
|
||||||
String algorithm = SigV4.single(headers, "x-amz-checksum-algorithm");
|
initiateUpload(exchange, key, verified, bucketName);
|
||||||
if (algorithm != null && !algorithm.equals("CRC32"))
|
|
||||||
S3Request.unsupported("Multipart checksum algorithm");
|
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
|
||||||
Map<String, String> acl = Acl.fromHeaders(headers, authentication.identities());
|
|
||||||
BucketSecurity.checkAcl(store.bucket(bucketName), headers, acl, false);
|
|
||||||
if (!acl.isEmpty() && !owner.equals(verified.principal()))
|
|
||||||
access.requireBucket(bucketName, verified.principal(), Acl.WRITE_ACP);
|
|
||||||
String id = multipart.create(bucketName, key, S3Request.contentType(headers),
|
|
||||||
ObjectAttributes.userMetadata(headers),
|
|
||||||
ObjectAttributes.tagsHeader(SigV4.single(headers, "x-amz-tagging")), acl);
|
|
||||||
S3Xml.sendXml(exchange, 200, "<InitiateMultipartUploadResult><Bucket>" + S3Xml.xml(bucketName) +
|
|
||||||
"</Bucket><Key>" + S3Xml.xml(key) + "</Key><UploadId>" + id +
|
|
||||||
"</UploadId></InitiateMultipartUploadResult>");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String id = query.get("uploadId");
|
String id = query.get("uploadId");
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "PUT" -> {
|
case "PUT" -> uploadPart(exchange, id, key, query.get("partNumber"), verified, bucketName);
|
||||||
int number;
|
case "POST" -> completeUpload(exchange, id, key, hash, bucketName);
|
||||||
try { number = Integer.parseInt(query.get("partNumber")); }
|
|
||||||
catch (NumberFormatException e) { throw new StoreException(400, "InvalidArgument", "Invalid part number"); }
|
|
||||||
long length = verified.streaming() ? S3Request.decodedLength(headers) : S3Request.contentLength(headers);
|
|
||||||
UploadChecksums checksums = UploadChecksums.from(headers);
|
|
||||||
AwsChunkedInputStream chunked = verified.streaming() ? S3Request.chunkedBody(exchange, verified) : null;
|
|
||||||
String etag = multipart.putPart(id, bucketName, key, number,
|
|
||||||
checksums.verifying(chunked == null ? exchange.getRequestBody() : chunked),
|
|
||||||
length, S3Request.expectedPayloadHash(hash), checksums.sha256());
|
|
||||||
exchange.getResponseHeaders().set("ETag", "\"" + etag + "\"");
|
|
||||||
checksums.response(exchange.getResponseHeaders());
|
|
||||||
if (chunked != null && chunked.trailerValue() != null)
|
|
||||||
exchange.getResponseHeaders().set(SigV4.single(headers, "x-amz-trailer"), chunked.trailerValue());
|
|
||||||
exchange.sendResponseHeaders(200, -1);
|
|
||||||
}
|
|
||||||
case "POST" -> {
|
|
||||||
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
|
||||||
List<MultipartStorage.Part> parts = S3Xml.completedParts(body);
|
|
||||||
var meta = multipart.complete(id, bucketName, key, parts);
|
|
||||||
if (meta.versionId() != null)
|
|
||||||
exchange.getResponseHeaders().set("x-amz-version-id", meta.versionId());
|
|
||||||
S3Xml.sendXml(exchange, 200, "<CompleteMultipartUploadResult><Location>" +
|
|
||||||
S3Xml.xml(location(exchange, bucketName, key)) + "</Location><Bucket>" + S3Xml.xml(bucketName) +
|
|
||||||
"</Bucket><Key>" + S3Xml.xml(key) + "</Key><ETag>"" + meta.etag() +
|
|
||||||
""</ETag></CompleteMultipartUploadResult>");
|
|
||||||
}
|
|
||||||
case "DELETE" -> {
|
case "DELETE" -> {
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
multipart.abort(id, bucketName, key);
|
multipart.abort(id, bucketName, key);
|
||||||
@@ -111,6 +56,86 @@ final class MultipartHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void validateHeaders(Headers headers, String method, boolean streaming, boolean initiate) {
|
||||||
|
if ((headers.containsKey("content-encoding") && !(method.equals("PUT") && streaming)) ||
|
||||||
|
headers.containsKey("if-none-match"))
|
||||||
|
S3Request.unsupported("Multipart request header");
|
||||||
|
if (headers.containsKey("x-amz-copy-source") || headers.containsKey("x-amz-metadata-directive") ||
|
||||||
|
headers.containsKey("x-amz-tagging-directive"))
|
||||||
|
S3Request.unsupported("Multipart copy request");
|
||||||
|
if (!initiate && hasObjectAttributes(headers))
|
||||||
|
S3Request.unsupported("Multipart object attributes belong on initiation");
|
||||||
|
validateChecksumHeaders(headers, method, initiate);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasObjectAttributes(Headers headers) {
|
||||||
|
return headers.containsKey("x-amz-tagging") || !ObjectAttributes.userMetadata(headers).isEmpty() ||
|
||||||
|
headers.containsKey("x-amz-acl") || headers.keySet().stream().anyMatch(
|
||||||
|
name -> name.toLowerCase(Locale.ROOT).startsWith("x-amz-grant-"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateChecksumHeaders(Headers headers, String method, boolean initiate) {
|
||||||
|
if (!method.equals("PUT") && (headers.containsKey("content-md5") ||
|
||||||
|
headers.containsKey("x-amz-sdk-checksum-algorithm") ||
|
||||||
|
headers.keySet().stream().anyMatch(name -> {
|
||||||
|
String lower = name.toLowerCase(Locale.ROOT);
|
||||||
|
return lower.startsWith("x-amz-checksum-") &&
|
||||||
|
!(initiate && lower.equals("x-amz-checksum-algorithm"));
|
||||||
|
})))
|
||||||
|
S3Request.unsupported("Multipart checksum header");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initiateUpload(HttpExchange exchange, String key, SigV4.Verified verified,
|
||||||
|
String bucketName) throws IOException {
|
||||||
|
var headers = exchange.getRequestHeaders();
|
||||||
|
String algorithm = SigV4.single(headers, "x-amz-checksum-algorithm");
|
||||||
|
if (algorithm != null && !algorithm.equals("CRC32"))
|
||||||
|
S3Request.unsupported("Multipart checksum algorithm");
|
||||||
|
S3Request.requireEmptyBody(exchange, verified.payload());
|
||||||
|
Map<String, String> acl = Acl.fromHeaders(headers, authentication.identities());
|
||||||
|
BucketSecurity.checkAcl(store.bucket(bucketName), headers, acl, false);
|
||||||
|
if (!acl.isEmpty() && !owner.equals(verified.principal()))
|
||||||
|
access.requireBucket(bucketName, verified.principal(), Acl.WRITE_ACP);
|
||||||
|
String id = multipart.create(bucketName, key, S3Request.contentType(headers),
|
||||||
|
ObjectAttributes.userMetadata(headers),
|
||||||
|
ObjectAttributes.tagsHeader(SigV4.single(headers, "x-amz-tagging")), acl);
|
||||||
|
S3Xml.sendXml(exchange, 200, "<InitiateMultipartUploadResult><Bucket>" + S3Xml.xml(bucketName) +
|
||||||
|
"</Bucket><Key>" + S3Xml.xml(key) + "</Key><UploadId>" + id +
|
||||||
|
"</UploadId></InitiateMultipartUploadResult>");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void uploadPart(HttpExchange exchange, String id, String key, String partNumber,
|
||||||
|
SigV4.Verified verified, String bucketName) throws IOException {
|
||||||
|
var headers = exchange.getRequestHeaders();
|
||||||
|
int number;
|
||||||
|
try { number = Integer.parseInt(partNumber); }
|
||||||
|
catch (NumberFormatException e) { throw new StoreException(400, "InvalidArgument", "Invalid part number"); }
|
||||||
|
long length = verified.streaming() ? S3Request.decodedLength(headers) : S3Request.contentLength(headers);
|
||||||
|
UploadChecksums checksums = UploadChecksums.from(headers);
|
||||||
|
AwsChunkedInputStream chunked = verified.streaming() ? S3Request.chunkedBody(exchange, verified) : null;
|
||||||
|
String etag = multipart.putPart(id, bucketName, key, number,
|
||||||
|
checksums.verifying(chunked == null ? exchange.getRequestBody() : chunked),
|
||||||
|
length, S3Request.expectedPayloadHash(verified.payload()), checksums.sha256());
|
||||||
|
exchange.getResponseHeaders().set("ETag", "\"" + etag + "\"");
|
||||||
|
checksums.response(exchange.getResponseHeaders());
|
||||||
|
if (chunked != null && chunked.trailerValue() != null)
|
||||||
|
exchange.getResponseHeaders().set(SigV4.single(headers, "x-amz-trailer"), chunked.trailerValue());
|
||||||
|
exchange.sendResponseHeaders(200, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void completeUpload(HttpExchange exchange, String id, String key, String hash,
|
||||||
|
String bucketName) throws IOException {
|
||||||
|
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
||||||
|
List<MultipartStorage.Part> parts = S3Xml.completedParts(body);
|
||||||
|
var meta = multipart.complete(id, bucketName, key, parts);
|
||||||
|
if (meta.versionId() != null)
|
||||||
|
exchange.getResponseHeaders().set("x-amz-version-id", meta.versionId());
|
||||||
|
S3Xml.sendXml(exchange, 200, "<CompleteMultipartUploadResult><Location>" +
|
||||||
|
S3Xml.xml(location(exchange, bucketName, key)) + "</Location><Bucket>" + S3Xml.xml(bucketName) +
|
||||||
|
"</Bucket><Key>" + S3Xml.xml(key) + "</Key><ETag>"" + meta.etag() +
|
||||||
|
""</ETag></CompleteMultipartUploadResult>");
|
||||||
|
}
|
||||||
|
|
||||||
private String location(HttpExchange exchange, String bucket, String key) {
|
private String location(HttpExchange exchange, String bucket, String key) {
|
||||||
String host = SigV4.single(exchange.getRequestHeaders(), "host");
|
String host = SigV4.single(exchange.getRequestHeaders(), "host");
|
||||||
if (host == null) throw new StoreException(400, "InvalidRequest", "Missing Host header");
|
if (host == null) throw new StoreException(400, "InvalidRequest", "Missing Host header");
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package cloud.lunarsky.store;
|
package cloud.lunarsky.store;
|
||||||
|
|
||||||
|
import com.sun.net.httpserver.Headers;
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
@@ -33,57 +35,11 @@ final class ObjectHandler {
|
|||||||
if (key.isEmpty() || key.getBytes(StandardCharsets.UTF_8).length > 1024 || key.indexOf('\0') >= 0)
|
if (key.isEmpty() || key.getBytes(StandardCharsets.UTF_8).length > 1024 || key.indexOf('\0') >= 0)
|
||||||
throw new StoreException(400, "InvalidArgument", "Invalid object key");
|
throw new StoreException(400, "InvalidArgument", "Invalid object key");
|
||||||
String method = exchange.getRequestMethod();
|
String method = exchange.getRequestMethod();
|
||||||
if (query.containsKey("acl") && query.get("acl").isEmpty() &&
|
if (isAclRequest(method, query)) {
|
||||||
query.keySet().stream().allMatch(java.util.Set.of("acl", "versionId", "x-id")::contains) &&
|
handleAcl(exchange, key, hash, principal, bucketName, query.get("versionId"));
|
||||||
(!query.containsKey("x-id") ||
|
|
||||||
query.get("x-id").equals(method.equals("GET") ? "GetObjectAcl" : "PutObjectAcl")) &&
|
|
||||||
(method.equals("GET") || method.equals("PUT"))) {
|
|
||||||
String versionId = query.get("versionId");
|
|
||||||
try (var object = store.open(bucketName, key, versionId)) {
|
|
||||||
var metadata = object.metadata();
|
|
||||||
if (method.equals("GET")) {
|
|
||||||
access.requireObject(bucketName, key, metadata, principal, Acl.READ_ACP,
|
|
||||||
versionId == null ? "s3:GetObjectAcl" : "s3:GetObjectVersionAcl");
|
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
|
||||||
S3Xml.sendXml(exchange, 200, Acl.xml(
|
|
||||||
store.bucket(bucketName).ownership() == ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED
|
|
||||||
? Map.of() : metadata.acl(), owner));
|
|
||||||
} else {
|
|
||||||
if (store.bucket(bucketName).ownership() == ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED)
|
|
||||||
throw new StoreException(400, "AccessControlListNotSupported", "ACLs are disabled for this bucket");
|
|
||||||
if (!owner.equals(principal) &&
|
|
||||||
(metadata.versionId() == null || metadata.versionId().equals("null")))
|
|
||||||
throw new StoreException(403, "AccessDenied", "Access denied");
|
|
||||||
access.requireObject(bucketName, key, metadata, principal, Acl.WRITE_ACP,
|
|
||||||
versionId == null ? "s3:PutObjectAcl" : "s3:PutObjectVersionAcl");
|
|
||||||
Map<String, String> acl;
|
|
||||||
if (exchange.getRequestHeaders().containsKey("x-amz-acl") ||
|
|
||||||
exchange.getRequestHeaders().keySet().stream().anyMatch(
|
|
||||||
name -> name.toLowerCase(Locale.ROOT).startsWith("x-amz-grant-"))) {
|
|
||||||
S3Request.requireEmptyBody(exchange, hash);
|
|
||||||
S3Request.verifyContentMd5(exchange.getRequestHeaders(), new byte[0]);
|
|
||||||
acl = Acl.fromHeaders(exchange.getRequestHeaders(), authentication.identities());
|
|
||||||
} else {
|
|
||||||
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
|
||||||
S3Request.verifyContentMd5(exchange.getRequestHeaders(), body);
|
|
||||||
acl = Acl.fromXml(body, owner, authentication.identities());
|
|
||||||
}
|
|
||||||
BucketSecurity.checkAcl(store.bucket(bucketName), exchange.getRequestHeaders(), acl, true);
|
|
||||||
store.setObjectAcl(bucketName, key, metadata.versionId(), acl);
|
|
||||||
exchange.sendResponseHeaders(200, -1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String taggingOperation = switch (method) {
|
if (isTaggingRequest(method, query)) {
|
||||||
case "GET" -> "GetObjectTagging";
|
|
||||||
case "PUT" -> "PutObjectTagging";
|
|
||||||
case "DELETE" -> "DeleteObjectTagging";
|
|
||||||
default -> null;
|
|
||||||
};
|
|
||||||
if (taggingOperation != null && query.containsKey("tagging") && query.get("tagging").isEmpty() &&
|
|
||||||
query.keySet().stream().allMatch(java.util.Set.of("tagging", "versionId", "x-id")::contains) &&
|
|
||||||
(!query.containsKey("x-id") || taggingOperation.equals(query.get("x-id")))) {
|
|
||||||
S3Request.validateObjectHeaders(exchange.getRequestHeaders());
|
S3Request.validateObjectHeaders(exchange.getRequestHeaders());
|
||||||
access.requireOwner(principal);
|
access.requireOwner(principal);
|
||||||
handleTagging(exchange, method, key, hash, bucketName, query.get("versionId"));
|
handleTagging(exchange, method, key, hash, bucketName, query.get("versionId"));
|
||||||
@@ -91,28 +47,7 @@ final class ObjectHandler {
|
|||||||
}
|
}
|
||||||
boolean multipartRequest = S3Request.multipartRequest(method, query);
|
boolean multipartRequest = S3Request.multipartRequest(method, query);
|
||||||
boolean copy = exchange.getRequestHeaders().containsKey("x-amz-copy-source");
|
boolean copy = exchange.getRequestHeaders().containsKey("x-amz-copy-source");
|
||||||
if (query.containsKey("x-amz-acl")) {
|
validateObjectQuery(exchange, query, multipartRequest, copy);
|
||||||
if (!method.equals("PUT") || copy || !"private".equals(query.get("x-amz-acl")))
|
|
||||||
throw new StoreException(400, "InvalidArgument", "Unsupported ACL query value");
|
|
||||||
if (exchange.getRequestHeaders().containsKey("x-amz-acl"))
|
|
||||||
throw new StoreException(400, "InvalidRequest", "Supply ACL once");
|
|
||||||
}
|
|
||||||
boolean versionRequest = (method.equals("GET") || method.equals("HEAD") || method.equals("DELETE")) &&
|
|
||||||
query.containsKey("versionId") && (query.size() == 1 || query.size() == 2 && query.containsKey("x-id"));
|
|
||||||
if (versionRequest && query.containsKey("x-id") &&
|
|
||||||
!(method.equals("GET") ? "GetObject" : method.equals("HEAD") ? "HeadObject" : "DeleteObject")
|
|
||||||
.equals(query.get("x-id"))) S3Request.unsupported("Version query operation");
|
|
||||||
boolean directUploadQuery = method.equals("PUT") && !copy &&
|
|
||||||
(query.get("x-id") == null || query.get("x-id").equals("PutObject")) &&
|
|
||||||
query.keySet().stream().allMatch(name -> name.equals("x-id") ||
|
|
||||||
name.equals("x-amz-acl") ||
|
|
||||||
name.equals("x-amz-sdk-checksum-algorithm") ||
|
|
||||||
name.startsWith("x-amz-checksum-") && name.equals(name.toLowerCase(Locale.ROOT)));
|
|
||||||
if (!multipartRequest && !versionRequest && !query.isEmpty() && !directUploadQuery && !(query.size() == 1 &&
|
|
||||||
("PutObject".equals(query.get("x-id")) || "CopyObject".equals(query.get("x-id")) ||
|
|
||||||
"GetObject".equals(query.get("x-id")) ||
|
|
||||||
"HeadObject".equals(query.get("x-id")) || "DeleteObject".equals(query.get("x-id")))))
|
|
||||||
S3Request.unsupported("Query operation");
|
|
||||||
S3Request.validateObjectHeaders(exchange.getRequestHeaders());
|
S3Request.validateObjectHeaders(exchange.getRequestHeaders());
|
||||||
if (multipartRequest) {
|
if (multipartRequest) {
|
||||||
if (method.equals("GET")) access.requireOwner(principal);
|
if (method.equals("GET")) access.requireOwner(principal);
|
||||||
@@ -121,7 +56,119 @@ final class ObjectHandler {
|
|||||||
multipartHandler.handleMultipart(exchange, method, query, key, verified, bucketName);
|
multipartHandler.handleMultipart(exchange, method, query, key, verified, bucketName);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!method.equals("PUT") && (copy || exchange.getRequestHeaders().containsKey("content-md5") ||
|
validateUploadHeaders(exchange, copy);
|
||||||
|
if (!method.equals("PUT") || copy) S3Request.requireEmptyBody(exchange, hash);
|
||||||
|
handleObjectMethod(exchange, key, query, verified, bucketName, copy);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isAclRequest(String method, Map<String, String> query) {
|
||||||
|
return query.containsKey("acl") && query.get("acl").isEmpty() &&
|
||||||
|
query.keySet().stream().allMatch(java.util.Set.of("acl", "versionId", "x-id")::contains) &&
|
||||||
|
(!query.containsKey("x-id") ||
|
||||||
|
query.get("x-id").equals(method.equals("GET") ? "GetObjectAcl" : "PutObjectAcl")) &&
|
||||||
|
(method.equals("GET") || method.equals("PUT"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleAcl(HttpExchange exchange, String key, String hash, String principal,
|
||||||
|
String bucketName, String versionId) throws IOException {
|
||||||
|
try (var object = store.open(bucketName, key, versionId)) {
|
||||||
|
var metadata = object.metadata();
|
||||||
|
if (exchange.getRequestMethod().equals("GET")) {
|
||||||
|
access.requireObject(bucketName, key, metadata, principal, Acl.READ_ACP,
|
||||||
|
versionId == null ? "s3:GetObjectAcl" : "s3:GetObjectVersionAcl");
|
||||||
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
|
S3Xml.sendXml(exchange, 200, Acl.xml(
|
||||||
|
store.bucket(bucketName).ownership() == ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED
|
||||||
|
? Map.of() : metadata.acl(), owner));
|
||||||
|
} else {
|
||||||
|
putAcl(exchange, key, hash, principal, bucketName, versionId, metadata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void putAcl(HttpExchange exchange, String key, String hash, String principal,
|
||||||
|
String bucketName, String versionId, ObjectStorage.Metadata metadata) throws IOException {
|
||||||
|
if (store.bucket(bucketName).ownership() == ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED)
|
||||||
|
throw new StoreException(400, "AccessControlListNotSupported", "ACLs are disabled for this bucket");
|
||||||
|
if (!owner.equals(principal) &&
|
||||||
|
(metadata.versionId() == null || metadata.versionId().equals("null")))
|
||||||
|
throw new StoreException(403, "AccessDenied", "Access denied");
|
||||||
|
access.requireObject(bucketName, key, metadata, principal, Acl.WRITE_ACP,
|
||||||
|
versionId == null ? "s3:PutObjectAcl" : "s3:PutObjectVersionAcl");
|
||||||
|
Map<String, String> acl = requestAcl(exchange, hash);
|
||||||
|
BucketSecurity.checkAcl(store.bucket(bucketName), exchange.getRequestHeaders(), acl, true);
|
||||||
|
store.setObjectAcl(bucketName, key, metadata.versionId(), acl);
|
||||||
|
exchange.sendResponseHeaders(200, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> requestAcl(HttpExchange exchange, String hash) throws IOException {
|
||||||
|
var headers = exchange.getRequestHeaders();
|
||||||
|
if (headers.containsKey("x-amz-acl") || headers.keySet().stream().anyMatch(
|
||||||
|
name -> name.toLowerCase(Locale.ROOT).startsWith("x-amz-grant-"))) {
|
||||||
|
S3Request.requireEmptyBody(exchange, hash);
|
||||||
|
S3Request.verifyContentMd5(headers, new byte[0]);
|
||||||
|
return Acl.fromHeaders(headers, authentication.identities());
|
||||||
|
}
|
||||||
|
byte[] body = S3Request.signedBody(exchange, hash, 65536);
|
||||||
|
S3Request.verifyContentMd5(headers, body);
|
||||||
|
return Acl.fromXml(body, owner, authentication.identities());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isTaggingRequest(String method, Map<String, String> query) {
|
||||||
|
String taggingOperation = switch (method) {
|
||||||
|
case "GET" -> "GetObjectTagging";
|
||||||
|
case "PUT" -> "PutObjectTagging";
|
||||||
|
case "DELETE" -> "DeleteObjectTagging";
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
return taggingOperation != null && query.containsKey("tagging") && query.get("tagging").isEmpty() &&
|
||||||
|
query.keySet().stream().allMatch(java.util.Set.of("tagging", "versionId", "x-id")::contains) &&
|
||||||
|
(!query.containsKey("x-id") || taggingOperation.equals(query.get("x-id")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateObjectQuery(HttpExchange exchange, Map<String, String> query,
|
||||||
|
boolean multipartRequest, boolean copy) {
|
||||||
|
String method = exchange.getRequestMethod();
|
||||||
|
if (query.containsKey("x-amz-acl")) {
|
||||||
|
if (!method.equals("PUT") || copy || !"private".equals(query.get("x-amz-acl")))
|
||||||
|
throw new StoreException(400, "InvalidArgument", "Unsupported ACL query value");
|
||||||
|
if (exchange.getRequestHeaders().containsKey("x-amz-acl"))
|
||||||
|
throw new StoreException(400, "InvalidRequest", "Supply ACL once");
|
||||||
|
}
|
||||||
|
boolean versionRequest = isVersionRequest(method, query);
|
||||||
|
boolean directUploadQuery = isDirectUploadQuery(method, query, copy);
|
||||||
|
if (!multipartRequest && !versionRequest && !query.isEmpty() && !directUploadQuery &&
|
||||||
|
!isObjectOperationQuery(query))
|
||||||
|
S3Request.unsupported("Query operation");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isVersionRequest(String method, Map<String, String> query) {
|
||||||
|
boolean versionRequest = (method.equals("GET") || method.equals("HEAD") || method.equals("DELETE")) &&
|
||||||
|
query.containsKey("versionId") && (query.size() == 1 || query.size() == 2 && query.containsKey("x-id"));
|
||||||
|
if (versionRequest && query.containsKey("x-id") &&
|
||||||
|
!(method.equals("GET") ? "GetObject" : method.equals("HEAD") ? "HeadObject" : "DeleteObject")
|
||||||
|
.equals(query.get("x-id"))) S3Request.unsupported("Version query operation");
|
||||||
|
return versionRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isDirectUploadQuery(String method, Map<String, String> query, boolean copy) {
|
||||||
|
return method.equals("PUT") && !copy &&
|
||||||
|
(query.get("x-id") == null || query.get("x-id").equals("PutObject")) &&
|
||||||
|
query.keySet().stream().allMatch(name -> name.equals("x-id") ||
|
||||||
|
name.equals("x-amz-acl") ||
|
||||||
|
name.equals("x-amz-sdk-checksum-algorithm") ||
|
||||||
|
name.startsWith("x-amz-checksum-") && name.equals(name.toLowerCase(Locale.ROOT)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isObjectOperationQuery(Map<String, String> query) {
|
||||||
|
return query.size() == 1 &&
|
||||||
|
("PutObject".equals(query.get("x-id")) || "CopyObject".equals(query.get("x-id")) ||
|
||||||
|
"GetObject".equals(query.get("x-id")) || "HeadObject".equals(query.get("x-id")) ||
|
||||||
|
"DeleteObject".equals(query.get("x-id")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateUploadHeaders(HttpExchange exchange, boolean copy) {
|
||||||
|
if (!exchange.getRequestMethod().equals("PUT") && (copy || exchange.getRequestHeaders().containsKey("content-md5") ||
|
||||||
exchange.getRequestHeaders().containsKey("x-amz-metadata-directive") ||
|
exchange.getRequestHeaders().containsKey("x-amz-metadata-directive") ||
|
||||||
exchange.getRequestHeaders().containsKey("x-amz-tagging") ||
|
exchange.getRequestHeaders().containsKey("x-amz-tagging") ||
|
||||||
exchange.getRequestHeaders().containsKey("x-amz-tagging-directive") ||
|
exchange.getRequestHeaders().containsKey("x-amz-tagging-directive") ||
|
||||||
@@ -130,8 +177,12 @@ final class ObjectHandler {
|
|||||||
name.toLowerCase(Locale.ROOT).startsWith("x-amz-checksum-") &&
|
name.toLowerCase(Locale.ROOT).startsWith("x-amz-checksum-") &&
|
||||||
!name.equalsIgnoreCase("x-amz-checksum-mode"))))
|
!name.equalsIgnoreCase("x-amz-checksum-mode"))))
|
||||||
S3Request.unsupported("Object upload header");
|
S3Request.unsupported("Object upload header");
|
||||||
if (!method.equals("PUT") || copy) S3Request.requireEmptyBody(exchange, hash);
|
}
|
||||||
switch (method) {
|
|
||||||
|
private void handleObjectMethod(HttpExchange exchange, String key, Map<String, String> query,
|
||||||
|
SigV4.Verified verified, String bucketName, boolean copy) throws IOException {
|
||||||
|
String principal = verified.principal();
|
||||||
|
switch (exchange.getRequestMethod()) {
|
||||||
case "PUT" -> {
|
case "PUT" -> {
|
||||||
access.requireWrite(bucketName, key, principal, "s3:PutObject");
|
access.requireWrite(bucketName, key, principal, "s3:PutObject");
|
||||||
if (copy) copyObject(exchange, key, bucketName, principal);
|
if (copy) copyObject(exchange, key, bucketName, principal);
|
||||||
@@ -152,34 +203,19 @@ final class ObjectHandler {
|
|||||||
var headers = exchange.getRequestHeaders();
|
var headers = exchange.getRequestHeaders();
|
||||||
String length = SigV4.single(headers, "content-length"), condition = SigV4.single(headers, "if-none-match");
|
String length = SigV4.single(headers, "content-length"), condition = SigV4.single(headers, "if-none-match");
|
||||||
if (condition != null && !condition.equals("*")) S3Request.unsupported("Write condition");
|
if (condition != null && !condition.equals("*")) S3Request.unsupported("Write condition");
|
||||||
long bytes;
|
long bytes = contentLength(length);
|
||||||
try { bytes = length == null ? -1 : Long.parseLong(length); }
|
validatePutHeaders(headers, verified.streaming());
|
||||||
catch (NumberFormatException e) { throw new StoreException(400, "InvalidArgument", "Invalid Content-Length"); }
|
|
||||||
if (!verified.streaming() && headers.containsKey("content-encoding")) S3Request.unsupported("Encoded payload");
|
|
||||||
if (headers.containsKey("x-amz-metadata-directive")) S3Request.unsupported("Copy metadata directive");
|
|
||||||
if (headers.containsKey("x-amz-tagging-directive")) S3Request.unsupported("Copy tagging directive");
|
|
||||||
UploadChecksums checksums = S3Request.uploadChecksums(headers, query);
|
UploadChecksums checksums = S3Request.uploadChecksums(headers, query);
|
||||||
AwsChunkedInputStream chunked = verified.streaming() ? S3Request.chunkedBody(exchange, verified) : null;
|
AwsChunkedInputStream chunked = verified.streaming() ? S3Request.chunkedBody(exchange, verified) : null;
|
||||||
if (chunked != null) bytes = S3Request.decodedLength(headers);
|
bytes = uploadLength(headers, chunked, bytes);
|
||||||
else if (headers.containsKey("x-amz-decoded-content-length") || headers.containsKey("x-amz-trailer"))
|
|
||||||
S3Request.unsupported("Chunked upload header");
|
|
||||||
String type = S3Request.contentType(headers);
|
String type = S3Request.contentType(headers);
|
||||||
Map<String, String> metadata = ObjectAttributes.userMetadata(headers);
|
Map<String, String> metadata = ObjectAttributes.userMetadata(headers);
|
||||||
Map<String, String> tags = ObjectAttributes.tagsHeader(SigV4.single(headers, "x-amz-tagging"));
|
Map<String, String> tags = ObjectAttributes.tagsHeader(SigV4.single(headers, "x-amz-tagging"));
|
||||||
Map<String, String> acl = Acl.fromHeaders(headers, authentication.identities());
|
Map<String, String> acl = uploadAcl(headers, bucketName, verified.principal());
|
||||||
BucketSecurity.checkAcl(store.bucket(bucketName), headers, acl, false);
|
|
||||||
if (!acl.isEmpty() && !owner.equals(verified.principal()))
|
|
||||||
access.requireBucket(bucketName, verified.principal(), Acl.WRITE_ACP);
|
|
||||||
ObjectStorage.Metadata data = store.put(bucketName, key,
|
ObjectStorage.Metadata data = store.put(bucketName, key,
|
||||||
checksums.verifying(chunked == null ? exchange.getRequestBody() : chunked),
|
checksums.verifying(chunked == null ? exchange.getRequestBody() : chunked),
|
||||||
bytes, S3Request.expectedPayloadHash(verified.payload()), checksums.sha256(), condition != null, type,
|
bytes, S3Request.expectedPayloadHash(verified.payload()), checksums.sha256(), condition != null, type,
|
||||||
metadata, tags, () -> {
|
metadata, tags, () -> uploadChecksumMetadata(headers, chunked, checksums), acl);
|
||||||
if (chunked == null || chunked.trailerValue() == null) return checksums.metadata();
|
|
||||||
String trailer = SigV4.single(headers, "x-amz-trailer");
|
|
||||||
if (!checksums.metadata().isEmpty())
|
|
||||||
throw new StoreException(400, "InvalidRequest", "Supply one checksum algorithm");
|
|
||||||
return Map.of(trailer, chunked.trailerValue());
|
|
||||||
}, acl);
|
|
||||||
exchange.getResponseHeaders().set("ETag", "\"" + data.etag() + "\"");
|
exchange.getResponseHeaders().set("ETag", "\"" + data.etag() + "\"");
|
||||||
if (data.versionId() != null) exchange.getResponseHeaders().set("x-amz-version-id", data.versionId());
|
if (data.versionId() != null) exchange.getResponseHeaders().set("x-amz-version-id", data.versionId());
|
||||||
exchange.getResponseHeaders().set("x-amz-checksum-sha256", Base64.getEncoder().encodeToString(data.sha256()));
|
exchange.getResponseHeaders().set("x-amz-checksum-sha256", Base64.getEncoder().encodeToString(data.sha256()));
|
||||||
@@ -187,13 +223,79 @@ final class ObjectHandler {
|
|||||||
exchange.sendResponseHeaders(200, -1);
|
exchange.sendResponseHeaders(200, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static long contentLength(String length) {
|
||||||
|
try { return length == null ? -1 : Long.parseLong(length); }
|
||||||
|
catch (NumberFormatException e) { throw new StoreException(400, "InvalidArgument", "Invalid Content-Length"); }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validatePutHeaders(Headers headers, boolean streaming) {
|
||||||
|
if (!streaming && headers.containsKey("content-encoding")) S3Request.unsupported("Encoded payload");
|
||||||
|
if (headers.containsKey("x-amz-metadata-directive")) S3Request.unsupported("Copy metadata directive");
|
||||||
|
if (headers.containsKey("x-amz-tagging-directive")) S3Request.unsupported("Copy tagging directive");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long uploadLength(Headers headers, AwsChunkedInputStream chunked, long bytes) {
|
||||||
|
if (chunked != null) return S3Request.decodedLength(headers);
|
||||||
|
if (headers.containsKey("x-amz-decoded-content-length") || headers.containsKey("x-amz-trailer"))
|
||||||
|
S3Request.unsupported("Chunked upload header");
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> uploadAcl(Headers headers, String bucketName, String principal) throws IOException {
|
||||||
|
Map<String, String> acl = Acl.fromHeaders(headers, authentication.identities());
|
||||||
|
BucketSecurity.checkAcl(store.bucket(bucketName), headers, acl, false);
|
||||||
|
if (!acl.isEmpty() && !owner.equals(principal))
|
||||||
|
access.requireBucket(bucketName, principal, Acl.WRITE_ACP);
|
||||||
|
return acl;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, String> uploadChecksumMetadata(Headers headers, AwsChunkedInputStream chunked,
|
||||||
|
UploadChecksums checksums) {
|
||||||
|
if (chunked == null || chunked.trailerValue() == null) return checksums.metadata();
|
||||||
|
String trailer = SigV4.single(headers, "x-amz-trailer");
|
||||||
|
if (!checksums.metadata().isEmpty())
|
||||||
|
throw new StoreException(400, "InvalidRequest", "Supply one checksum algorithm");
|
||||||
|
return Map.of(trailer, chunked.trailerValue());
|
||||||
|
}
|
||||||
|
|
||||||
private void copyObject(HttpExchange exchange, String key, String bucketName,
|
private void copyObject(HttpExchange exchange, String key, String bucketName,
|
||||||
String principal) throws IOException {
|
String principal) throws IOException {
|
||||||
var headers = exchange.getRequestHeaders();
|
var headers = exchange.getRequestHeaders();
|
||||||
|
validateCopyHeaders(headers);
|
||||||
|
CopySource source = copySource(headers);
|
||||||
|
CopyDirectives directives = copyDirectives(headers);
|
||||||
|
try (var object = store.open(source.bucket(), source.key(), source.versionId())) {
|
||||||
|
var sourceMetadata = object.metadata();
|
||||||
|
access.requireObject(source.bucket(), source.key(), sourceMetadata, principal, Acl.READ,
|
||||||
|
source.versionId() == null ? "s3:GetObject" : "s3:GetObjectVersion");
|
||||||
|
if (sourceMetadata.versionId() != null)
|
||||||
|
exchange.getResponseHeaders().set("x-amz-copy-source-version-id", sourceMetadata.versionId());
|
||||||
|
String type = "REPLACE".equals(directives.metadata())
|
||||||
|
? S3Request.contentType(headers) : sourceMetadata.contentType();
|
||||||
|
Map<String, String> metadata = "REPLACE".equals(directives.metadata())
|
||||||
|
? ObjectAttributes.userMetadata(headers) : sourceMetadata.userMetadata();
|
||||||
|
Map<String, String> tags = "REPLACE".equals(directives.tagging())
|
||||||
|
? ObjectAttributes.tagsHeader(SigV4.single(headers, "x-amz-tagging")) : sourceMetadata.tags();
|
||||||
|
Map<String, String> acl = uploadAcl(headers, bucketName, principal);
|
||||||
|
var copied = store.put(bucketName, key, object.stream(), sourceMetadata.length(),
|
||||||
|
SigV4.hex(sourceMetadata.sha256()), null, false, type, metadata, tags,
|
||||||
|
sourceMetadata::checksums, acl);
|
||||||
|
if (copied.versionId() != null)
|
||||||
|
exchange.getResponseHeaders().set("x-amz-version-id", copied.versionId());
|
||||||
|
S3Xml.sendXml(exchange, 200, "<CopyObjectResult><LastModified>" +
|
||||||
|
Instant.ofEpochMilli(copied.modified()) + "</LastModified><ETag>"" +
|
||||||
|
copied.etag() + ""</ETag></CopyObjectResult>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateCopyHeaders(Headers headers) {
|
||||||
if (headers.containsKey("content-encoding") || headers.containsKey("content-md5") ||
|
if (headers.containsKey("content-encoding") || headers.containsKey("content-md5") ||
|
||||||
headers.containsKey("if-none-match") || headers.containsKey("x-amz-sdk-checksum-algorithm") ||
|
headers.containsKey("if-none-match") || headers.containsKey("x-amz-sdk-checksum-algorithm") ||
|
||||||
headers.keySet().stream().anyMatch(name -> name.toLowerCase(Locale.ROOT).startsWith("x-amz-checksum-")))
|
headers.keySet().stream().anyMatch(name -> name.toLowerCase(Locale.ROOT).startsWith("x-amz-checksum-")))
|
||||||
S3Request.unsupported("Copy request header");
|
S3Request.unsupported("Copy request header");
|
||||||
|
}
|
||||||
|
|
||||||
|
private CopySource copySource(Headers headers) throws IOException {
|
||||||
String source = SigV4.single(headers, "x-amz-copy-source");
|
String source = SigV4.single(headers, "x-amz-copy-source");
|
||||||
if (source == null) throw new StoreException(400, "InvalidArgument", "Missing copy source");
|
if (source == null) throw new StoreException(400, "InvalidArgument", "Missing copy source");
|
||||||
if (source.startsWith("/")) source = source.substring(1);
|
if (source.startsWith("/")) source = source.substring(1);
|
||||||
@@ -215,6 +317,10 @@ final class ObjectHandler {
|
|||||||
if (sourceKey.isEmpty() || sourceKey.getBytes(StandardCharsets.UTF_8).length > 1024 ||
|
if (sourceKey.isEmpty() || sourceKey.getBytes(StandardCharsets.UTF_8).length > 1024 ||
|
||||||
sourceKey.indexOf('\0') >= 0)
|
sourceKey.indexOf('\0') >= 0)
|
||||||
throw new StoreException(400, "InvalidArgument", "Invalid copy source key");
|
throw new StoreException(400, "InvalidArgument", "Invalid copy source key");
|
||||||
|
return new CopySource(sourceBucket, sourceKey, sourceVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static CopyDirectives copyDirectives(Headers headers) {
|
||||||
String directive = SigV4.single(headers, "x-amz-metadata-directive");
|
String directive = SigV4.single(headers, "x-amz-metadata-directive");
|
||||||
if (directive != null && !directive.equals("COPY") && !directive.equals("REPLACE"))
|
if (directive != null && !directive.equals("COPY") && !directive.equals("REPLACE"))
|
||||||
throw new StoreException(400, "InvalidArgument", "Invalid metadata directive");
|
throw new StoreException(400, "InvalidArgument", "Invalid metadata directive");
|
||||||
@@ -225,30 +331,7 @@ final class ObjectHandler {
|
|||||||
S3Request.unsupported("Tagging header requires REPLACE tagging directive");
|
S3Request.unsupported("Tagging header requires REPLACE tagging directive");
|
||||||
if (!"REPLACE".equals(directive) && !ObjectAttributes.userMetadata(headers).isEmpty())
|
if (!"REPLACE".equals(directive) && !ObjectAttributes.userMetadata(headers).isEmpty())
|
||||||
S3Request.unsupported("User metadata requires REPLACE metadata directive");
|
S3Request.unsupported("User metadata requires REPLACE metadata directive");
|
||||||
try (var object = store.open(sourceBucket, sourceKey, sourceVersion)) {
|
return new CopyDirectives(directive, tagDirective);
|
||||||
var sourceMetadata = object.metadata();
|
|
||||||
access.requireObject(sourceBucket, sourceKey, sourceMetadata, principal, Acl.READ,
|
|
||||||
sourceVersion == null ? "s3:GetObject" : "s3:GetObjectVersion");
|
|
||||||
if (sourceMetadata.versionId() != null)
|
|
||||||
exchange.getResponseHeaders().set("x-amz-copy-source-version-id", sourceMetadata.versionId());
|
|
||||||
String type = "REPLACE".equals(directive) ? S3Request.contentType(headers) : sourceMetadata.contentType();
|
|
||||||
Map<String, String> metadata = "REPLACE".equals(directive)
|
|
||||||
? ObjectAttributes.userMetadata(headers) : sourceMetadata.userMetadata();
|
|
||||||
Map<String, String> tags = "REPLACE".equals(tagDirective)
|
|
||||||
? ObjectAttributes.tagsHeader(SigV4.single(headers, "x-amz-tagging")) : sourceMetadata.tags();
|
|
||||||
Map<String, String> acl = Acl.fromHeaders(headers, authentication.identities());
|
|
||||||
BucketSecurity.checkAcl(store.bucket(bucketName), headers, acl, false);
|
|
||||||
if (!acl.isEmpty() && !owner.equals(principal))
|
|
||||||
access.requireBucket(bucketName, principal, Acl.WRITE_ACP);
|
|
||||||
var copied = store.put(bucketName, key, object.stream(), sourceMetadata.length(),
|
|
||||||
SigV4.hex(sourceMetadata.sha256()), null, false, type, metadata, tags,
|
|
||||||
sourceMetadata::checksums, acl);
|
|
||||||
if (copied.versionId() != null)
|
|
||||||
exchange.getResponseHeaders().set("x-amz-version-id", copied.versionId());
|
|
||||||
S3Xml.sendXml(exchange, 200, "<CopyObjectResult><LastModified>" +
|
|
||||||
Instant.ofEpochMilli(copied.modified()) + "</LastModified><ETag>"" +
|
|
||||||
copied.etag() + ""</ETag></CopyObjectResult>");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteObject(HttpExchange exchange, String key, String bucketName,
|
private void deleteObject(HttpExchange exchange, String key, String bucketName,
|
||||||
@@ -297,57 +380,80 @@ final class ObjectHandler {
|
|||||||
private void readObject(HttpExchange exchange, String key, String bucketName,
|
private void readObject(HttpExchange exchange, String key, String bucketName,
|
||||||
String versionId, String principal) throws IOException {
|
String versionId, String principal) throws IOException {
|
||||||
var headers = exchange.getRequestHeaders();
|
var headers = exchange.getRequestHeaders();
|
||||||
String checksumMode = SigV4.single(headers, "x-amz-checksum-mode");
|
String checksumMode = validateReadHeaders(headers);
|
||||||
if (checksumMode != null && !checksumMode.equals("ENABLED"))
|
|
||||||
throw new StoreException(400, "InvalidArgument", "Invalid checksum mode");
|
|
||||||
if (headers.containsKey("if-match") || headers.containsKey("if-modified-since") ||
|
|
||||||
headers.containsKey("if-unmodified-since")) S3Request.unsupported("Conditional read");
|
|
||||||
try (var object = store.open(bucketName, key, versionId)) {
|
try (var object = store.open(bucketName, key, versionId)) {
|
||||||
var meta = object.metadata();
|
var meta = object.metadata();
|
||||||
access.requireObject(bucketName, key, meta, principal, Acl.READ,
|
access.requireObject(bucketName, key, meta, principal, Acl.READ,
|
||||||
versionId == null ? "s3:GetObject" : "s3:GetObjectVersion");
|
versionId == null ? "s3:GetObject" : "s3:GetObjectVersion");
|
||||||
String etag = "\"" + meta.etag() + "\"";
|
String etag = "\"" + meta.etag() + "\"";
|
||||||
String noneMatch = SigV4.single(headers, "if-none-match");
|
String noneMatch = SigV4.single(headers, "if-none-match");
|
||||||
if (noneMatch != null && (noneMatch.equals("*") ||
|
if (matchesEtag(noneMatch, etag)) {
|
||||||
java.util.Arrays.stream(noneMatch.split(",")).map(String::trim).anyMatch(etag::equals))) {
|
|
||||||
exchange.getResponseHeaders().set("ETag", etag);
|
exchange.getResponseHeaders().set("ETag", etag);
|
||||||
exchange.sendResponseHeaders(304, -1);
|
exchange.sendResponseHeaders(304, -1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
S3Request.Range range;
|
S3Request.Range range = readRange(exchange, meta.length());
|
||||||
try { range = S3Request.range(SigV4.single(headers, "range"), meta.length()); }
|
setReadHeaders(exchange.getResponseHeaders(), meta, range, etag, checksumMode);
|
||||||
catch (StoreException error) {
|
sendReadBody(exchange, object.stream(), range);
|
||||||
if (error.status == 416) exchange.getResponseHeaders().set("Content-Range", "bytes */" + meta.length());
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
var response = exchange.getResponseHeaders();
|
|
||||||
response.set("Content-Type", meta.contentType());
|
|
||||||
response.set("Content-Length", Long.toString(range.length()));
|
|
||||||
response.set("Accept-Ranges", "bytes");
|
|
||||||
response.set("ETag", etag);
|
|
||||||
if (meta.versionId() != null) response.set("x-amz-version-id", meta.versionId());
|
|
||||||
response.set("Last-Modified", DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC)
|
|
||||||
.format(Instant.ofEpochMilli(meta.modified())));
|
|
||||||
meta.userMetadata().forEach((name, value) -> response.set("x-amz-meta-" + name, value));
|
|
||||||
if (checksumMode != null)
|
|
||||||
meta.checksums().forEach(response::set);
|
|
||||||
if (!meta.tags().isEmpty()) response.set("x-amz-tagging-count", Integer.toString(meta.tags().size()));
|
|
||||||
if (range.partial()) response.set("Content-Range", "bytes " + range.start() + "-" + range.end() + "/" + meta.length());
|
|
||||||
int status = range.partial() ? 206 : 200;
|
|
||||||
if (exchange.getRequestMethod().equals("HEAD") || range.length() == 0)
|
|
||||||
exchange.sendResponseHeaders(status, -1);
|
|
||||||
else {
|
|
||||||
object.stream().skipNBytes(range.start());
|
|
||||||
exchange.sendResponseHeaders(status, range.length());
|
|
||||||
byte[] buffer = new byte[65536];
|
|
||||||
long left = range.length();
|
|
||||||
while (left > 0) {
|
|
||||||
int n = object.stream().read(buffer, 0, (int) Math.min(buffer.length, left));
|
|
||||||
if (n < 0) throw new IOException("Object body ended before its recorded length");
|
|
||||||
exchange.getResponseBody().write(buffer, 0, n);
|
|
||||||
left -= n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String validateReadHeaders(Headers headers) {
|
||||||
|
String checksumMode = SigV4.single(headers, "x-amz-checksum-mode");
|
||||||
|
if (checksumMode != null && !checksumMode.equals("ENABLED"))
|
||||||
|
throw new StoreException(400, "InvalidArgument", "Invalid checksum mode");
|
||||||
|
if (headers.containsKey("if-match") || headers.containsKey("if-modified-since") ||
|
||||||
|
headers.containsKey("if-unmodified-since")) S3Request.unsupported("Conditional read");
|
||||||
|
return checksumMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean matchesEtag(String noneMatch, String etag) {
|
||||||
|
return noneMatch != null && (noneMatch.equals("*") ||
|
||||||
|
java.util.Arrays.stream(noneMatch.split(",")).map(String::trim).anyMatch(etag::equals));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static S3Request.Range readRange(HttpExchange exchange, long length) {
|
||||||
|
try { return S3Request.range(SigV4.single(exchange.getRequestHeaders(), "range"), length); }
|
||||||
|
catch (StoreException error) {
|
||||||
|
if (error.status == 416) exchange.getResponseHeaders().set("Content-Range", "bytes */" + length);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setReadHeaders(Headers response, ObjectStorage.Metadata meta, S3Request.Range range,
|
||||||
|
String etag, String checksumMode) {
|
||||||
|
response.set("Content-Type", meta.contentType());
|
||||||
|
response.set("Content-Length", Long.toString(range.length()));
|
||||||
|
response.set("Accept-Ranges", "bytes");
|
||||||
|
response.set("ETag", etag);
|
||||||
|
if (meta.versionId() != null) response.set("x-amz-version-id", meta.versionId());
|
||||||
|
response.set("Last-Modified", DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC)
|
||||||
|
.format(Instant.ofEpochMilli(meta.modified())));
|
||||||
|
meta.userMetadata().forEach((name, value) -> response.set("x-amz-meta-" + name, value));
|
||||||
|
if (checksumMode != null)
|
||||||
|
meta.checksums().forEach(response::set);
|
||||||
|
if (!meta.tags().isEmpty()) response.set("x-amz-tagging-count", Integer.toString(meta.tags().size()));
|
||||||
|
if (range.partial()) response.set("Content-Range", "bytes " + range.start() + "-" + range.end() + "/" + meta.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sendReadBody(HttpExchange exchange, InputStream input, S3Request.Range range) throws IOException {
|
||||||
|
int status = range.partial() ? 206 : 200;
|
||||||
|
if (exchange.getRequestMethod().equals("HEAD") || range.length() == 0) {
|
||||||
|
exchange.sendResponseHeaders(status, -1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
input.skipNBytes(range.start());
|
||||||
|
exchange.sendResponseHeaders(status, range.length());
|
||||||
|
byte[] buffer = new byte[65536];
|
||||||
|
long left = range.length();
|
||||||
|
while (left > 0) {
|
||||||
|
int n = input.read(buffer, 0, (int) Math.min(buffer.length, left));
|
||||||
|
if (n < 0) throw new IOException("Object body ended before its recorded length");
|
||||||
|
exchange.getResponseBody().write(buffer, 0, n);
|
||||||
|
left -= n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private record CopySource(String bucket, String key, String versionId) {}
|
||||||
|
private record CopyDirectives(String metadata, String tagging) {}
|
||||||
}
|
}
|
||||||
@@ -74,22 +74,33 @@ final class S3Request {
|
|||||||
|
|
||||||
static boolean multipartRequest(String method, Map<String, String> query) {
|
static boolean multipartRequest(String method, Map<String, String> query) {
|
||||||
if (query.containsKey("uploads"))
|
if (query.containsKey("uploads"))
|
||||||
return method.equals("POST") && query.get("uploads").isEmpty() &&
|
return multipartInitiationRequest(method, query);
|
||||||
query.keySet().stream().allMatch(java.util.Set.of("uploads", "x-id")::contains) &&
|
|
||||||
(!query.containsKey("x-id") || query.get("x-id").equals("CreateMultipartUpload"));
|
|
||||||
if (!query.containsKey("uploadId") ||
|
if (!query.containsKey("uploadId") ||
|
||||||
!query.keySet().stream().allMatch(java.util.Set.of("uploadId", "partNumber",
|
!query.keySet().stream().allMatch(java.util.Set.of("uploadId", "partNumber",
|
||||||
"part-number-marker", "max-parts", "x-id")::contains))
|
"part-number-marker", "max-parts", "x-id")::contains))
|
||||||
return false;
|
return false;
|
||||||
String xId = query.get("x-id");
|
String xId = query.get("x-id");
|
||||||
if (method.equals("PUT")) return query.containsKey("partNumber") &&
|
if (method.equals("PUT")) return multipartPartRequest(query, xId);
|
||||||
!query.containsKey("part-number-marker") && !query.containsKey("max-parts") &&
|
|
||||||
(xId == null || xId.equals("UploadPart"));
|
|
||||||
if (query.containsKey("partNumber")) return false;
|
if (query.containsKey("partNumber")) return false;
|
||||||
if (method.equals("GET")) return xId == null || xId.equals("ListParts");
|
if (method.equals("GET")) return matchesOperationId(xId, "ListParts");
|
||||||
if (query.containsKey("part-number-marker") || query.containsKey("max-parts")) return false;
|
if (query.containsKey("part-number-marker") || query.containsKey("max-parts")) return false;
|
||||||
return (method.equals("POST") && (xId == null || xId.equals("CompleteMultipartUpload"))) ||
|
return (method.equals("POST") && matchesOperationId(xId, "CompleteMultipartUpload")) ||
|
||||||
(method.equals("DELETE") && (xId == null || xId.equals("AbortMultipartUpload")));
|
(method.equals("DELETE") && matchesOperationId(xId, "AbortMultipartUpload"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean multipartInitiationRequest(String method, Map<String, String> query) {
|
||||||
|
return method.equals("POST") && query.get("uploads").isEmpty() &&
|
||||||
|
query.keySet().stream().allMatch(java.util.Set.of("uploads", "x-id")::contains) &&
|
||||||
|
(!query.containsKey("x-id") || query.get("x-id").equals("CreateMultipartUpload"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean multipartPartRequest(Map<String, String> query, String xId) {
|
||||||
|
return query.containsKey("partNumber") && !query.containsKey("part-number-marker") &&
|
||||||
|
!query.containsKey("max-parts") && matchesOperationId(xId, "UploadPart");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean matchesOperationId(String xId, String expected) {
|
||||||
|
return xId == null || xId.equals(expected);
|
||||||
}
|
}
|
||||||
static int boundedNumber(String text, int minimum, int maximum, int defaultValue) {
|
static int boundedNumber(String text, int minimum, int maximum, int defaultValue) {
|
||||||
if (text == null) return defaultValue;
|
if (text == null) return defaultValue;
|
||||||
|
|||||||
@@ -422,6 +422,35 @@ public final class HttpTest {
|
|||||||
HttpResponse.BodyHandlers.ofByteArray()));
|
HttpResponse.BodyHandlers.ofByteArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void testServiceRouting(HttpClient client, String base) throws Exception {
|
||||||
|
for (String path : new String[]{"/", "/?x-id=ListBuckets"}) {
|
||||||
|
URI uri = URI.create(base + path);
|
||||||
|
status(200, client.send(signedUri(uri, "GET", new byte[0], Map.of()),
|
||||||
|
HttpResponse.BodyHandlers.ofByteArray()));
|
||||||
|
status(403, client.send(signedUriAs(uri, "GET", new byte[0], Map.of(),
|
||||||
|
SECONDARY, SECONDARY_SECRET), HttpResponse.BodyHandlers.ofByteArray()));
|
||||||
|
status(403, client.send(HttpRequest.newBuilder(uri).GET().build(),
|
||||||
|
HttpResponse.BodyHandlers.ofByteArray()));
|
||||||
|
}
|
||||||
|
for (String path : new String[]{"/?extra=1", "/?x-id=GetObject", "/?x-id=ListBuckets&extra=1"}) {
|
||||||
|
URI uri = URI.create(base + path);
|
||||||
|
status(501, client.send(signedUri(uri, "GET", new byte[0], Map.of()),
|
||||||
|
HttpResponse.BodyHandlers.ofByteArray()));
|
||||||
|
status(403, client.send(signedUriAs(uri, "GET", new byte[0], Map.of(),
|
||||||
|
SECONDARY, SECONDARY_SECRET), HttpResponse.BodyHandlers.ofByteArray()));
|
||||||
|
}
|
||||||
|
for (String path : new String[]{"/", "/_objectstore/capabilities"}) {
|
||||||
|
URI uri = URI.create(base + path);
|
||||||
|
status(501, client.send(signedUri(uri, "POST", new byte[0], Map.of()),
|
||||||
|
HttpResponse.BodyHandlers.ofByteArray()));
|
||||||
|
status(403, client.send(signedUriAs(uri, "POST", new byte[0], Map.of(),
|
||||||
|
SECONDARY, SECONDARY_SECRET), HttpResponse.BodyHandlers.ofByteArray()));
|
||||||
|
}
|
||||||
|
for (String path : new String[]{"/objects", "/objects/"})
|
||||||
|
status(200, client.send(signedUri(URI.create(base + path), "HEAD", new byte[0], Map.of()),
|
||||||
|
HttpResponse.BodyHandlers.ofByteArray()));
|
||||||
|
}
|
||||||
|
|
||||||
private static void status(int expected, HttpResponse<?> response) {
|
private static void status(int expected, HttpResponse<?> response) {
|
||||||
if (response.statusCode() != expected) {
|
if (response.statusCode() != expected) {
|
||||||
throw new AssertionError("Expected HTTP " + expected + ", got " + response.statusCode()
|
throw new AssertionError("Expected HTTP " + expected + ", got " + response.statusCode()
|
||||||
@@ -988,6 +1017,7 @@ public final class HttpTest {
|
|||||||
String base = "http://127.0.0.1:" + server.getAddress().getPort();
|
String base = "http://127.0.0.1:" + server.getAddress().getPort();
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
testCapabilities(client, base);
|
testCapabilities(client, base);
|
||||||
|
testServiceRouting(client, base);
|
||||||
testPresigned(client, base);
|
testPresigned(client, base);
|
||||||
testStreaming(client, base);
|
testStreaming(client, base);
|
||||||
testStreamingTrailer(client, base);
|
testStreamingTrailer(client, base);
|
||||||
|
|||||||
Reference in new issue
Block a user