mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ecs): resolve Secrets Manager JSON-key selectors in task definition secrets (#2133)
* fix(ecs): resolve Secrets Manager JSON-key selectors in task definition secrets containerDefinitions[].secrets[].valueFrom now supports the ECS selector suffix (:json-key:version-stage:version-id) that CDK emits for Secret.fromSecretsManager with a field. Parsing mirrors the real ECS agent: the ARN resource is split on colons and disambiguated by segment count (2 = plain reference passed through verbatim, 5 = selector with empty segments meaning unset, anything else is the agent's invalid-ARN error surfaced in stoppedReason). The base ARN is rebuilt before lookup so partial-ARN resolution keeps working, JSON keys are extracted top-level-only after the fetch, and SecretsManagerService now validates that a supplied VersionId and VersionStage name the same version. Closes #1912 * fix(ecs): document and pin JSON serialization of object selector values Review follow-up: object/array json-key values serializing as JSON (rather than Go's map[a:1 b:2] rendering from the agent's %v) is now recorded as the third deliberate divergence in the javadoc and pinned by a test.
This commit is contained in:
1 parent
760115db14
commit
e3c180b952
7 files changed
+364
-14
No files matched your search
+17
-7
@@ -404,14 +404,16 @@ public class EcsContainerManager {
|
||||
// to the task region.
|
||||
String secretRegion = arnRegion(valueFrom, region);
|
||||
String value;
|
||||
String jsonKey = null;
|
||||
try {
|
||||
if (valueFrom != null && valueFrom.startsWith("arn:aws:secretsmanager:")) {
|
||||
// A plain secret ARN (or partial ARN) resolves to its full SecretString.
|
||||
// The ECS `:json-key:version-stage:version-id` selector suffix is not yet
|
||||
// supported: it is passed through unparsed, so a valueFrom carrying one fails
|
||||
// to resolve and stops the task with ResourceInitializationError rather than
|
||||
// silently returning the whole secret. See floci-io/floci#1624.
|
||||
var secret = secretsManagerService.getSecretValue(valueFrom, null, null, secretRegion);
|
||||
// The valueFrom may carry the ECS selector suffix
|
||||
// (:json-key:version-stage:version-id); the parser strips it so the base ARN
|
||||
// reaches SecretsManagerService intact, keeping its partial-ARN fallback working.
|
||||
var selector = SecretsManagerSelector.parse(valueFrom);
|
||||
jsonKey = selector.jsonKey();
|
||||
var secret = secretsManagerService.getSecretValue(selector.secretId(),
|
||||
selector.versionId(), selector.versionStage(), secretRegion);
|
||||
value = secret == null ? null : secret.getSecretString();
|
||||
} else {
|
||||
String parameterName = ssmParameterName(valueFrom);
|
||||
@@ -425,9 +427,17 @@ public class EcsContainerManager {
|
||||
// A Secrets Manager secret stored as SecretBinary (no SecretString) has no string
|
||||
// value to inject as an env var. Real AWS fails the task launch rather than starting
|
||||
// the container with a missing value, so surface the same ResourceInitializationError
|
||||
// instead of emitting a literal "NAME=null".
|
||||
// instead of emitting a literal "NAME=null". Checked before JSON extraction: the real
|
||||
// agent nil-derefs on this input, so failing cleanly is a deliberate improvement.
|
||||
throw resourceInitializationError(valueFrom, "secret value is not a string", 400);
|
||||
}
|
||||
if (jsonKey != null) {
|
||||
try {
|
||||
value = SecretsManagerSelector.extractJsonKey(value, jsonKey);
|
||||
} catch (AwsException e) {
|
||||
throw resourceInitializationError(valueFrom, e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package io.github.hectorvent.floci.services.ecs.container;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
/**
|
||||
* The ECS task-definition secret selector for Secrets Manager {@code valueFrom} values:
|
||||
* {@code arn:aws:secretsmanager:region:account:secret:name:json-key:version-stage:version-id}.
|
||||
*
|
||||
* <p>Parsing mirrors the real ECS agent (amazon-ecs-agent, asmsecret.go): the ARN's resource
|
||||
* segment is split on {@code :} and disambiguated purely by segment count, which is safe
|
||||
* because a secret name can never contain a colon. Exactly two segments
|
||||
* ({@code secret:name}) is a plain reference and the original string is used verbatim as the
|
||||
* secret id, preserving partial-ARN lookups. Exactly five segments is a selector: the secret
|
||||
* id is rebuilt from the first two segments, and empty selector segments mean "unset" — CDK
|
||||
* routinely emits forms like {@code :field::}, {@code :::version-id} and
|
||||
* {@code ::version-stage:}. Any other count is the agent's invalid-ARN error.
|
||||
*/
|
||||
record SecretsManagerSelector(String secretId, String jsonKey, String versionStage, String versionId) {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String INVALID_ARN_MESSAGE =
|
||||
"an invalid ARN format for the AWS Secrets Manager secret was specified. "
|
||||
+ "Specify a valid ARN and try again.";
|
||||
|
||||
static SecretsManagerSelector parse(String valueFrom) {
|
||||
AwsArnUtils.Arn arn;
|
||||
try {
|
||||
arn = AwsArnUtils.parse(valueFrom);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw invalidArn();
|
||||
}
|
||||
String[] segments = arn.resource().split(":", -1);
|
||||
if (segments.length == 2) {
|
||||
return new SecretsManagerSelector(valueFrom, null, null, null);
|
||||
}
|
||||
if (segments.length != 5) {
|
||||
throw invalidArn();
|
||||
}
|
||||
String baseArn = new AwsArnUtils.Arn(arn.partition(), arn.service(), arn.region(),
|
||||
arn.accountId(), segments[0] + ":" + segments[1]).toString();
|
||||
return new SecretsManagerSelector(baseArn,
|
||||
emptyToNull(segments[2]), emptyToNull(segments[3]), emptyToNull(segments[4]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a top-level JSON field from a secret string, mirroring the agent's behavior
|
||||
* (asm.go): missing key and non-JSON secrets are errors. Scalar values are rendered with
|
||||
* {@code asText()}, which keeps large numbers in plain notation where the Go agent's
|
||||
* {@code %v} would print scientific notation. Object and array values deliberately serialize
|
||||
* as JSON, another divergence: the agent's {@code %v} on the unmarshalled value would inject
|
||||
* Go's native rendering ({@code map[a:1 b:2]}), which no consumer can parse back.
|
||||
*/
|
||||
static String extractJsonKey(String secretString, String jsonKey) {
|
||||
JsonNode root;
|
||||
try {
|
||||
root = MAPPER.readTree(secretString);
|
||||
} catch (Exception e) {
|
||||
root = null;
|
||||
}
|
||||
if (root == null || !root.isObject()) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"the secret value is not valid JSON, cannot extract json key " + jsonKey, 400);
|
||||
}
|
||||
JsonNode value = root.get(jsonKey);
|
||||
if (value == null) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"retrieved secret from Secrets Manager did not contain json key " + jsonKey, 400);
|
||||
}
|
||||
return value.isValueNode() ? value.asText() : value.toString();
|
||||
}
|
||||
|
||||
private static String emptyToNull(String segment) {
|
||||
return segment == null || segment.isEmpty() ? null : segment;
|
||||
}
|
||||
|
||||
private static AwsException invalidArn() {
|
||||
return new AwsException("InvalidParameterException", INVALID_ARN_MESSAGE, 400);
|
||||
}
|
||||
}
|
||||
+7
@@ -136,6 +136,13 @@ public class SecretsManagerService {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Secrets Manager can't find the specified secret version.", 400);
|
||||
}
|
||||
// When both selectors are supplied they must name the same version.
|
||||
if (versionStage != null && !versionStage.isEmpty()
|
||||
&& (version.getVersionStages() == null
|
||||
|| !version.getVersionStages().contains(versionStage))) {
|
||||
throw new AwsException("InvalidRequestException",
|
||||
"You provided a VersionStage that is not associated to the provided VersionId.", 400);
|
||||
}
|
||||
} else {
|
||||
String stage = (versionStage != null && !versionStage.isEmpty()) ? versionStage : AWSCURRENT;
|
||||
version = findVersionByStage(secret, stage);
|
||||
|
||||
Reference in new issue
Block a user