Release ObjectStore 0.1.0
This commit is contained in:
1 parent
0cf5c11202
commit
35bd1d5703
34 files changed
+1273
-87
No files matched your search
@@ -19,6 +19,9 @@ public final class FeatureTest {
|
||||
private static final String LIMITED = "LIMITEDACCESS1234";
|
||||
private static final String OWNER_SECRET = "test-secret-key-that-is-at-least-32-characters";
|
||||
private static final String LIMITED_SECRET = "limited-secret-key-that-is-at-least-32-characters";
|
||||
private static final String SESSION = "SESSIONACCESS1234";
|
||||
private static final String SESSION_SECRET = "session-secret-key-that-is-at-least-32-characters";
|
||||
private static final String SESSION_TOKEN = "session-token-that-is-at-least-32-characters";
|
||||
|
||||
private static void status(int expected, HttpResponse<?> response) {
|
||||
if (response.statusCode() != expected)
|
||||
@@ -27,10 +30,30 @@ public final class FeatureTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Path root = Files.createTempDirectory("objectstore-feature-test-");
|
||||
Path sessionFile = root.resolve("sessions");
|
||||
Files.writeString(sessionFile, SESSION + "\t" + SESSION_SECRET + "\t" + SESSION_TOKEN +
|
||||
"\t" + java.time.Instant.now().plusSeconds(3600).getEpochSecond() + "\n");
|
||||
var sessions = SessionCredentials.fromEnvironment(
|
||||
Map.of("S3_SESSION_CREDENTIALS_FILE", sessionFile.toString()), Set.of(OWNER, LIMITED));
|
||||
if (!sessions.containsKey(SESSION)) throw new AssertionError("Session credentials not loaded");
|
||||
Path identityDir = Files.createDirectory(root.resolve("identity-policies"));
|
||||
Files.writeString(identityDir.resolve(LIMITED + ".json"),
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[" +
|
||||
statement("Deny", null, "s3:PutObject", "arn:aws:s3:::photos/users/one/blocked") + "," +
|
||||
statement("Allow", null, "s3:GetObject", "arn:aws:s3:::photos/users/one/self_readable") + "," +
|
||||
statement("Allow", null, "s3:PutObject", "arn:aws:s3:::secure/users/one/*") + "," +
|
||||
statement("Allow", null, "s3:GetObject", "arn:aws:s3:::secure/users/one/*") + "]}");
|
||||
Files.writeString(identityDir.resolve(SESSION + ".json"), policy("Allow", null,
|
||||
"s3:GetObject", "arn:aws:s3:::photos/users/one/allowed"));
|
||||
var identityPolicies = IdentityPolicies.fromEnvironment(
|
||||
Map.of("S3_IDENTITY_POLICIES_DIR", identityDir.toString()), OWNER,
|
||||
Set.of(OWNER, LIMITED, SESSION));
|
||||
Path policy = root.resolve("scopes");
|
||||
Files.writeString(policy, LIMITED + "\tphotos\tusers/one/\tread,write,list\n");
|
||||
Files.writeString(policy, LIMITED + "\tphotos\tusers/one/\tread,write,list\n" +
|
||||
LIMITED + "\tsecure\tusers/one/\tread,write\n" +
|
||||
SESSION + "\tphotos\tusers/one/\tread\n");
|
||||
KeyScopes scopes = KeyScopes.fromEnvironment(Map.of("S3_KEY_SCOPES_FILE", policy.toString()),
|
||||
OWNER, Set.of(OWNER, LIMITED));
|
||||
OWNER, Set.of(OWNER, LIMITED, SESSION));
|
||||
scopes.require(LIMITED, "write", "photos", "users/one/image.jpg");
|
||||
scopes.checkBucket(LIMITED, "photos", "GET", Map.of("list-type", "2", "prefix", "users/one/"));
|
||||
denied(() -> scopes.require(LIMITED, "read", "photos", "users/two/image.jpg"));
|
||||
@@ -47,9 +70,11 @@ public final class FeatureTest {
|
||||
DiskStore store = new DiskStore(root.resolve("data"), 1024, 4096);
|
||||
try {
|
||||
Main app = new Main(store, new MultipartStore(store),
|
||||
new SigV4(Map.of(OWNER, OWNER_SECRET, LIMITED, LIMITED_SECRET), OWNER,
|
||||
new SigV4(Map.of(OWNER, OWNER_SECRET, LIMITED, LIMITED_SECRET), sessions, OWNER,
|
||||
"us-east-1", Clock.systemUTC()),
|
||||
"photos", ClientLimits.disabled(), 16, "", cors, scopes, metrics);
|
||||
"photos", ClientLimits.disabled(), 16, "", cors, scopes, metrics, identityPolicies);
|
||||
store.setBucketOwnership("photos", ObjectStorage.ObjectOwnership.OBJECT_WRITER);
|
||||
store.setPublicAccessBlock("photos", ObjectStorage.PublicAccessBlock.open());
|
||||
server.setExecutor(executor);
|
||||
server.createContext("/", app::handle);
|
||||
server.start();
|
||||
@@ -83,9 +108,21 @@ public final class FeatureTest {
|
||||
status(200, client.send(HttpTest.signedUri(URI.create(base + "/photos?acl"),
|
||||
"PUT", new byte[0], Map.of("x-amz-grant-full-control", "id=\"" + LIMITED + "\"")),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(405, client.send(HttpTest.signedUri(URI.create(base + "/photos?acl"),
|
||||
"PUT", new byte[0], Map.of("x-amz-grant-read", "emailAddress=\"example@example.test\"")),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
byte[] content = "allowed".getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
status(200, client.send(HttpTest.signedUriAs(URI.create(base + "/photos/users/one/allowed"),
|
||||
"PUT", content, Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
URI ownReadable = URI.create(base + "/photos/users/one/self_readable");
|
||||
status(200, client.send(HttpTest.signedUriAs(ownReadable,
|
||||
"PUT", content, Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(200, client.send(HttpTest.signedUriAs(ownReadable,
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(URI.create(base + "/photos/users/one/allowed"),
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(URI.create(base + "/photos/users/one/blocked"),
|
||||
"PUT", content, Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(URI.create(base + "/photos/users/two/denied"),
|
||||
"PUT", content, Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(200, client.send(HttpTest.signedUriAs(URI.create(base + "/photos?list-type=2&prefix=users%2Fone%2F"),
|
||||
@@ -94,6 +131,107 @@ public final class FeatureTest {
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(URI.create(base + "/photos/users/one/allowed?acl"),
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(200, client.send(HttpTest.signedUri(URI.create(base + "/photos/users/one/allowed?acl"),
|
||||
"PUT", new byte[0], Map.of("x-amz-acl", "public-read")),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(200, client.send(HttpRequest.newBuilder(
|
||||
URI.create(base + "/photos/users/one/allowed")).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
store.setPublicAccessBlock("photos", ObjectStorage.PublicAccessBlock.secure());
|
||||
status(403, client.send(HttpRequest.newBuilder(
|
||||
URI.create(base + "/photos/users/one/allowed")).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUri(URI.create(base + "/photos/users/one/public"),
|
||||
"PUT", content, Map.of("x-amz-acl", "public-read")),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
store.createBucket("secure");
|
||||
var secure = store.bucket("secure");
|
||||
if (secure.ownership() != ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED ||
|
||||
!secure.publicAccessBlock().blockPublicAcls() ||
|
||||
!secure.publicAccessBlock().ignorePublicAcls() ||
|
||||
!secure.publicAccessBlock().blockPublicPolicy() ||
|
||||
!secure.publicAccessBlock().restrictPublicBuckets())
|
||||
throw new AssertionError("New bucket did not default to private access");
|
||||
URI scopedObject = URI.create(base + "/secure/users/one/photo.jpg");
|
||||
status(200, client.send(HttpTest.signedUriAs(scopedObject,
|
||||
"PUT", content, Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(200, client.send(HttpTest.signedUriAs(scopedObject,
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(URI.create(base + "/secure/users/two/photo.jpg"),
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(400, client.send(HttpTest.signedUri(URI.create(base + "/secure/item"),
|
||||
"PUT", content, Map.of("x-amz-acl", "public-read")),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
URI ownership = URI.create(base + "/photos?ownershipControls");
|
||||
byte[] enforced = ("<OwnershipControls><Rule><ObjectOwnership>BucketOwnerEnforced" +
|
||||
"</ObjectOwnership></Rule></OwnershipControls>")
|
||||
.getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
status(200, client.send(HttpTest.signedUri(ownership, "PUT", enforced, Map.of()),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(URI.create(base + "/photos/users/one/allowed"),
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
URI bucketPolicy = URI.create(base + "/photos?policy");
|
||||
String allow = policy("Allow", LIMITED, "s3:GetObject", "arn:aws:s3:::photos/users/one/*");
|
||||
status(204, client.send(HttpTest.signedUri(bucketPolicy, "PUT", allow.getBytes(), Map.of()),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(200, client.send(HttpTest.signedUriAs(URI.create(base + "/photos/users/one/allowed"),
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
String deny = "{\"Version\":\"2012-10-17\",\"Statement\":[" +
|
||||
statement("Allow", LIMITED, "s3:GetObject", "arn:aws:s3:::photos/users/one/*") + "," +
|
||||
statement("Deny", "*", "s3:GetObject", "arn:aws:s3:::photos/users/one/allowed") + "]}";
|
||||
status(204, client.send(HttpTest.signedUri(bucketPolicy, "PUT", deny.getBytes(), Map.of()),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(URI.create(base + "/photos/users/one/allowed"),
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUri(URI.create(base + "/photos/users/one/allowed"),
|
||||
"GET", new byte[0], Map.of()), HttpResponse.BodyHandlers.ofString()));
|
||||
String publicPolicy = policy("Allow", "*", "s3:GetObject", "arn:aws:s3:::photos/users/one/*");
|
||||
status(403, client.send(HttpTest.signedUri(bucketPolicy, "PUT", publicPolicy.getBytes(), Map.of()),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
byte[] block = ("<PublicAccessBlockConfiguration><BlockPublicAcls>true</BlockPublicAcls>" +
|
||||
"<IgnorePublicAcls>true</IgnorePublicAcls><BlockPublicPolicy>false</BlockPublicPolicy>" +
|
||||
"<RestrictPublicBuckets>true</RestrictPublicBuckets></PublicAccessBlockConfiguration>").getBytes();
|
||||
status(200, client.send(HttpTest.signedUri(URI.create(base + "/photos?publicAccessBlock"),
|
||||
"PUT", block, Map.of()), HttpResponse.BodyHandlers.ofString()));
|
||||
status(204, client.send(HttpTest.signedUri(bucketPolicy, "PUT", publicPolicy.getBytes(), Map.of()),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpRequest.newBuilder(
|
||||
URI.create(base + "/photos/users/one/allowed")).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(200, client.send(HttpTest.signedUri(bucketPolicy, "GET", new byte[0], Map.of()),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
String sessionPolicy = policy("Allow", LIMITED, "s3:GetObject",
|
||||
"arn:aws:s3:::photos/users/one/*");
|
||||
status(204, client.send(HttpTest.signedUri(bucketPolicy, "PUT", sessionPolicy.getBytes(), Map.of()),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
URI sessionObject = URI.create(base + "/photos/users/one/allowed");
|
||||
status(200, client.send(HttpTest.signedUriAs(sessionObject, "GET", new byte[0],
|
||||
Map.of("x-amz-security-token", SESSION_TOKEN), SESSION, SESSION_SECRET),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(sessionObject, "GET", new byte[0],
|
||||
Map.of(), SESSION, SESSION_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(sessionObject, "GET", new byte[0],
|
||||
Map.of("x-amz-security-token", SESSION_TOKEN + "wrong"), SESSION, SESSION_SECRET),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(sessionObject, "PUT", content,
|
||||
Map.of("x-amz-security-token", SESSION_TOKEN), SESSION, SESSION_SECRET),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(200, client.send(HttpRequest.newBuilder(HttpTest.presignedUriAs(sessionObject, "GET", 60,
|
||||
SESSION, SESSION_SECRET, SESSION_TOKEN)).GET().build(), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpRequest.newBuilder(HttpTest.presignedUriAs(sessionObject, "GET", 60,
|
||||
SESSION, SESSION_SECRET, null)).GET().build(), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(sessionObject, "GET", new byte[0],
|
||||
Map.of("x-amz-security-token", SESSION_TOKEN), OWNER, OWNER_SECRET),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
var expired = new SigV4(Map.of(OWNER, OWNER_SECRET), Map.of(SESSION,
|
||||
new SessionCredentials.Session(SESSION_SECRET, SESSION_TOKEN,
|
||||
java.time.Instant.now().minusSeconds(1))), OWNER, "us-east-1", Clock.systemUTC());
|
||||
var signed = HttpTest.signedUriAs(sessionObject, "GET", new byte[0],
|
||||
Map.of("x-amz-security-token", SESSION_TOKEN), SESSION, SESSION_SECRET);
|
||||
var signedHeaders = new com.sun.net.httpserver.Headers();
|
||||
signed.headers().map().forEach(signedHeaders::put);
|
||||
signedHeaders.set("host", sessionObject.getAuthority());
|
||||
denied(() -> expired.verifyRequest("GET", sessionObject, signedHeaders));
|
||||
} finally {
|
||||
server.stop(0);
|
||||
executor.close();
|
||||
@@ -102,7 +240,18 @@ public final class FeatureTest {
|
||||
for (Path path : files.sorted(Comparator.reverseOrder()).toList()) Files.delete(path);
|
||||
}
|
||||
}
|
||||
System.out.println("Feature tests passed: scoped keys, CORS, metrics");
|
||||
System.out.println("Feature tests passed: scoped keys, CORS, metrics, bucket policy and public access");
|
||||
}
|
||||
|
||||
private static String policy(String effect, String principal, String action, String resource) {
|
||||
return "{\"Version\":\"2012-10-17\",\"Statement\":[" +
|
||||
statement(effect, principal, action, resource) + "]}";
|
||||
}
|
||||
|
||||
private static String statement(String effect, String principal, String action, String resource) {
|
||||
return "{\"Effect\":\"" + effect + "\"," +
|
||||
(principal == null ? "" : "\"Principal\":\"" + principal + "\",") +
|
||||
"\"Action\":\"" + action + "\",\"Resource\":\"" + resource + "\"}";
|
||||
}
|
||||
|
||||
private static com.sun.net.httpserver.Headers headers(String name, String value) {
|
||||
|
||||
@@ -71,17 +71,35 @@ public final class HttpTest {
|
||||
}
|
||||
|
||||
private static URI presignedUri(URI uri, String method, int expires) {
|
||||
return presignedUriAs(uri, method, expires, ACCESS, SECRET, null);
|
||||
}
|
||||
|
||||
static URI presignedUriAs(URI uri, String method, int expires, String access, String secret,
|
||||
String token) {
|
||||
return presignedUriAs(uri, method, expires, access, secret, token, false);
|
||||
}
|
||||
|
||||
private static URI presignedUriAs(URI uri, String method, int expires, String access, String secret,
|
||||
String token, boolean sdkContentHash) {
|
||||
return presignedUriAs(uri, method, expires, access, secret, token, sdkContentHash, "");
|
||||
}
|
||||
|
||||
private static URI presignedUriAs(URI uri, String method, int expires, String access, String secret,
|
||||
String token, boolean sdkContentHash, String applicationQuery) {
|
||||
String date = DATE.format(Instant.now());
|
||||
String scope = date.substring(0, 8) + "/" + REGION + "/s3/aws4_request";
|
||||
String query = "X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=" +
|
||||
SigV4.encode(ACCESS + "/" + scope, false) + "&X-Amz-Date=" + date +
|
||||
SigV4.encode(access + "/" + scope, false) + "&X-Amz-Date=" + date +
|
||||
"&X-Amz-Expires=" + expires + "&X-Amz-SignedHeaders=host";
|
||||
if (token != null) query += "&X-Amz-Security-Token=" + SigV4.encode(token, false);
|
||||
if (sdkContentHash) query += "&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD";
|
||||
if (!applicationQuery.isEmpty()) query += "&" + applicationQuery;
|
||||
String canonical = method + "\n" + uri.getRawPath() + "\n" +
|
||||
SigV4.canonicalQuery(query) + "\nhost:" + uri.getAuthority() +
|
||||
"\n\nhost\nUNSIGNED-PAYLOAD";
|
||||
String toSign = "AWS4-HMAC-SHA256\n" + date + "\n" + scope + "\n" +
|
||||
SigV4.hex(SigV4.hash(canonical.getBytes(StandardCharsets.UTF_8)));
|
||||
String signature = SigV4.hex(SigV4.hmac(SigV4.signingKey(SECRET,
|
||||
String signature = SigV4.hex(SigV4.hmac(SigV4.signingKey(secret,
|
||||
date.substring(0, 8), REGION), toSign));
|
||||
return URI.create(uri + "?" + query + "&X-Amz-Signature=" + signature);
|
||||
}
|
||||
@@ -96,6 +114,27 @@ public final class HttpTest {
|
||||
HttpResponse.BodyHandlers.ofByteArray());
|
||||
status(200, read);
|
||||
if (!java.util.Arrays.equals(body, read.body())) throw new AssertionError("Presigned object mismatch");
|
||||
URI sdkObject = URI.create(base + "/objects/sdk-v3-presigned");
|
||||
URI sdkPut = presignedUriAs(sdkObject, "PUT", 60, ACCESS, SECRET, null, true);
|
||||
status(200, client.send(HttpRequest.newBuilder(sdkPut)
|
||||
.PUT(HttpRequest.BodyPublishers.ofByteArray(body)).build(), HttpResponse.BodyHandlers.ofByteArray()));
|
||||
URI sdkGet = presignedUriAs(sdkObject, "GET", 60, ACCESS, SECRET, null, true);
|
||||
status(200, client.send(HttpRequest.newBuilder(sdkGet).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofByteArray()));
|
||||
status(403, client.send(HttpRequest.newBuilder(URI.create(sdkGet.toString().replace(
|
||||
"X-Amz-Content-Sha256=UNSIGNED-PAYLOAD", "X-Amz-Content-Sha256=changed")))
|
||||
.GET().build(), HttpResponse.BodyHandlers.ofByteArray()));
|
||||
status(204, client.send(HttpRequest.newBuilder(presignedUriAs(sdkObject, "DELETE", 60,
|
||||
ACCESS, SECRET, null, true)).DELETE().build(), HttpResponse.BodyHandlers.ofByteArray()));
|
||||
URI checksumObject = URI.create(base + "/objects/sdk-v3-checksum");
|
||||
String checksumQuery = "x-amz-checksum-crc32=" +
|
||||
SigV4.encode(encodedChecksum("CRC32", body), false) +
|
||||
"&x-amz-sdk-checksum-algorithm=CRC32&x-id=PutObject";
|
||||
status(200, client.send(HttpRequest.newBuilder(presignedUriAs(checksumObject, "PUT", 60,
|
||||
ACCESS, SECRET, null, true, checksumQuery))
|
||||
.PUT(HttpRequest.BodyPublishers.ofByteArray(body)).build(), HttpResponse.BodyHandlers.ofByteArray()));
|
||||
status(204, client.send(HttpRequest.newBuilder(presignedUri(checksumObject, "DELETE", 60))
|
||||
.DELETE().build(), HttpResponse.BodyHandlers.ofByteArray()));
|
||||
URI tampered = URI.create(presignedUri(object, "GET", 60).toString().replace("presigned-test", "different"));
|
||||
status(403, client.send(HttpRequest.newBuilder(tampered).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofByteArray()));
|
||||
@@ -104,6 +143,7 @@ public final class HttpTest {
|
||||
}
|
||||
|
||||
private static void testAcl(HttpClient client, String base) throws Exception {
|
||||
enableAcl(client, URI.create(base + "/objects"));
|
||||
byte[] body = "private object".getBytes(StandardCharsets.UTF_8);
|
||||
URI object = URI.create(base + "/objects/acl-test");
|
||||
URI objectAcl = URI.create(object + "?acl");
|
||||
@@ -166,6 +206,7 @@ public final class HttpTest {
|
||||
URI historyBucket = URI.create(base + "/acl-history");
|
||||
status(200, client.send(signedUri(historyBucket, "PUT", new byte[0], Map.of()),
|
||||
HttpResponse.BodyHandlers.ofByteArray()));
|
||||
enableAcl(client, historyBucket);
|
||||
byte[] versioning = "<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>"
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
status(200, client.send(signedUri(URI.create(historyBucket + "?versioning"),
|
||||
@@ -197,6 +238,19 @@ public final class HttpTest {
|
||||
HttpResponse.BodyHandlers.ofByteArray()));
|
||||
}
|
||||
|
||||
private static void enableAcl(HttpClient client, URI bucket) throws Exception {
|
||||
byte[] ownership = ("<OwnershipControls><Rule><ObjectOwnership>ObjectWriter</ObjectOwnership>" +
|
||||
"</Rule></OwnershipControls>").getBytes(StandardCharsets.UTF_8);
|
||||
status(200, client.send(signedUri(URI.create(bucket + "?ownershipControls"),
|
||||
"PUT", ownership, Map.of()), HttpResponse.BodyHandlers.ofByteArray()));
|
||||
byte[] publicAccess = ("<PublicAccessBlockConfiguration><BlockPublicAcls>false</BlockPublicAcls>" +
|
||||
"<IgnorePublicAcls>false</IgnorePublicAcls><BlockPublicPolicy>false</BlockPublicPolicy>" +
|
||||
"<RestrictPublicBuckets>false</RestrictPublicBuckets></PublicAccessBlockConfiguration>")
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
status(200, client.send(signedUri(URI.create(bucket + "?publicAccessBlock"),
|
||||
"PUT", publicAccess, Map.of()), HttpResponse.BodyHandlers.ofByteArray()));
|
||||
}
|
||||
|
||||
private static void testStreaming(HttpClient client, String base) throws Exception {
|
||||
URI object = URI.create(base + "/objects/streaming-test");
|
||||
byte[] body = "verified streaming payload".getBytes(StandardCharsets.UTF_8);
|
||||
@@ -413,7 +467,8 @@ public final class HttpTest {
|
||||
"PUT", body, Map.of("content-type", "text/plain")), HttpResponse.BodyHandlers.ofByteArray()));
|
||||
String header = "/objects/" + SigV4.encode(source, true);
|
||||
var result = client.send(signedUri(URI.create(base + "/objects/" + target), "PUT",
|
||||
new byte[0], Map.of("x-amz-copy-source", header)), HttpResponse.BodyHandlers.ofString());
|
||||
new byte[0], Map.of("x-amz-copy-source", header,
|
||||
"content-type", "application/x-sdk-copy")), HttpResponse.BodyHandlers.ofString());
|
||||
if (result.statusCode() != 200 || !result.body().contains("<CopyObjectResult>") ||
|
||||
!result.body().contains("<ETag>""))
|
||||
throw new AssertionError("CopyObject failed: " + result.body());
|
||||
@@ -536,18 +591,32 @@ public final class HttpTest {
|
||||
}
|
||||
|
||||
private static void testMultipart(HttpClient client, String base) throws Exception {
|
||||
URI endpoint = ObjectStoreServer.publicEndpoint(Map.of("S3_PUBLIC_ENDPOINT", "https://s3.example.test/"));
|
||||
if (!"https://s3.example.test/objects/folder/a%20b.mp4".equals(
|
||||
MultipartHandler.objectLocation(endpoint, "127.0.0.1:9000", "http",
|
||||
"/objects/folder/a%20b.mp4", "objects", "folder/a b.mp4")))
|
||||
throw new AssertionError("Configured multipart location is incorrect");
|
||||
try {
|
||||
ObjectStoreServer.publicEndpoint(Map.of("S3_PUBLIC_ENDPOINT", "https://s3.example.test/subpath"));
|
||||
throw new AssertionError("Public endpoint with a path was accepted");
|
||||
} catch (IllegalArgumentException expected) { }
|
||||
String movie = "folder/video.mp4";
|
||||
URI initiate = URI.create(base + "/objects/" + movie + "?uploads=");
|
||||
var created = client.send(signedUri(initiate, "POST", new byte[0],
|
||||
Map.of("content-type", "video/mp4")), HttpResponse.BodyHandlers.ofString());
|
||||
Map.of("content-type", "video/mp4", "x-amz-checksum-algorithm", "CRC32")),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
if (created.statusCode() != 200) throw new AssertionError("Multipart initiation failed: " + created.body());
|
||||
String upload = created.body().split("<UploadId>")[1].split("</UploadId>")[0];
|
||||
byte[] first = "hello ".getBytes(StandardCharsets.UTF_8);
|
||||
byte[] second = "world".getBytes(StandardCharsets.UTF_8);
|
||||
var partOne = client.send(signedUri(URI.create(base + "/objects/" + movie +
|
||||
"?partNumber=1&uploadId=" + upload), "PUT", first, Map.of()), HttpResponse.BodyHandlers.ofByteArray());
|
||||
"?partNumber=1&uploadId=" + upload), "PUT", first,
|
||||
Map.of("x-amz-checksum-crc32", encodedChecksum("CRC32", first),
|
||||
"x-amz-sdk-checksum-algorithm", "CRC32")), HttpResponse.BodyHandlers.ofByteArray());
|
||||
var partTwo = client.send(signedUri(URI.create(base + "/objects/" + movie +
|
||||
"?partNumber=2&uploadId=" + upload), "PUT", second, Map.of()), HttpResponse.BodyHandlers.ofByteArray());
|
||||
"?partNumber=2&uploadId=" + upload), "PUT", second,
|
||||
Map.of("x-amz-checksum-crc32", encodedChecksum("CRC32", second),
|
||||
"x-amz-sdk-checksum-algorithm", "CRC32")), HttpResponse.BodyHandlers.ofByteArray());
|
||||
status(200, partOne);
|
||||
status(200, partTwo);
|
||||
var rejectedPart = client.send(signedUri(URI.create(base + "/objects/" + movie +
|
||||
@@ -567,11 +636,20 @@ public final class HttpTest {
|
||||
throw new AssertionError("Multipart upload listing failed: " + uploads.body());
|
||||
String completion = "<CompleteMultipartUpload><Part><PartNumber>1</PartNumber><ETag>" +
|
||||
partOne.headers().firstValue("etag").orElseThrow() +
|
||||
"</ETag></Part><Part><PartNumber>2</PartNumber><ETag>" +
|
||||
"</ETag><ChecksumCRC32>" + encodedChecksum("CRC32", first) +
|
||||
"</ChecksumCRC32></Part><Part><PartNumber>2</PartNumber><ETag>" +
|
||||
partTwo.headers().firstValue("etag").orElseThrow() +
|
||||
"</ETag></Part></CompleteMultipartUpload>";
|
||||
status(200, client.send(signedUri(URI.create(base + "/objects/" + movie + "?uploadId=" + upload),
|
||||
"POST", completion.getBytes(StandardCharsets.UTF_8), Map.of()), HttpResponse.BodyHandlers.ofByteArray()));
|
||||
"</ETag><ChecksumCRC32>" + encodedChecksum("CRC32", second) +
|
||||
"</ChecksumCRC32></Part></CompleteMultipartUpload>";
|
||||
String mismatched = completion.replace(encodedChecksum("CRC32", first), "AAAAAA==");
|
||||
status(400, client.send(signedUri(URI.create(base + "/objects/" + movie + "?uploadId=" + upload),
|
||||
"POST", mismatched.getBytes(StandardCharsets.UTF_8), Map.of()),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
var completed = client.send(signedUri(URI.create(base + "/objects/" + movie + "?uploadId=" + upload),
|
||||
"POST", completion.getBytes(StandardCharsets.UTF_8), Map.of()), HttpResponse.BodyHandlers.ofString());
|
||||
status(200, completed);
|
||||
if (!completed.body().contains("<Location>" + base + "/objects/" + movie + "</Location>"))
|
||||
throw new AssertionError("Multipart completion omitted the object location: " + completed.body());
|
||||
var assembled = client.send(signed(base, "GET", movie, new byte[0]), HttpResponse.BodyHandlers.ofByteArray());
|
||||
status(200, assembled);
|
||||
if (!"hello world".equals(new String(assembled.body(), StandardCharsets.UTF_8)) ||
|
||||
|
||||
@@ -295,6 +295,40 @@ public final class StoreTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static void testBucketAccessUpgrade(Path root) throws Exception {
|
||||
Path data = Files.createDirectory(root.resolve("bucket-access-upgrade"));
|
||||
try (var output = new DataOutputStream(Files.newOutputStream(data.resolve("buckets.bin")))) {
|
||||
output.writeInt(0x4c534245);
|
||||
output.writeInt(1);
|
||||
output.writeUTF("legacy-bucket");
|
||||
output.writeLong(System.currentTimeMillis());
|
||||
output.writeByte(0);
|
||||
output.writeShort(0);
|
||||
}
|
||||
try (var store = new DiskStore(data, 8, 16)) {
|
||||
var legacy = store.bucket("legacy-bucket");
|
||||
if (legacy.ownership() != ObjectStorage.ObjectOwnership.OBJECT_WRITER ||
|
||||
legacy.publicAccessBlock().blockPublicAcls())
|
||||
throw new AssertionError("Existing bucket access changed during upgrade");
|
||||
store.createBucket("new-bucket");
|
||||
if (store.bucket("new-bucket").ownership() !=
|
||||
ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED ||
|
||||
!store.bucket("new-bucket").publicAccessBlock().blockPublicPolicy())
|
||||
throw new AssertionError("New bucket did not use secure defaults");
|
||||
store.setBucketPolicy("legacy-bucket", "{\"Version\":\"2012-10-17\"," +
|
||||
"\"Statement\":{\"Effect\":\"Deny\",\"Principal\":\"*\"," +
|
||||
"\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::legacy-bucket/*\"}}");
|
||||
}
|
||||
try (var store = new DiskStore(data, 8, 16)) {
|
||||
if (store.bucket("legacy-bucket").ownership() !=
|
||||
ObjectStorage.ObjectOwnership.OBJECT_WRITER ||
|
||||
store.bucket("legacy-bucket").policy() == null ||
|
||||
store.bucket("new-bucket").ownership() !=
|
||||
ObjectStorage.ObjectOwnership.BUCKET_OWNER_ENFORCED)
|
||||
throw new AssertionError("Bucket access settings were not durable");
|
||||
}
|
||||
}
|
||||
|
||||
private static void testAclPersistence(Path root) throws Exception {
|
||||
Path data = root.resolve("acl");
|
||||
Map<String, String> grant = Map.of("SECONDARYKEY1234", Integer.toString(Acl.READ));
|
||||
@@ -423,6 +457,7 @@ public final class StoreTest {
|
||||
testAttributesRestart(root);
|
||||
testV3Record(root);
|
||||
testBucketRestart(root);
|
||||
testBucketAccessUpgrade(root);
|
||||
testAclPersistence(root);
|
||||
testAdditionalKeys(root);
|
||||
testVersioning(root);
|
||||
|
||||
Reference in new issue
Block a user