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) {
|
||||
|
||||
Reference in new issue
Block a user