mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ec2): support ModifyVpcEndpoint and persist the endpoint policy (#2526)
* feat(ec2): implement ModifyVpcEndpoint and store the endpoint policy
ModifyVpcEndpoint returned UnsupportedOperation. In a survey that drove 183
Terraform roots against Floci it was the single largest blocker, with 205 recorded
occurrences -- roughly five times the next entry.
A gateway endpoint is normally declared with its route tables, subnets and policy
attached after the endpoint itself exists, so aws_vpc_endpoint reaches for this
action in the ordinary case rather than an exotic one. EC2 is otherwise well
supported, which makes the failure especially expensive: a configuration builds most
of its infrastructure and then loses all of it to one refused call.
Error: modifying EC2 VPC Endpoint (vpce-...): operation error EC2:
ModifyVpcEndpoint, https response error StatusCode: 400,
api error UnsupportedOperation: Operation ModifyVpcEndpoint is not supported.
Nearly all the state was already modelled -- VpcEndpoint carries routeTableIds,
subnetIds, securityGroupIds and privateDnsEnabled -- so this adds the handler, the
add/remove set semantics, and the one missing field.
The update is taken under the endpoint's stripe lock, following the convention
already used for network ACL entries and prefix lists. Terraform declares each
aws_vpc_endpoint_route_table_association as its own resource and applies them in
parallel, so several ModifyVpcEndpoint calls land on one endpoint at once; an
unsynchronized read-modify-write loses all but the last, and the losers' waiters then
poll for an association that was silently dropped:
Error: waiting for VPC Endpoint Route Table Association (vpce-.../rtb-...) to
become available: couldn't find resource (21 retries)
PolicyDocument is the missing field. CreateVpcEndpoint accepted it and silently
dropped it, and DescribeVpcEndpoints reported no policy, so an endpoint declared
with a policy read back without one and showed as drift on every plan even when
ModifyVpcEndpoint was never called. It is now honoured on create, on modify, and
cleared by ResetPolicy, which AWS documents as returning the endpoint to the default
full-access policy (modelled here as carrying no document).
Add/remove are set operations. AWS accepts adding an id that is already associated,
or removing one that is not, so both directions are idempotent. Every referenced id
is validated before anything is mutated, so a request naming one bad id cannot leave
the endpoint half-modified -- covered by a test.
Reference: request parameters, their optionality, and the minimal
{return, requestId} response --
https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_ModifyVpcEndpoint.html
Not implemented, as nothing in the model backs them yet: DnsOptions, IpAddressType
and SubnetConfiguration.N. They are accepted and ignored rather than rejected, which
matches how the other unmodelled endpoint attributes already behave.
Verified end to end. A Terraform configuration with two
aws_vpc_endpoint_route_table_association resources previously failed with the
"couldn't find resource" wait above, leaving one of the two associations dropped; it
now applies 6 resources with both associated. 378 existing EC2 and CloudFormation
tests still pass.
The concurrency test drives the service directly rather than over HTTP: requests
through the test server are serialized enough that an HTTP-level test passes even
with the lock removed. Driven through the service it fails on every run without the
lock, which is what makes it worth having.
* fix(ec2): honour ModifyVpcEndpoint presence semantics and the CFN endpoint policy
Follow-up corrections to the ModifyVpcEndpoint implementation in the preceding
commit. Three behaviour changes, each with a test that fails without it.
PolicyDocument was treated as absent when blank. The EC2 model gives the member no
`min`, so the only test the request supports is whether it is present; an empty
string is a value the caller supplied, and discarding it is the same silent-drop bug
the previous commit fixed for the create path. Presence is now the test, on create,
on modify, and when rendering DescribeVpcEndpoints.
An omitted VpcEndpointId reported InvalidVpcEndpointId.NotFound for the id "null".
VpcEndpointId is the sole required member of ModifyVpcEndpointRequest, and an absent
required Query parameter is MissingParameter here, as it already is for CreateSubnet
and CreateVpc. A present-but-unknown id still reports NotFound.
Ec2VpcEndpointCfnProvisioner passed null for the new policy parameter, so an
AWS::EC2::VPCEndpoint declaring PolicyDocument still lost it -- the EC2 API path was
fixed while the CloudFormation path kept the original bug. The property is declared
as JSON in the template and so arrives as an object node; it is serialised the way
IamRoleCfnProvisioner already serialises AssumeRolePolicyDocument.
Also tightens two error assertions in Ec2ModifyVpcEndpointTest that checked only for
status 400. UnsupportedOperation is also a 400, so both passed unchanged with the
action unrouted -- they proved nothing about the fix. Asserting the error code takes
the class from 8 of 11 failing to 9 of 11 when ModifyVpcEndpoint is unrouted.
Documents the action in docs/services/ec2.md, including the members accepted and
ignored.
* fix(cfn): resolve intrinsics in the VPC endpoint PolicyDocument
The AWS::EC2::VPCEndpoint provisioner serialised PolicyDocument straight
off the raw template node, so a policy built with Ref, Fn::Sub or
Fn::Join was stored as literal template syntax and DescribeVpcEndpoints
reported it verbatim.
Route it through CloudFormationTemplateEngine.resolveJsonAttribute, the
helper added in #2317 for JSON-valued attributes. It resolves the tree
and then unwraps a textual result instead of re-serialising it, avoiding
the double-encoding a naive resolveNode(...).toString() produces. It
also returns null for a null or missing node, so the old has()/isNull()
guard is redundant and goes away.
The javadoc previously cited IamRoleCfnProvisioner's bare toString() of
AssumeRolePolicyDocument as precedent; that precedent predates #2317 and
is no longer the right one to follow, so it now cites the helper instead.
This commit is contained in:
1 parent
7ef698c1f4
commit
2e8cbc13d7
8 files changed
+573
-19
No files matched your search
+24
@@ -59,6 +59,7 @@ public class Ec2VpcEndpointCfnProvisioner implements CfnResourceProvisioner {
|
||||
resolveIdList(props, "SubnetIds", ctx),
|
||||
resolveIdList(props, "SecurityGroupIds", ctx),
|
||||
privateDnsEnabled,
|
||||
policyDocument(props, ctx),
|
||||
List.of());
|
||||
r.setPhysicalId(endpoint.getVpcEndpointId());
|
||||
r.getAttributes().put("Id", endpoint.getVpcEndpointId());
|
||||
@@ -77,6 +78,29 @@ public class Ec2VpcEndpointCfnProvisioner implements CfnResourceProvisioner {
|
||||
ec2Service.deleteVpcEndpoints(region, List.of(physicalId));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code PolicyDocument} is declared as JSON in the template, so it arrives as an object node
|
||||
* whose fields may still carry intrinsics ({@code Ref}, {@code Fn::Sub}, {@code Fn::Join}).
|
||||
* Serialising the raw node would store literal template syntax and DescribeVpcEndpoints would
|
||||
* report it verbatim, so it goes through
|
||||
* {@link io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine#resolveJsonAttribute},
|
||||
* the engine helper for JSON-valued attributes (policy documents, RedrivePolicy,
|
||||
* FilterPolicy, Step Functions definitions). That helper also avoids the double-encoding
|
||||
* trap a naive {@code resolveNode(...).toString()} hits: resolveNode collapses an intrinsic
|
||||
* to a TextNode holding already-serialized JSON, which {@code toString()} then re-quotes.
|
||||
*
|
||||
* <p>It returns null for a null or missing node, so no {@code has}/{@code isNull} guard is
|
||||
* needed here beyond the null-props check.
|
||||
*
|
||||
* @see <a href="https://github.com/floci-io/floci/issues/2317">#2317</a>
|
||||
*/
|
||||
private String policyDocument(JsonNode props, ProvisionContext ctx) {
|
||||
if (props == null) {
|
||||
return null;
|
||||
}
|
||||
return ctx.engine().resolveJsonAttribute(props.path("PolicyDocument"));
|
||||
}
|
||||
|
||||
/** Resolve an array property of Ref/GetAtt entries into plain id strings. */
|
||||
private List<String> resolveIdList(JsonNode props, String field, ProvisionContext ctx) {
|
||||
List<String> ids = new ArrayList<>();
|
||||
|
||||
@@ -73,6 +73,7 @@ public class Ec2QueryHandler {
|
||||
case "DescribeVpcEndpointServices" -> handleDescribeVpcEndpointServices(params, region);
|
||||
case "CreateVpcEndpoint" -> handleCreateVpcEndpoint(params, region);
|
||||
case "DescribeVpcEndpoints" -> handleDescribeVpcEndpoints(params, region);
|
||||
case "ModifyVpcEndpoint" -> handleModifyVpcEndpoint(params, region);
|
||||
case "DeleteVpcEndpoints" -> handleDeleteVpcEndpoints(params, region);
|
||||
// Flow Logs
|
||||
case "CreateFlowLogs" -> handleCreateFlowLogs(params, region);
|
||||
@@ -1102,6 +1103,7 @@ public class Ec2QueryHandler {
|
||||
getList(p, "SubnetId"),
|
||||
getList(p, "SecurityGroupId"),
|
||||
p.getFirst("PrivateDnsEnabled") != null ? Boolean.valueOf(p.getFirst("PrivateDnsEnabled")) : null,
|
||||
p.getFirst("PolicyDocument"),
|
||||
parseTagsForResource(p, "vpc-endpoint"));
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("CreateVpcEndpointResponse", AwsNamespaces.EC2)
|
||||
@@ -1111,6 +1113,29 @@ public class Ec2QueryHandler {
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private Response handleModifyVpcEndpoint(MultivaluedMap<String, String> p, String region) {
|
||||
service.modifyVpcEndpoint(
|
||||
region,
|
||||
p.getFirst("VpcEndpointId"),
|
||||
getList(p, "AddRouteTableId"),
|
||||
getList(p, "RemoveRouteTableId"),
|
||||
getList(p, "AddSubnetId"),
|
||||
getList(p, "RemoveSubnetId"),
|
||||
getList(p, "AddSecurityGroupId"),
|
||||
getList(p, "RemoveSecurityGroupId"),
|
||||
p.getFirst("PolicyDocument"),
|
||||
p.getFirst("ResetPolicy") != null ? Boolean.valueOf(p.getFirst("ResetPolicy")) : null,
|
||||
p.getFirst("PrivateDnsEnabled") != null ? Boolean.valueOf(p.getFirst("PrivateDnsEnabled")) : null);
|
||||
// ModifyVpcEndpoint returns only a boolean; the caller re-reads the endpoint
|
||||
// through DescribeVpcEndpoints to see the result.
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("ModifyVpcEndpointResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.elem("return", true)
|
||||
.end("ModifyVpcEndpointResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private Response handleDescribeVpcEndpoints(MultivaluedMap<String, String> p, String region) {
|
||||
List<String> endpointIds = getList(p, "VpcEndpointId");
|
||||
Map<String, List<String>> filters = getFilters(p);
|
||||
@@ -3415,8 +3440,11 @@ public class Ec2QueryHandler {
|
||||
for (String securityGroupId : endpoint.getSecurityGroupIds()) {
|
||||
xml.start("item").elem("groupId", securityGroupId).end("item");
|
||||
}
|
||||
xml.end("groupSet")
|
||||
.raw(tagSetXml(endpoint.getTags()));
|
||||
xml.end("groupSet");
|
||||
if (endpoint.getPolicyDocument() != null) {
|
||||
xml.elem("policyDocument", endpoint.getPolicyDocument());
|
||||
}
|
||||
xml.raw(tagSetXml(endpoint.getTags()));
|
||||
return xml.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -2738,7 +2738,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
|
||||
public VpcEndpoint createVpcEndpoint(String region, String vpcId, String serviceName, String endpointType,
|
||||
List<String> routeTableIds, List<String> subnetIds,
|
||||
List<String> securityGroupIds, Boolean privateDnsEnabled, List<Tag> endpointTags) {
|
||||
List<String> securityGroupIds, Boolean privateDnsEnabled,
|
||||
String policyDocument, List<Tag> endpointTags) {
|
||||
ensureDefaultResources(region);
|
||||
getRequiredVpc(region, vpcId);
|
||||
for (String routeTableId : routeTableIds) {
|
||||
@@ -2763,6 +2764,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
endpoint.setRouteTableIds(new ArrayList<>(routeTableIds));
|
||||
endpoint.setSubnetIds(new ArrayList<>(subnetIds));
|
||||
endpoint.setSecurityGroupIds(new ArrayList<>(securityGroupIds));
|
||||
endpoint.setPolicyDocument(policyDocument);
|
||||
if (endpointTags != null && !endpointTags.isEmpty()) {
|
||||
endpoint.setTags(new ArrayList<>(endpointTags));
|
||||
tags.put(endpoint.getVpcEndpointId(), new ArrayList<>(endpointTags));
|
||||
@@ -2771,6 +2773,89 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a ModifyVpcEndpoint request. Every parameter is optional and each applies
|
||||
* independently, so one request may move route tables and rewrite the policy.
|
||||
*
|
||||
* <p>The add/remove parameters are set operations. AWS accepts an id that is already
|
||||
* associated, or a removal of one that is not, without complaint, so this is
|
||||
* idempotent on both sides. {@code resetPolicy} returns the endpoint to the default
|
||||
* full-access policy, modelled here as carrying no document at all.
|
||||
*/
|
||||
public VpcEndpoint modifyVpcEndpoint(String region, String endpointId,
|
||||
List<String> addRouteTableIds, List<String> removeRouteTableIds,
|
||||
List<String> addSubnetIds, List<String> removeSubnetIds,
|
||||
List<String> addSecurityGroupIds, List<String> removeSecurityGroupIds,
|
||||
String policyDocument, Boolean resetPolicy, Boolean privateDnsEnabled) {
|
||||
// VpcEndpointId is the one required member of ModifyVpcEndpointRequest. The model
|
||||
// requires it to be present, not to be non-empty, so only an absent value is a
|
||||
// MissingParameter; a present-but-unknown id is an InvalidVpcEndpointId.NotFound.
|
||||
if (endpointId == null) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The request must contain the parameter VpcEndpointId", 400);
|
||||
}
|
||||
ensureDefaultResources(region);
|
||||
// Terraform declares each aws_vpc_endpoint_route_table_association as its own
|
||||
// resource and applies them in parallel, so several ModifyVpcEndpoint calls land
|
||||
// on one endpoint at once. Without the lock this read-modify-write loses updates:
|
||||
// each caller reads the same association list, adds its own id, and the last write
|
||||
// wins -- leaving the other caller's waiter polling for an association that was
|
||||
// silently dropped.
|
||||
synchronized (lockFor(key(region, endpointId))) {
|
||||
return modifyVpcEndpointLocked(region, endpointId,
|
||||
addRouteTableIds, removeRouteTableIds, addSubnetIds, removeSubnetIds,
|
||||
addSecurityGroupIds, removeSecurityGroupIds,
|
||||
policyDocument, resetPolicy, privateDnsEnabled);
|
||||
}
|
||||
}
|
||||
|
||||
private VpcEndpoint modifyVpcEndpointLocked(String region, String endpointId,
|
||||
List<String> addRouteTableIds, List<String> removeRouteTableIds,
|
||||
List<String> addSubnetIds, List<String> removeSubnetIds,
|
||||
List<String> addSecurityGroupIds, List<String> removeSecurityGroupIds,
|
||||
String policyDocument, Boolean resetPolicy,
|
||||
Boolean privateDnsEnabled) {
|
||||
VpcEndpoint endpoint = getRequiredVpcEndpoint(region, endpointId);
|
||||
|
||||
// Validate every referenced id before mutating anything, so a request naming one
|
||||
// bad id does not leave the endpoint half-modified.
|
||||
for (String routeTableId : addRouteTableIds) {
|
||||
getRequiredRouteTable(region, routeTableId);
|
||||
}
|
||||
for (String subnetId : addSubnetIds) {
|
||||
requireSubnet(region, subnetId);
|
||||
}
|
||||
for (String securityGroupId : addSecurityGroupIds) {
|
||||
getRequiredSecurityGroup(region, securityGroupId);
|
||||
}
|
||||
|
||||
applyIdChanges(endpoint.getRouteTableIds(), addRouteTableIds, removeRouteTableIds);
|
||||
applyIdChanges(endpoint.getSubnetIds(), addSubnetIds, removeSubnetIds);
|
||||
applyIdChanges(endpoint.getSecurityGroupIds(), addSecurityGroupIds, removeSecurityGroupIds);
|
||||
|
||||
if (Boolean.TRUE.equals(resetPolicy)) {
|
||||
endpoint.setPolicyDocument(null);
|
||||
} else if (policyDocument != null) {
|
||||
endpoint.setPolicyDocument(policyDocument);
|
||||
}
|
||||
if (privateDnsEnabled != null) {
|
||||
endpoint.setPrivateDnsEnabled(privateDnsEnabled);
|
||||
}
|
||||
|
||||
vpcEndpoints.put(key(region, endpointId), endpoint);
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
/** Removals apply before additions, and an id is never added twice. */
|
||||
private static void applyIdChanges(List<String> current, List<String> toAdd, List<String> toRemove) {
|
||||
current.removeAll(toRemove);
|
||||
for (String id : toAdd) {
|
||||
if (!current.contains(id)) {
|
||||
current.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<VpcEndpoint> describeVpcEndpoints(String region, List<String> endpointIds,
|
||||
Map<String, List<String>> filters) {
|
||||
ensureDefaultResources(region);
|
||||
|
||||
@@ -22,6 +22,7 @@ public class VpcEndpoint {
|
||||
private List<String> subnetIds = new ArrayList<>();
|
||||
private List<String> securityGroupIds = new ArrayList<>();
|
||||
private boolean privateDnsEnabled;
|
||||
private String policyDocument;
|
||||
private List<Tag> tags = new ArrayList<>();
|
||||
|
||||
public VpcEndpoint() {}
|
||||
@@ -59,6 +60,9 @@ public class VpcEndpoint {
|
||||
public boolean isPrivateDnsEnabled() { return privateDnsEnabled; }
|
||||
public void setPrivateDnsEnabled(boolean privateDnsEnabled) { this.privateDnsEnabled = privateDnsEnabled; }
|
||||
|
||||
public String getPolicyDocument() { return policyDocument; }
|
||||
public void setPolicyDocument(String policyDocument) { this.policyDocument = policyDocument; }
|
||||
|
||||
public List<Tag> getTags() { return tags; }
|
||||
public void setTags(List<Tag> tags) { this.tags = tags; }
|
||||
}
|
||||
Reference in new issue
Block a user