Release ObjectStore 0.0.9
This commit is contained in:
1 parent
31eb4d092e
commit
0cf5c11202
39 files changed
+4739
-1167
No files matched your search
@@ -3,14 +3,68 @@ package cloud.lunarsky.store;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public final class ConcurrencyTest {
|
||||
private static void parallelCapacity() throws Exception {
|
||||
Path root = Files.createTempDirectory("store-parallel-capacity-");
|
||||
byte[] body = new byte[4096];
|
||||
Set<String> saved = new HashSet<>();
|
||||
try {
|
||||
try (var store = new DiskStore(root, body.length, 8L * body.length);
|
||||
var workers = Executors.newFixedThreadPool(16)) {
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
List<Future<String>> writes = new ArrayList<>();
|
||||
for (int i = 0; i < 16; i++) {
|
||||
String key = "parallel-" + i;
|
||||
writes.add(workers.submit(() -> {
|
||||
start.await();
|
||||
try {
|
||||
store.put("test", key, new ByteArrayInputStream(body), body.length,
|
||||
SigV4.hex(SigV4.hash(body)), null, true, "application/octet-stream");
|
||||
return key;
|
||||
} catch (StoreException error) {
|
||||
if (error.status != 507) throw error;
|
||||
return null;
|
||||
}
|
||||
}));
|
||||
}
|
||||
start.countDown();
|
||||
for (Future<String> write : writes) {
|
||||
String key = write.get();
|
||||
if (key != null) saved.add(key);
|
||||
}
|
||||
if (saved.size() != 8 || store.usedBytes() != 8L * body.length ||
|
||||
store.list("test", "", "", 100, null).objects().size() != 8)
|
||||
throw new AssertionError("Concurrent writes exceeded capacity or lost an acknowledgement");
|
||||
}
|
||||
try (var restarted = new DiskStore(root, body.length, 8L * body.length)) {
|
||||
if (restarted.usedBytes() != 8L * body.length ||
|
||||
restarted.list("test", "", "", 100, null).objects().size() != 8)
|
||||
throw new AssertionError("Concurrent writes were not recovered after restart");
|
||||
for (String key : saved) {
|
||||
try (var object = restarted.open("test", key)) {
|
||||
if (!Arrays.equals(object.stream().readAllBytes(), body))
|
||||
throw new AssertionError("Recovered object differs from acknowledged write");
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try (var paths = Files.walk(root)) {
|
||||
for (Path path : paths.sorted(Comparator.reverseOrder()).toList()) Files.delete(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ObjectStorage.Metadata put(DiskStore store, byte[] body) throws Exception {
|
||||
return store.put("test", "shared", new ByteArrayInputStream(body), body.length,
|
||||
SigV4.hex(SigV4.hash(body)), null, false, "application/octet-stream");
|
||||
@@ -78,11 +132,12 @@ public final class ConcurrencyTest {
|
||||
} catch (StoreException expected) {
|
||||
if (expected.status != 404) throw expected;
|
||||
}
|
||||
System.out.println("Concurrent storage tests passed: atomic overwrite, read and listing after acknowledgement, delete");
|
||||
} finally {
|
||||
try (var paths = Files.walk(root)) {
|
||||
for (Path path : paths.sorted(Comparator.reverseOrder()).toList()) Files.delete(path);
|
||||
}
|
||||
}
|
||||
parallelCapacity();
|
||||
System.out.println("Concurrent storage tests passed: atomic overwrite, read and listing after acknowledgement, delete, parallel quota and restart");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package cloud.lunarsky.store;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Clock;
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public final class FeatureTest {
|
||||
private static final String OWNER = "TESTACCESSKEY123";
|
||||
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 void status(int expected, HttpResponse<?> response) {
|
||||
if (response.statusCode() != expected)
|
||||
throw new AssertionError("Expected " + expected + ", got " + response.statusCode() + ": " + response.body());
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Path root = Files.createTempDirectory("objectstore-feature-test-");
|
||||
Path policy = root.resolve("scopes");
|
||||
Files.writeString(policy, LIMITED + "\tphotos\tusers/one/\tread,write,list\n");
|
||||
KeyScopes scopes = KeyScopes.fromEnvironment(Map.of("S3_KEY_SCOPES_FILE", policy.toString()),
|
||||
OWNER, Set.of(OWNER, LIMITED));
|
||||
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"));
|
||||
denied(() -> scopes.checkBucket(LIMITED, "photos", "GET", Map.of("list-type", "2")));
|
||||
denied(() -> scopes.checkBucket(LIMITED, "photos", "GET", Map.of("list-type", "2", "prefix", "users/")));
|
||||
denied(() -> scopes.checkObject(LIMITED, "photos", "users/one/copy.jpg", "PUT", Map.of(),
|
||||
headers("x-amz-copy-source", "/photos/users/two/private.jpg")));
|
||||
denied(() -> scopes.checkObject(LIMITED, "photos", "users/one/file", "PUT", Map.of(),
|
||||
headers("x-amz-acl", "public-read")));
|
||||
var cors = BrowserCors.fromEnvironment(Map.of("CORS_ALLOWED_ORIGINS", "https://dashboard.example"));
|
||||
var metrics = new RequestMetrics("metrics-test-token-at-least-32-characters");
|
||||
var executor = Executors.newVirtualThreadPerTaskExecutor();
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 16);
|
||||
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,
|
||||
"us-east-1", Clock.systemUTC()),
|
||||
"photos", ClientLimits.disabled(), 16, "", cors, scopes, metrics);
|
||||
server.setExecutor(executor);
|
||||
server.createContext("/", app::handle);
|
||||
server.start();
|
||||
String base = "http://127.0.0.1:" + server.getAddress().getPort();
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
status(204, client.send(HttpRequest.newBuilder(URI.create(base + "/photos/file"))
|
||||
.header("Origin", "https://dashboard.example")
|
||||
.header("Access-Control-Request-Method", "PUT")
|
||||
.header("Access-Control-Request-Headers", "content-type,x-amz-date")
|
||||
.method("OPTIONS", HttpRequest.BodyPublishers.noBody()).build(),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpRequest.newBuilder(URI.create(base + "/photos/file"))
|
||||
.header("Origin", "https://other.example")
|
||||
.header("Access-Control-Request-Method", "PUT")
|
||||
.method("OPTIONS", HttpRequest.BodyPublishers.noBody()).build(),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
var health = client.send(HttpRequest.newBuilder(URI.create(base + "/health"))
|
||||
.header("Origin", "https://dashboard.example").GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
status(200, health);
|
||||
if (!"https://dashboard.example".equals(health.headers().firstValue("Access-Control-Allow-Origin").orElse(null)))
|
||||
throw new AssertionError("Allowed origin missing from response");
|
||||
status(401, client.send(HttpRequest.newBuilder(URI.create(base + "/metrics")).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString()));
|
||||
var snapshot = client.send(HttpRequest.newBuilder(URI.create(base + "/metrics"))
|
||||
.header("Authorization", "Bearer metrics-test-token-at-least-32-characters").GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
status(200, snapshot);
|
||||
if (!snapshot.body().contains("objectstore_requests_total 4"))
|
||||
throw new AssertionError("Metrics did not count requests: " + snapshot.body());
|
||||
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()));
|
||||
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()));
|
||||
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"),
|
||||
"GET", new byte[0], Map.of(), LIMITED, LIMITED_SECRET), HttpResponse.BodyHandlers.ofString()));
|
||||
status(403, client.send(HttpTest.signedUriAs(URI.create(base + "/photos?list-type=2"),
|
||||
"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()));
|
||||
} finally {
|
||||
server.stop(0);
|
||||
executor.close();
|
||||
store.close();
|
||||
try (var files = Files.walk(root)) {
|
||||
for (Path path : files.sorted(Comparator.reverseOrder()).toList()) Files.delete(path);
|
||||
}
|
||||
}
|
||||
System.out.println("Feature tests passed: scoped keys, CORS, metrics");
|
||||
}
|
||||
|
||||
private static com.sun.net.httpserver.Headers headers(String name, String value) {
|
||||
var headers = new com.sun.net.httpserver.Headers();
|
||||
headers.set(name, value);
|
||||
return headers;
|
||||
}
|
||||
|
||||
private static void denied(Runnable action) {
|
||||
try { action.run(); }
|
||||
catch (StoreException error) {
|
||||
if (error.status == 403) return;
|
||||
throw new AssertionError("Wrong denial", error);
|
||||
}
|
||||
throw new AssertionError("Expected access denial");
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ public final class HttpTest {
|
||||
return signedUriAs(uri, method, body, extra, ACCESS, SECRET);
|
||||
}
|
||||
|
||||
private static HttpRequest signedUriAs(URI uri, String method, byte[] body, Map<String, String> extra,
|
||||
static HttpRequest signedUriAs(URI uri, String method, byte[] body, Map<String, String> extra,
|
||||
String access, String secret) {
|
||||
String host = uri.getAuthority();
|
||||
String date = DATE.format(Instant.now());
|
||||
@@ -274,7 +274,7 @@ public final class HttpTest {
|
||||
String trailerToSign = "AWS4-HMAC-SHA256-TRAILER\n" + date + "\n" + scope + "\n" +
|
||||
previous + "\n" + SigV4.hex(SigV4.hash((trailerName + ":" + checksum + "\n")
|
||||
.getBytes(StandardCharsets.UTF_8)));
|
||||
encoded.write(("x-amz-trailer-signature=" + SigV4.hex(SigV4.hmac(key, trailerToSign)) +
|
||||
encoded.write(("x-amz-trailer-signature:" + SigV4.hex(SigV4.hmac(key, trailerToSign)) +
|
||||
"\r\n\r\n").getBytes(StandardCharsets.US_ASCII));
|
||||
byte[] upload = encoded.toByteArray();
|
||||
HttpRequest.Builder request = HttpRequest.newBuilder(object)
|
||||
|
||||
@@ -85,7 +85,7 @@ public final class StoreTest {
|
||||
withTrailer.write(body, 65536, 1024);
|
||||
withTrailer.write("\r\n0;chunk-signature=2ca2aba2005185cf7159c6277faf83795951dd77a3a99e6e65d5c9f85863f992\r\n".getBytes());
|
||||
withTrailer.write("x-amz-checksum-crc32c:sOO8/Q==\r\n".getBytes());
|
||||
withTrailer.write("x-amz-trailer-signature=d81f82fc3505edab99d459891051a732e8730629a2e4a59689829ca17fe2e435\r\n\r\n".getBytes());
|
||||
withTrailer.write("x-amz-trailer-signature:d81f82fc3505edab99d459891051a732e8730629a2e4a59689829ca17fe2e435\r\n\r\n".getBytes());
|
||||
var trailerAuthorization = new SigV4.Verified("STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER",
|
||||
"", key, date, scope, "106e2a8a18243abcf37539882f36619c00e2dfc72633413f02d3b74544bfeb8e");
|
||||
try (var stream = new AwsChunkedInputStream(new ByteArrayInputStream(withTrailer.toByteArray()),
|
||||
@@ -328,14 +328,14 @@ public final class StoreTest {
|
||||
private static void testAdditionalKeys(Path root) throws Exception {
|
||||
Path file = root.resolve("access-keys");
|
||||
Files.writeString(file, "SECONDARYKEY1234:secondary-secret-key-that-is-at-least-32-characters\n");
|
||||
Map<String, String> keys = Main.identities(
|
||||
Map<String, String> keys = ObjectStoreServer.identities(
|
||||
Map.of("S3_CREDENTIALS_FILE", file.toString()),
|
||||
"TESTACCESSKEY123", "test-secret-key-that-is-at-least-32-characters");
|
||||
if (keys.size() != 2 || !keys.containsKey("SECONDARYKEY1234"))
|
||||
throw new AssertionError("Additional access key was not loaded");
|
||||
Files.writeString(file, "TESTACCESSKEY123:duplicate-root-secret-that-is-at-least-32-characters\n");
|
||||
try {
|
||||
Main.identities(Map.of("S3_CREDENTIALS_FILE", file.toString()),
|
||||
ObjectStoreServer.identities(Map.of("S3_CREDENTIALS_FILE", file.toString()),
|
||||
"TESTACCESSKEY123", "test-secret-key-that-is-at-least-32-characters");
|
||||
throw new AssertionError("Duplicate root key was accepted");
|
||||
} catch (IllegalArgumentException expected) { }
|
||||
|
||||
Reference in new issue
Block a user