mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(pipes): deliver to a Redshift cluster or Serverless workgroup through the Data API (#7165)
This commit is contained in:
1 parent
5556bbae58
commit
24b152a274
12 files changed
+798
-98
No files matched your search
+8
-4
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.core.common.SsrfProtection;
|
||||
import io.github.hectorvent.floci.services.batch.BatchService;
|
||||
@@ -30,6 +31,7 @@ import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataTargetSubmitter;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftTargetAuthorizer;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
|
||||
@@ -67,6 +69,7 @@ public class EventBridgeInvoker {
|
||||
// AWS can't route an event from a sender bus on to a third bus; the second hop is dropped.
|
||||
private static final int MAX_BUS_TO_BUS_DEPTH = 1;
|
||||
private static final int MAX_IDLE_RATE_WINDOWS = 256;
|
||||
private static final String EVENTS_PRINCIPAL = ServicePrincipals.of("events");
|
||||
private static final Pattern DYNAMIC_PARAMETER_PATH =
|
||||
Pattern.compile("\\$(?:\\.[\\w*/-]+|\\[\\d+\\])+");
|
||||
// Headers the JDK HttpClient manages itself; setting them by hand throws IllegalArgumentException.
|
||||
@@ -84,7 +87,7 @@ public class EventBridgeInvoker {
|
||||
private final EcsJsonHandler ecsJsonHandler;
|
||||
private final StepFunctionsService stepFunctionsService;
|
||||
private final RedshiftDataService redshiftDataService;
|
||||
private final EventBridgeRedshiftTargetAuthorizer redshiftTargetAuthorizer;
|
||||
private final RedshiftTargetAuthorizer redshiftTargetAuthorizer;
|
||||
private final RedshiftDataTargetSubmitter redshiftSubmitter;
|
||||
private final ServiceRegistry serviceRegistry;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -104,7 +107,7 @@ public class EventBridgeInvoker {
|
||||
EcsJsonHandler ecsJsonHandler,
|
||||
StepFunctionsService stepFunctionsService,
|
||||
RedshiftDataService redshiftDataService,
|
||||
EventBridgeRedshiftTargetAuthorizer redshiftTargetAuthorizer,
|
||||
RedshiftTargetAuthorizer redshiftTargetAuthorizer,
|
||||
ServiceRegistry serviceRegistry,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper,
|
||||
@@ -125,7 +128,7 @@ public class EventBridgeInvoker {
|
||||
EcsJsonHandler ecsJsonHandler,
|
||||
StepFunctionsService stepFunctionsService,
|
||||
RedshiftDataService redshiftDataService,
|
||||
EventBridgeRedshiftTargetAuthorizer redshiftTargetAuthorizer,
|
||||
RedshiftTargetAuthorizer redshiftTargetAuthorizer,
|
||||
ServiceRegistry serviceRegistry,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper,
|
||||
@@ -372,7 +375,8 @@ public class EventBridgeInvoker {
|
||||
redshiftSubmitter.submit(targetArn, statement, target.getRoleArn(), ruleAccount, targetRegion,
|
||||
value -> resolveRedshiftParameter(value, eventJson),
|
||||
permissions -> redshiftTargetAuthorizer == null ? Optional.empty()
|
||||
: redshiftTargetAuthorizer.refusal(target.getRoleArn(), ruleAccount, ruleArn, permissions));
|
||||
: redshiftTargetAuthorizer.refusal(EVENTS_PRINCIPAL, target.getRoleArn(), ruleAccount,
|
||||
ruleArn, permissions));
|
||||
}
|
||||
|
||||
private String resolveRedshiftParameter(String value, String eventJson) {
|
||||
|
||||
-53
@@ -1,53 +0,0 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer.Permission;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Decides whether the role of an EventBridge Redshift Data API target may submit its SQL. EventBridge
|
||||
* assumes the target's role, so the role has to trust {@code events.amazonaws.com}, and its identity
|
||||
* policies, permissions boundary and SCPs have to allow every {@link Permission} the submission needs:
|
||||
* the Data API action itself and the action that authenticates it. Only active with IAM enforcement on,
|
||||
* as for every other API call. A role of another account is refused, because the rule's account does
|
||||
* not own it.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EventBridgeRedshiftTargetAuthorizer {
|
||||
|
||||
private static final String EVENTS_PRINCIPAL = ServicePrincipals.of("events");
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final ServiceRoleAuthorizer authorizer;
|
||||
|
||||
@Inject
|
||||
public EventBridgeRedshiftTargetAuthorizer(EmulatorConfig config, ServiceRoleAuthorizer authorizer) {
|
||||
this.config = config;
|
||||
this.authorizer = authorizer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns why {@code roleArn} may not run the target, or nothing when it may.
|
||||
*
|
||||
* @param accountId the account that owns the rule
|
||||
* @param ruleArn the rule that matched, which a trust policy may require in {@code aws:SourceArn}; without
|
||||
* it such a condition does not match
|
||||
* @param permissions what the submission needs, each of which has to be allowed
|
||||
*/
|
||||
public Optional<String> refusal(String roleArn, String accountId, String ruleArn,
|
||||
List<Permission> permissions) {
|
||||
if (!config.services().iam().enforcementEnabled()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (roleArn == null || roleArn.isBlank()) {
|
||||
return Optional.of("A Redshift target requires an IAM role, and this one names none.");
|
||||
}
|
||||
return authorizer.refusal(roleArn, accountId, EVENTS_PRINCIPAL, ruleArn, permissions);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.pipes;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
@@ -35,6 +36,9 @@ public class PipesService implements TagHandler, ResourceProvider {
|
||||
|
||||
private static final int MIN_PARALLELIZATION_FACTOR = 1;
|
||||
private static final int MAX_PARALLELIZATION_FACTOR = 10;
|
||||
private static final int MIN_REDSHIFT_SQLS = 1;
|
||||
private static final int MAX_REDSHIFT_SQLS = 40;
|
||||
private static final int MAX_REDSHIFT_SQL_LENGTH = 100_000;
|
||||
|
||||
private final StorageBackend<String, Pipe> storage;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -82,6 +86,7 @@ public class PipesService implements TagHandler, ResourceProvider {
|
||||
}
|
||||
|
||||
validateSourceConfiguration(source, sourceParameters);
|
||||
validateTargetConfiguration(target, targetParameters);
|
||||
|
||||
String key = region + "::" + name;
|
||||
if (storage.get(key).isPresent()) {
|
||||
@@ -178,6 +183,8 @@ public class PipesService implements TagHandler, ResourceProvider {
|
||||
if (!clearUnsetProperties) {
|
||||
validateSourceConfiguration(pipe.getSource(),
|
||||
sourceParameters != null ? sourceParameters : pipe.getSourceParameters());
|
||||
validateTargetConfiguration(target != null ? target : pipe.getTarget(),
|
||||
targetParameters != null ? targetParameters : pipe.getTargetParameters());
|
||||
}
|
||||
|
||||
writeProperty(target, clearUnsetProperties, pipe::setTarget);
|
||||
@@ -330,6 +337,36 @@ public class PipesService implements TagHandler, ResourceProvider {
|
||||
"Resource " + arn + " does not exist.", 404));
|
||||
}
|
||||
|
||||
/**
|
||||
* A Redshift target runs its {@code Sqls} as one BatchExecuteStatement, which the PipeTargetRedshiftDataParameters
|
||||
* reference makes required together with {@code Database}, with 1 to 40 statements of at most 100,000
|
||||
* characters each.
|
||||
*/
|
||||
private void validateTargetConfiguration(String target, JsonNode targetParameters) {
|
||||
if (!PipesTargetInvoker.isRedshiftTarget(target)) {
|
||||
return;
|
||||
}
|
||||
JsonNode parameters = targetParameters == null
|
||||
? MissingNode.getInstance() : targetParameters.path("RedshiftDataParameters");
|
||||
if (!parameters.path("Database").isTextual() || parameters.path("Database").asText().isBlank()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"TargetParameters.RedshiftDataParameters.Database is required for a Redshift target.", 400);
|
||||
}
|
||||
JsonNode sqls = parameters.path("Sqls");
|
||||
if (!sqls.isArray() || sqls.size() < MIN_REDSHIFT_SQLS || sqls.size() > MAX_REDSHIFT_SQLS) {
|
||||
throw new AwsException("ValidationException",
|
||||
"TargetParameters.RedshiftDataParameters.Sqls must hold between " + MIN_REDSHIFT_SQLS
|
||||
+ " and " + MAX_REDSHIFT_SQLS + " statements for a Redshift target.", 400);
|
||||
}
|
||||
for (JsonNode sql : sqls) {
|
||||
if (!sql.isTextual() || sql.asText().length() > MAX_REDSHIFT_SQL_LENGTH) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Each entry of TargetParameters.RedshiftDataParameters.Sqls must be a string of at most "
|
||||
+ MAX_REDSHIFT_SQL_LENGTH + " characters.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void validateSourceConfiguration(String source, JsonNode sourceParameters) {
|
||||
if (source == null) {
|
||||
return;
|
||||
|
||||
@@ -3,14 +3,19 @@ package io.github.hectorvent.floci.services.pipes;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
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.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.services.pipes.model.Pipe;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataTargetSubmitter;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftTargetAuthorizer;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
|
||||
@@ -21,6 +26,7 @@ import org.jboss.logging.Logger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -34,13 +40,19 @@ public class PipesTargetInvoker {
|
||||
private static final Logger LOG = Logger.getLogger(PipesTargetInvoker.class);
|
||||
private static final Pattern TEMPLATE_PLACEHOLDER =
|
||||
Pattern.compile("<(\\$[^>]+|aws\\.pipes\\.event\\.ingestion-time)>");
|
||||
private static final Pattern ARRAY_INDEX = Pattern.compile("^(.+?)\\[(\\d+)]$");
|
||||
private static final Pattern ARRAY_INDEX = Pattern.compile("^(.*?)\\[(\\d+)]$");
|
||||
private static final String PIPES_PRINCIPAL = ServicePrincipals.of("pipes");
|
||||
private static final Pattern DYNAMIC_PARAMETER_PATH =
|
||||
Pattern.compile("\\$(?:\\.[\\w*/-]+|\\[\\d+\\])+");
|
||||
|
||||
private final LambdaService lambdaService;
|
||||
private final SqsService sqsService;
|
||||
private final SnsService snsService;
|
||||
private final EventBridgeService eventBridgeService;
|
||||
private final StepFunctionsService stepFunctionsService;
|
||||
private final RedshiftDataTargetSubmitter redshiftSubmitter;
|
||||
private final RedshiftTargetAuthorizer redshiftTargetAuthorizer;
|
||||
private final ServiceRegistry serviceRegistry;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final String baseUrl;
|
||||
|
||||
@@ -50,6 +62,9 @@ public class PipesTargetInvoker {
|
||||
SnsService snsService,
|
||||
EventBridgeService eventBridgeService,
|
||||
StepFunctionsService stepFunctionsService,
|
||||
RedshiftDataTargetSubmitter redshiftSubmitter,
|
||||
RedshiftTargetAuthorizer redshiftTargetAuthorizer,
|
||||
ServiceRegistry serviceRegistry,
|
||||
ObjectMapper objectMapper,
|
||||
EmulatorConfig config) {
|
||||
this.lambdaService = lambdaService;
|
||||
@@ -57,13 +72,28 @@ public class PipesTargetInvoker {
|
||||
this.snsService = snsService;
|
||||
this.eventBridgeService = eventBridgeService;
|
||||
this.stepFunctionsService = stepFunctionsService;
|
||||
this.redshiftSubmitter = redshiftSubmitter;
|
||||
this.redshiftTargetAuthorizer = redshiftTargetAuthorizer;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.objectMapper = objectMapper;
|
||||
this.baseUrl = config.effectiveBaseUrl();
|
||||
}
|
||||
|
||||
/** Whether {@code targetArn} names a Redshift cluster or a Redshift Serverless workgroup. */
|
||||
static boolean isRedshiftTarget(String targetArn) {
|
||||
return AwsArnUtils.isArnFor(targetArn, "redshift") || AwsArnUtils.isArnFor(targetArn, "redshift-serverless");
|
||||
}
|
||||
|
||||
public void invoke(Pipe pipe, String payload, String region) {
|
||||
String targetArn = pipe.getTarget();
|
||||
|
||||
// A dynamic path in the Redshift parameters reads the source event as it arrived, and AWS does not
|
||||
// let it name a value an input transformation produced, so this does not go through the template.
|
||||
if (isRedshiftTarget(targetArn)) {
|
||||
invokeRedshiftData(pipe, payload, region);
|
||||
return;
|
||||
}
|
||||
|
||||
JsonNode tp = pipe.getTargetParameters();
|
||||
if (tp != null && tp.has("InputTemplate")) {
|
||||
payload = applyInputTemplate(tp.get("InputTemplate").asText(), payload);
|
||||
@@ -220,6 +250,61 @@ public class PipesTargetInvoker {
|
||||
LOG.debugv("Pipe delivered to Step Functions: {0}", arn);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Redshift target always runs its {@code Sqls} as one BatchExecuteStatement, under the pipe's role.
|
||||
* The Data API accepts the statements and the pipe does not wait for them, so a statement that later
|
||||
* fails does not fail the delivery or get retried: only a refusal or a rejected request does.
|
||||
*/
|
||||
private void invokeRedshiftData(Pipe pipe, String payload, String region) {
|
||||
if (serviceRegistry != null && !serviceRegistry.isServiceEnabled("redshift-data")) {
|
||||
throw new AwsException("ServiceNotAvailableException", "Service redshift-data is not enabled.", 400);
|
||||
}
|
||||
JsonNode targetParameters = pipe.getTargetParameters();
|
||||
JsonNode parameters = targetParameters == null
|
||||
? MissingNode.getInstance() : targetParameters.path("RedshiftDataParameters");
|
||||
JsonNode sqlNodes = parameters.path("Sqls");
|
||||
if (!sqlNodes.isArray() || sqlNodes.isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Pipe " + pipe.getName() + " has a Redshift target without RedshiftDataParameters.Sqls.", 400);
|
||||
}
|
||||
List<String> sqls = new ArrayList<>();
|
||||
sqlNodes.forEach(sql -> sqls.add(sql.asText()));
|
||||
|
||||
String targetArn = pipe.getTarget();
|
||||
String targetRegion = extractRegionFromArn(targetArn, region);
|
||||
String account = pipe.getAccountId() != null ? pipe.getAccountId() : AwsArnUtils.parse(targetArn).accountId();
|
||||
RedshiftDataTargetSubmitter.Statement statement = new RedshiftDataTargetSubmitter.Statement(
|
||||
text(parameters, "Database"), text(parameters, "DbUser"), text(parameters, "SecretManagerArn"),
|
||||
null, sqls, text(parameters, "StatementName"),
|
||||
parameters.path("WithEvent").isBoolean() ? parameters.get("WithEvent").asBoolean() : null);
|
||||
redshiftSubmitter.submit(targetArn, statement, pipe.getRoleArn(), account, targetRegion,
|
||||
value -> resolveRedshiftParameter(value, payload),
|
||||
permissions -> redshiftTargetAuthorizer.refusal(
|
||||
PIPES_PRINCIPAL, pipe.getRoleArn(), account, pipe.getArn(), permissions));
|
||||
LOG.debugv("Pipe delivered to Redshift Data API: {0}", targetArn);
|
||||
}
|
||||
|
||||
private static String text(JsonNode parent, String field) {
|
||||
JsonNode node = parent.path(field);
|
||||
return node.isTextual() ? node.asText() : null;
|
||||
}
|
||||
|
||||
private String resolveRedshiftParameter(String value, String payload) {
|
||||
if (value == null || !DYNAMIC_PARAMETER_PATH.matcher(value).matches()) {
|
||||
return value;
|
||||
}
|
||||
String extracted = extractJsonPath(value, payload);
|
||||
if (extracted == null) {
|
||||
throw new AwsException("ValidationException", "Cannot resolve Redshift target path: " + value, 400);
|
||||
}
|
||||
try {
|
||||
JsonNode node = objectMapper.readTree(extracted);
|
||||
return node.isTextual() ? node.asText() : node.toString();
|
||||
} catch (JsonProcessingException e) {
|
||||
return extracted;
|
||||
}
|
||||
}
|
||||
|
||||
String applyInputTemplate(String template, String payload) {
|
||||
Matcher m = TEMPLATE_PLACEHOLDER.matcher(template);
|
||||
String ingestionTime = objectMapper.valueToTree(Instant.now().toString()).toString();
|
||||
@@ -281,7 +366,10 @@ public class PipesTargetInvoker {
|
||||
for (String seg : rawSegments) {
|
||||
Matcher am = ARRAY_INDEX.matcher(seg);
|
||||
if (am.matches()) {
|
||||
segments.add(am.group(1));
|
||||
// A leading index such as "$[0].x" has no name before the bracket
|
||||
if (!am.group(1).isEmpty()) {
|
||||
segments.add(am.group(1));
|
||||
}
|
||||
segments.add(am.group(2));
|
||||
} else if (!seg.isEmpty()) {
|
||||
segments.add(seg);
|
||||
@@ -290,12 +378,15 @@ public class PipesTargetInvoker {
|
||||
|
||||
JsonNode current = objectMapper.readTree(json);
|
||||
String previousSegment = null;
|
||||
JsonNode container = null;
|
||||
JsonNode outer = null;
|
||||
boolean sourceFieldDecoded = false;
|
||||
for (String segment : segments) {
|
||||
// AWS Pipes parses an encoded source body when traversing into it.
|
||||
if (!sourceFieldDecoded && isEncodedSourceField(previousSegment) && current.isTextual()) {
|
||||
try {
|
||||
JsonNode decoded = objectMapper.readTree(current.asText());
|
||||
JsonNode decoded = objectMapper.readTree(
|
||||
decodedSourceText(previousSegment, current.asText(), container, outer));
|
||||
if (decoded != null && (decoded.isObject() || decoded.isArray())) {
|
||||
current = decoded;
|
||||
}
|
||||
@@ -304,28 +395,32 @@ public class PipesTargetInvoker {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (current.isArray()) {
|
||||
current = current.path(Integer.parseInt(segment));
|
||||
} else {
|
||||
current = current.path(segment);
|
||||
}
|
||||
if (current.isMissingNode() || current.isNull()) {
|
||||
JsonNode next = current.isArray()
|
||||
? current.path(Integer.parseInt(segment))
|
||||
: current.path(segment);
|
||||
if (next.isMissingNode() || next.isNull()) {
|
||||
return null;
|
||||
}
|
||||
outer = container;
|
||||
container = current;
|
||||
current = next;
|
||||
previousSegment = segment;
|
||||
}
|
||||
if (!sourceFieldDecoded && isEncodedSourceField(previousSegment) && current.isTextual()) {
|
||||
// SQS bodies commonly contain a JSON document as a string. Pipes input
|
||||
// transformation resolves that document when the body itself is selected.
|
||||
String text = decodedSourceText(previousSegment, current.asText(), container, outer);
|
||||
try {
|
||||
JsonNode decoded = objectMapper.readTree(current.asText());
|
||||
sourceFieldDecoded = true;
|
||||
JsonNode decoded = objectMapper.readTree(text);
|
||||
if (decoded != null && (decoded.isObject() || decoded.isArray())) {
|
||||
return decoded.toString();
|
||||
}
|
||||
} catch (JsonProcessingException ignored) {
|
||||
// Keep ordinary textual fields as JSON strings.
|
||||
}
|
||||
if (!text.equals(current.asText())) {
|
||||
return objectMapper.writeValueAsString(text);
|
||||
}
|
||||
}
|
||||
if (current.isValueNode()) {
|
||||
return objectMapper.writeValueAsString(current);
|
||||
@@ -337,6 +432,42 @@ public class PipesTargetInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kinesis {@code data} and Kafka {@code key} and {@code value} arrive as base64, so a path into them has to
|
||||
* read the decoded bytes. Only the fields of a Kinesis or Kafka source record qualify: a field of the same
|
||||
* name in an enrichment response or an SQS body is ordinary text and stays as it is, and so does text that
|
||||
* is not base64.
|
||||
*
|
||||
* @param container the object that holds {@code field}
|
||||
* @param outer the object that holds {@code container}
|
||||
*/
|
||||
private static String decodedSourceText(String field, String text, JsonNode container, JsonNode outer) {
|
||||
if (!isBase64SourceField(field, container, outer)) {
|
||||
return text;
|
||||
}
|
||||
try {
|
||||
return new String(Base64.getDecoder().decode(text), StandardCharsets.UTF_8);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
// Not base64, so the field holds plain text such as an inline JSON document.
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isBase64SourceField(String field, JsonNode container, JsonNode outer) {
|
||||
if ("data".equals(field)) {
|
||||
return outer != null && "aws:kinesis".equals(outer.path("eventSource").asText())
|
||||
&& outer.path("kinesis") == container;
|
||||
}
|
||||
if ("key".equals(field) || "value".equals(field)) {
|
||||
return container != null && isKafkaEventSource(container.path("eventSource").asText());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isKafkaEventSource(String eventSource) {
|
||||
return "aws:kafka".equals(eventSource) || "SelfManagedKafka".equals(eventSource);
|
||||
}
|
||||
|
||||
private static boolean isEncodedSourceField(String segment) {
|
||||
return "body".equals(segment) || "data".equals(segment)
|
||||
|| "key".equals(segment) || "value".equals(segment);
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package io.github.hectorvent.floci.services.redshiftdata;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer.Permission;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Decides whether the role of a Redshift Data API target may submit its SQL, for any service that
|
||||
* delivers events to one (EventBridge, Pipes). The delivering service assumes the target's role, so the
|
||||
* role has to trust that service's principal (with a condition on {@code aws:SourceArn} naming the rule
|
||||
* or pipe, if it has one), and its identity policies, permissions boundary and SCPs have to allow every
|
||||
* {@link Permission} the submission needs. Only active with IAM enforcement on, as for every other API
|
||||
* call. A role of another account is refused, because the account that owns the rule or pipe does not
|
||||
* own it.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class RedshiftTargetAuthorizer {
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final ServiceRoleAuthorizer authorizer;
|
||||
|
||||
@Inject
|
||||
public RedshiftTargetAuthorizer(EmulatorConfig config, ServiceRoleAuthorizer authorizer) {
|
||||
this.config = config;
|
||||
this.authorizer = authorizer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns why {@code roleArn} may not run the target, or nothing when it may.
|
||||
*
|
||||
* @param principal the service principal that assumes the role, such as {@code events.amazonaws.com}
|
||||
* @param accountId the account that owns the rule or pipe
|
||||
* @param sourceArn the rule or pipe, which a trust policy may require in {@code aws:SourceArn}; without
|
||||
* it such a condition does not match
|
||||
* @param permissions what the submission needs, each of which has to be allowed
|
||||
*/
|
||||
public Optional<String> refusal(String principal, String roleArn, String accountId, String sourceArn,
|
||||
List<Permission> permissions) {
|
||||
if (!config.services().iam().enforcementEnabled()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (roleArn == null || roleArn.isBlank()) {
|
||||
return Optional.of("A Redshift target requires an IAM role, and this one names none.");
|
||||
}
|
||||
return authorizer.refusal(roleArn, accountId, principal, sourceArn, permissions);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user