mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(pipes): deliver to a Kinesis stream target with its PartitionKey (#9185)
This commit is contained in:
1 parent
d8809f6f0e
commit
4d96d3e662
8 files changed
+553
-25
No files matched your search
@@ -365,6 +365,8 @@ public class PipesService implements TagHandler, ResourceProvider {
|
||||
private void validateTargetConfiguration(String target, JsonNode targetParameters) {
|
||||
if (AwsArnUtils.isArnFor(target, "sqs")) {
|
||||
validateSqsTarget(target, targetParameters);
|
||||
} else if (AwsArnUtils.isArnFor(target, "kinesis")) {
|
||||
validateKinesisTarget(target, targetParameters);
|
||||
} else if (PipesTargetInvoker.isRedshiftTarget(target)) {
|
||||
validateRedshiftTarget(targetParameters);
|
||||
}
|
||||
@@ -430,6 +432,68 @@ public class PipesService implements TagHandler, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private void validateKinesisTarget(String target, JsonNode targetParameters) {
|
||||
JsonNode kinesisParameters = targetParameters == null
|
||||
? MissingNode.getInstance() : targetParameters.path("KinesisStreamParameters");
|
||||
if (kinesisParameters.isObject()) {
|
||||
validatePartitionKeySchema(kinesisParameters.path("PartitionKey"));
|
||||
}
|
||||
List<ValidationField> errors = new ArrayList<>();
|
||||
String resource = AwsArnUtils.resourceIfArnFor(target, "kinesis").orElse("");
|
||||
boolean plainStream = resource.startsWith("stream/") && resource.length() > "stream/".length()
|
||||
&& resource.indexOf('/', "stream/".length()) < 0;
|
||||
if (!plainStream) {
|
||||
errors.add(new ValidationField("Target", "Invalid pipe target ARN"));
|
||||
} else if (!AwsArnUtils.parse(target).accountId().equals(regionResolver.getAccountId())) {
|
||||
errors.add(new ValidationField("Target", "Creating cross-account pipe is not permitted."));
|
||||
}
|
||||
if (kinesisParameters.isMissingNode() || kinesisParameters.isNull()) {
|
||||
errors.add(new ValidationField(
|
||||
"TargetParameters.KinesisStreamParameters", "Missing required target parameter."));
|
||||
} else {
|
||||
String partitionKey = PipesTargetInvoker.text(kinesisParameters, "PartitionKey");
|
||||
if (partitionKey != null && isInvalidDynamicPath(partitionKey)) {
|
||||
errors.add(new ValidationField("PartitionKey", invalidPathMessage(partitionKey)));
|
||||
}
|
||||
}
|
||||
if (!errors.isEmpty()) {
|
||||
throw validationFields(errors);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePartitionKeySchema(JsonNode partitionKey) {
|
||||
if (partitionKey.isMissingNode()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"[object has missing required properties ([\"PartitionKey\"])]", 400);
|
||||
}
|
||||
if (!partitionKey.isTextual()) {
|
||||
throw new AwsException("ValidationException", "[instance type (" + jsonTypeName(partitionKey)
|
||||
+ ") does not match any allowed primitive type (allowed: [\"string\"])]", 400);
|
||||
}
|
||||
String value = partitionKey.asText();
|
||||
int length = value.codePointCount(0, value.length());
|
||||
if (length > PipesTargetInvoker.MAX_PARTITION_KEY_LENGTH) {
|
||||
throw new AwsException("ValidationException", "[string \"" + value + "\" is too long (length: "
|
||||
+ length + ", maximum allowed: " + PipesTargetInvoker.MAX_PARTITION_KEY_LENGTH + ")]", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static String jsonTypeName(JsonNode node) {
|
||||
if (node.isNull()) {
|
||||
return "null";
|
||||
}
|
||||
if (node.isIntegralNumber()) {
|
||||
return "integer";
|
||||
}
|
||||
if (node.isNumber()) {
|
||||
return "number";
|
||||
}
|
||||
if (node.isBoolean()) {
|
||||
return "boolean";
|
||||
}
|
||||
return node.isObject() ? "object" : "array";
|
||||
}
|
||||
|
||||
private static boolean isInvalidDynamicPath(String value) {
|
||||
return value.startsWith("$.") && !TARGET_PARAMETER_PATH.matcher(value).matches();
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.ServicePrincipals;
|
||||
import io.floci.aws.core.common.ServiceRegistry;
|
||||
import io.floci.aws.services.eventbridge.EventBridgeService;
|
||||
import io.floci.aws.services.kinesis.KinesisService;
|
||||
import io.floci.aws.services.lambda.LambdaService;
|
||||
import io.floci.aws.services.lambda.model.InvocationType;
|
||||
import io.floci.aws.services.lambda.model.InvokeResult;
|
||||
@@ -42,12 +43,14 @@ public class PipesTargetInvoker {
|
||||
Pattern.compile("<(\\$[^>]+|aws\\.pipes\\.event\\.ingestion-time)>");
|
||||
private static final Pattern ARRAY_INDEX = Pattern.compile("^(.*?)\\[(\\d+)]$");
|
||||
private static final String PIPES_PRINCIPAL = ServicePrincipals.of("pipes");
|
||||
static final int MAX_PARTITION_KEY_LENGTH = 256;
|
||||
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 KinesisService kinesisService;
|
||||
private final EventBridgeService eventBridgeService;
|
||||
private final StepFunctionsService stepFunctionsService;
|
||||
private final RedshiftDataTargetSubmitter redshiftSubmitter;
|
||||
@@ -60,6 +63,7 @@ public class PipesTargetInvoker {
|
||||
public PipesTargetInvoker(LambdaService lambdaService,
|
||||
SqsService sqsService,
|
||||
SnsService snsService,
|
||||
KinesisService kinesisService,
|
||||
EventBridgeService eventBridgeService,
|
||||
StepFunctionsService stepFunctionsService,
|
||||
RedshiftDataTargetSubmitter redshiftSubmitter,
|
||||
@@ -70,6 +74,7 @@ public class PipesTargetInvoker {
|
||||
this.lambdaService = lambdaService;
|
||||
this.sqsService = sqsService;
|
||||
this.snsService = snsService;
|
||||
this.kinesisService = kinesisService;
|
||||
this.eventBridgeService = eventBridgeService;
|
||||
this.stepFunctionsService = stepFunctionsService;
|
||||
this.redshiftSubmitter = redshiftSubmitter;
|
||||
@@ -107,6 +112,8 @@ public class PipesTargetInvoker {
|
||||
invokeSqs(targetArn, payload, region, tp, event);
|
||||
} else if (targetArn.contains(":sns:")) {
|
||||
invokeSns(targetArn, payload, region);
|
||||
} else if (AwsArnUtils.isArnFor(targetArn, "kinesis")) {
|
||||
invokeKinesis(targetArn, payload, region, tp, event);
|
||||
} else if (targetArn.contains(":events:")) {
|
||||
invokeEventBridge(targetArn, payload, region, tp, event);
|
||||
} else if (targetArn.contains(":states:")) {
|
||||
@@ -215,6 +222,32 @@ public class PipesTargetInvoker {
|
||||
LOG.debugv("Pipe delivered to SNS: {0}", arn);
|
||||
}
|
||||
|
||||
private void invokeKinesis(String arn, String payload, String region, JsonNode tp, String event) {
|
||||
JsonNode kinesisParams = tp == null ? MissingNode.getInstance() : tp.path("KinesisStreamParameters");
|
||||
String partitionKey = resolveDynamicParameter(text(kinesisParams, "PartitionKey"), event,
|
||||
"target parameter PartitionKey");
|
||||
validateKinesisPartitionKey(partitionKey);
|
||||
AwsArnUtils.Arn stream = AwsArnUtils.parse(arn);
|
||||
kinesisService.putRecordForAccount(stream.accountId(), PipesPoller.kinesisStreamName(arn),
|
||||
payload.getBytes(StandardCharsets.UTF_8), partitionKey, extractRegionFromArn(arn, region));
|
||||
LOG.debugv("Pipe delivered to Kinesis: {0}", arn);
|
||||
}
|
||||
|
||||
private static void validateKinesisPartitionKey(String partitionKey) {
|
||||
String key = partitionKey == null ? "" : partitionKey;
|
||||
String constraint;
|
||||
if (key.isEmpty()) {
|
||||
constraint = "greater than or equal to 1";
|
||||
} else if (key.codePointCount(0, key.length()) > MAX_PARTITION_KEY_LENGTH) {
|
||||
constraint = "less than or equal to " + MAX_PARTITION_KEY_LENGTH;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
throw new AwsException("ValidationException", "1 validation error detected: Value '" + key
|
||||
+ "' at 'records.1.member.partitionKey' failed to satisfy constraint: Member must have length "
|
||||
+ constraint, 400);
|
||||
}
|
||||
|
||||
private void invokeEventBridge(String arn, String payload, String region, JsonNode tp, String event) {
|
||||
String busName = AwsArnUtils.resourceIfArnFor(arn, "events")
|
||||
.filter(resource -> resource.startsWith("event-bus/"))
|
||||
|
||||
Reference in new issue
Block a user