mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(msk): support SCRAM secret associations (#6221)
* feat(msk): add SCRAM secret association metadata APIs Add association, listing and disassociation endpoints with persisted metadata and secret validation. Broker user management and reconciliation follow separately. * test(msk): enhance cleanup logic in MskTest for SCRAM secrets and KMS keys * fix(msk): accept resolved secret ARNs with generated suffixes * fix(msk): reject KMS alias ARNs for SCRAM secrets Closes #6229 * fix(msk): reject partial secret ARNs with operation-specific errors * fix(msk): clean up SCRAM associations for deleted clusters Add scheduled, account-aware cleanup of association left after cluster delete * fix(msk): allow disassociation of deleted SCRAM secrets
This commit is contained in:
1 parent
deeb3b3075
commit
756e72273c
10 files changed
+879
-3
No files matched your search
@@ -30,10 +30,12 @@ public class MskController {
|
||||
private static final String PROVISIONED_CLUSTER_TYPE = "PROVISIONED";
|
||||
|
||||
private final MskService mskService;
|
||||
private final MskScramSecretsService mskScramSecretsService;
|
||||
|
||||
@Inject
|
||||
public MskController(MskService mskService) {
|
||||
public MskController(MskService mskService, MskScramSecretsService mskScramSecretsService) {
|
||||
this.mskService = mskService;
|
||||
this.mskScramSecretsService = mskScramSecretsService;
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -96,6 +98,43 @@ public class MskController {
|
||||
return Response.ok(Map.of("bootstrapBrokerString", bootstrapBrokers)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/v1/clusters/{clusterArn}/scram-secrets")
|
||||
public Response batchAssociateScramSecret(@PathParam("clusterArn") String clusterArn,
|
||||
Map<String, Object> request) {
|
||||
List<String> secretArns = request == null ? null
|
||||
: asStringList(request.get("secretArnList"), "secretArnList");
|
||||
List<Map<String, String>> unprocessedScramSecrets =
|
||||
mskScramSecretsService.batchAssociateScramSecret(clusterArn, secretArns);
|
||||
return Response.ok(Map.of("clusterArn", clusterArn, "unprocessedScramSecrets", unprocessedScramSecrets)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/clusters/{clusterArn}/scram-secrets")
|
||||
public Response listScramSecrets(@PathParam("clusterArn") String clusterArn,
|
||||
@QueryParam("maxResults") String maxResults,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
PaginatedResult<String> result = mskScramSecretsService.listScramSecrets(clusterArn,
|
||||
Pagination.parseMaxResults(maxResults, "BadRequestException"), nextToken);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("secretArnList", result.items());
|
||||
if (result.nextToken() != null) {
|
||||
response.put("nextToken", result.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@PATCH
|
||||
@Path("/v1/clusters/{clusterArn}/scram-secrets")
|
||||
public Response batchDisassociateScramSecret(@PathParam("clusterArn") String clusterArn,
|
||||
Map<String, Object> request) {
|
||||
List<String> secretArns = request == null ? null
|
||||
: asStringList(request.get("secretArnList"), "secretArnList");
|
||||
List<Map<String, String>> unprocessedScramSecrets =
|
||||
mskScramSecretsService.batchDisassociateScramSecret(clusterArn, secretArns);
|
||||
return Response.ok(Map.of("clusterArn", clusterArn, "unprocessedScramSecrets", unprocessedScramSecrets)).build();
|
||||
}
|
||||
|
||||
// ── Configurations ───────────────────────────────────────────────────────
|
||||
|
||||
@POST
|
||||
|
||||
@@ -0,0 +1,297 @@
|
||||
package io.github.hectorvent.floci.services.msk;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
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;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.kms.KmsService;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsKey;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsKeySpec;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsKeyUsage;
|
||||
import io.github.hectorvent.floci.services.msk.model.MskCluster;
|
||||
import io.github.hectorvent.floci.services.msk.model.MskScramAssociations;
|
||||
import io.github.hectorvent.floci.services.msk.model.ScramSecretAssociation;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService.BatchGetSecretValueError;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService.BatchGetSecretValueResult;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService.BatchSecretValue;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ApplicationScoped
|
||||
public class MskScramSecretsService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(MskScramSecretsService.class);
|
||||
private static final int MAX_PAGE = 100;
|
||||
|
||||
private final AccountAwareStorageBackend<MskScramAssociations> storage;
|
||||
private final MskService mskService;
|
||||
private final SecretsManagerService secretsManagerService;
|
||||
private final KmsService kmsService;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final ScheduledExecutorService reconciler = Executors.newSingleThreadScheduledExecutor();
|
||||
private final Object lock = new Object();
|
||||
|
||||
@Inject
|
||||
public MskScramSecretsService(StorageFactory storageFactory, MskService mskService,
|
||||
SecretsManagerService secretsManagerService,
|
||||
KmsService kmsService, ObjectMapper objectMapper) {
|
||||
this.storage = storageFactory.create("msk", "msk-scram-associations.json",
|
||||
new TypeReference<Map<String, MskScramAssociations>>() {});
|
||||
this.mskService = mskService;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.kmsService = kmsService;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
reconciler.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
reconcileScramSecrets();
|
||||
} catch (RuntimeException e) {
|
||||
LOG.error("MSK SCRAM reconciliation failed; retrying on the next scheduled run", e);
|
||||
}
|
||||
}, 5, 5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void shutdown() {
|
||||
reconciler.shutdown();
|
||||
}
|
||||
|
||||
void reconcileScramSecrets() {
|
||||
for (AccountAwareStorageBackend.AccountEntry<MskScramAssociations> entry
|
||||
: storage.scanAllAccountEntries(key -> true)) {
|
||||
synchronized (lock) {
|
||||
if (mskService.getClusterForAccount(entry.accountId(), entry.key()).isEmpty()) {
|
||||
storage.deleteForAccount(entry.accountId(), entry.key());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<Map<String, String>> batchAssociateScramSecret(String clusterArn, List<String> secretArns) {
|
||||
synchronized (lock) {
|
||||
MskCluster cluster = scramCluster(clusterArn);
|
||||
validateSecretArns(secretArns);
|
||||
if (new HashSet<>(secretArns).size() != secretArns.size()) {
|
||||
throw new AwsException("BadRequestException", "secretArnList must not contain duplicate ARNs.", 400,
|
||||
Map.of("invalidParameter", "secretArnList"));
|
||||
}
|
||||
MskScramAssociations state = associationsFor(clusterArn);
|
||||
List<Map<String, String>> unprocessedScramSecrets = new ArrayList<>();
|
||||
for (String secretArn : secretArns) {
|
||||
boolean newAssociation = !state.getAssociations().containsKey(secretArn);
|
||||
try {
|
||||
Secret secret = validateSecretArn(clusterArn, secretArn);
|
||||
if (!newAssociation) {
|
||||
throw new AwsException("InvalidSecretArn", "The provided secret ARN is invalid.", 400);
|
||||
}
|
||||
validateSecretEncryption(secret);
|
||||
// Amazon MSK reads only the AWSCURRENT version of an associated secret
|
||||
BatchSecretValue secretValue = readSecret(secretArn);
|
||||
ScramSecretAssociation association = state.getAssociations()
|
||||
.computeIfAbsent(secretArn, ignored -> new ScramSecretAssociation());
|
||||
ScramCredentials credentials = readScramCredentials(secretValue.secretString());
|
||||
association.setUsername(credentials.username());
|
||||
putAssociations(clusterArn, state);
|
||||
} catch (AwsException e) {
|
||||
if (newAssociation && state.getAssociations().remove(secretArn) != null) {
|
||||
putAssociations(clusterArn, state);
|
||||
}
|
||||
unprocessedScramSecrets.add(scramFailure(secretArn, e));
|
||||
}
|
||||
}
|
||||
return unprocessedScramSecrets;
|
||||
}
|
||||
}
|
||||
|
||||
public PaginatedResult<String> listScramSecrets(String clusterArn, Integer maxResults, String nextToken) {
|
||||
synchronized (lock) {
|
||||
scramCluster(clusterArn);
|
||||
return Pagination.paginate(new ArrayList<>(associationsFor(clusterArn).getAssociations().keySet()),
|
||||
arn -> arn, maxResults, nextToken, MAX_PAGE, "BadRequestException");
|
||||
}
|
||||
}
|
||||
|
||||
public List<Map<String, String>> batchDisassociateScramSecret(String clusterArn, List<String> secretArns) {
|
||||
synchronized (lock) {
|
||||
scramCluster(clusterArn);
|
||||
validateSecretArns(secretArns);
|
||||
MskScramAssociations state = associationsFor(clusterArn);
|
||||
List<Map<String, String>> unprocessedScramSecrets = new ArrayList<>();
|
||||
for (String secretArn : secretArns) {
|
||||
try {
|
||||
if (state.getAssociations().remove(secretArn) == null) {
|
||||
throw new AwsException("InvalidSecretArn", "The provided secret ARN is invalid.", 400);
|
||||
}
|
||||
putAssociations(clusterArn, state);
|
||||
} catch (AwsException e) {
|
||||
unprocessedScramSecrets.add(scramFailure(secretArn, e));
|
||||
}
|
||||
}
|
||||
return unprocessedScramSecrets;
|
||||
}
|
||||
}
|
||||
|
||||
private MskCluster scramCluster(String clusterArn) {
|
||||
MskCluster cluster = mskService.describeCluster(clusterArn);
|
||||
if (mskService.isServerless(cluster)) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"SCRAM secrets are not supported for serverless clusters.", 400,
|
||||
Map.of("invalidParameter", "clusterArn"));
|
||||
}
|
||||
return cluster;
|
||||
}
|
||||
|
||||
private void validateSecretArns(List<String> secretArns) {
|
||||
if (secretArns == null || secretArns.isEmpty() || secretArns.size() > 10
|
||||
|| secretArns.stream().anyMatch(arn -> arn == null || arn.isBlank())) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"secretArnList must contain between 1 and 10 secret ARNs.", 400,
|
||||
Map.of("invalidParameter", "secretArnList"));
|
||||
}
|
||||
}
|
||||
|
||||
private Secret validateSecretArn(String clusterArn, String secretArn) {
|
||||
if (!AwsArnUtils.isArn(secretArn)) {
|
||||
throw new AwsException("BadRequestException", "Invalid secret ARN.", 400,
|
||||
Map.of("invalidParameter", "secretArnList"));
|
||||
}
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(secretArn);
|
||||
AwsArnUtils.Arn owner = AwsArnUtils.parse(clusterArn);
|
||||
if (!"secretsmanager".equals(arn.service()) || !arn.resource().startsWith("secret:")
|
||||
|| !owner.partition().equals(arn.partition()) || !owner.region().equals(arn.region())
|
||||
|| !owner.accountId().equals(arn.accountId())) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Secret must belong to the cluster's account, region and partition.", 400,
|
||||
Map.of("invalidParameter", "secretArnList"));
|
||||
}
|
||||
String secretName = arn.resource().substring("secret:".length());
|
||||
if (!secretName.startsWith("AmazonMSK_")) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"The name of secrets associated with an Amazon MSK cluster must have the prefix AmazonMSK_.", 400,
|
||||
Map.of("invalidParameter", "secretArnList"));
|
||||
}
|
||||
Secret secret = secretsManagerService.describeSecret(secretArn, arn.region());
|
||||
if (!secretArn.equals(secret.getArn())) {
|
||||
throw new AwsException("InvalidSecretArn", "Amazon MSK wasn't able to access the provided secret.", 400);
|
||||
}
|
||||
return secret;
|
||||
}
|
||||
|
||||
private void validateSecretEncryption(Secret secret) {
|
||||
String region = AwsArnUtils.parse(secret.getArn()).region();
|
||||
String keyId = secret.getKmsKeyId();
|
||||
boolean hasAliasPrefix = keyId != null && keyId.startsWith("alias/");
|
||||
boolean hasAliasArn = AwsArnUtils.isArnFor(keyId, "kms") && AwsArnUtils.parse(keyId).resource().startsWith("alias/");
|
||||
if (hasAliasPrefix || hasAliasArn) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"SCRAM secrets must reference a KMS key ID or key ARN instead of an alias.", 400,
|
||||
Map.of("invalidParameter", "secretArnList"));
|
||||
}
|
||||
|
||||
if (keyId != null && !keyId.isBlank()) {
|
||||
KmsKey key = kmsService.describeKey(keyId, region);
|
||||
if ("CUSTOMER".equals(key.getKeyManager())
|
||||
&& key.getKeySpec() == KmsKeySpec.SYMMETRIC_DEFAULT
|
||||
&& key.getKeyUsage() == KmsKeyUsage.ENCRYPT_DECRYPT) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new AwsException("BadRequestException",
|
||||
"SCRAM secrets must be encrypted with a customer-managed symmetric KMS key.", 400,
|
||||
Map.of("invalidParameter", "secretArnList"));
|
||||
}
|
||||
|
||||
private Map<String, String> scramFailure(String secretArn, AwsException error) {
|
||||
return Map.of("secretArn", secretArn, "errorCode", error.getErrorCode(), "errorMessage", error.getMessage());
|
||||
}
|
||||
|
||||
private MskScramAssociations associationsFor(String clusterArn) {
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(clusterArn);
|
||||
return storage.getForAccount(arn.accountId(), clusterArn).orElseGet(MskScramAssociations::new);
|
||||
}
|
||||
|
||||
private void putAssociations(String clusterArn, MskScramAssociations associations) {
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(clusterArn);
|
||||
if (associations.getAssociations().isEmpty()) {
|
||||
storage.deleteForAccount(arn.accountId(), clusterArn);
|
||||
} else {
|
||||
storage.putForAccount(arn.accountId(), clusterArn, associations);
|
||||
}
|
||||
}
|
||||
|
||||
private BatchSecretValue readSecret(String secretArn) {
|
||||
// Exact-ARN batch reads select AWSCURRENT without updating access timestamps.
|
||||
BatchGetSecretValueResult result = secretsManagerService.batchGetSecretValue(
|
||||
List.of(secretArn), AwsArnUtils.parse(secretArn).region());
|
||||
if (result != null && result.values() != null && result.errors() != null) {
|
||||
if (result.values().isEmpty() && result.errors().size() == 1) {
|
||||
BatchGetSecretValueError error = result.errors().getFirst();
|
||||
if (error != null && secretArn.equals(error.secretId())
|
||||
&& error.errorCode() != null && error.message() != null) {
|
||||
int status = "AccessDeniedException".equals(error.errorCode()) ? 403 : 400;
|
||||
throw new AwsException(error.errorCode(), error.message(), status);
|
||||
}
|
||||
} else if (result.values().size() == 1 && result.errors().isEmpty()) {
|
||||
BatchSecretValue value = result.values().getFirst();
|
||||
if (value != null && secretArn.equals(value.arn()) && value.versionId() != null) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new AwsException("InternalServerError", "Unexpected Secrets Manager batch response.", 500);
|
||||
}
|
||||
|
||||
record ScramCredentials(String username, String password) {
|
||||
}
|
||||
|
||||
ScramCredentials readScramCredentials(String secretString) {
|
||||
if (secretString == null) {
|
||||
throw new AwsException("InternalServerError",
|
||||
"The request could not be processed because of an internal error. Try again.", 500);
|
||||
}
|
||||
JsonNode credentials;
|
||||
try {
|
||||
credentials = objectMapper.readTree(secretString);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new AwsException("InvalidSecretArn", "The provided secret has an invalid schema.", 400);
|
||||
}
|
||||
if (credentials != null && credentials.isNull()) {
|
||||
throw new AwsException("InternalServerError",
|
||||
"The request could not be processed because of an internal error. Try again.", 500);
|
||||
}
|
||||
if (credentials == null || !credentials.isObject()) {
|
||||
throw new AwsException("InvalidSecretArn", "The provided secret has an invalid schema.", 400);
|
||||
}
|
||||
JsonNode username = credentials.path("username");
|
||||
if ((!username.isTextual() && !username.isNumber()) || username.asText().isBlank()) {
|
||||
throw new AwsException("InvalidSecretArn", "The provided secret has an invalid schema.", 400);
|
||||
}
|
||||
JsonNode password = credentials.path("password");
|
||||
if ((!password.isTextual() && !password.isBoolean()) || password.asText().isBlank()) {
|
||||
throw new AwsException("InvalidSecretArn", "The provided secret has an invalid schema.", 400);
|
||||
}
|
||||
return new ScramCredentials(username.asText(), password.asText());
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -647,6 +648,13 @@ public class MskService implements ResourceProvider {
|
||||
return storage.scan(k -> true);
|
||||
}
|
||||
|
||||
public Optional<MskCluster> getClusterForAccount(String accountId, String clusterArn) {
|
||||
if (storage instanceof AccountAwareStorageBackend<MskCluster> aware) {
|
||||
return aware.getForAccount(accountId, clusterArn);
|
||||
}
|
||||
return storage.get(clusterArn);
|
||||
}
|
||||
|
||||
private void putCluster(MskCluster cluster) {
|
||||
if (cluster.getAccountId() != null && storage instanceof AccountAwareStorageBackend<MskCluster> aware) {
|
||||
aware.putForAccount(cluster.getAccountId(), cluster.getClusterArn(), cluster);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.github.hectorvent.floci.services.msk.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
public class MskScramAssociations {
|
||||
|
||||
private Map<String, ScramSecretAssociation> associations = new LinkedHashMap<>();
|
||||
|
||||
public Map<String, ScramSecretAssociation> getAssociations() {
|
||||
return associations;
|
||||
}
|
||||
|
||||
public void setAssociations(Map<String, ScramSecretAssociation> associations) {
|
||||
this.associations = associations;
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package io.github.hectorvent.floci.services.msk.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public class ScramSecretAssociation {
|
||||
|
||||
private String username;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user