mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(secrets-manager): return partial result for missing secrets in batch (#1591)
* fix(secrets-manager): return partial result for missing secrets in batch * fix(secrets-manager): remove hardcoded errorCode mapping * fix(secrets-manager): report deleted and missing current version secrets in BatchGetSecretValue errors
This commit is contained in:
1 parent
97b64a1d3b
commit
bf53ff71d8
6 files changed
+239
-39
No files matched your search
+16
-9
@@ -1,7 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.secretsmanager;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@@ -72,18 +71,16 @@ public class SecretsManagerJsonHandler {
|
||||
}
|
||||
|
||||
List<SecretsManagerService.BatchSecretValue> values;
|
||||
List<SecretsManagerService.BatchGetSecretValueError> errors = List.of();
|
||||
String nextToken = null;
|
||||
|
||||
if (request.has("SecretIdList")) {
|
||||
List<String> secretIdList = new ArrayList<>();
|
||||
request.path("SecretIdList").forEach(id -> secretIdList.add(id.asText()));
|
||||
try {
|
||||
values = service.batchGetSecretValue(secretIdList, region);
|
||||
} catch (AwsException e) {
|
||||
return Response.status(e.getHttpStatus())
|
||||
.entity(new AwsErrorResponse(e.jsonType(), e.getMessage()))
|
||||
.build();
|
||||
}
|
||||
SecretsManagerService.BatchGetSecretValueResult result =
|
||||
service.batchGetSecretValue(secretIdList, region);
|
||||
values = result.values();
|
||||
errors = result.errors();
|
||||
} else {
|
||||
// Validate paging inputs before the service scans and filters the whole store.
|
||||
int maxResults = request.has("MaxResults") ? request.path("MaxResults").asInt() : 20;
|
||||
@@ -145,10 +142,20 @@ public class SecretsManagerJsonHandler {
|
||||
secretValues.add(node);
|
||||
}
|
||||
response.set("SecretValues", secretValues);
|
||||
|
||||
ArrayNode errorNodes = objectMapper.createArrayNode();
|
||||
for (SecretsManagerService.BatchGetSecretValueError error : errors) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("SecretId", error.secretId());
|
||||
node.put("ErrorCode", error.errorCode());
|
||||
node.put("Message", error.message());
|
||||
errorNodes.add(node);
|
||||
}
|
||||
response.set("Errors", errorNodes);
|
||||
|
||||
if (nextToken != null) {
|
||||
response.put("NextToken", nextToken);
|
||||
}
|
||||
response.set("Errors", objectMapper.createArrayNode());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
|
||||
+42
-19
@@ -18,6 +18,7 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@@ -614,39 +615,45 @@ public class SecretsManagerService {
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<BatchSecretValue> batchGetSecretValue(List<String> secretIdList, String region) {
|
||||
List<BatchSecretValue> result = new ArrayList<>();
|
||||
public BatchGetSecretValueResult batchGetSecretValue(List<String> secretIdList, String region) {
|
||||
List<BatchSecretValue> secretValues = new ArrayList<>();
|
||||
List<BatchGetSecretValueError> errors = new ArrayList<>();
|
||||
|
||||
if (secretIdList == null) {
|
||||
return result;
|
||||
return BatchGetSecretValueResult.empty();
|
||||
}
|
||||
|
||||
for (String secretId : secretIdList) {
|
||||
try {
|
||||
Secret secret = resolveSecret(secretId, region);
|
||||
if (secret.getDeletedDate() != null) {
|
||||
errors.add(new BatchGetSecretValueError(secretId, "InvalidRequestException",
|
||||
"You can't perform this operation on the secret because it was marked for deletion."));
|
||||
continue;
|
||||
}
|
||||
|
||||
SecretVersion version = findVersionByStage(secret, AWSCURRENT);
|
||||
if (version != null) {
|
||||
result.add(new BatchSecretValue(
|
||||
secret.getArn(),
|
||||
secret.getName(),
|
||||
version.getSecretString(),
|
||||
version.getSecretBinary(),
|
||||
version.getVersionId(),
|
||||
version.getVersionStages(),
|
||||
version.getCreatedDate()
|
||||
));
|
||||
if (version == null) {
|
||||
errors.add(new BatchGetSecretValueError(secretId, "ResourceNotFoundException",
|
||||
"Secrets Manager can't find the specified secret value for staging label: " + AWSCURRENT));
|
||||
continue;
|
||||
}
|
||||
|
||||
secretValues.add(new BatchSecretValue(
|
||||
secret.getArn(),
|
||||
secret.getName(),
|
||||
version.getSecretString(),
|
||||
version.getSecretBinary(),
|
||||
version.getVersionId(),
|
||||
version.getVersionStages(),
|
||||
version.getCreatedDate()
|
||||
));
|
||||
} catch (AwsException e) {
|
||||
// AWS documentation says: "Secrets Manager doesn't return an error if a secret in the SecretIdList doesn't exist."
|
||||
// Wait, let me re-check that.
|
||||
// Actually, "If any of the secrets in the SecretIdList don't exist, Secrets Manager returns an error."
|
||||
// Let me verify this in the AWS docs.
|
||||
throw e;
|
||||
errors.add(new BatchGetSecretValueError(secretId, e.getErrorCode(), e.getMessage()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
return new BatchGetSecretValueResult(secretValues, errors);
|
||||
}
|
||||
|
||||
public Secret updateSecretVersionStage(String secretId, String moveToVersionId, String removeFromVersionId, String versionStage, String region) {
|
||||
@@ -742,6 +749,22 @@ public class SecretsManagerService {
|
||||
) {
|
||||
}
|
||||
|
||||
public record BatchGetSecretValueError(
|
||||
String secretId,
|
||||
String errorCode,
|
||||
String message
|
||||
) {
|
||||
}
|
||||
|
||||
public record BatchGetSecretValueResult(
|
||||
List<BatchSecretValue> values,
|
||||
List<BatchGetSecretValueError> errors
|
||||
) {
|
||||
public static BatchGetSecretValueResult empty() {
|
||||
return new BatchGetSecretValueResult(Collections.emptyList(), Collections.emptyList());
|
||||
}
|
||||
}
|
||||
|
||||
private Secret resolveSecret(String secretId, String region) {
|
||||
if (secretId.startsWith("arn:")) {
|
||||
// 1. Exact full-ARN match
|
||||
|
||||
Reference in new issue
Block a user