mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ec2): create and list network interface permissions (#8891)
This commit is contained in:
1 parent
e3b7fd286c
commit
7c2018ffd6
8 files changed
+475
-1
No files matched your search
@@ -29,9 +29,11 @@ import java.util.function.Consumer;
|
||||
|
||||
import static io.floci.aws.services.ec2.Ec2QueryProtocol.boolParam;
|
||||
import static io.floci.aws.services.ec2.Ec2QueryProtocol.booleanResponse;
|
||||
import static io.floci.aws.services.ec2.Ec2QueryProtocol.endResponse;
|
||||
import static io.floci.aws.services.ec2.Ec2QueryProtocol.getFiltersList;
|
||||
import static io.floci.aws.services.ec2.Ec2QueryProtocol.getList;
|
||||
import static io.floci.aws.services.ec2.Ec2QueryProtocol.intParam;
|
||||
import static io.floci.aws.services.ec2.Ec2QueryProtocol.startResponse;
|
||||
import static io.floci.aws.services.ec2.Ec2QueryProtocol.tagSetXml;
|
||||
import static io.floci.aws.services.ec2.Ec2QueryProtocol.xmlResponse;
|
||||
|
||||
@@ -357,6 +359,9 @@ public class Ec2QueryHandler {
|
||||
case "ModifyNetworkInterfaceAttribute" -> handleModifyNetworkInterfaceAttribute(params, region);
|
||||
case "AssignIpv6Addresses" -> handleAssignIpv6Addresses(params, region);
|
||||
case "UnassignIpv6Addresses" -> handleUnassignIpv6Addresses(params, region);
|
||||
case "CreateNetworkInterfacePermission" -> handleCreateNetworkInterfacePermission(params, region);
|
||||
case "DescribeNetworkInterfacePermissions" ->
|
||||
handleDescribeNetworkInterfacePermissions(params, region);
|
||||
// Volumes
|
||||
case "CreateVolume" -> handleCreateVolume(params, region);
|
||||
case "DescribeVolumes" -> handleDescribeVolumes(params, region);
|
||||
@@ -5669,6 +5674,41 @@ public class Ec2QueryHandler {
|
||||
return booleanResponse("ModifyNetworkInterfaceAttribute");
|
||||
}
|
||||
|
||||
private Response handleCreateNetworkInterfacePermission(MultivaluedMap<String, String> p, String region) {
|
||||
checkDryRun(p);
|
||||
NetworkInterfacePermission permission = service.createNetworkInterfacePermission(region,
|
||||
p.getFirst("NetworkInterfaceId"), p.getFirst("AwsAccountId"), p.getFirst("AwsService"),
|
||||
p.getFirst("Permission"));
|
||||
XmlBuilder xml = startResponse("CreateNetworkInterfacePermission")
|
||||
.start("interfacePermission").raw(networkInterfacePermissionXml(permission)).end("interfacePermission");
|
||||
return endResponse(xml, "CreateNetworkInterfacePermission");
|
||||
}
|
||||
|
||||
private Response handleDescribeNetworkInterfacePermissions(MultivaluedMap<String, String> p, String region) {
|
||||
Ec2Service.NetworkInterfacePermissionPage page = service.describeNetworkInterfacePermissions(region,
|
||||
getList(p, "NetworkInterfacePermissionId"), getFiltersList(p),
|
||||
parseOptionalInt(p.getFirst("MaxResults"), "MaxResults"), p.getFirst("NextToken"));
|
||||
XmlBuilder xml = startResponse("DescribeNetworkInterfacePermissions").start("networkInterfacePermissions");
|
||||
for (NetworkInterfacePermission permission : page.permissions()) {
|
||||
xml.start("item").raw(networkInterfacePermissionXml(permission)).end("item");
|
||||
}
|
||||
xml.end("networkInterfacePermissions");
|
||||
if (page.nextToken() != null) {
|
||||
xml.elem("nextToken", page.nextToken());
|
||||
}
|
||||
return endResponse(xml, "DescribeNetworkInterfacePermissions");
|
||||
}
|
||||
|
||||
private static String networkInterfacePermissionXml(NetworkInterfacePermission permission) {
|
||||
return new XmlBuilder()
|
||||
.elem("networkInterfacePermissionId", permission.getNetworkInterfacePermissionId())
|
||||
.elem("networkInterfaceId", permission.getNetworkInterfaceId())
|
||||
.elem("awsAccountId", permission.getAwsAccountId())
|
||||
.elem("permission", permission.getPermission())
|
||||
.start("permissionState").elem("state", permission.getState()).end("permissionState")
|
||||
.build();
|
||||
}
|
||||
|
||||
// ─── XML fragment builders ────────────────────────────────────────────────
|
||||
|
||||
private String instanceXml(Instance inst, Map<String, String> capacityAssociations,
|
||||
|
||||
@@ -66,6 +66,7 @@ import io.floci.aws.services.ec2.model.NetworkInterface;
|
||||
import io.floci.aws.services.ec2.model.NetworkInterfaceAssociation;
|
||||
import io.floci.aws.services.ec2.model.NetworkInterfaceAttachment;
|
||||
import io.floci.aws.services.ec2.model.NetworkInterfaceListResult;
|
||||
import io.floci.aws.services.ec2.model.NetworkInterfacePermission;
|
||||
import io.floci.aws.services.ec2.model.NetworkInterfacePrivateIpAddress;
|
||||
import io.floci.aws.services.ec2.model.Placement;
|
||||
import io.floci.aws.services.ec2.model.PrefixList;
|
||||
@@ -17351,6 +17352,172 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return detached;
|
||||
}
|
||||
|
||||
// ─── Network interface permissions ───────────────────────────────────────────────
|
||||
|
||||
/** The permission that lets another account attach an interface to an instance of its own. */
|
||||
public static final String INSTANCE_ATTACH = "INSTANCE-ATTACH";
|
||||
private static final String EIP_ASSOCIATE = "EIP-ASSOCIATE";
|
||||
/** {@code NetworkInterfacePermissionState.State} once a permission applies. */
|
||||
private static final String PERMISSION_GRANTED = "granted";
|
||||
private static final String NETWORK_INTERFACE_PERMISSION_ID_PREFIX = "eni-perm-";
|
||||
private static final Pattern ACCOUNT_ID = Pattern.compile("\\d{12}");
|
||||
private static final Set<String> NETWORK_INTERFACE_PERMISSION_FILTERS = Set.of(
|
||||
"network-interface-permission.network-interface-permission-id",
|
||||
"network-interface-permission.network-interface-id",
|
||||
"network-interface-permission.aws-account-id",
|
||||
"network-interface-permission.aws-service",
|
||||
"network-interface-permission.permission");
|
||||
|
||||
/** One page of DescribeNetworkInterfacePermissions. */
|
||||
public record NetworkInterfacePermissionPage(List<NetworkInterfacePermission> permissions, String nextToken) {}
|
||||
|
||||
/**
|
||||
* CreateNetworkInterfacePermission: the interface's owner records an {@code INSTANCE-ATTACH}
|
||||
* grant for another account. Attachment execution does not yet consume these grants.
|
||||
* Only the owner can grant: the interface is looked up in the caller's account, and
|
||||
* one another account owns is {@code InvalidNetworkInterfaceID.NotFound}, as it is to every
|
||||
* other action of that account.
|
||||
*
|
||||
* <p>A second grant of the same permission to the same account answers the grant already in
|
||||
* place rather than minting another: AWS documents no duplicate error for this action, and the
|
||||
* permission either holds or it does not. {@code EIP-ASSOCIATE} is refused as unsupported, since
|
||||
* nothing here would honour it, and so is {@code AwsService}, which AWS documents as "Currently
|
||||
* not supported".
|
||||
*/
|
||||
public NetworkInterfacePermission createNetworkInterfacePermission(String region, String networkInterfaceId,
|
||||
String awsAccountId, String awsService,
|
||||
String permission) {
|
||||
if (!isSet(networkInterfaceId)) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The request must contain the parameter NetworkInterfaceId", 400);
|
||||
}
|
||||
if (!isSet(permission)) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter Permission", 400);
|
||||
}
|
||||
requireParameterValue("Permission", permission, INSTANCE_ATTACH, EIP_ASSOCIATE);
|
||||
if (EIP_ASSOCIATE.equals(permission)) {
|
||||
throw new AwsException("UnsupportedOperation",
|
||||
"The EIP-ASSOCIATE network interface permission is not supported.", 400);
|
||||
}
|
||||
if (isSet(awsService)) {
|
||||
throw new AwsException("UnsupportedOperation",
|
||||
"Network interface permissions for an AwsService are not supported.", 400);
|
||||
}
|
||||
if (!isSet(awsAccountId)) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter AwsAccountId", 400);
|
||||
}
|
||||
requireAccountId("AwsAccountId", awsAccountId);
|
||||
ensureDefaultResources(region);
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
synchronized (privateIpAllocationLock) {
|
||||
NetworkInterface ni = requireStandaloneNetworkInterface(region, networkInterfaceId);
|
||||
return grantInstanceAttach(callerAccountId(), region, ni, awsAccountId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DescribeNetworkInterfacePermissions: the permissions the caller has granted over the
|
||||
* interfaces it owns in {@code region}, by id and by the five documented filters. MaxResults
|
||||
* runs 5 to 255 and defaults to 50, as AWS documents.
|
||||
*/
|
||||
public NetworkInterfacePermissionPage describeNetworkInterfacePermissions(
|
||||
String region, List<String> permissionIds, List<Map.Entry<String, List<String>>> filters,
|
||||
Integer maxResults, String nextToken) {
|
||||
requireSupportedFilters(filterNames(filters), NETWORK_INTERFACE_PERMISSION_FILTERS);
|
||||
if (maxResults != null && (maxResults < 5 || maxResults > 255)) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Value (" + maxResults + ") for parameter MaxResults is invalid. "
|
||||
+ "Expecting a value between 5 and 255.", 400);
|
||||
}
|
||||
for (String id : permissionIds) {
|
||||
if (!id.startsWith(NETWORK_INTERFACE_PERMISSION_ID_PREFIX)) {
|
||||
throw new AwsException("InvalidNetworkInterfacePermissionID.Malformed",
|
||||
"Invalid id: \"" + id + "\" (expecting \"" + NETWORK_INTERFACE_PERMISSION_ID_PREFIX + "...\")",
|
||||
400);
|
||||
}
|
||||
}
|
||||
ensureDefaultResources(region);
|
||||
List<NetworkInterfacePermission> held = new ArrayList<>();
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
for (NetworkInterface ni : networkInterfaces.scan(k -> k.startsWith(region + "::"))) {
|
||||
held.addAll(ni.getPermissions());
|
||||
}
|
||||
}
|
||||
for (String id : permissionIds) {
|
||||
if (held.stream().noneMatch(perm -> id.equals(perm.getNetworkInterfacePermissionId()))) {
|
||||
throw new AwsException("InvalidNetworkInterfacePermissionID.NotFound",
|
||||
"The NetworkInterfacePermission ID '" + id + "' does not exist", 400);
|
||||
}
|
||||
}
|
||||
List<NetworkInterfacePermission> matching = held.stream()
|
||||
.filter(perm -> permissionIds.isEmpty() || permissionIds.contains(perm.getNetworkInterfacePermissionId()))
|
||||
.filter(perm -> matchesPermissionFilters(perm, filters))
|
||||
.toList();
|
||||
int offset = decodeToken(nextToken);
|
||||
int pageSize = maxResults != null ? maxResults : 50;
|
||||
int end = Math.min(offset + pageSize, matching.size());
|
||||
List<NetworkInterfacePermission> page = offset < matching.size()
|
||||
? new ArrayList<>(matching.subList(offset, end)) : new ArrayList<>();
|
||||
return new NetworkInterfacePermissionPage(page, end < matching.size() ? encodeToken(end) : null);
|
||||
}
|
||||
|
||||
private boolean matchesPermissionFilters(NetworkInterfacePermission perm,
|
||||
List<Map.Entry<String, List<String>>> filters) {
|
||||
for (Map.Entry<String, List<String>> filter : filters == null ? List.<Map.Entry<String, List<String>>>of() : filters) {
|
||||
List<String> values = filter.getValue();
|
||||
boolean matches = switch (filter.getKey()) {
|
||||
case "network-interface-permission.network-interface-permission-id" ->
|
||||
matchesValue(values, perm.getNetworkInterfacePermissionId());
|
||||
case "network-interface-permission.network-interface-id" ->
|
||||
matchesValue(values, perm.getNetworkInterfaceId());
|
||||
case "network-interface-permission.aws-account-id" -> matchesValue(values, perm.getAwsAccountId());
|
||||
// No permission here is granted to a service, so none carries one to match.
|
||||
case "network-interface-permission.aws-service" -> false;
|
||||
case "network-interface-permission.permission" -> matchesValue(values, perm.getPermission());
|
||||
default -> true;
|
||||
};
|
||||
if (!matches) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** The grant of {@code INSTANCE-ATTACH} to {@code grantee}, minted unless one is already in place. */
|
||||
private NetworkInterfacePermission grantInstanceAttach(String ownerAccountId, String region, NetworkInterface ni,
|
||||
String grantee) {
|
||||
Optional<NetworkInterfacePermission> existing = ni.getPermissions().stream()
|
||||
.filter(perm -> grantee.equals(perm.getAwsAccountId()) && INSTANCE_ATTACH.equals(perm.getPermission()))
|
||||
.findFirst();
|
||||
if (existing.isPresent()) {
|
||||
return existing.get();
|
||||
}
|
||||
NetworkInterfacePermission granted = new NetworkInterfacePermission(
|
||||
NETWORK_INTERFACE_PERMISSION_ID_PREFIX + randomHex(17), ni.getNetworkInterfaceId(), grantee,
|
||||
INSTANCE_ATTACH, PERMISSION_GRANTED);
|
||||
ni.getPermissions().add(granted);
|
||||
putNetworkInterface(ownerAccountId, region, ni);
|
||||
return granted;
|
||||
}
|
||||
|
||||
private static void requireAccountId(String parameter, String value) {
|
||||
if (!ACCOUNT_ID.matcher(value).matches()) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Value (" + value + ") for parameter " + parameter + " is invalid. "
|
||||
+ "Expecting a 12-digit AWS account ID.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/** Stores an interface in its owner's partition, whichever account is calling. */
|
||||
private void putNetworkInterface(String accountId, String region, NetworkInterface ni) {
|
||||
if (networkInterfaces instanceof AccountAwareStorageBackend<NetworkInterface> aware) {
|
||||
aware.putForAccount(accountId, key(region, ni.getNetworkInterfaceId()), ni);
|
||||
} else {
|
||||
networkInterfaces.put(key(region, ni.getNetworkInterfaceId()), ni);
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes an interface from an instance's own list, the mirror of attaching it. */
|
||||
private void detachFromInstance(String region, String instanceId, String networkInterfaceId) {
|
||||
detachFromInstance(callerAccountId(), region, instanceId, networkInterfaceId);
|
||||
|
||||
@@ -40,6 +40,8 @@ public class NetworkInterface {
|
||||
private List<Tag> tagSet = new ArrayList<>();
|
||||
private List<NetworkInterfacePrivateIpAddress> privateIpAddresses = new ArrayList<>();
|
||||
private List<String> ipv6Addresses = new ArrayList<>();
|
||||
/** The permissions this interface's owner has granted other accounts over it. */
|
||||
private List<NetworkInterfacePermission> permissions = new ArrayList<>();
|
||||
|
||||
public NetworkInterface() {}
|
||||
|
||||
@@ -98,4 +100,7 @@ public class NetworkInterface {
|
||||
public List<String> getIpv6Addresses() { return ipv6Addresses; }
|
||||
public void setIpv6Addresses(List<String> ipv6Addresses) { this.ipv6Addresses = ipv6Addresses; }
|
||||
public void setPrivateIpAddresses(List<NetworkInterfacePrivateIpAddress> privateIpAddresses) { this.privateIpAddresses = privateIpAddresses; }
|
||||
|
||||
public List<NetworkInterfacePermission> getPermissions() { return permissions; }
|
||||
public void setPermissions(List<NetworkInterfacePermission> permissions) { this.permissions = permissions; }
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package io.floci.aws.services.ec2.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/**
|
||||
* A permission the account that owns a network interface has granted another account over it
|
||||
* ({@code NetworkInterfacePermission}). CreateNetworkInterfacePermission records an
|
||||
* {@code INSTANCE-ATTACH} grant on the interface it is over. Attachment execution does not yet
|
||||
* consume these grants.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class NetworkInterfacePermission {
|
||||
|
||||
private String networkInterfacePermissionId;
|
||||
private String networkInterfaceId;
|
||||
private String awsAccountId;
|
||||
private String permission;
|
||||
private String state;
|
||||
|
||||
public NetworkInterfacePermission() {}
|
||||
|
||||
public NetworkInterfacePermission(String networkInterfacePermissionId, String networkInterfaceId,
|
||||
String awsAccountId, String permission, String state) {
|
||||
this.networkInterfacePermissionId = networkInterfacePermissionId;
|
||||
this.networkInterfaceId = networkInterfaceId;
|
||||
this.awsAccountId = awsAccountId;
|
||||
this.permission = permission;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getNetworkInterfacePermissionId() { return networkInterfacePermissionId; }
|
||||
public void setNetworkInterfacePermissionId(String networkInterfacePermissionId) {
|
||||
this.networkInterfacePermissionId = networkInterfacePermissionId;
|
||||
}
|
||||
|
||||
public String getNetworkInterfaceId() { return networkInterfaceId; }
|
||||
public void setNetworkInterfaceId(String networkInterfaceId) { this.networkInterfaceId = networkInterfaceId; }
|
||||
|
||||
public String getAwsAccountId() { return awsAccountId; }
|
||||
public void setAwsAccountId(String awsAccountId) { this.awsAccountId = awsAccountId; }
|
||||
|
||||
public String getPermission() { return permission; }
|
||||
public void setPermission(String permission) { this.permission = permission; }
|
||||
|
||||
/** {@code PermissionState.State}: {@code granted} once it applies. */
|
||||
public String getState() { return state; }
|
||||
public void setState(String state) { this.state = state; }
|
||||
}
|
||||
Reference in new issue
Block a user