diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamAccessKeyCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamAccessKeyCfnProvisioner.java index 2269ea9e09..f1ea84e5bd 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamAccessKeyCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamAccessKeyCfnProvisioner.java @@ -16,12 +16,12 @@ import java.util.Set; * CloudFormation provisioning for {@code AWS::IAM::AccessKey}. {@code Ref} returns the access key * id, and {@code Fn::GetAtt} exposes the same id plus the one-time {@code SecretAccessKey}. * - *

{@code UserName} and {@code Serial} are the only inputs and both are createOnly. An update that - * changes neither reuses the existing key rather than minting another one, since a user is capped at - * two keys and recreating on every update would fail with {@code LimitExceeded} and orphan the - * earlier keys. A change to either replaces the key (AWS rotates a key by incrementing {@code - * Serial}): a new key is created and the displaced one is deleted once the update commits, through - * {@link ReplacementCleanup}. + *

{@code UserName} and {@code Serial} are createOnly. An update that changes neither reuses the + * existing key rather than minting another one, since a user is capped at two keys and recreating on + * every update would fail with {@code LimitExceeded} and orphan the earlier keys. A change to either + * replaces the key (AWS rotates a key by incrementing {@code Serial}): a new key is created and the + * displaced one is deleted once the update commits, through {@link ReplacementCleanup}. {@code Status} + * (Active/Inactive) is a mutable property, applied to the key in place and reconciled on a reuse. */ @ApplicationScoped public class IamAccessKeyCfnProvisioner implements CfnResourceProvisioner { @@ -48,10 +48,24 @@ public class IamAccessKeyCfnProvisioner implements CfnResourceProvisioner { throw new AwsException("ValidationError", "AWS::IAM::AccessKey requires a UserName.", 400); } String serial = serialOf(props, ctx); + // Status (Active/Inactive) is a mutable property. Validate it up front, before any key is + // created, so a bad value fails cleanly: a user is capped at two keys, so creating one and + // only then rejecting the status could orphan it. + String status = ctx.resolveOptional(props, "Status"); + if (status != null && !"Active".equals(status) && !"Inactive".equals(status)) { + throw new AwsException("ValidationError", + "AWS::IAM::AccessKey Status must be Active or Inactive.", 400); + } Map attributesBefore = new HashMap<>(r.getAttributes()); if (ctx.isUpdate() && !createOnlyChanged(r, ctx, userName, serial)) { - // Neither UserName nor Serial changed: keep the existing key and its id and secret. + // Neither UserName nor Serial changed: keep the existing key and its id and secret. Only + // reconcile Status when the template declares it; an update that omits it leaves the key + // as-is, the way AWS applies only the properties the template changed (so a key + // deactivated out of band is not silently reactivated by an unrelated update). + if (status != null) { + iamService.updateAccessKey(userName, ctx.priorPhysicalId(), status); + } ReplacementCleanup.record(r, ctx, attributesBefore); return; } @@ -61,6 +75,10 @@ public class IamAccessKeyCfnProvisioner implements CfnResourceProvisioner { r.getAttributes().put("Id", key.getAccessKeyId()); r.getAttributes().put("SecretAccessKey", key.getSecretAccessKey()); r.getAttributes().put(SERIAL_ATTR, serial); + // A new key is Active; only touch it when the template asked for Inactive. + if ("Inactive".equals(status)) { + iamService.updateAccessKey(userName, key.getAccessKeyId(), status); + } // On a createOnly change this new key replaced the prior one; record it so the displaced // key is deleted after the update commits, and rolled back to if a later resource fails. ReplacementCleanup.record(r, ctx, attributesBefore); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamAccessKeyCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamAccessKeyCfnProvisionerTest.java index 50b87f4e84..5938904ee7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamAccessKeyCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamAccessKeyCfnProvisionerTest.java @@ -78,6 +78,54 @@ class IamAccessKeyCfnProvisionerTest { verify(iam, never()).createAccessKey(anyString()); } + @Test + void createWithInactiveStatusDeactivatesTheKey() throws Exception { + AccessKey key = new AccessKey(); + key.setAccessKeyId("AKIA1"); + key.setSecretAccessKey("secret-1"); + when(iam.createAccessKey("alice")).thenReturn(key); + + provisioner.provision(resource(), props("{\"UserName\": \"alice\", \"Status\": \"Inactive\"}"), ctx(null)); + + verify(iam).createAccessKey("alice"); + verify(iam).updateAccessKey("alice", "AKIA1", "Inactive"); + } + + @Test + void anUnchangedUpdateReconcilesStatus() throws Exception { + StackResource r = resource(); + r.setPhysicalId("AKIA1"); + r.getAttributes().put(SERIAL_ATTR, ""); + when(iam.findUserNameByAccessKeyId("AKIA1")).thenReturn(Optional.of("alice")); + + provisioner.provision(r, props("{\"UserName\": \"alice\", \"Status\": \"Inactive\"}"), ctx("AKIA1")); + + verify(iam, never()).createAccessKey(anyString()); + verify(iam).updateAccessKey("alice", "AKIA1", "Inactive"); + } + + @Test + void anUpdateThatOmitsStatusLeavesItAlone() throws Exception { + StackResource r = resource(); + r.setPhysicalId("AKIA1"); + r.getAttributes().put(SERIAL_ATTR, ""); + when(iam.findUserNameByAccessKeyId("AKIA1")).thenReturn(Optional.of("alice")); + + provisioner.provision(r, props("{\"UserName\": \"alice\"}"), ctx("AKIA1")); + + verify(iam, never()).createAccessKey(anyString()); + verify(iam, never()).updateAccessKey(anyString(), anyString(), anyString()); + } + + @Test + void aBogusStatusIsRejectedBeforeCreatingAKey() { + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision( + resource(), props("{\"UserName\": \"alice\", \"Status\": \"Bogus\"}"), ctx(null))); + + assertEquals("ValidationError", e.getErrorCode()); + verify(iam, never()).createAccessKey(anyString()); + } + @Test void bumpingSerialOnUpdateRotatesTheKey() throws Exception { StackResource r = resource();