mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(route53): support reusable delegation sets (#7020)
* feat(route53): add reusable delegation set APIs * feat(route53): associate reusable delegation sets with hosted zones * fix(route53): preserve delegation ownership and assignments * fix(route53): authorize delegation set operations by matched route * fix(route53): filter hosted zones by reusable delegation set * fix(iam): select REST XML denials from the service catalog * perf(test): reuse Route53 IAM enforcement fixtures
This commit is contained in:
1 parent
a2e12ee004
commit
7f785fdc4c
17 files changed
+1343
-34
No files matched your search
@@ -776,7 +776,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
boolean rest = claimValue instanceof ProtocolClaim claim && claim.protocol() == WireProtocol.REST;
|
||||
String servingScope = servingCredentialScope(claimedScope, ctx);
|
||||
if (rest) {
|
||||
return new ResolvedAuthorization(servingScope, actionRegistry.resolveRoute(servingScope, ctx));
|
||||
return new ResolvedAuthorization(servingScope, routeAction(servingScope, ctx));
|
||||
}
|
||||
String targetOperation = IamActionRegistry.targetOperation(ctx);
|
||||
String action = actionRegistry.resolve(servingScope, ctx);
|
||||
@@ -907,13 +907,18 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
&& claim.protocol() != WireProtocol.REST;
|
||||
}
|
||||
|
||||
private String routeAction(String scope, ContainerRequestContext ctx) {
|
||||
String matched = actionRegistry.resolveMatchedRoute(scope, resourceInfo);
|
||||
return matched != null ? matched : actionRegistry.resolveRoute(scope, ctx);
|
||||
}
|
||||
|
||||
private ResolvedAuthorization resolveRestAuthorization(ServiceDescriptor descriptor,
|
||||
ContainerRequestContext ctx) {
|
||||
return descriptor.credentialScopes().stream()
|
||||
.map(catalog::canonicalCredentialScope)
|
||||
.distinct()
|
||||
.sorted()
|
||||
.map(scope -> new ResolvedAuthorization(scope, actionRegistry.resolveRoute(scope, ctx)))
|
||||
.map(scope -> new ResolvedAuthorization(scope, routeAction(scope, ctx)))
|
||||
.filter(authorization -> authorization.action() != null)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
@@ -1661,8 +1666,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
|| "apigateway".equals(credentialScope)) {
|
||||
return arnBuilder.buildResources(credentialScope, ctx, region, accountId, requestPartition());
|
||||
}
|
||||
return arnBuilder.buildResources(credentialScope, operation, ctx, region, accountId,
|
||||
AwsRegions.partitionFor(region));
|
||||
String partition = "route53".equals(credentialScope) ? requestPartition() : AwsRegions.partitionFor(region);
|
||||
return arnBuilder.buildResources(credentialScope, operation, ctx, region, accountId, partition);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1775,11 +1780,11 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
return accessDeniedResponse(action, credentialScope, requestMediaType, resourceArn, WireProtocol.REST);
|
||||
}
|
||||
|
||||
private static Response accessDeniedForRequest(String action, String credentialScope, ContainerRequestContext ctx) {
|
||||
private Response accessDeniedForRequest(String action, String credentialScope, ContainerRequestContext ctx) {
|
||||
return accessDeniedForRequest(action, credentialScope, ctx, null);
|
||||
}
|
||||
|
||||
private static Response accessDeniedForRequest(String action, String credentialScope,
|
||||
private Response accessDeniedForRequest(String action, String credentialScope,
|
||||
ContainerRequestContext ctx, String resourceArn) {
|
||||
WireProtocol protocol = claimedProtocol(ctx);
|
||||
if (isCbor(protocol)) {
|
||||
@@ -1792,7 +1797,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
if (IamActionRegistry.isS3ControlRequest(credentialScope, ctx)) {
|
||||
return s3ControlXmlAccessDenied(accessDeniedMessage(action));
|
||||
}
|
||||
return accessDeniedResponse(action, credentialScope, ctx.getMediaType(), resourceArn, protocol);
|
||||
boolean restXml = catalog.byCredentialScope(credentialScope)
|
||||
.filter(descriptor -> descriptor.supportsProtocol(ServiceProtocol.REST_XML)).isPresent();
|
||||
return accessDeniedResponse(action, credentialScope, ctx.getMediaType(), resourceArn, protocol, restXml);
|
||||
}
|
||||
|
||||
private static String accessDeniedMessage(String action) {
|
||||
@@ -1801,12 +1808,17 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
|
||||
static Response accessDeniedResponse(String action, String credentialScope, MediaType requestMediaType,
|
||||
String resourceArn, WireProtocol protocol) {
|
||||
return accessDeniedResponse(action, credentialScope, requestMediaType, resourceArn, protocol, false);
|
||||
}
|
||||
|
||||
private static Response accessDeniedResponse(String action, String credentialScope, MediaType requestMediaType,
|
||||
String resourceArn, WireProtocol protocol, boolean restXml) {
|
||||
String message = accessDeniedMessage(action);
|
||||
if ("s3".equals(credentialScope)) {
|
||||
String resourcePath = formatS3ResourcePath(resourceArn);
|
||||
return s3XmlAccessDenied(message, resourcePath);
|
||||
}
|
||||
if (protocol == WireProtocol.AWS_QUERY || isFormEncoded(requestMediaType)) {
|
||||
if (restXml || protocol == WireProtocol.AWS_QUERY || isFormEncoded(requestMediaType)) {
|
||||
return queryXmlError(queryAccessDeniedCode(credentialScope), message);
|
||||
}
|
||||
int status = protocol == WireProtocol.AWS_JSON_1_0 || protocol == WireProtocol.AWS_JSON_1_1 ? 400 : 403;
|
||||
|
||||
@@ -7,15 +7,18 @@ import io.github.hectorvent.floci.core.common.ProtocolClaim;
|
||||
import io.github.hectorvent.floci.core.common.WireProtocol;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser;
|
||||
import io.github.hectorvent.floci.services.eks.EksController;
|
||||
import io.github.hectorvent.floci.services.route53.Route53Controller;
|
||||
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ResourceInfo;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -30,6 +33,12 @@ import java.util.regex.Pattern;
|
||||
@ApplicationScoped
|
||||
public class IamActionRegistry {
|
||||
|
||||
private static final Map<String, String> ROUTE53_DELEGATION_ACTIONS = Map.of(
|
||||
"createReusableDelegationSet", "route53:CreateReusableDelegationSet",
|
||||
"getReusableDelegationSet", "route53:GetReusableDelegationSet",
|
||||
"listReusableDelegationSets", "route53:ListReusableDelegationSets",
|
||||
"deleteReusableDelegationSet", "route53:DeleteReusableDelegationSet");
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(IamActionRegistry.class);
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@@ -631,6 +640,16 @@ public class IamActionRegistry {
|
||||
return IamAuthorizedActions.actionFor(credentialScope, operationName);
|
||||
}
|
||||
|
||||
/** The serving method identifies these actions even when path encoding or matrix parameters vary. */
|
||||
public String resolveMatchedRoute(String credentialScope, ResourceInfo resourceInfo) {
|
||||
if ("route53".equals(credentialScope) && resourceInfo != null
|
||||
&& Route53Controller.class.equals(resourceInfo.getResourceClass())
|
||||
&& resourceInfo.getResourceMethod() != null) {
|
||||
return ROUTE53_DELEGATION_ACTIONS.get(resourceInfo.getResourceMethod().getName());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The action of a REST request, from its method and path alone. A REST request is the operation
|
||||
* whose route it reached, so neither an {@code X-Amz-Target} header nor a Query {@code Action}
|
||||
|
||||
@@ -203,6 +203,10 @@ public class ResourceArnBuilder {
|
||||
case "dsql" -> List.of(buildDsqlArn(path, ctx, region, accountId, partition));
|
||||
case "eks" -> List.of(buildEksArn(path, ctx, region, accountId));
|
||||
case "apigateway" -> List.of(buildApiGatewayArn(ctx, region, partition));
|
||||
case "route53" -> "route53:GetReusableDelegationSet".equals(operation)
|
||||
|| "route53:DeleteReusableDelegationSet".equals(operation)
|
||||
? List.of(buildRoute53DelegationSetArn(ctx, partition))
|
||||
: namedResources(credentialScope, operation, ctx, region, accountId, partition);
|
||||
default -> namedResources(credentialScope, operation, ctx, region, accountId, partition);
|
||||
};
|
||||
}
|
||||
@@ -233,6 +237,17 @@ public class ResourceArnBuilder {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String buildRoute53DelegationSetArn(ContainerRequestContext ctx, String partition) {
|
||||
String id = ctx.getUriInfo().getPathParameters().getFirst("Id");
|
||||
if (id == null) {
|
||||
return "*";
|
||||
}
|
||||
if (id.startsWith("/delegationset/")) {
|
||||
id = id.substring(15);
|
||||
}
|
||||
return AwsArnUtils.Arn.global(partition, "route53", "", "delegationset/" + id).toString();
|
||||
}
|
||||
|
||||
private String buildResourceGroupArn(ContainerRequestContext ctx, String region, String accountId) {
|
||||
JsonNode body = readJsonBody(ctx);
|
||||
if (body == null) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.services.route53.Route53Service.CreateZoneResult;
|
||||
import io.github.hectorvent.floci.services.route53.model.AliasTarget;
|
||||
import io.github.hectorvent.floci.services.route53.model.ChangeInfo;
|
||||
import io.github.hectorvent.floci.services.route53.model.DelegationSet;
|
||||
import io.github.hectorvent.floci.services.route53.model.HealthCheck;
|
||||
import io.github.hectorvent.floci.services.route53.model.HealthCheckConfig;
|
||||
import io.github.hectorvent.floci.services.route53.model.HostedZone;
|
||||
@@ -45,6 +46,70 @@ public class Route53Controller {
|
||||
@Inject
|
||||
Route53Service service;
|
||||
|
||||
@POST
|
||||
@Path("/delegationset")
|
||||
public Response createReusableDelegationSet(String body) {
|
||||
try {
|
||||
if (!"CreateReusableDelegationSetRequest".equals(XmlParser.rootElementName(body))) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"The request body must be a CreateReusableDelegationSetRequest document.", 400);
|
||||
}
|
||||
DelegationSet set = service.createReusableDelegationSet(XmlParser.extractFirst(body, "CallerReference", null),
|
||||
XmlParser.extractFirst(body, "HostedZoneId", null));
|
||||
String xml = new XmlBuilder().start("CreateReusableDelegationSetResponse", NS)
|
||||
.raw(xmlDelegationSet(set)).end("CreateReusableDelegationSetResponse").build();
|
||||
return Response.created(URI.create("/2013-04-01/delegationset/" + set.id())).type(XML).entity(xml).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/delegationset/{Id}")
|
||||
public Response getReusableDelegationSet(@PathParam("Id") String id) {
|
||||
try {
|
||||
String xml = new XmlBuilder().start("GetReusableDelegationSetResponse", NS)
|
||||
.raw(xmlDelegationSet(service.getReusableDelegationSet(id)))
|
||||
.end("GetReusableDelegationSetResponse").build();
|
||||
return Response.ok(xml, XML).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/delegationset/{Id}")
|
||||
public Response deleteReusableDelegationSet(@PathParam("Id") String id) {
|
||||
try {
|
||||
service.deleteReusableDelegationSet(id);
|
||||
return Response.ok().build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/delegationset")
|
||||
public Response listReusableDelegationSets(@QueryParam("marker") String marker,
|
||||
@QueryParam("maxitems") @DefaultValue("100") String maxItems) {
|
||||
try {
|
||||
Route53Pagination.Page<DelegationSet> page = service.pageReusableDelegationSets(marker, maxItems);
|
||||
XmlBuilder xml = new XmlBuilder().start("ListReusableDelegationSetsResponse", NS).start("DelegationSets");
|
||||
for (DelegationSet set : page.items()) {
|
||||
xml.raw(xmlDelegationSet(set));
|
||||
}
|
||||
xml.end("DelegationSets").elem("Marker", marker == null ? "" : marker)
|
||||
.elem("IsTruncated", page.isTruncated());
|
||||
if (page.isTruncated()) {
|
||||
xml.elem("NextMarker", page.nextMarker());
|
||||
}
|
||||
xml.elem("MaxItems", maxItems).end("ListReusableDelegationSetsResponse");
|
||||
return Response.ok(xml.build(), XML).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Hosted Zones ──────────────────────────────────────────────────────────
|
||||
|
||||
@POST
|
||||
@@ -63,12 +128,16 @@ public class Route53Controller {
|
||||
throw new AwsException("InvalidInput", "Name and CallerReference are required.", 400);
|
||||
}
|
||||
|
||||
CreateZoneResult result = service.createHostedZone(name, callerRef, comment, vpcAssociation);
|
||||
String delegationSetId = XmlParser.extractFirst(body, "DelegationSetId", null);
|
||||
if (delegationSetId != null && Boolean.parseBoolean(XmlParser.extractFirst(body, "PrivateZone", "false"))) {
|
||||
throw new AwsException("InvalidInput", "A private hosted zone cannot use a reusable delegation set.", 400);
|
||||
}
|
||||
CreateZoneResult result = service.createHostedZone(name, callerRef, comment, vpcAssociation, delegationSetId);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("CreateHostedZoneResponse", NS)
|
||||
.raw(xmlHostedZone(result.zone()))
|
||||
.raw(xmlChangeInfo(result.change()))
|
||||
.raw(xmlDelegationSet());
|
||||
.raw(xmlDelegationSet(service.getDelegationSet(result.zone())));
|
||||
if (!result.zone().getVpcAssociations().isEmpty()) {
|
||||
xml.raw(xmlVpcAssociation(result.zone().getVpcAssociations().getFirst()));
|
||||
}
|
||||
@@ -91,7 +160,7 @@ public class Route53Controller {
|
||||
String xml = new XmlBuilder()
|
||||
.start("GetHostedZoneResponse", NS)
|
||||
.raw(xmlHostedZone(zone))
|
||||
.raw(xmlDelegationSet())
|
||||
.raw(xmlDelegationSet(service.getDelegationSet(zone)))
|
||||
.raw(xmlVpcAssociations(zone.getVpcAssociations()))
|
||||
.end("GetHostedZoneResponse")
|
||||
.build();
|
||||
@@ -142,9 +211,10 @@ public class Route53Controller {
|
||||
@GET
|
||||
@Path("/hostedzone")
|
||||
public Response listHostedZones(@QueryParam("marker") String marker,
|
||||
@QueryParam("maxitems") @DefaultValue("100") int maxItems) {
|
||||
@QueryParam("maxitems") @DefaultValue("100") int maxItems,
|
||||
@QueryParam("delegationsetid") String delegationSetId) {
|
||||
try {
|
||||
Route53Pagination.Page<HostedZone> page = service.pageHostedZones(marker, maxItems);
|
||||
Route53Pagination.Page<HostedZone> page = service.pageHostedZones(marker, maxItems, delegationSetId);
|
||||
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("ListHostedZonesResponse", NS)
|
||||
@@ -628,7 +698,7 @@ public class Route53Controller {
|
||||
.elem("Type", type)
|
||||
.elem("Value", value)
|
||||
.end("Limit")
|
||||
.elem("Count", 0L)
|
||||
.elem("Count", "MAX_REUSABLE_DELEGATION_SETS_BY_OWNER".equals(type) ? service.getReusableDelegationSetCount() : 0L)
|
||||
.end("GetAccountLimitResponse")
|
||||
.build();
|
||||
return Response.ok(xml, XML).build();
|
||||
@@ -735,15 +805,14 @@ public class Route53Controller {
|
||||
.build();
|
||||
}
|
||||
|
||||
private String xmlDelegationSet() {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DelegationSet")
|
||||
private String xmlDelegationSet(DelegationSet set) {
|
||||
XmlBuilder xml = new XmlBuilder().start("DelegationSet")
|
||||
.elem("Id", set.id() == null ? null : "/delegationset/" + set.id()).elem("CallerReference", set.callerReference())
|
||||
.start("NameServers");
|
||||
for (String ns : service.getNameServers()) {
|
||||
for (String ns : set.nameServers()) {
|
||||
xml.elem("NameServer", ns);
|
||||
}
|
||||
xml.end("NameServers").end("DelegationSet");
|
||||
return xml.build();
|
||||
return xml.end("NameServers").end("DelegationSet").build();
|
||||
}
|
||||
|
||||
private String xmlVpcAssociation(VpcAssociation association) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.route53.model.AliasTarget;
|
||||
import io.github.hectorvent.floci.services.route53.model.ChangeInfo;
|
||||
import io.github.hectorvent.floci.services.route53.model.DelegationSet;
|
||||
import io.github.hectorvent.floci.services.route53.model.HealthCheck;
|
||||
import io.github.hectorvent.floci.services.route53.model.HealthCheckConfig;
|
||||
import io.github.hectorvent.floci.services.route53.model.HostedZone;
|
||||
@@ -22,6 +23,7 @@ import io.github.hectorvent.floci.services.route53.model.VpcAssociation;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
@@ -48,6 +50,7 @@ public class Route53Service implements Resettable {
|
||||
private record OwnedZone(String accountId, HostedZone zone) {}
|
||||
|
||||
private final StorageBackend<String, HostedZone> zoneStore;
|
||||
private final StorageBackend<String, DelegationSet> delegationSetStore;
|
||||
private final StorageBackend<String, List<ResourceRecordSet>> recordStore;
|
||||
private final StorageBackend<String, HealthCheck> healthCheckStore;
|
||||
private final StorageBackend<String, ChangeInfo> changeStore;
|
||||
@@ -68,6 +71,8 @@ public class Route53Service implements Resettable {
|
||||
Ec2Service ec2Service) {
|
||||
this.zoneStore = factory.create("route53", "route53-zones.json",
|
||||
new TypeReference<Map<String, HostedZone>>() {});
|
||||
this.delegationSetStore = factory.create("route53", "route53-delegation-sets.json",
|
||||
new TypeReference<Map<String, DelegationSet>>() {});
|
||||
this.recordStore = factory.create("route53", "route53-records.json",
|
||||
new TypeReference<Map<String, List<ResourceRecordSet>>>() {});
|
||||
this.healthCheckStore = factory.create("route53", "route53-health-checks.json",
|
||||
@@ -96,10 +101,127 @@ public class Route53Service implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized DelegationSet createReusableDelegationSet(String callerReference, String hostedZoneId) {
|
||||
if (callerReference == null || callerReference.isEmpty() || callerReference.length() > 128) {
|
||||
throw new AwsException("InvalidInput", "CallerReference must contain between 1 and 128 characters.", 400);
|
||||
}
|
||||
for (DelegationSet existing : delegationSetStore.scan(k -> true)) {
|
||||
if (existing.callerReference().equals(callerReference)) {
|
||||
throw new AwsException("DelegationSetAlreadyCreated",
|
||||
"A reusable delegation set with this caller reference already exists.", 400);
|
||||
}
|
||||
}
|
||||
if (delegationSetStore.keys().size() >= 100) {
|
||||
throw new AwsException("LimitsExceeded", "The reusable delegation set limit has been reached.", 400);
|
||||
}
|
||||
HostedZone source = null;
|
||||
if (hostedZoneId != null) {
|
||||
String zoneId = hostedZoneId.startsWith("/hostedzone/") ? hostedZoneId.substring(12) : hostedZoneId;
|
||||
if (zoneId.isEmpty() || zoneId.length() > 32) {
|
||||
throw new AwsException("InvalidInput", "HostedZoneId must contain between 1 and 32 characters.", 400);
|
||||
}
|
||||
try {
|
||||
source = getHostedZoneOwnedByCaller(zoneId);
|
||||
} catch (AwsException e) {
|
||||
if (!"NoSuchHostedZone".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
throw new AwsException("HostedZoneNotFound", "The specified hosted zone cannot be found.", 400);
|
||||
}
|
||||
if (source.isPrivateZone()) {
|
||||
throw new AwsException("InvalidArgument", "A private hosted zone cannot have a reusable delegation set.", 400);
|
||||
}
|
||||
if (source.getDelegationSetId() != null) {
|
||||
throw new AwsException("DelegationSetAlreadyReusable", "The hosted zone's delegation set is already reusable.", 400);
|
||||
}
|
||||
}
|
||||
String id = "N" + generateZoneId().substring(1);
|
||||
DelegationSet set = new DelegationSet(id, callerReference,
|
||||
source == null ? nameServers : assignedNameServers(source));
|
||||
delegationSetStore.put(id, set);
|
||||
if (source != null) {
|
||||
source.setDelegationSetId(id);
|
||||
source.setDelegationNameServers(set.nameServers());
|
||||
zoneStore.put(source.getId(), source);
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
public DelegationSet getReusableDelegationSet(String id) {
|
||||
String cleanId = delegationSetId(id);
|
||||
return delegationSetStore.get(cleanId).orElseThrow(() ->
|
||||
new AwsException("NoSuchDelegationSet", "No reusable delegation set found with ID: " + cleanId, 400));
|
||||
}
|
||||
|
||||
public synchronized void deleteReusableDelegationSet(String id) {
|
||||
DelegationSet set = getReusableDelegationSet(id);
|
||||
if (zoneStore.scan(k -> true).stream().anyMatch(zone -> set.id().equals(zone.getDelegationSetId()))) {
|
||||
throw new AwsException("DelegationSetInUse", "The reusable delegation set is associated with a hosted zone.", 400);
|
||||
}
|
||||
delegationSetStore.delete(set.id());
|
||||
}
|
||||
|
||||
public DelegationSet getDelegationSet(HostedZone zone) {
|
||||
return zone.getDelegationSetId() == null ? new DelegationSet(null, null, nameServers)
|
||||
: getReusableDelegationSet(zone.getDelegationSetId());
|
||||
}
|
||||
|
||||
public long getReusableDelegationSetCount() {
|
||||
return delegationSetStore.keys().size();
|
||||
}
|
||||
|
||||
Route53Pagination.Page<DelegationSet> pageReusableDelegationSets(String marker, String maxItems) {
|
||||
if (marker != null && marker.length() > 64) {
|
||||
throw new AwsException("InvalidInput", "Marker must contain at most 64 characters.", 400);
|
||||
}
|
||||
int pageSize;
|
||||
try {
|
||||
BigInteger requested = new BigInteger(maxItems);
|
||||
if (requested.signum() <= 0) {
|
||||
throw new NumberFormatException("Nonpositive page size");
|
||||
}
|
||||
pageSize = requested.min(BigInteger.valueOf(100)).intValueExact();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidInput", "MaxItems must be a positive integer.", 400);
|
||||
}
|
||||
List<DelegationSet> sets = delegationSetStore.scan(k -> true).stream()
|
||||
.filter(set -> marker == null || set.id().compareTo(marker) >= 0)
|
||||
.sorted(Comparator.comparing(DelegationSet::id)).toList();
|
||||
return Route53Pagination.page(sets, null, pageSize, DelegationSet::id);
|
||||
}
|
||||
|
||||
private List<String> assignedNameServers(HostedZone zone) {
|
||||
if (zone.getDelegationSetId() != null) {
|
||||
return getReusableDelegationSet(zone.getDelegationSetId()).nameServers();
|
||||
}
|
||||
if (!zone.getDelegationNameServers().isEmpty()) {
|
||||
return zone.getDelegationNameServers();
|
||||
}
|
||||
// Older zones did not store their assignment separately from the apex NS records.
|
||||
List<String> stored = recordStore.get(zone.getId()).orElse(List.of()).stream()
|
||||
.filter(record -> "NS".equals(record.getType()) && isApexSoaOrNs(record, zone.getName()))
|
||||
.flatMap(record -> record.getRecords().stream()).map(ResourceRecord::getValue)
|
||||
.map(Route53Service::delegationName).toList();
|
||||
return stored.size() == 4 ? stored : nameServers;
|
||||
}
|
||||
|
||||
private static String delegationSetId(String id) {
|
||||
String cleanId = id == null ? "" : id.startsWith("/delegationset/") ? id.substring(15) : id;
|
||||
if (cleanId.isEmpty() || cleanId.length() > 32) {
|
||||
throw new AwsException("InvalidInput", "DelegationSetId must contain between 1 and 32 characters.", 400);
|
||||
}
|
||||
return cleanId;
|
||||
}
|
||||
|
||||
// ── Hosted Zones ──────────────────────────────────────────────────────────
|
||||
|
||||
public synchronized CreateZoneResult createHostedZone(String name, String callerReference,
|
||||
String comment, VpcAssociation vpcAssociation) {
|
||||
return createHostedZone(name, callerReference, comment, vpcAssociation, null);
|
||||
}
|
||||
|
||||
public synchronized CreateZoneResult createHostedZone(String name, String callerReference, String comment,
|
||||
VpcAssociation vpcAssociation, String delegationSetId) {
|
||||
String normalizedName = normalizeName(name);
|
||||
|
||||
for (HostedZone existing : zoneStore.scan(k -> true)) {
|
||||
@@ -109,25 +231,57 @@ public class Route53Service implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
DelegationSet set = null;
|
||||
if (delegationSetId != null) {
|
||||
if (vpcAssociation != null) {
|
||||
throw new AwsException("InvalidInput", "A private hosted zone cannot use a reusable delegation set.", 400);
|
||||
}
|
||||
set = getReusableDelegationSet(delegationSetId);
|
||||
validateDelegationSetForZone(set, normalizedName);
|
||||
}
|
||||
String id = generateZoneId();
|
||||
String callerAccountId = callerAccountId();
|
||||
validateVpcOwnershipIfKnown(vpcAssociation, callerAccountId);
|
||||
HostedZone zone = new HostedZone(id, normalizedName, callerReference, comment, vpcAssociation);
|
||||
zone.setOwnerAccountId(callerAccountId);
|
||||
zone.setDelegationSetId(set == null ? null : set.id());
|
||||
zone.setDelegationNameServers(set == null ? nameServers : set.nameServers());
|
||||
if (vpcAssociation != null) {
|
||||
vpcAssociation.setOwnerAccountId(callerAccountId);
|
||||
}
|
||||
zoneNameCounts(zone.isPrivateZone()).merge(normalizedName.toLowerCase(), 1, Integer::sum);
|
||||
zoneStore.put(id, zone);
|
||||
recordStore.put(id, buildDefaultRecords(normalizedName));
|
||||
recordStore.put(id, buildDefaultRecords(normalizedName, zone.getDelegationNameServers()));
|
||||
ChangeInfo change = newChange(null);
|
||||
return new CreateZoneResult(zone, change);
|
||||
}
|
||||
|
||||
private void validateDelegationSetForZone(DelegationSet set, String name) {
|
||||
String accountId = callerAccountId();
|
||||
List<HostedZone> zones = allHostedZonesAcrossAccounts().stream()
|
||||
.filter(owned -> accountId.equals(owned.accountId()) && accountId.equals(ownerAccountId(owned.zone())))
|
||||
.map(OwnedZone::zone).toList();
|
||||
if (zones.stream().filter(zone -> set.id().equals(zone.getDelegationSetId())).count() >= 100) {
|
||||
throw new AwsException("TooManyHostedZones", "The hosted zone limit for this reusable delegation set has been reached.", 400);
|
||||
}
|
||||
String normalizedName = normalizeRecordName(name);
|
||||
for (HostedZone existing : zones) {
|
||||
if (existing.isPrivateZone()) {
|
||||
continue;
|
||||
}
|
||||
String existingName = normalizeRecordName(existing.getName());
|
||||
boolean related = DomainNames.isWithin(normalizedName, existingName)
|
||||
|| DomainNames.isWithin(existingName, normalizedName);
|
||||
if (related && assignedNameServers(existing).stream().anyMatch(ns ->
|
||||
set.nameServers().stream().anyMatch(candidate -> ns.equalsIgnoreCase(candidate)))) {
|
||||
throw new AwsException("ConflictingDomainExists",
|
||||
"The domain overlaps a public hosted zone that uses the same name servers.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HostedZone getHostedZone(String id) {
|
||||
HostedZone zone = zoneStore.get(id).orElseThrow(() ->
|
||||
new AwsException("NoSuchHostedZone",
|
||||
"No hosted zone found with ID: " + id, 404));
|
||||
HostedZone zone = getHostedZoneOwnedByCaller(id);
|
||||
zone.setResourceRecordSetCount(recordCount(id));
|
||||
return zone;
|
||||
}
|
||||
@@ -167,7 +321,15 @@ public class Route53Service implements Resettable {
|
||||
}
|
||||
|
||||
Route53Pagination.Page<HostedZone> pageHostedZones(String marker, int maxItems) {
|
||||
return pageHostedZones(marker, maxItems, null);
|
||||
}
|
||||
|
||||
Route53Pagination.Page<HostedZone> pageHostedZones(String marker, int maxItems, String delegationSetId) {
|
||||
String selected = delegationSetId == null ? null : getReusableDelegationSet(delegationSetId).id();
|
||||
List<HostedZone> all = new ArrayList<>(zoneStore.scan(k -> true));
|
||||
if (selected != null) {
|
||||
all.removeIf(zone -> !selected.equals(zone.getDelegationSetId()));
|
||||
}
|
||||
all.sort(Comparator.comparing(HostedZone::getName).thenComparing(HostedZone::getId));
|
||||
for (HostedZone zone : all) {
|
||||
zone.setResourceRecordSetCount(recordCount(zone.getId()));
|
||||
@@ -831,7 +993,8 @@ public class Route53Service implements Resettable {
|
||||
return zone;
|
||||
}
|
||||
|
||||
HostedZone zone = getHostedZone(zoneId);
|
||||
HostedZone zone = zoneStore.get(zoneId).orElseThrow(() ->
|
||||
new AwsException("NoSuchHostedZone", "No hosted zone found with ID: " + zoneId, 404));
|
||||
if (!ownerAccountId(zone).equals(callerAccountId)) {
|
||||
throw new AwsException("NoSuchHostedZone",
|
||||
"No hosted zone found with ID: " + zoneId, 404);
|
||||
@@ -952,7 +1115,7 @@ public class Route53Service implements Resettable {
|
||||
return change;
|
||||
}
|
||||
|
||||
private List<ResourceRecordSet> buildDefaultRecords(String zoneName) {
|
||||
private List<ResourceRecordSet> buildDefaultRecords(String zoneName, List<String> zoneNameServers) {
|
||||
List<ResourceRecordSet> records = new ArrayList<>();
|
||||
|
||||
ResourceRecordSet soa = new ResourceRecordSet();
|
||||
@@ -960,14 +1123,14 @@ public class Route53Service implements Resettable {
|
||||
soa.setType("SOA");
|
||||
soa.setTtl(900L);
|
||||
soa.setRecords(List.of(new ResourceRecord(
|
||||
normalizeName(nameServers.get(0)) + " awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400")));
|
||||
normalizeName(zoneNameServers.get(0)) + " awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400")));
|
||||
records.add(soa);
|
||||
|
||||
ResourceRecordSet ns = new ResourceRecordSet();
|
||||
ns.setName(zoneName);
|
||||
ns.setType("NS");
|
||||
ns.setTtl(172800L);
|
||||
ns.setRecords(nameServers.stream()
|
||||
ns.setRecords(zoneNameServers.stream()
|
||||
.map(n -> new ResourceRecord(normalizeName(n)))
|
||||
.toList());
|
||||
records.add(ns);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package io.github.hectorvent.floci.services.route53.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RegisterForReflection
|
||||
public record DelegationSet(String id, String callerReference, List<String> nameServers) {
|
||||
public DelegationSet {
|
||||
nameServers = List.copyOf(nameServers);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@ public class HostedZone {
|
||||
private String callerReference;
|
||||
private String comment;
|
||||
private String ownerAccountId;
|
||||
private String delegationSetId;
|
||||
private List<String> delegationNameServers = List.of();
|
||||
private boolean privateZone;
|
||||
private int resourceRecordSetCount;
|
||||
private List<VpcAssociation> vpcAssociations = new ArrayList<>();
|
||||
@@ -52,6 +54,14 @@ public class HostedZone {
|
||||
public String getOwnerAccountId() { return ownerAccountId; }
|
||||
public void setOwnerAccountId(String ownerAccountId) { this.ownerAccountId = ownerAccountId; }
|
||||
|
||||
public String getDelegationSetId() { return delegationSetId; }
|
||||
public void setDelegationSetId(String delegationSetId) { this.delegationSetId = delegationSetId; }
|
||||
|
||||
public List<String> getDelegationNameServers() { return delegationNameServers; }
|
||||
public void setDelegationNameServers(List<String> delegationNameServers) {
|
||||
this.delegationNameServers = delegationNameServers == null ? List.of() : List.copyOf(delegationNameServers);
|
||||
}
|
||||
|
||||
public boolean isPrivateZone() { return privateZone; }
|
||||
public void setPrivateZone(boolean privateZone) { this.privateZone = privateZone; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user