mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 01:43:02 -05:00
feat(redshift-serverless): export the audit logs of a namespace to CloudWatch Logs (#8333)
This commit is contained in:
1 parent
6fa18cc99e
commit
7a98f47742
9 files changed
+762
-15
No files matched your search
@@ -27,6 +27,7 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -105,6 +106,11 @@ public class RedshiftAuditLogPublisher {
|
||||
registrations.put(registryKey(accountId, target.identifier()), new Registration(region, target));
|
||||
}
|
||||
|
||||
/** The target registered under {@code identifier}, which a caller that only knows the backend publishes for. */
|
||||
public Optional<RedshiftAuditTarget> registeredTarget(String accountId, String identifier) {
|
||||
return Optional.ofNullable(registrations.get(registryKey(accountId, identifier))).map(Registration::target);
|
||||
}
|
||||
|
||||
public void unregister(String accountId, String clusterIdentifier) {
|
||||
registrations.remove(registryKey(accountId, clusterIdentifier));
|
||||
}
|
||||
@@ -153,7 +159,7 @@ public class RedshiftAuditLogPublisher {
|
||||
* Builds the target of a cluster for the publish overloads, which must never fail the statement being audited:
|
||||
* a cluster whose stored logging settings are malformed is reported and treated as having no target.
|
||||
*/
|
||||
private static RedshiftAuditTarget targetOf(Cluster cluster) {
|
||||
public static RedshiftAuditTarget targetOf(Cluster cluster) {
|
||||
try {
|
||||
return RedshiftAuditTarget.of(cluster);
|
||||
} catch (RuntimeException ex) {
|
||||
|
||||
@@ -103,8 +103,9 @@ class RedshiftDataResourceResolver {
|
||||
RedshiftServerlessService.WorkgroupTarget workgroup =
|
||||
serverlessService.getWorkgroupTarget(request.get("WorkgroupName").asText(), region);
|
||||
String accountId = AwsArnUtils.parse(workgroup.arn()).accountId();
|
||||
String backendKey = accountId + ":" + RedshiftServerlessRuntime.backendId(region, workgroup.workgroupName());
|
||||
SpectrumSession spectrum = new SpectrumSession(accountId,
|
||||
accountId + ":" + RedshiftServerlessRuntime.backendId(region, workgroup.workgroupName()), database,
|
||||
backendKey, database,
|
||||
workgroup.iamRoleArns(), false);
|
||||
if (!database.equals(workgroup.database())) {
|
||||
throw validation("Database " + database + " does not exist in workgroup " + workgroup.workgroupName()
|
||||
@@ -116,7 +117,6 @@ class RedshiftDataResourceResolver {
|
||||
int port = hasProxy ? proxyPort : workgroup.port();
|
||||
if (!hasText(request, "SecretArn")) {
|
||||
String iamUser = resolveIamDbUser();
|
||||
String backendKey = accountId + ":" + RedshiftServerlessRuntime.backendId(region, workgroup.workgroupName());
|
||||
DbIdentity identity = new DbIdentity(iamUser, true, List.of());
|
||||
return new DatabaseTarget(workgroup.arn(), host, port, database,
|
||||
workgroup.masterUsername(), workgroup.masterPassword(), spectrum, null,
|
||||
@@ -129,7 +129,7 @@ class RedshiftDataResourceResolver {
|
||||
user = workgroup.masterUsername();
|
||||
}
|
||||
return new DatabaseTarget(workgroup.arn(), host, port, database,
|
||||
user, creds.password(), spectrum, null, user, workgroup.workgroupName());
|
||||
user, creds.password(), spectrum, null, user, workgroup.workgroupName(), null, backendKey);
|
||||
}
|
||||
|
||||
private String resolveIamDbUser() {
|
||||
|
||||
@@ -17,6 +17,7 @@ import io.floci.aws.services.redshift.RedshiftAuditLogPublisher;
|
||||
import io.floci.aws.services.redshift.RedshiftAuditSql;
|
||||
import io.floci.aws.services.redshift.RedshiftAuditSql.UserChange;
|
||||
import io.floci.aws.services.redshift.RedshiftAuditSql.UserChanges;
|
||||
import io.floci.aws.services.redshift.RedshiftAuditTarget;
|
||||
import io.floci.aws.services.redshift.proxy.CopyJobSql;
|
||||
import io.floci.aws.services.redshift.proxy.RedshiftSqlInterceptor;
|
||||
import io.floci.aws.services.redshift.spectrum.BackendSql;
|
||||
@@ -473,10 +474,28 @@ public class RedshiftDataService implements Resettable, StoredResourceArns {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* What the audit logs of the target's cluster or workgroup take: a cluster's settings, or the target its
|
||||
* workgroup's namespace registered, which is none while the namespace exports no logs.
|
||||
*/
|
||||
private RedshiftAuditTarget auditTarget(RedshiftDataResourceResolver.DatabaseTarget target) {
|
||||
if (auditLogPublisher == null) {
|
||||
return null;
|
||||
}
|
||||
if (target.cluster() != null) {
|
||||
return RedshiftAuditLogPublisher.targetOf(target.cluster());
|
||||
}
|
||||
String backendKey = target.backendKey();
|
||||
int separator = backendKey == null ? -1 : backendKey.indexOf(':');
|
||||
return separator < 0 ? null : auditLogPublisher.registeredTarget(backendKey.substring(0, separator),
|
||||
backendKey.substring(separator + 1)).orElse(null);
|
||||
}
|
||||
|
||||
private void publishConnection(RedshiftDataResourceResolver.DatabaseTarget target, String event, String error) {
|
||||
if (auditLogPublisher != null && target.cluster() != null) {
|
||||
RedshiftAuditTarget auditTarget = auditTarget(target);
|
||||
if (auditTarget != null) {
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(target.arn());
|
||||
auditLogPublisher.publishConnection(target.cluster(), arn.accountId(), arn.region(), target.database(),
|
||||
auditLogPublisher.publishConnection(auditTarget, arn.accountId(), arn.region(), target.database(),
|
||||
target.auditUser(), event, null, error);
|
||||
}
|
||||
}
|
||||
@@ -572,8 +591,9 @@ public class RedshiftDataService implements Resettable, StoredResourceArns {
|
||||
|
||||
private void publishActivity(RedshiftDataStatementStore.StoredStatement stored,
|
||||
RedshiftDataResourceResolver.DatabaseTarget target, String sql) {
|
||||
if (auditLogPublisher != null && target.cluster() != null) {
|
||||
auditLogPublisher.publishActivity(target.cluster(), stored.accountId, stored.region,
|
||||
RedshiftAuditTarget auditTarget = auditTarget(target);
|
||||
if (auditTarget != null) {
|
||||
auditLogPublisher.publishActivity(auditTarget, stored.accountId, stored.region,
|
||||
target.database(), target.auditUser(), sql);
|
||||
}
|
||||
}
|
||||
@@ -590,9 +610,10 @@ public class RedshiftDataService implements Resettable, StoredResourceArns {
|
||||
|
||||
private void publishUserChanges(RedshiftDataStatementStore.StoredStatement stored,
|
||||
RedshiftDataResourceResolver.DatabaseTarget target, List<UserChange> changes) {
|
||||
if (auditLogPublisher != null && target.cluster() != null) {
|
||||
RedshiftAuditTarget auditTarget = auditTarget(target);
|
||||
if (auditTarget != null) {
|
||||
for (UserChange change : changes) {
|
||||
auditLogPublisher.publishUserChange(target.cluster(), stored.accountId, stored.region,
|
||||
auditLogPublisher.publishUserChange(auditTarget, stored.accountId, stored.region,
|
||||
target.database(), change.username(), change.action(), change.oldUsername());
|
||||
}
|
||||
}
|
||||
|
||||
+78
-5
@@ -11,6 +11,8 @@ import io.floci.aws.core.common.Resettable;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.redshift.RedshiftAuditLogPublisher;
|
||||
import io.floci.aws.services.redshift.RedshiftAuditTarget;
|
||||
import io.floci.aws.services.redshift.SnapshotDumpCopier;
|
||||
import io.floci.aws.services.redshift.TempCredential;
|
||||
import io.floci.aws.services.redshift.model.Endpoint;
|
||||
@@ -128,6 +130,9 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
|
||||
/** Looked up lazily: the endpoint service is built on this one. */
|
||||
@Inject
|
||||
Instance<RedshiftServerlessEndpointAccessService> endpointAccess;
|
||||
/** Looked up lazily so a build without the publisher, and the unit tests that never wire one, still start. */
|
||||
@Inject
|
||||
Instance<RedshiftAuditLogPublisher> auditPublisher;
|
||||
|
||||
public RedshiftServerlessService(StorageFactory storageFactory, RegionResolver regionResolver,
|
||||
RedshiftServerlessEndpoints endpoints, RedshiftServerlessRuntime runtime,
|
||||
@@ -210,6 +215,63 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
|
||||
workgroup.setMasterUsername(masterUsername);
|
||||
workgroup.setRuntimeHost(backend.host());
|
||||
workgroup.setRuntimePort(backend.port());
|
||||
registerAudit(accountId, region, workgroup, namespace);
|
||||
}
|
||||
|
||||
/** Stops the workgroup's runtime, and with it the audit export of its namespace. */
|
||||
private void stopRuntime(String accountId, String region, String workgroupName) {
|
||||
runtime.stop(accountId, region, workgroupName);
|
||||
unregisterAudit(accountId, region, workgroupName);
|
||||
}
|
||||
|
||||
private void unregisterAudit(String accountId, String region, String workgroupName) {
|
||||
publisher().ifPresent(publisher -> publisher.unregister(accountId,
|
||||
RedshiftServerlessRuntime.backendId(region, workgroupName)));
|
||||
}
|
||||
|
||||
private Optional<RedshiftAuditLogPublisher> publisher() {
|
||||
return auditPublisher == null || auditPublisher.isUnsatisfied()
|
||||
? Optional.empty() : Optional.of(auditPublisher.get());
|
||||
}
|
||||
|
||||
/** Re-registers the audit export of the namespace's workgroup after the namespace's settings changed. */
|
||||
private void syncAudit(String accountId, String region, Namespace namespace) {
|
||||
workgroupOf(namespace.getNamespaceName(), region)
|
||||
.ifPresent(workgroup -> registerAudit(accountId, region, workgroup, namespace));
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers what the workgroup's backend exports to CloudWatch Logs: the log types its namespace selects in
|
||||
* {@code logExports}, under {@code /aws/redshift/<namespace>/<log type>}. A workgroup without a running
|
||||
* backend has nothing to export.
|
||||
*/
|
||||
private void registerAudit(String accountId, String region, Workgroup workgroup, Namespace namespace) {
|
||||
publisher().ifPresent(publisher -> {
|
||||
String backendId = RedshiftServerlessRuntime.backendId(region, workgroup.getWorkgroupName());
|
||||
if (hasLiveRuntime(workgroup)) {
|
||||
try {
|
||||
publisher.register(accountId, region, auditTarget(backendId, workgroup, namespace));
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "Failed to register Redshift audit target for workgroup {0}",
|
||||
workgroup.getWorkgroupName());
|
||||
}
|
||||
} else {
|
||||
publisher.unregister(accountId, backendId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static RedshiftAuditTarget auditTarget(String backendId, Workgroup workgroup, Namespace namespace) {
|
||||
List<String> exports = namespace.getLogExports() == null ? List.of() : namespace.getLogExports().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
boolean activityLogging = workgroup.getConfigParameters() != null
|
||||
&& workgroup.getConfigParameters().stream()
|
||||
.anyMatch(parameter -> "enable_user_activity_logging".equals(parameter.getParameterKey())
|
||||
&& "true".equalsIgnoreCase(parameter.getParameterValue()));
|
||||
return new RedshiftAuditTarget(backendId, "serverless", "/aws/redshift/" + namespace.getNamespaceName(),
|
||||
"namespace/" + namespace.getNamespaceName(), !exports.isEmpty(), "cloudwatch", null, null, exports,
|
||||
activityLogging, 0L);
|
||||
}
|
||||
|
||||
private Supplier<Optional<RedshiftServerlessRuntime.AdminCredentials>> adminSupplier(
|
||||
@@ -428,12 +490,16 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
|
||||
}
|
||||
storeAttempted = true;
|
||||
namespaces.put(key, updated);
|
||||
syncAudit(workgroups.accountId(), region, updated);
|
||||
if (wasManaged && !managed) {
|
||||
requireAdminSecrets().delete(current, region);
|
||||
}
|
||||
} catch (RuntimeException failure) {
|
||||
if (storeAttempted) {
|
||||
compensate(() -> namespaces.put(key, current), failure, "restore namespace credentials");
|
||||
compensate(() -> {
|
||||
namespaces.put(key, current);
|
||||
syncAudit(workgroups.accountId(), region, current);
|
||||
}, failure, "restore namespace credentials");
|
||||
}
|
||||
if (backendChanged) {
|
||||
compensate(() -> restoreAdminPassword(current, region), failure, "restore backend admin password");
|
||||
@@ -482,6 +548,7 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
|
||||
}
|
||||
}
|
||||
namespaces.put(storageKey(region, previous.getNamespaceName()), previous);
|
||||
syncAudit(workgroups.accountId(), region, previous);
|
||||
}
|
||||
|
||||
public synchronized Namespace deleteNamespace(String namespaceName, String region) {
|
||||
@@ -1251,7 +1318,7 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
|
||||
// The runtime may have started before the failure (a failed put), so tear it down; a
|
||||
// runtime that failed to start has already rolled itself back and this is then a no-op.
|
||||
try {
|
||||
runtime.stop(workgroups.accountId(), region, workgroupName);
|
||||
stopRuntime(workgroups.accountId(), region, workgroupName);
|
||||
} catch (RuntimeException stopFailure) {
|
||||
LOG.warnv(stopFailure, "Could not stop the runtime of workgroup {0} after a failed create",
|
||||
workgroupName);
|
||||
@@ -1325,6 +1392,8 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
|
||||
}
|
||||
}
|
||||
workgroups.put(storageKey(region, workgroupName), updated);
|
||||
namespaces.get(storageKey(region, updated.getNamespaceName()))
|
||||
.ifPresent(namespace -> registerAudit(workgroups.accountId(), region, updated, namespace));
|
||||
return updated;
|
||||
}
|
||||
|
||||
@@ -1333,7 +1402,7 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
|
||||
// The proxy goes first and its failure aborts the delete: a listener that could not be closed
|
||||
// keeps its port, so the workgroup must stay on record and the call can be retried.
|
||||
try {
|
||||
runtime.stop(workgroups.accountId(), region, workgroupName);
|
||||
stopRuntime(workgroups.accountId(), region, workgroupName);
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "Failed to stop the runtime of workgroup {0}", workgroupName);
|
||||
throw new AwsException("InternalServerException",
|
||||
@@ -1623,14 +1692,18 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
|
||||
@Override
|
||||
public void clear() {
|
||||
for (AccountAwareStorageBackend.AccountEntry<Workgroup> entry : workgroups.scanAllAccountEntries(key -> true)) {
|
||||
String region = regionOf(entry.key());
|
||||
String workgroupName = entry.value().getWorkgroupName();
|
||||
try {
|
||||
runtime.stop(entry.accountId(), regionOf(entry.key()), entry.value().getWorkgroupName());
|
||||
stopRuntime(entry.accountId(), region, workgroupName);
|
||||
endpoints.release(entry.value().getEndpoint());
|
||||
} catch (RuntimeException e) {
|
||||
// The workgroup record is about to be cleared, so drop its audit target even if shutdown failed.
|
||||
unregisterAudit(entry.accountId(), region, workgroupName);
|
||||
// The listener may still be bound, so the endpoint stays reserved rather than being
|
||||
// handed to a later cluster or workgroup whose proxy could not bind it.
|
||||
LOG.warnv(e, "Could not stop the runtime of workgroup {0} while clearing state; "
|
||||
+ "its endpoint stays reserved", entry.value().getWorkgroupName());
|
||||
+ "its endpoint stays reserved", workgroupName);
|
||||
}
|
||||
}
|
||||
namespaces.clear();
|
||||
|
||||
Reference in new issue
Block a user