refactor(cloudformation): move IAM leaf types into per-type provisioners (#3913)

Move AWS::IAM::AccessKey and AWS::IAM::InstanceProfile out of the CloudFormationResourceProvisioner monolith into IamAccessKeyCfnProvisioner and IamInstanceProfileCfnProvisioner, each over IamService, matching the existing one-type-per-class IAM provisioners (IamRole, IamUser). The monolith keeps iamService, still used by the Policy and ManagedPolicy arms that migrate in a later PR.

Ref is preserved: the access key id for AccessKey, the profile name for InstanceProfile. The move also publishes AccessKey Id as an Fn::GetAtt key the monolith left unresolved, because leaving LEGACY_SWITCH subjects a type to CfnSchemaCoverageTest, which requires every readOnly attribute to be set; SecretAccessKey and InstanceProfile Arn were already set, so no gap rows are needed. InstanceProfile delete tolerates an already-gone profile via CfnDeletes.safeDelete on NoSuchEntity while letting DeleteConflict propagate. AccessKey keeps the legacy no-op delete (removing a key needs the owning user name the id-only path lacks); deleting it properly is a follow-up.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-19 09:58:21 -04:00
1 parent a154470172
commit fb9deb1e89
8 files changed
+461 -32

No files matched your search

@@ -108,8 +108,6 @@ public class CloudFormationResourceProvisioner {
"AWS::ApiGatewayV2::Route",
"AWS::ApiGatewayV2::Stage",
"AWS::CloudFormation::CustomResource",
"AWS::IAM::AccessKey",
"AWS::IAM::InstanceProfile",
"AWS::IAM::ManagedPolicy",
"AWS::IAM::Policy",
"AWS::Lambda::Function",
@@ -217,11 +215,9 @@ public class CloudFormationResourceProvisioner {
case "AWS::Lambda::Function" -> provisionLambda(resource, properties, engine, region, accountId, stackName);
case "AWS::Lambda::LayerVersion" ->
provisionLambdaLayerVersion(resource, properties, engine, region, stackName);
case "AWS::IAM::AccessKey" -> provisionIamAccessKey(resource, properties, engine);
case "AWS::IAM::Policy" -> provisionIamInlinePolicy(resource, properties, engine, stackName);
case "AWS::IAM::ManagedPolicy" ->
provisionIamManagedPolicy(resource, properties, engine, accountId, stackName);
case "AWS::IAM::InstanceProfile" -> provisionInstanceProfile(resource, properties, engine, accountId, stackName);
case "AWS::ApiGatewayV2::Api" -> provisionApiGatewayV2Api(resource, properties, engine, region, accountId, stackName);
case "AWS::ApiGatewayV2::Authorizer" -> provisionApiGatewayV2Authorizer(resource, properties, engine, region);
case "AWS::ApiGatewayV2::Route" -> provisionApiGatewayV2Route(resource, properties, engine, region);
@@ -425,7 +421,6 @@ public class CloudFormationResourceProvisioner {
// Nothing to do here when only the physical id (policy name) is known, as on rollback.
case "AWS::IAM::Policy" -> { }
case "AWS::IAM::ManagedPolicy" -> deletePolicySafe(physicalId);
case "AWS::IAM::InstanceProfile" -> iamService.deleteInstanceProfile(physicalId);
// No bus context on the type/physicalId path (e.g. CREATE-rollback); targets the default bus.
case "AWS::ApiGatewayV2::Api" -> apiGatewayV2Service.deleteApi(region, physicalId);
case "AWS::Lambda::LayerVersion" -> deleteLambdaLayerVersion(physicalId, region);
@@ -1530,22 +1525,6 @@ public class CloudFormationResourceProvisioner {
// ── IAM Instance Profile ──────────────────────────────────────────────────
private void provisionInstanceProfile(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String accountId, String stackName) {
String name = resolveOptional(props, "InstanceProfileName", engine);
if (name == null || name.isBlank()) {
name = generatePhysicalName(stackName, r.getLogicalId(), 128, false);
}
try {
var profile = iamService.createInstanceProfile(name, "/");
r.setPhysicalId(name);
r.getAttributes().put("Arn", profile.getArn());
} catch (Exception e) {
r.setPhysicalId(name);
r.getAttributes().put("Arn", AwsArnUtils.Arn.of("iam", "", accountId, "instance-profile/" + name).toString());
}
}
// ── Pipes ──────────────────────────────────────────────────────────────────
/**
@@ -1617,15 +1596,6 @@ public class CloudFormationResourceProvisioner {
// ── Helpers ───────────────────────────────────────────────────────────────
private void provisionIamAccessKey(StackResource r, JsonNode props, CloudFormationTemplateEngine engine) {
String userName = resolveOptional(props, "UserName", engine);
if (userName != null) {
var key = iamService.createAccessKey(userName);
r.setPhysicalId(key.getAccessKeyId());
r.getAttributes().put("SecretAccessKey", key.getSecretAccessKey());
}
}
// ── ApiGateway (V1) ──────────────────────────────────────────────────────
// ── ApiGatewayV2 (HTTP/WebSocket) ────────────────────────────────────────
@@ -0,0 +1,48 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.iam.model.AccessKey;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.Set;
/**
* CloudFormation provisioning for {@code AWS::IAM::AccessKey}. {@code Ref} returns the access key
* id (the primary identifier), and {@code Fn::GetAtt} exposes the same id plus the one-time
* {@code SecretAccessKey}.
*
* <p>No delete is overridden: removing an access key needs the owning user name, which the
* id-only delete path does not carry, so the legacy switch left it a no-op. Storing the user name
* and deleting the key is a follow-up.
*/
@ApplicationScoped
public class IamAccessKeyCfnProvisioner implements CfnResourceProvisioner {
private static final String TYPE = "AWS::IAM::AccessKey";
private final IamService iamService;
@Inject
public IamAccessKeyCfnProvisioner(IamService iamService) {
this.iamService = iamService;
}
@Override
public Set<String> resourceTypes() {
return Set.of(TYPE);
}
@Override
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
String userName = ctx.resolveOptional(props, "UserName");
if (userName != null) {
AccessKey key = iamService.createAccessKey(userName);
r.setPhysicalId(key.getAccessKeyId());
r.getAttributes().put("Id", key.getAccessKeyId());
r.getAttributes().put("SecretAccessKey", key.getSecretAccessKey());
}
}
}
@@ -0,0 +1,64 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.iam.model.InstanceProfile;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.util.Set;
/**
* CloudFormation provisioning for {@code AWS::IAM::InstanceProfile}. {@code Ref} returns the
* instance profile name (the primary identifier) and {@code Fn::GetAtt Arn} its arn.
*/
@ApplicationScoped
public class IamInstanceProfileCfnProvisioner implements CfnResourceProvisioner {
private static final Logger LOG = Logger.getLogger(IamInstanceProfileCfnProvisioner.class);
private static final String TYPE = "AWS::IAM::InstanceProfile";
private final IamService iamService;
@Inject
public IamInstanceProfileCfnProvisioner(IamService iamService) {
this.iamService = iamService;
}
@Override
public Set<String> resourceTypes() {
return Set.of(TYPE);
}
@Override
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
String name = ctx.resolveOptional(props, "InstanceProfileName");
if (name == null || name.isBlank()) {
name = ctx.generatePhysicalName(r.getLogicalId(), 128, false);
}
r.setPhysicalId(name);
try {
InstanceProfile profile = iamService.createInstanceProfile(name, "/");
r.getAttributes().put("Arn", profile.getArn());
} catch (RuntimeException e) {
// A create collision (the profile already exists) is tolerated as the legacy switch did:
// synthesize the arn so Fn::GetAtt Arn still resolves rather than failing the stack.
LOG.debugv("createInstanceProfile fell back to a synthesized arn for {0}: {1}",
name, e.getMessage());
r.getAttributes().put("Arn",
AwsArnUtils.Arn.of("iam", "", ctx.accountId(), "instance-profile/" + name).toString());
}
}
@Override
public void delete(String resourceType, String physicalId, String region) {
// deleteInstanceProfile still throws DeleteConflict when roles are attached, which must
// propagate so the stack reports DELETE_FAILED; only the already-gone case is tolerated.
CfnDeletes.safeDelete("IAM instance profile", physicalId,
() -> iamService.deleteInstanceProfile(physicalId), "NoSuchEntity");
}
}