mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(dynamodb): optional DynamoDB Local backend (#4504)
* feat(dynamodb): add the external Local operation client Adds DynamoDbLocalClient, which forwards DynamoDB and DynamoDB Streams wire calls to an external DynamoDB Local over SigV4-signed HTTP/1.1. The caller's account becomes the access key and its region the signing region, so Local keeps each account and region apart. Requests are never retried. Reply.successBody now owns the AWS error body conversion, which the API stream reader reuses. * feat(dynamodb): add the DynamoDB Local backend Adds DynamoDbLocalBackend, a DynamoDbBackend that forwards every wire call to an external DynamoDB Local through DynamoDbLocalClient. - Namespaces: the caller's account and region select the Local namespace; table and stream ARNs from another account or region are rejected. - Identities: table, index and stream ARNs in replies are rewritten from Local's fixed ddblocal shape to public ARNs, and stream records carry the caller's region. - Metadata: tags live in a Floci store because Local keeps none; replica updates are refused; a global table is its single-region table; stream enable and disable are idempotent like native. - Lifecycle: startup waits up to 30s for Local to accept connections, and emulator resets are refused because the data lives outside Floci. DynamoDbBackend groups the four seam interfaces so one object is the whole engine. * feat(dynamodb): enable the optional Local backend floci.services.dynamodb.backend selects the engine: native (default) or local, which forwards to the DynamoDB Local at local-endpoint. Both engines are client proxies, so the unselected one is never constructed. With local selected, the stream reader keeps checkpoints for the life of the stream, and /_floci/info reports the backend in dynamodb_backend. DynamoDbLocalBackendIntegrationTest runs against a real DynamoDB Local when FLOCI_SERVICES_DYNAMODB_LOCAL_ENDPOINT is set. * docs(dynamodb): document the DynamoDB Local backend * fix(dynamodb): harden the Local backend endpoint check, stream paging and startup probe Reject AWS endpoints in every partition, with a trailing dot, and endpoints with user info. Translate ListStreams paging ARNs, retry any I/O failure until the startup deadline, and call DescribeTable outside the tag lock. * fix(dynamodb): tighten Local ARN ownership, batch table names and global TableId A table ARN of another account or partition is ResourceNotFoundException, a stream ARN of another partition is refused, and a batch naming one table twice is a ValidationException instead of a dropped entry. A global table on Local has an empty TableId instead of a random one. Document the Resource Explorer limit. * fix(dynamodb): mark the Local readiness probe region as partition-invariant * test(dynamodb): pin the table record of an untagged typed Local create * docs(dynamodb): state the Local readiness bound and stack delete protection * fix(dynamodb): lift deletion protection on the Local direct delete and cap the readiness probe - DynamoDbTableAccess.deleteTable is the direct engine delete with no deletion-protection check. The Local backend now clears DeletionProtectionEnabled before DeleteTable, so CloudFormation deletes behave the same on both backends. The wire DeleteTable still enforces it. - Each readiness probe is bounded by the remaining 30 second budget and never exceeds the configured request timeout, so startup fails within the budget. * fix(dynamodb): align Local tag errors with the native wire path - ListTagsOfResource on a missing table is AccessDeniedException, as on the native wire path. - A table ARN whose partition is not an AWS partition is a ValidationException instead of a foreign resource. - Tag operations report a table ARN of another region as ResourceNotFoundException, as native does; TableName fields keep the region-mismatch ValidationException. - Document which regions Resource Explorer searches, and note that a failed direct delete leaves deletion protection off. * docs(dynamodb): state the tested DynamoDB Local version
This commit is contained in:
1 parent
833a43bfa7
commit
c326dce977
21 files changed
+2338
-32
No files matched your search
@@ -1342,6 +1342,21 @@ public interface EmulatorConfig {
|
||||
*/
|
||||
@WithDefault("10")
|
||||
int vectorIndexBackfillSeconds();
|
||||
|
||||
/** The engine behind the DynamoDB API: {@code native} or {@code local}. Env: FLOCI_SERVICES_DYNAMODB_BACKEND. */
|
||||
@WithDefault("native")
|
||||
String backend();
|
||||
|
||||
/** The DynamoDB Local base URL, required when backend is local. Env: FLOCI_SERVICES_DYNAMODB_LOCAL_ENDPOINT. */
|
||||
Optional<String> localEndpoint();
|
||||
|
||||
/** Env: FLOCI_SERVICES_DYNAMODB_LOCAL_CONNECT_TIMEOUT_SECONDS. */
|
||||
@WithDefault("2")
|
||||
int localConnectTimeoutSeconds();
|
||||
|
||||
/** Env: FLOCI_SERVICES_DYNAMODB_LOCAL_REQUEST_TIMEOUT_SECONDS. */
|
||||
@WithDefault("10")
|
||||
int localRequestTimeoutSeconds();
|
||||
}
|
||||
|
||||
interface SnsServiceConfig {
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.ContainerTeardown;
|
||||
import io.github.hectorvent.floci.core.common.ContainerTeardowns;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbBackendSelector;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
@@ -44,6 +45,7 @@ public class EmulatorInfoController {
|
||||
private final Instance<ContainerTeardown> containerTeardowns;
|
||||
private final FlociCertificateAuthority certificateAuthority;
|
||||
private final EmulatorConfig config;
|
||||
private final DynamoDbBackendSelector dynamoDbBackendSelector;
|
||||
|
||||
@Inject
|
||||
public EmulatorInfoController(ServiceRegistry serviceRegistry,
|
||||
@@ -52,7 +54,8 @@ public class EmulatorInfoController {
|
||||
Instance<Resettable> resettables,
|
||||
Instance<ContainerTeardown> containerTeardowns,
|
||||
FlociCertificateAuthority certificateAuthority,
|
||||
EmulatorConfig config) {
|
||||
EmulatorConfig config,
|
||||
DynamoDbBackendSelector dynamoDbBackendSelector) {
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.initLifecycleState = initLifecycleState;
|
||||
this.storageFactory = storageFactory;
|
||||
@@ -60,6 +63,7 @@ public class EmulatorInfoController {
|
||||
this.containerTeardowns = containerTeardowns;
|
||||
this.certificateAuthority = certificateAuthority;
|
||||
this.config = config;
|
||||
this.dynamoDbBackendSelector = dynamoDbBackendSelector;
|
||||
this.version = resolveVersion();
|
||||
}
|
||||
|
||||
@@ -98,7 +102,8 @@ public class EmulatorInfoController {
|
||||
@GET
|
||||
@Path("/info")
|
||||
public Response info() {
|
||||
return Response.ok(Map.of("version", version, "edition", "community", "original_edition", "floci-always-free")).build();
|
||||
return Response.ok(Map.of("version", version, "edition", "community", "original_edition", "floci-always-free",
|
||||
"dynamodb_backend", dynamoDbBackendSelector.selected())).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
+1
-12
@@ -3,10 +3,8 @@ package io.github.hectorvent.floci.services.dynamodb.backend;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Api;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Call;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Reply;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -78,22 +76,13 @@ public final class DynamoDbApiStreamReader implements DynamoDbStreamReader {
|
||||
}
|
||||
|
||||
private JsonNode call(Stream stream, String action, ObjectNode body) {
|
||||
Reply reply;
|
||||
try {
|
||||
reply = operations.execute(new Call(stream.scope(), Api.DYNAMODB_STREAMS, action, body));
|
||||
return operations.execute(new Call(stream.scope(), Api.DYNAMODB_STREAMS, action, body)).successBody();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
JsonNode replyBody = reply.body() == null ? JSON.missingNode() : reply.body();
|
||||
if (reply.status() >= 300) {
|
||||
String type = text(replyBody, "__type");
|
||||
String code = type == null ? "InternalServerError" : type.substring(type.lastIndexOf('#') + 1);
|
||||
String message = text(replyBody, "message");
|
||||
throw new AwsException(code, message != null ? message : text(replyBody, "Message"), reply.status());
|
||||
}
|
||||
return replyBody;
|
||||
}
|
||||
|
||||
private static String text(JsonNode node, String field) {
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb.backend;
|
||||
|
||||
/** One selectable DynamoDB engine: every seam interface from a single object. */
|
||||
public interface DynamoDbBackend extends DynamoDbOperations, DynamoDbItemAccess, DynamoDbTableAccess,
|
||||
DynamoDbBackendLifecycle {}
|
||||
+35
-13
@@ -1,49 +1,71 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb.backend;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbStreamReader.CheckpointLifetime;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Produces;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* The one immutable DynamoDB backend selection. Native is the only engine today; a configurable
|
||||
* choice arrives with the alternative engine. {@link NativeDynamoDbBackend} is typed to its concrete
|
||||
* class and {@link DynamoDbApiStreamReader} is not a bean, so these producers are the only
|
||||
* unqualified beans for the five seam interfaces.
|
||||
* The one immutable DynamoDB backend selection, read from {@code floci.services.dynamodb.backend}:
|
||||
* {@code native} (the default) runs the in-process engine, {@code local} forwards to an external
|
||||
* DynamoDB Local. Both engines are {@code @ApplicationScoped} client proxies, so the unselected one
|
||||
* is never constructed. Each engine is typed to its concrete class and {@link DynamoDbApiStreamReader}
|
||||
* is not a bean, so these producers are the only unqualified beans for the five seam interfaces.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class DynamoDbBackendSelector {
|
||||
|
||||
private final NativeDynamoDbBackend nativeBackend;
|
||||
private final String selected;
|
||||
private final DynamoDbBackend backend;
|
||||
|
||||
@Inject
|
||||
public DynamoDbBackendSelector(NativeDynamoDbBackend nativeBackend) {
|
||||
this.nativeBackend = nativeBackend;
|
||||
public DynamoDbBackendSelector(EmulatorConfig config, NativeDynamoDbBackend nativeBackend,
|
||||
DynamoDbLocalBackend localBackend) {
|
||||
String configured = config.services().dynamodb().backend();
|
||||
selected = configured.trim().toLowerCase(Locale.ROOT);
|
||||
backend = switch (selected) {
|
||||
case "native" -> nativeBackend;
|
||||
case "local" -> localBackend;
|
||||
default -> throw new IllegalStateException(
|
||||
"floci.services.dynamodb.backend must be 'native' or 'local', got '" + configured + "'");
|
||||
};
|
||||
}
|
||||
|
||||
/** The selected engine name, {@code native} or {@code local}. */
|
||||
public String selected() {
|
||||
return selected;
|
||||
}
|
||||
|
||||
@Produces
|
||||
DynamoDbOperations operations() {
|
||||
return nativeBackend;
|
||||
return backend;
|
||||
}
|
||||
|
||||
@Produces
|
||||
DynamoDbItemAccess itemAccess() {
|
||||
return nativeBackend;
|
||||
return backend;
|
||||
}
|
||||
|
||||
@Produces
|
||||
DynamoDbTableAccess tableAccess() {
|
||||
return nativeBackend;
|
||||
return backend;
|
||||
}
|
||||
|
||||
@Produces
|
||||
DynamoDbBackendLifecycle lifecycle() {
|
||||
return nativeBackend;
|
||||
return backend;
|
||||
}
|
||||
|
||||
/** Native stream history lives only in memory, so committed progress ends with the process. */
|
||||
/**
|
||||
* Native stream history lives only in memory, so committed progress ends with the process;
|
||||
* DynamoDB Local keeps stream history outside it, so progress lasts as long as the stream.
|
||||
*/
|
||||
@Produces
|
||||
DynamoDbStreamReader streamReader() {
|
||||
return new DynamoDbApiStreamReader(nativeBackend, CheckpointLifetime.PROCESS);
|
||||
return new DynamoDbApiStreamReader(backend,
|
||||
"local".equals(selected) ? CheckpointLifetime.STREAM : CheckpointLifetime.PROCESS);
|
||||
}
|
||||
}
|
||||
+627
@@ -0,0 +1,627 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb.backend;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import com.fasterxml.jackson.databind.node.NullNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsPartitions;
|
||||
import io.github.hectorvent.floci.core.resource.ExplorerResource;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbTableNames;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Typed;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* The DynamoDB engine backed by an external, official DynamoDB Local. Wire calls are forwarded
|
||||
* through {@link DynamoDbLocalClient} under the caller's account and region; table and stream ARNs
|
||||
* are translated between the public shape and Local's fixed {@code ddblocal} shape. Local keeps no
|
||||
* tags, so Floci holds them in its own store keyed by account, region and table name.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
@Typed(DynamoDbLocalBackend.class)
|
||||
public class DynamoDbLocalBackend implements DynamoDbBackend {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(DynamoDbLocalBackend.class);
|
||||
|
||||
private static final String LOCAL_ARN_PREFIX = "arn:aws:dynamodb:ddblocal:000000000000:"; // partition-literal: DynamoDB Local's fixed ARN prefix
|
||||
private static final String LOCAL_REGION = "ddblocal";
|
||||
private static final Duration READINESS_BUDGET = Duration.ofSeconds(30);
|
||||
private static final Scope PROBE = new Scope("000000000000", "us-east-1"); // partition-literal: readiness probe namespace, Local accepts any region
|
||||
private static final String REPLICAS_UNSUPPORTED = "Replicas are not supported by the DynamoDB Local backend";
|
||||
private static final Pattern PARTITION = Pattern.compile(AwsArnUtils.PARTITION_REGEX);
|
||||
private static final List<String> PUBLIC_ARN_FIELDS = List.of("TableArn", "IndexArn", "LatestStreamArn",
|
||||
"StreamArn", "LastEvaluatedStreamArn", "message", "Message");
|
||||
|
||||
private final DynamoDbLocalClient client;
|
||||
private final AccountAwareStorageBackend<Map<String, String>> tags;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final ObjectMapper aws;
|
||||
private final Duration readiness;
|
||||
|
||||
@Inject
|
||||
public DynamoDbLocalBackend(EmulatorConfig config, StorageFactory storageFactory, ObjectMapper objectMapper) {
|
||||
this(new DynamoDbLocalClient(validatedEndpoint(config.services().dynamodb().localEndpoint()),
|
||||
Duration.ofSeconds(config.services().dynamodb().localConnectTimeoutSeconds()),
|
||||
Duration.ofSeconds(config.services().dynamodb().localRequestTimeoutSeconds()), objectMapper),
|
||||
storageFactory.create("dynamodb", "dynamodb-local-tags.json",
|
||||
new TypeReference<Map<String, Map<String, String>>>() {}),
|
||||
objectMapper, READINESS_BUDGET);
|
||||
}
|
||||
|
||||
DynamoDbLocalBackend(DynamoDbLocalClient client, AccountAwareStorageBackend<Map<String, String>> tags,
|
||||
ObjectMapper objectMapper, Duration readiness) {
|
||||
this.client = client;
|
||||
this.tags = tags;
|
||||
this.objectMapper = objectMapper;
|
||||
this.aws = objectMapper.copy().setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE);
|
||||
this.readiness = readiness;
|
||||
}
|
||||
|
||||
static URI validatedEndpoint(Optional<String> value) {
|
||||
String raw = value.map(String::trim).filter(v -> !v.isEmpty()).orElseThrow(() -> new IllegalStateException(
|
||||
"floci.services.dynamodb.local-endpoint is required when floci.services.dynamodb.backend is local"));
|
||||
URI uri;
|
||||
try {
|
||||
uri = new URI(raw);
|
||||
} catch (URISyntaxException e) {
|
||||
throw invalidEndpoint(raw, e);
|
||||
}
|
||||
String scheme = uri.getScheme();
|
||||
if (scheme == null || !(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))
|
||||
|| uri.getHost() == null || uri.getRawUserInfo() != null) {
|
||||
throw invalidEndpoint(raw, null);
|
||||
}
|
||||
String host = uri.getHost().replaceFirst("\\.$", "");
|
||||
if (AwsPartitions.isDnsSuffix(host) || AwsPartitions.stripKnownDnsSuffix(host).isPresent()) {
|
||||
throw new IllegalStateException("floci.services.dynamodb.local-endpoint '" + raw
|
||||
+ "' must point at DynamoDB Local, not an AWS endpoint");
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
|
||||
private static IllegalStateException invalidEndpoint(String raw, Exception cause) {
|
||||
return new IllegalStateException("floci.services.dynamodb.local-endpoint '" + raw
|
||||
+ "' is not a valid http or https URL", cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reply execute(Call call) {
|
||||
Scope scope = call.scope();
|
||||
String action = call.action();
|
||||
JsonNode body = call.body() == null ? objectMapper.createObjectNode() : call.body().deepCopy();
|
||||
if (!"CreateTable".equals(action)) {
|
||||
toLocal(scope, body);
|
||||
}
|
||||
Reply served = call.api() != Api.DYNAMODB ? null : switch (action) {
|
||||
case "TagResource" -> {
|
||||
tagResource(scope, text(body, "ResourceArn"), tagsFrom(body.path("Tags")));
|
||||
yield new Reply(200, objectMapper.createObjectNode(), Map.of());
|
||||
}
|
||||
case "UntagResource" -> {
|
||||
List<String> keys = new ArrayList<>();
|
||||
body.path("TagKeys").forEach(key -> keys.add(key.asText()));
|
||||
untagResource(scope, text(body, "ResourceArn"), keys);
|
||||
yield new Reply(200, objectMapper.createObjectNode(), Map.of());
|
||||
}
|
||||
case "ListTagsOfResource" -> {
|
||||
String resourceArn = text(body, "ResourceArn");
|
||||
Map<String, String> found;
|
||||
try {
|
||||
found = listTagsOfResource(scope, resourceArn);
|
||||
} catch (AwsException e) {
|
||||
if (!"ResourceNotFoundException".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"User is not authorized to perform: dynamodb:ListTagsOfResource on resource: "
|
||||
+ resourceArn, 400);
|
||||
}
|
||||
ObjectNode reply = objectMapper.createObjectNode();
|
||||
ArrayNode tagList = reply.putArray("Tags");
|
||||
found.forEach((key, value) -> tagList.addObject().put("Key", key).put("Value", value));
|
||||
yield new Reply(200, reply, Map.of());
|
||||
}
|
||||
case "UpdateTable" -> {
|
||||
if (body.has("ReplicaUpdates")) {
|
||||
throw new AwsException("ValidationException", REPLICAS_UNSUPPORTED, 400);
|
||||
}
|
||||
yield null;
|
||||
}
|
||||
default -> null;
|
||||
};
|
||||
if (served != null) {
|
||||
return served;
|
||||
}
|
||||
Reply reply = send(scope, call.api(), action, body);
|
||||
if (reply.status() >= 200 && reply.status() < 300 && call.api() == Api.DYNAMODB) {
|
||||
JsonNode replyBody = reply.body() == null ? MissingNode.getInstance() : reply.body();
|
||||
String tableName = text(replyBody.path("TableDescription"), "TableName");
|
||||
if (tableName != null && "CreateTable".equals(action)) {
|
||||
tags.putForAccount(scope.accountId(), key(scope.region(), tableName), tagsFrom(body.path("Tags")));
|
||||
} else if (tableName != null && "DeleteTable".equals(action)) {
|
||||
tags.deleteForAccount(scope.accountId(), key(scope.region(), tableName));
|
||||
}
|
||||
}
|
||||
toPublic(scope, reply.body());
|
||||
return reply;
|
||||
}
|
||||
|
||||
private Reply send(Scope scope, Api api, String action, JsonNode body) {
|
||||
try {
|
||||
return client.send(scope, api, action, body);
|
||||
} catch (IOException e) {
|
||||
throw notAnswered(e);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw notAnswered(e);
|
||||
}
|
||||
}
|
||||
|
||||
private AwsException notAnswered(Exception e) {
|
||||
return new AwsException("InternalServerError",
|
||||
"DynamoDB Local at " + client.endpoint() + " did not answer: " + e, 500);
|
||||
}
|
||||
|
||||
static void toLocal(Scope scope, JsonNode node) {
|
||||
if (node instanceof ObjectNode object) {
|
||||
if (object.get("RequestItems") instanceof ObjectNode requestItems) {
|
||||
ObjectNode renamed = object.objectNode();
|
||||
for (Map.Entry<String, JsonNode> entry : requestItems.properties()) {
|
||||
String name = tableName(scope, entry.getKey());
|
||||
if (renamed.replace(name, entry.getValue()) != null) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Table " + name + " is named more than once in RequestItems", 400);
|
||||
}
|
||||
}
|
||||
object.set("RequestItems", renamed);
|
||||
}
|
||||
for (Map.Entry<String, JsonNode> field : object.properties()) {
|
||||
JsonNode value = field.getValue();
|
||||
if (value.isTextual() && "TableName".equals(field.getKey())) {
|
||||
field.setValue(TextNode.valueOf(tableName(scope, value.asText())));
|
||||
} else if (value.isTextual()
|
||||
&& ("StreamArn".equals(field.getKey()) || "ExclusiveStartStreamArn".equals(field.getKey()))) {
|
||||
field.setValue(TextNode.valueOf(localStreamArn(scope, value.asText())));
|
||||
} else if (value.isContainerNode()) {
|
||||
toLocal(scope, value);
|
||||
}
|
||||
}
|
||||
} else if (node instanceof ArrayNode array) {
|
||||
array.forEach(element -> toLocal(scope, element));
|
||||
}
|
||||
}
|
||||
|
||||
static String tableName(Scope scope, String value) {
|
||||
if (!value.startsWith("arn:")) {
|
||||
return value;
|
||||
}
|
||||
DynamoDbTableNames.resolveWithRegion(value, scope.region());
|
||||
return ownTableName(scope, value);
|
||||
}
|
||||
|
||||
// A well-formed table ARN of another account, region or partition names no table in this namespace.
|
||||
private static String ownTableName(Scope scope, String arn) {
|
||||
String name = DynamoDbTableNames.resolve(arn);
|
||||
if (!PARTITION.matcher(AwsArnUtils.parse(arn).partition()).matches()) {
|
||||
throw new AwsException("ValidationException", "Invalid table ARN: " + arn, 400);
|
||||
}
|
||||
if (!arn.startsWith(publicPrefix(scope))) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Requested resource not found: Table: " + arn + " not found", 400);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
static String localStreamArn(Scope scope, String value) {
|
||||
if (!AwsArnUtils.isArn(value)) {
|
||||
return value;
|
||||
}
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(value);
|
||||
if (!"dynamodb".equals(arn.service()) || !arn.resource().contains("/stream/")) {
|
||||
return value;
|
||||
}
|
||||
if (!value.startsWith(publicPrefix(scope))) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Requested resource not found: Stream: " + value + " not found", 400);
|
||||
}
|
||||
return LOCAL_ARN_PREFIX + arn.resource();
|
||||
}
|
||||
|
||||
private static String publicPrefix(Scope scope) {
|
||||
return AwsArnUtils.Arn.of("dynamodb", scope.region(), scope.accountId(), "").toString();
|
||||
}
|
||||
|
||||
static void toPublic(Scope scope, JsonNode node) {
|
||||
toPublic(node, publicPrefix(scope), scope.region());
|
||||
}
|
||||
|
||||
private static void toPublic(JsonNode node, String prefix, String region) {
|
||||
if (node instanceof ObjectNode object) {
|
||||
for (Map.Entry<String, JsonNode> field : object.properties()) {
|
||||
JsonNode value = field.getValue();
|
||||
if (value.isTextual() && PUBLIC_ARN_FIELDS.contains(field.getKey())) {
|
||||
field.setValue(TextNode.valueOf(value.asText().replace(LOCAL_ARN_PREFIX, prefix)));
|
||||
} else if (value.isTextual() && "awsRegion".equals(field.getKey())
|
||||
&& LOCAL_REGION.equals(value.asText())) {
|
||||
field.setValue(TextNode.valueOf(region));
|
||||
} else if (value.isContainerNode()) {
|
||||
toPublic(value, prefix, region);
|
||||
}
|
||||
}
|
||||
} else if (node instanceof ArrayNode array) {
|
||||
array.forEach(element -> toPublic(element, prefix, region));
|
||||
}
|
||||
}
|
||||
|
||||
private JsonNode call(Scope scope, String action, ObjectNode body) {
|
||||
return execute(new Call(scope, Api.DYNAMODB, action, body)).successBody();
|
||||
}
|
||||
|
||||
private ObjectNode body(String tableName, Object... nameValuePairs) {
|
||||
ObjectNode body = aws.createObjectNode().put("TableName", tableName);
|
||||
for (int i = 0; i < nameValuePairs.length; i += 2) {
|
||||
Object value = nameValuePairs[i + 1];
|
||||
if (value == null || value instanceof NullNode || value instanceof MissingNode) {
|
||||
continue;
|
||||
}
|
||||
String name = (String) nameValuePairs[i];
|
||||
body.set(name, value instanceof JsonNode node ? node : aws.valueToTree(value));
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putItem(Scope scope, String tableName, JsonNode item, String conditionExpression,
|
||||
JsonNode expressionAttributeNames, JsonNode expressionAttributeValues) {
|
||||
call(scope, "PutItem", body(tableName, "Item", item, "ConditionExpression", conditionExpression,
|
||||
"ExpressionAttributeNames", expressionAttributeNames,
|
||||
"ExpressionAttributeValues", expressionAttributeValues));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode getItem(Scope scope, String tableName, JsonNode key) {
|
||||
return present(call(scope, "GetItem", body(tableName, "Key", key)).get("Item"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteItem(Scope scope, String tableName, JsonNode key, String conditionExpression,
|
||||
JsonNode expressionAttributeNames, JsonNode expressionAttributeValues) {
|
||||
call(scope, "DeleteItem", body(tableName, "Key", key, "ConditionExpression", conditionExpression,
|
||||
"ExpressionAttributeNames", expressionAttributeNames,
|
||||
"ExpressionAttributeValues", expressionAttributeValues));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode updateItem(Scope scope, String tableName, JsonNode key, JsonNode attributeUpdates,
|
||||
String updateExpression, JsonNode expressionAttributeNames,
|
||||
JsonNode expressionAttributeValues, String returnValues, String conditionExpression) {
|
||||
JsonNode reply = call(scope, "UpdateItem", body(tableName, "Key", key, "AttributeUpdates", attributeUpdates,
|
||||
"UpdateExpression", updateExpression, "ExpressionAttributeNames", expressionAttributeNames,
|
||||
"ExpressionAttributeValues", expressionAttributeValues, "ReturnValues", returnValues,
|
||||
"ConditionExpression", conditionExpression));
|
||||
if ("ALL_NEW".equals(returnValues) || "ALL_OLD".equals(returnValues)) {
|
||||
return present(reply.get("Attributes"));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScanPage scan(Scope scope, String tableName, String filterExpression, JsonNode expressionAttributeNames,
|
||||
JsonNode expressionAttributeValues, JsonNode scanFilter, Integer limit,
|
||||
JsonNode exclusiveStartKey) {
|
||||
JsonNode reply = call(scope, "Scan", body(tableName, "FilterExpression", filterExpression,
|
||||
"ExpressionAttributeNames", expressionAttributeNames,
|
||||
"ExpressionAttributeValues", expressionAttributeValues, "ScanFilter", scanFilter, "Limit", limit,
|
||||
"ExclusiveStartKey", exclusiveStartKey));
|
||||
List<JsonNode> items = new ArrayList<>();
|
||||
reply.path("Items").forEach(items::add);
|
||||
return new ScanPage(items, reply.path("ScannedCount").asInt(), present(reply.get("LastEvaluatedKey")));
|
||||
}
|
||||
|
||||
private static JsonNode present(JsonNode node) {
|
||||
return node == null || node.isNull() ? null : node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDefinition createTable(Scope scope, String tableName, List<KeySchemaElement> keySchema,
|
||||
List<AttributeDefinition> attributeDefinitions, Long readCapacity,
|
||||
Long writeCapacity, List<GlobalSecondaryIndex> globalSecondaryIndexes,
|
||||
List<LocalSecondaryIndex> localSecondaryIndexes) {
|
||||
ObjectNode body = body(tableName, "KeySchema", keySchema, "AttributeDefinitions", attributeDefinitions);
|
||||
ObjectNode throughput = null;
|
||||
if (readCapacity != null && writeCapacity != null) {
|
||||
throughput = aws.createObjectNode()
|
||||
.put("ReadCapacityUnits", readCapacity)
|
||||
.put("WriteCapacityUnits", writeCapacity);
|
||||
body.put("BillingMode", "PROVISIONED").set("ProvisionedThroughput", throughput);
|
||||
} else {
|
||||
body.put("BillingMode", "PAY_PER_REQUEST");
|
||||
}
|
||||
if (globalSecondaryIndexes != null && !globalSecondaryIndexes.isEmpty()) {
|
||||
ArrayNode indexes = body.putArray("GlobalSecondaryIndexes");
|
||||
for (GlobalSecondaryIndex gsi : globalSecondaryIndexes) {
|
||||
ObjectNode index = index(indexes, gsi.getIndexName(), gsi.getKeySchema(), gsi.getProjectionType(),
|
||||
gsi.getNonKeyAttributes());
|
||||
if (throughput != null) {
|
||||
index.set("ProvisionedThroughput", throughput.deepCopy());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (localSecondaryIndexes != null && !localSecondaryIndexes.isEmpty()) {
|
||||
ArrayNode indexes = body.putArray("LocalSecondaryIndexes");
|
||||
for (LocalSecondaryIndex lsi : localSecondaryIndexes) {
|
||||
index(indexes, lsi.getIndexName(), lsi.getKeySchema(), lsi.getProjectionType(),
|
||||
lsi.getNonKeyAttributes());
|
||||
}
|
||||
}
|
||||
return table(call(scope, "CreateTable", body).path("TableDescription"));
|
||||
}
|
||||
|
||||
private ObjectNode index(ArrayNode indexes, String indexName, List<KeySchemaElement> keySchema,
|
||||
String projectionType, List<String> nonKeyAttributes) {
|
||||
ObjectNode index = indexes.addObject().put("IndexName", indexName);
|
||||
index.set("KeySchema", aws.valueToTree(keySchema));
|
||||
ObjectNode projection = index.putObject("Projection")
|
||||
.put("ProjectionType", projectionType == null ? "ALL" : projectionType);
|
||||
if (nonKeyAttributes != null && !nonKeyAttributes.isEmpty()) {
|
||||
projection.set("NonKeyAttributes", aws.valueToTree(nonKeyAttributes));
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDefinition describeTable(Scope scope, String tableName) {
|
||||
return table(call(scope, "DescribeTable", body(tableName)).path("Table"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<TableDefinition> findTable(Scope scope, String tableName) {
|
||||
try {
|
||||
return Optional.of(describeTable(scope, tableName));
|
||||
} catch (AwsException e) {
|
||||
if ("ResourceNotFoundException".equals(e.getErrorCode())) {
|
||||
return Optional.empty();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// The seam's direct delete skips deletion protection, like native; the wire DeleteTable still enforces it.
|
||||
// ponytail: a DeleteTable that fails after the UpdateTable leaves protection off; the stack delete that asked
|
||||
// for removal reports DELETE_FAILED and a retry completes it.
|
||||
@Override
|
||||
public void deleteTable(Scope scope, String tableName) {
|
||||
if (describeTable(scope, tableName).isDeletionProtectionEnabled()) {
|
||||
call(scope, "UpdateTable", body(tableName, "DeletionProtectionEnabled", false));
|
||||
}
|
||||
call(scope, "DeleteTable", body(tableName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDefinition enableStream(Scope scope, String tableName, String viewType) {
|
||||
String type = viewType == null ? "NEW_AND_OLD_IMAGES" : viewType;
|
||||
TableDefinition current = describeTable(scope, tableName);
|
||||
if (current.isStreamEnabled()) {
|
||||
if (viewType == null || type.equals(current.getStreamViewType())) {
|
||||
return current;
|
||||
}
|
||||
// ponytail: Local cannot retarget a live stream, so a new view type starts a new stream ARN
|
||||
updateStream(scope, tableName, false, null);
|
||||
}
|
||||
return updateStream(scope, tableName, true, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDefinition disableStream(Scope scope, String tableName) {
|
||||
TableDefinition current = describeTable(scope, tableName);
|
||||
if (!current.isStreamEnabled()) {
|
||||
return current;
|
||||
}
|
||||
return updateStream(scope, tableName, false, null);
|
||||
}
|
||||
|
||||
private TableDefinition updateStream(Scope scope, String tableName, boolean enabled, String viewType) {
|
||||
ObjectNode body = body(tableName);
|
||||
ObjectNode spec = body.putObject("StreamSpecification").put("StreamEnabled", enabled);
|
||||
if (viewType != null) {
|
||||
spec.put("StreamViewType", viewType);
|
||||
}
|
||||
return table(call(scope, "UpdateTable", body).path("TableDescription"));
|
||||
}
|
||||
|
||||
private TableDefinition table(JsonNode description) {
|
||||
TableDefinition table = aws.convertValue(description, TableDefinition.class);
|
||||
JsonNode spec = description.path("StreamSpecification");
|
||||
table.setStreamEnabled(spec.path("StreamEnabled").asBoolean(false));
|
||||
table.setStreamViewType(text(spec, "StreamViewType"));
|
||||
table.setStreamArn(table.isStreamEnabled() ? text(description, "LatestStreamArn") : null);
|
||||
table.setTableId(description.path("TableId").asText());
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> listTagsOfResource(Scope scope, String resourceArn) {
|
||||
return new LinkedHashMap<>(tags.getForAccount(scope.accountId(), existingTableKey(scope, resourceArn))
|
||||
.orElse(Map.of()));
|
||||
}
|
||||
|
||||
// ponytail: no tag limits or tag-key validation; Floci records what callers send.
|
||||
@Override
|
||||
public void tagResource(Scope scope, String resourceArn, Map<String, String> tagsToAdd) {
|
||||
String key = existingTableKey(scope, resourceArn);
|
||||
synchronized (tags) {
|
||||
Map<String, String> current = new LinkedHashMap<>(tags.getForAccount(scope.accountId(), key).orElse(Map.of()));
|
||||
current.putAll(tagsToAdd);
|
||||
tags.putForAccount(scope.accountId(), key, current);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void untagResource(Scope scope, String resourceArn, List<String> tagKeys) {
|
||||
String key = existingTableKey(scope, resourceArn);
|
||||
synchronized (tags) {
|
||||
Map<String, String> current = new LinkedHashMap<>(tags.getForAccount(scope.accountId(), key).orElse(Map.of()));
|
||||
tagKeys.forEach(current::remove);
|
||||
tags.putForAccount(scope.accountId(), key, current);
|
||||
}
|
||||
}
|
||||
|
||||
private String existingTableKey(Scope scope, String resourceArn) {
|
||||
if (resourceArn == null || !resourceArn.startsWith("arn:")) {
|
||||
throw new AwsException("ValidationException", "Invalid TableArn", 400);
|
||||
}
|
||||
String name = ownTableName(scope, resourceArn);
|
||||
describeTable(scope, name);
|
||||
return key(scope.region(), name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDefinition applyReplicaUpdates(Scope scope, String tableName, List<String> addRegions,
|
||||
List<String> removeRegions) {
|
||||
throw new AwsException("ValidationException", REPLICAS_UNSUPPORTED, 400);
|
||||
}
|
||||
|
||||
// ponytail: a single-region global table has no replicas, so it is the table itself.
|
||||
@Override
|
||||
public TableDefinition ensureGlobalTable(Scope scope, String tableName) {
|
||||
return describeTable(scope, tableName);
|
||||
}
|
||||
|
||||
// ponytail: Local cannot list its namespaces, so regions come from the tag store: a region Floci
|
||||
// has no table record for (made directly on Local, or lost with memory storage) is not listed.
|
||||
@Override
|
||||
public List<ExplorerResource> resources(String accountId) {
|
||||
List<String> regions = tags.keysForAccount(accountId).stream()
|
||||
.map(k -> k.substring(0, k.indexOf('/')))
|
||||
.distinct()
|
||||
.sorted()
|
||||
.toList();
|
||||
List<ExplorerResource> resources = new ArrayList<>();
|
||||
for (String region : regions) {
|
||||
Scope scope = new Scope(accountId, region);
|
||||
String start = null;
|
||||
do {
|
||||
ObjectNode request = objectMapper.createObjectNode();
|
||||
if (start != null) {
|
||||
request.put("ExclusiveStartTableName", start);
|
||||
}
|
||||
JsonNode page = call(scope, "ListTables", request);
|
||||
for (JsonNode tableName : page.path("TableNames")) {
|
||||
String name = tableName.asText();
|
||||
resources.add(new ExplorerResource(
|
||||
AwsArnUtils.Arn.of("dynamodb", region, accountId, "table/" + name).toString(),
|
||||
"dynamodb:table", "dynamodb", region, accountId, Instant.now(),
|
||||
tags.getForAccount(accountId, key(region, name)).orElse(Map.of())));
|
||||
}
|
||||
start = text(page, "LastEvaluatedTableName");
|
||||
} while (start != null);
|
||||
}
|
||||
return resources;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
long deadline = System.nanoTime() + readiness.toNanos();
|
||||
while (true) {
|
||||
if (ready(deadline)) {
|
||||
LOG.infov("DynamoDB backend: DynamoDB Local at {0}", client.endpoint());
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while waiting for DynamoDB Local at "
|
||||
+ client.endpoint(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean ready(long deadline) {
|
||||
Reply reply;
|
||||
try {
|
||||
reply = client.send(PROBE, Api.DYNAMODB, "ListTables", objectMapper.createObjectNode().put("Limit", 1),
|
||||
Duration.ofNanos(Math.max(1, deadline - System.nanoTime())));
|
||||
} catch (IOException e) {
|
||||
if (System.nanoTime() - deadline >= 0) {
|
||||
throw new IllegalStateException("DynamoDB Local at " + client.endpoint() + " is not reachable after "
|
||||
+ readiness.toSeconds() + "s", e);
|
||||
}
|
||||
return false;
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while waiting for DynamoDB Local at " + client.endpoint(), e);
|
||||
}
|
||||
if (reply.status() >= 200 && reply.status() < 300) {
|
||||
return true;
|
||||
}
|
||||
throw new IllegalStateException("DynamoDB Local at " + client.endpoint() + " answered ListTables with status "
|
||||
+ reply.status() + ": " + reply.body());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkReset() {
|
||||
throw new AwsException("UnsupportedOperation", "The DynamoDB Local backend keeps its data outside Floci; "
|
||||
+ "reset the external DynamoDB Local instance instead", 409);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeReset() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterReset() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
client.close();
|
||||
}
|
||||
|
||||
private static String key(String region, String tableName) {
|
||||
return region + "/" + tableName;
|
||||
}
|
||||
|
||||
private static Map<String, String> tagsFrom(JsonNode tagList) {
|
||||
Map<String, String> result = new LinkedHashMap<>();
|
||||
tagList.forEach(tag -> result.put(tag.path("Key").asText(), tag.path("Value").asText()));
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String text(JsonNode node, String field) {
|
||||
JsonNode value = node == null ? null : node.get(field);
|
||||
return value == null || value.isNull() ? null : value.asText();
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb.backend;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Api;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Reply;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Scope;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Forwards DynamoDB and DynamoDB Streams wire calls to an external DynamoDB Local over SigV4-signed
|
||||
* HTTP. The caller's account becomes the access key and its region the signing region, so Local
|
||||
* keeps each account and region apart. Requests are never retried.
|
||||
*/
|
||||
class DynamoDbLocalClient implements AutoCloseable {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String SIGNED_HEADERS = "content-type;host;x-amz-date;x-amz-target";
|
||||
private static final String SERVICE = "dynamodb";
|
||||
private static final String SECRET_KEY = "floci";
|
||||
private static final DateTimeFormatter AMZ_DATE = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'");
|
||||
|
||||
private final URI endpoint;
|
||||
private final Duration requestTimeout;
|
||||
private final ObjectMapper mapper;
|
||||
private final HttpClient http;
|
||||
private final String host;
|
||||
private final String path;
|
||||
|
||||
DynamoDbLocalClient(URI endpoint, Duration connectTimeout, Duration requestTimeout, ObjectMapper mapper) {
|
||||
this.endpoint = endpoint;
|
||||
this.requestTimeout = requestTimeout;
|
||||
this.mapper = mapper;
|
||||
this.http = HttpClient.newBuilder()
|
||||
.version(HttpClient.Version.HTTP_1_1)
|
||||
.connectTimeout(connectTimeout)
|
||||
.build();
|
||||
this.host = endpoint.getHost() + (endpoint.getPort() != -1 ? ":" + endpoint.getPort() : "");
|
||||
String rawPath = endpoint.getRawPath();
|
||||
this.path = rawPath == null || rawPath.isEmpty() ? "/" : rawPath;
|
||||
}
|
||||
|
||||
Reply send(Scope scope, Api api, String action, JsonNode body) throws IOException, InterruptedException {
|
||||
return send(scope, api, action, body, requestTimeout);
|
||||
}
|
||||
|
||||
Reply send(Scope scope, Api api, String action, JsonNode body, Duration budget)
|
||||
throws IOException, InterruptedException {
|
||||
byte[] payload = mapper.writeValueAsBytes(body == null ? mapper.createObjectNode() : body);
|
||||
String target = (api == Api.DYNAMODB ? "DynamoDB_20120810." : "DynamoDBStreams_20120810.") + action;
|
||||
String amzDate = AMZ_DATE.format(ZonedDateTime.now(ZoneOffset.UTC));
|
||||
HttpRequest request = HttpRequest.newBuilder(endpoint.resolve(path))
|
||||
.timeout(budget.compareTo(requestTimeout) < 0 ? budget : requestTimeout)
|
||||
.header("Content-Type", CONTENT_TYPE)
|
||||
.header("X-Amz-Date", amzDate)
|
||||
.header("X-Amz-Target", target)
|
||||
.header("Authorization", authorization(scope, target, amzDate, payload))
|
||||
.POST(HttpRequest.BodyPublishers.ofByteArray(payload))
|
||||
.build();
|
||||
HttpResponse<byte[]> response = http.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
||||
byte[] responseBody = response.body();
|
||||
JsonNode replyBody = responseBody == null || responseBody.length == 0 ? null : mapper.readTree(responseBody);
|
||||
return new Reply(response.statusCode(), replyBody, Map.of());
|
||||
}
|
||||
|
||||
URI endpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
http.shutdownNow();
|
||||
}
|
||||
|
||||
private String authorization(Scope scope, String target, String amzDate, byte[] payload) {
|
||||
String date = amzDate.substring(0, 8);
|
||||
String credentialScope = date + "/" + scope.region() + "/" + SERVICE + "/aws4_request";
|
||||
try {
|
||||
String canonical = "POST\n" + path + "\n\n"
|
||||
+ "content-type:" + CONTENT_TYPE + "\n"
|
||||
+ "host:" + host + "\n"
|
||||
+ "x-amz-date:" + amzDate + "\n"
|
||||
+ "x-amz-target:" + target + "\n\n"
|
||||
+ SIGNED_HEADERS + "\n"
|
||||
+ SigV4RequestValidator.sha256Hex(payload);
|
||||
String toSign = "AWS4-HMAC-SHA256\n" + amzDate + "\n" + credentialScope + "\n"
|
||||
+ SigV4RequestValidator.sha256Hex(canonical);
|
||||
byte[] signingKey = SigV4RequestValidator.deriveSigningKey(SECRET_KEY, date, scope.region(), SERVICE);
|
||||
String signature = SigV4RequestValidator.hexEncode(SigV4RequestValidator.hmacSha256(signingKey, toSign));
|
||||
return "AWS4-HMAC-SHA256 Credential=floci" + scope.accountId() + "/" + credentialScope
|
||||
+ ", SignedHeaders=" + SIGNED_HEADERS + ", Signature=" + signature;
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Failed to sign DynamoDB Local request", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -1,6 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb.backend;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -43,6 +45,23 @@ public interface DynamoDbOperations {
|
||||
public Reply {
|
||||
headers = headers == null ? Map.of() : Map.copyOf(headers);
|
||||
}
|
||||
|
||||
/** The body of a 2xx reply; a non-2xx reply is thrown as the AwsException its AWS error body names. */
|
||||
public JsonNode successBody() {
|
||||
JsonNode replyBody = body == null ? MissingNode.getInstance() : body;
|
||||
if (status >= 300) {
|
||||
String type = text(replyBody, "__type");
|
||||
String code = type == null ? "InternalServerError" : type.substring(type.lastIndexOf('#') + 1);
|
||||
String message = text(replyBody, "message");
|
||||
throw new AwsException(code, message != null ? message : text(replyBody, "Message"), status);
|
||||
}
|
||||
return replyBody;
|
||||
}
|
||||
|
||||
private static String text(JsonNode node, String field) {
|
||||
JsonNode value = node.get(field);
|
||||
return value == null || value.isNull() ? null : value.asText();
|
||||
}
|
||||
}
|
||||
|
||||
Reply execute(Call call) throws Exception;
|
||||
|
||||
+1
-2
@@ -30,8 +30,7 @@ import java.util.Optional;
|
||||
*/
|
||||
@ApplicationScoped
|
||||
@Typed(NativeDynamoDbBackend.class)
|
||||
public class NativeDynamoDbBackend implements DynamoDbOperations, DynamoDbItemAccess, DynamoDbTableAccess,
|
||||
DynamoDbBackendLifecycle {
|
||||
public class NativeDynamoDbBackend implements DynamoDbBackend {
|
||||
|
||||
private final NativeDynamoDbJsonHandler jsonHandler;
|
||||
private final NativeDynamoDbStreamsJsonHandler streamsHandler;
|
||||
|
||||
Reference in new issue
Block a user