Files
floci/compatibility-tests/compat-terraform/test/kms_update_alias.bats
T
Priamo Germosen 984886f697 feat(kms): implement UpdateAlias (#2324)
* feat(kms): implement UpdateAlias

Repoints an existing alias at a different KMS key, following the
existing createAlias/deleteAlias/resolveKey patterns. Required by the
Terraform AWS provider, which treats a target_key_id change on
aws_kms_alias as an in-place update.

* fix(kms): reject invalid replacement keys in UpdateAlias

UpdateAlias accepted any existing key as a replacement target,
including keys pending deletion or with a different key usage/spec
than the alias's current target. Real AWS KMS rejects both: the
replacement key must be in a compatible state and match the current
target's key usage and key spec.

* fix(kms): compare key type instead of exact key spec in UpdateAlias

The exact KeySpec comparison rejected valid updates between same-type
keys with different specs (e.g. RSA_2048 to RSA_3072), which AWS KMS
accepts as long as both keys share the same key type and usage.

* test(kms): add Terraform compatibility test for UpdateAlias

Verifies the exact scenario from #2291: the Terraform AWS provider
treats target_key_id on aws_kms_alias as an in-place update, so
re-applying with a different target must call UpdateAlias and update
the alias without a destroy/recreate. Self-contained config, mirroring
the eni-destroy-tf pattern, since it needs a second apply with a
different var rather than the shared single-apply main.tf.

* fix(kms): compare key family instead of KeyType in UpdateAlias

Comparing the fine-grained KeyType enum rejected valid cross-family
asymmetric updates (e.g. RSA_2048 to ECC_NIST_P256) that AWS KMS
accepts. AWS only requires the current and new key to be "the same
type (both symmetric or both asymmetric or both HMAC)" - a coarser
three-way split - so compare on that instead.
2026-08-15 12:54:39 -04:00

88 lines
3.1 KiB
Bash

#!/usr/bin/env bats
# KMS UpdateAlias Compatibility Test
#
# Reproduces https://github.com/floci-io/floci/issues/2291
#
# The Terraform AWS provider treats a target_key_id change on aws_kms_alias
# as an in-place update (not a replacement), which requires the emulator to
# support the UpdateAlias action. This verifies that re-applying with a
# different target key updates the existing alias rather than erroring or
# recreating it.
setup_file() {
load 'test_helper/common-setup'
KMS_TF_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")/kms-update-alias-tf" && pwd)"
cd "$KMS_TF_DIR"
echo "# === KMS UpdateAlias Test ===" >&3
echo "# Endpoint: $FLOCI_ENDPOINT" >&3
echo "# Config: $KMS_TF_DIR" >&3
rm -rf .terraform .terraform.lock.hcl terraform.tfstate* 2>/dev/null || true
echo "# --- terraform init ---" >&3
run terraform init -input=false -no-color
if [ "$status" -ne 0 ]; then
echo "# terraform init failed: $output" >&3
return 1
fi
echo "# --- terraform apply (alias -> key A) ---" >&3
run terraform apply -var="endpoint=${FLOCI_ENDPOINT}" -input=false -auto-approve -no-color
if [ "$status" -ne 0 ]; then
echo "# terraform apply failed: $output" >&3
return 1
fi
}
teardown_file() {
load 'test_helper/common-setup'
KMS_TF_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")/kms-update-alias-tf" && pwd)"
cd "$KMS_TF_DIR"
terraform destroy -var="endpoint=${FLOCI_ENDPOINT}" -input=false -auto-approve -no-color || true
rm -rf .terraform .terraform.lock.hcl terraform.tfstate* 2>/dev/null || true
}
setup() {
load 'test_helper/common-setup'
KMS_TF_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")/kms-update-alias-tf" && pwd)"
}
@test "KMS UpdateAlias: alias initially targets key A" {
KEY_A=$(terraform -chdir="$KMS_TF_DIR" output -raw key_a_id)
run aws_cmd kms list-aliases --key-id "$KEY_A" \
--query "Aliases[?AliasName=='alias/floci-kms-update-alias-test'].TargetKeyId | [0]" --output text
assert_success
assert_output "$KEY_A"
}
# The critical assertion: re-applying with a different target_key_id must
# update the existing alias in place, not destroy/recreate it and not fail.
@test "KMS UpdateAlias: re-apply with a different target updates in place" {
cd "$KMS_TF_DIR"
echo "# --- terraform apply (alias -> key B) ---" >&3
run terraform apply -var="endpoint=${FLOCI_ENDPOINT}" -var="use_second_key=true" \
-input=false -auto-approve -no-color
assert_success
assert_output --partial "1 changed"
refute_output --partial "1 destroyed"
KEY_B=$(terraform -chdir="$KMS_TF_DIR" output -raw key_b_id)
run aws_cmd kms list-aliases --key-id "$KEY_B" \
--query "Aliases[?AliasName=='alias/floci-kms-update-alias-test'].TargetKeyId | [0]" --output text
assert_success
assert_output "$KEY_B"
}
@test "KMS UpdateAlias: alias no longer resolves under key A" {
KEY_A=$(terraform -chdir="$KMS_TF_DIR" output -raw key_a_id)
run aws_cmd kms list-aliases --key-id "$KEY_A" \
--query "Aliases[?AliasName=='alias/floci-kms-update-alias-test']" --output text
assert_success
[ -z "$output" ]
}