Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ func newProvider(o *config.Options, shared *GlobalServices) *Provider {
// matching real EKS (which auto-creates the cluster SG and infers the VPC).
p.EKS.SetSubnetResolver(p.VPC)
p.SageMaker.SetMonitoring(p.CloudWatch)
// MSK publishes DEFAULT-level AWS/Kafka metrics for each ACTIVE provisioned
// cluster once a minute, from the serve scheduler (see Tickables).
p.Kafka.SetMonitoring(p.CloudWatch)
// CloudWatch alarm -> SNS: an alarm state transition fires its configured
// SNS-topic actions, fanning a notification out to the topic's subscribers.
p.CloudWatch.SetSNSPublisher(p.SNS)
Expand Down Expand Up @@ -638,9 +641,10 @@ func awsDrivers(p *Provider) *resourcediscovery.Drivers {
// background ticker calls each one on its interval. New time-driven services
// register here.
func (p *Provider) Tickables() []config.Tickable {
// SQS and EFS run before CloudWatch so the alarms evaluated in the same
// tick see the queue gauges and AWS/EFS metrics they just published.
return []config.Tickable{p.SQS, p.EFS, p.CloudWatch, p.SSM, p.CloudFormation, p.Batch}
// SQS, EFS and Kafka run before CloudWatch so the alarms evaluated in the
// same tick see the queue gauges and AWS/EFS and AWS/Kafka metrics they just
// published.
return []config.Tickable{p.SQS, p.EFS, p.Kafka, p.CloudWatch, p.SSM, p.CloudFormation, p.Batch}
}

// Close tears down any real engines wired into the provider via
Expand Down
8 changes: 8 additions & 0 deletions providers/aws/kafka/cluster_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (m *Mock) PutClusterPolicy(_ context.Context, clusterARN string, body json.
cd.mu.Lock()
defer cd.mu.Unlock()

if err := m.requireActive(cd); err != nil {
return "", err
}

if req.CurrentVersion != "" && req.CurrentVersion != cd.policyVersion {
return "", badRequest(
"currentVersion %q does not match policy version %q", req.CurrentVersion, cd.policyVersion)
Expand Down Expand Up @@ -75,6 +79,10 @@ func (m *Mock) DeleteClusterPolicy(_ context.Context, clusterARN string) error {
cd.mu.Lock()
defer cd.mu.Unlock()

if err := m.requireActive(cd); err != nil {
return err
}

cd.policy = ""
cd.policyVersion = ""

Expand Down
101 changes: 61 additions & 40 deletions providers/aws/kafka/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/json"
"sort"
"strings"
"time"

"github.com/stackshy/cloudemu/v2/internal/idgen"
"github.com/stackshy/cloudemu/v2/services/kafka/driver"
)

Expand Down Expand Up @@ -150,16 +150,17 @@ func (m *Mock) getClusterBR(arn string) (*clusterData, error) {
// with mkErr so each op returns the exception the SDK actually models for it.
func (m *Mock) getClusterErr(arn string, mkErr func(string, ...any) error) (*clusterData, error) {
cd, ok := m.clusters.Get(arn)
if !ok {
if !ok || m.reapIfGone(arn, cd) {
return nil, mkErr("cluster not found: %s", arn)
}

return cd, nil
}

// CreateCluster provisions a provisioned (v1) cluster that is immediately
// Active. The cluster name is claimed atomically under createMu so a duplicate
// name is rejected with ConflictException.
// CreateCluster provisions a provisioned (v1) cluster. It reports ACTIVE, or
// CREATING for a settle window under AsyncSettle. The cluster name is claimed
// atomically under createMu so a duplicate name is rejected with
// ConflictException.
//
//nolint:gocritic // hugeParam: signature fixed by driver.Kafka (by-value input).
func (m *Mock) CreateCluster(_ context.Context, in driver.CreateClusterInput) (*driver.Cluster, error) {
Expand Down Expand Up @@ -192,37 +193,17 @@ func (m *Mock) CreateCluster(_ context.Context, in driver.CreateClusterInput) (*
kafkaVer = defaultKafkaVer
}

m.createMu.Lock()
defer m.createMu.Unlock()

if _, dup := m.clusterNames.Get(in.ClusterName); dup {
return nil, conflict("cluster already exists: %s", in.ClusterName)
}

arn := m.clusterARN(in.ClusterName)

cluster := driver.Cluster{
ClusterARN: arn,
return m.insertCluster(driver.Cluster{
ClusterName: in.ClusterName,
ClusterType: driver.ClusterTypeProvisioned,
State: driver.ClusterStateActive,
CurrentVersion: "K" + idgen.GenerateID(""),
KafkaVersion: kafkaVer,
NumberOfBrokerNodes: in.NumberOfBrokerNodes,
BrokerNodeGroupInfo: copyBrokerNodeGroupInfo(in.BrokerNodeGroupInfo),
StorageMode: in.StorageMode,
EnhancedMonitoring: in.EnhancedMonitoring,
Tags: copyTags(in.Tags),
CreationTime: m.now(),
RawOptions: copyRaw(in.RawOptions),
}

m.clusters.Set(arn, &clusterData{cluster: cluster})
m.clusterNames.Set(in.ClusterName, arn)

out := snapshotCluster(cluster)

return &out, nil
})
}

// DescribeCluster returns a deep copy of the stored cluster.
Expand All @@ -233,34 +214,55 @@ func (m *Mock) DescribeCluster(_ context.Context, arn string) (*driver.Cluster,
}

cd.mu.RLock()
defer cd.mu.RUnlock()

out := snapshotCluster(cd.cluster)
out := m.observedClusterLocked(cd)
cd.mu.RUnlock()

return &out, nil
}

// ListClusters lists clusters (optionally filtered by name prefix), sorted by
// name for deterministic paging.
func (m *Mock) ListClusters(
_ context.Context, namePrefix string, page driver.Page,
) (clusters []driver.Cluster, next string, err error) {
// visibleClusters returns observed deep copies of every cluster real MSK would
// still list, lazily removing any whose DELETING window has elapsed.
func (m *Mock) visibleClusters() []driver.Cluster {
vals := m.clusters.SortedValues()

all := make([]driver.Cluster, 0, len(vals))

for _, cd := range vals {
cd.mu.RLock()
snap := snapshotCluster(cd.cluster)
arn := cd.cluster.ClusterARN
cd.mu.RUnlock()

if namePrefix != "" && !strings.HasPrefix(snap.ClusterName, namePrefix) {
if m.reapIfGone(arn, cd) {
continue
}

cd.mu.RLock()
snap := m.observedClusterLocked(cd)
cd.mu.RUnlock()

all = append(all, snap)
}

return all
}

// ListClusters lists clusters (optionally filtered by name prefix), sorted by
// name for deterministic paging.
func (m *Mock) ListClusters(
_ context.Context, namePrefix string, page driver.Page,
) (clusters []driver.Cluster, next string, err error) {
visible := m.visibleClusters()

all := make([]driver.Cluster, 0, len(visible))

for i := range visible {
if namePrefix != "" && !strings.HasPrefix(visible[i].ClusterName, namePrefix) {
continue
}

all = append(all, visible[i])
}

sort.Slice(all, func(i, j int) bool { return all[i].ClusterName < all[j].ClusterName })

start, end, nextTok, err := m.paginate(len(all), page)
Expand All @@ -271,7 +273,11 @@ func (m *Mock) ListClusters(
return all[start:end], nextTok, nil
}

// DeleteCluster removes a cluster and frees its name for reuse.
// DeleteCluster deletes a cluster. With AsyncSettle off (the default) it is
// removed at once and its name freed. Under AsyncSettle it reports DELETING via
// Describe/List for a settle window and is then removed (lazily, on the next
// read), as real MSK does. Deleting a cluster that is already DELETING returns
// DELETING again.
func (m *Mock) DeleteCluster(_ context.Context, arn, currentVersion string) (arnOut, state string, err error) {
cd, err := m.getCluster(arn)
if err != nil {
Expand All @@ -280,6 +286,12 @@ func (m *Mock) DeleteCluster(_ context.Context, arn, currentVersion string) (arn

cd.mu.Lock()

if cd.cluster.State == driver.ClusterStateDeleting {
cd.mu.Unlock()

return arn, driver.ClusterStateDeleting, nil
}

// Honor the optimistic-concurrency version: a stale currentVersion is a 400.
if currentVersion != "" && cd.cluster.CurrentVersion != currentVersion {
cd.mu.Unlock()
Expand All @@ -290,10 +302,19 @@ func (m *Mock) DeleteCluster(_ context.Context, arn, currentVersion string) (arn

name := cd.cluster.ClusterName
cd.cluster.State = driver.ClusterStateDeleting
// A delete supersedes any still-open CREATING window.
cd.readyAt = time.Time{}
cd.deleteAt = m.settleUntil(clusterDeleteSettle)
async := !cd.deleteAt.IsZero()
cd.mu.Unlock()

m.clusters.Delete(arn)
m.clusterNames.Delete(name)
if async {
return arn, driver.ClusterStateDeleting, nil
}

m.reapMu.Lock()
m.removeCluster(arn, name)
m.reapMu.Unlock()

return arn, driver.ClusterStateDeleting, nil
}
Expand Down
29 changes: 20 additions & 9 deletions providers/aws/kafka/clusters_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,18 @@ func (m *Mock) createClusterV2Serverless(in driver.CreateClusterV2Input) (*drive
}

// insertCluster finishes minting a cluster (ARN, version, state, creation time)
// and stores it atomically under createMu, rejecting a duplicate name.
// and stores it atomically under createMu, rejecting a duplicate name. The
// stored state is ACTIVE; under AsyncSettle a CREATING window is overlaid on
// reads, and the returned cluster reports it.
//
//nolint:gocritic // hugeParam: takes the assembled cluster by value to finalize.
func (m *Mock) insertCluster(cluster driver.Cluster) (*driver.Cluster, error) {
m.createMu.Lock()
defer m.createMu.Unlock()

// A name held by a cluster that has finished deleting is free again.
m.reapName(cluster.ClusterName)

if _, dup := m.clusterNames.Get(cluster.ClusterName); dup {
return nil, conflict("cluster already exists: %s", cluster.ClusterName)
}
Expand All @@ -111,10 +116,18 @@ func (m *Mock) insertCluster(cluster driver.Cluster) (*driver.Cluster, error) {
cluster.CurrentVersion = "K" + idgen.GenerateID("")
cluster.CreationTime = m.now()

m.clusters.Set(cluster.ClusterARN, &clusterData{cluster: cluster})
if cluster.ClusterType == driver.ClusterTypeProvisioned {
cluster.RawOptions = m.withDefaultEncryption(cluster.RawOptions)
}

cd := &clusterData{cluster: cluster, readyAt: m.settleUntil(clusterCreateSettle)}

m.clusters.Set(cluster.ClusterARN, cd)
m.clusterNames.Set(cluster.ClusterName, cluster.ClusterARN)

out := snapshotCluster(cluster)
cd.mu.RLock()
out := m.observedClusterLocked(cd)
cd.mu.RUnlock()

return &out, nil
}
Expand All @@ -134,14 +147,12 @@ func (m *Mock) ListClustersV2(
return nil, "", vErr
}

vals := m.clusters.SortedValues()
visible := m.visibleClusters()

all := make([]driver.Cluster, 0, len(vals))
all := make([]driver.Cluster, 0, len(visible))

for _, cd := range vals {
cd.mu.RLock()
snap := snapshotCluster(cd.cluster)
cd.mu.RUnlock()
for i := range visible {
snap := visible[i]

if namePrefix != "" && !strings.HasPrefix(snap.ClusterName, namePrefix) {
continue
Expand Down
101 changes: 101 additions & 0 deletions providers/aws/kafka/encryption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package kafka

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"

"github.com/stackshy/cloudemu/v2/internal/idgen"
)

// encryptionInfoKey is the RawOptions key holding a provisioned cluster's
// EncryptionInfo block (the wire layer stores the request block under it).
const encryptionInfoKey = "encryptionInfo"

// managedKafkaKeyARN returns the ARN of this account+region's AWS managed key
// for MSK (alias/aws/kafka). Real MSK encrypts data at rest under that key when
// a create names none, and DescribeCluster reports its key ARN in
// encryptionAtRest.dataVolumeKMSKeyId
// (https://docs.aws.amazon.com/msk/latest/developerguide/msk-working-with-encryption.html).
// There is one such key per account and region, so the key id is a UUID derived
// deterministically from both: every cluster in the account+region reports the
// same key, as in AWS.
func (m *Mock) managedKafkaKeyARN() string {
sum := sha256.Sum256([]byte("alias/aws/kafka|" + m.opts.AccountID + "|" + m.opts.Region))
// Shape the digest as a version-4 UUID, like a real KMS key id.
sum[6] = sum[6]&0x0f | 0x40
sum[8] = sum[8]&0x3f | 0x80
h := hex.EncodeToString(sum[:16])
keyID := h[0:8] + "-" + h[8:12] + "-" + h[12:16] + "-" + h[16:20] + "-" + h[20:32]

return idgen.AWSARN("kms", m.opts.Region, m.opts.AccountID, "key/"+keyID)
}

// withDefaultEncryption returns raw with the cluster's EncryptionInfo completed
// the way real MSK completes it. Every field the request leaves out gets its
// documented default: encryptionAtRest.dataVolumeKMSKeyId the AWS managed key,
// encryptionInTransit.clientBroker TLS and encryptionInTransit.inCluster true
// (https://docs.aws.amazon.com/msk/1.0/apireference/clusters.html#clusters-model-encryptionintransit).
// Fields the request sets, including inCluster false, are kept, as is any other
// content of the block. A block that is not a JSON object is left as sent. raw
// is not mutated.
func (m *Mock) withDefaultEncryption(raw map[string]json.RawMessage) map[string]json.RawMessage {
out := copyRaw(raw)
if out == nil {
out = map[string]json.RawMessage{}
}

var info map[string]json.RawMessage
if blk := out[encryptionInfoKey]; len(blk) > 0 {
if err := json.Unmarshal(blk, &info); err != nil {
return out
}
}

if info == nil {
info = map[string]json.RawMessage{}
}

changed := defaultField(info, "encryptionAtRest", "dataVolumeKMSKeyId", m.managedKafkaKeyARN())
changed = defaultField(info, "encryptionInTransit", "clientBroker", "TLS") || changed
changed = defaultField(info, "encryptionInTransit", "inCluster", true) || changed

if changed {
out[encryptionInfoKey] = mustJSON(info)
}

return out
}

// defaultField sets info[block][field] to val when the field is absent, null or
// an empty string, creating the block if needed, and reports whether it did.
func defaultField(info map[string]json.RawMessage, block, field string, val any) bool {
var blk map[string]json.RawMessage
if r, ok := info[block]; ok {
_ = json.Unmarshal(r, &blk)
}

if blk == nil {
blk = map[string]json.RawMessage{}
}

if v, ok := blk[field]; ok && string(v) != "null" && string(v) != `""` {
return false
}

blk[field] = mustJSON(val)
info[block] = mustJSON(blk)

return true
}

// mustJSON marshals a value built from JSON-safe types (maps, strings, bools,
// raw messages), which cannot fail.
func mustJSON(v any) json.RawMessage {
b, err := json.Marshal(v)
if err != nil {
return json.RawMessage("null")
}

return b
}
Loading
Loading