mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): accept layer ARNs in GetLayerVersion (#8345)
GetLayerVersion took an unversioned layer ARN as a literal layer name and answered ResourceNotFoundException. It now resolves the ARN through the existing layer resolver, keeping its account and partition checks, and builds the archive URL from the layer's own Region. Both GetLayerVersion and GetLayerVersionByArn take their IAM resources from the generated request-resource table. The generator reads a name-or-ARN alternative on URI and query members, and fills an ARN variable from the integer shape named after it, so the layer version is read from the model. A REST route keeps its modeled operation when AWS authorizes it under another action name, and an ARN selector is checked on the stored version it resolves to, in that version's account and Region. The lookup tests share one layer archive fixture. Signed-off-by: Andre Kurait <akurait@amazon.com> Signed-off-by: Jackson Price <jxnprice@amazon.com> Co-authored-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
da23f42d11
commit
ade406bac2
19 files changed
+714
-59
No files matched your search
@@ -869,7 +869,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, routeAction(servingScope, ctx));
|
||||
return authorizationForRoute(servingScope, ctx);
|
||||
}
|
||||
String targetOperation = IamActionRegistry.targetOperation(ctx);
|
||||
String action = actionRegistry.resolve(servingScope, ctx);
|
||||
@@ -1011,16 +1011,21 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
.map(catalog::canonicalCredentialScope)
|
||||
.distinct()
|
||||
.sorted()
|
||||
.map(scope -> new ResolvedAuthorization(scope, routeAction(scope, ctx)))
|
||||
.map(scope -> authorizationForRoute(scope, ctx))
|
||||
.filter(authorization -> authorization.action() != null)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private ResolvedAuthorization authorizationForRoute(String scope, ContainerRequestContext ctx) {
|
||||
String action = routeAction(scope, ctx);
|
||||
return new ResolvedAuthorization(scope, action, actionRegistry.operationForRoute(scope, action, ctx));
|
||||
}
|
||||
|
||||
/**
|
||||
* The scope and action a request is authorized as, and the operation it calls as
|
||||
* {@code <namespace>:<Operation>}, which differs from the action for one AWS authorizes under another name.
|
||||
* A REST route maps straight to its action, which names the operation.
|
||||
* A modeled REST route retains its operation when AWS authorizes it under another action name.
|
||||
*/
|
||||
private record ResolvedAuthorization(String credentialScope, String action, String operation) {
|
||||
ResolvedAuthorization(String credentialScope, String action) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* The resources each operation's request names, for the namespaces whose ARNs {@code ResourceArnBuilder} does not
|
||||
@@ -97,14 +98,19 @@ final class RequestResourceMembers {
|
||||
* follows the one variable the format ends in with an id it mints, {@code /<id>}, which the format leaves out
|
||||
* (EventBridge's {@code connection/<name>/<id>}).
|
||||
*/
|
||||
record ArnFormat(String format, Pattern pattern, boolean unqualified, boolean minted) {
|
||||
record ArnFormat(String format, Pattern pattern, boolean unqualified, boolean minted, Pattern integers) {
|
||||
|
||||
ArnFormat(String format, Pattern pattern, boolean unqualified, boolean minted) {
|
||||
this(format, pattern, unqualified, minted, null);
|
||||
}
|
||||
|
||||
ArnFormat(String format, Pattern pattern, boolean unqualified) {
|
||||
this(format, pattern, unqualified, false);
|
||||
}
|
||||
|
||||
/** The ARN as its service checks it: an unqualified type's without the qualifier it may carry. */
|
||||
/** Normalize modeled integer variables and drop a qualifier only for an unqualified type. */
|
||||
String checked(String arn) {
|
||||
arn = normalizeIntegers(arn);
|
||||
if (!unqualified) {
|
||||
return arn;
|
||||
}
|
||||
@@ -119,6 +125,29 @@ final class RequestResourceMembers {
|
||||
return arn.substring(0, at);
|
||||
}
|
||||
|
||||
private String normalizeIntegers(String arn) {
|
||||
if (integers == null) {
|
||||
return arn;
|
||||
}
|
||||
Matcher values = integers.matcher(arn);
|
||||
if (!values.matches()) {
|
||||
return arn;
|
||||
}
|
||||
StringBuilder normalized = new StringBuilder();
|
||||
int at = 0;
|
||||
for (int group = 1; group <= values.groupCount(); group++) {
|
||||
String value = values.group(group);
|
||||
try {
|
||||
value = Long.toString(Long.parseLong(value));
|
||||
} catch (NumberFormatException ignored) {
|
||||
// Keep invalid selectors unchanged for the service's normal validation.
|
||||
}
|
||||
normalized.append(arn, at, values.start(group)).append(value);
|
||||
at = values.end(group);
|
||||
}
|
||||
return normalized.append(arn.substring(at)).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether AWS owns the type's resources, its format's account being {@code aws}
|
||||
* ({@code arn:${Partition}:sso::aws:applicationProvider/...}), so an ARN of one names none of the caller's.
|
||||
@@ -284,6 +313,7 @@ final class RequestResourceMembers {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " carries no operations");
|
||||
}
|
||||
Set<String> namespaces = Set.copyOf(document.path("namespaces").propertyStream().map(Map.Entry::getKey).toList());
|
||||
validateIntegerVariables(document.path("integerVariables"), document.path("arnFormats"));
|
||||
Map<String, Map<String, ArnFormat>> formats = new LinkedHashMap<>();
|
||||
document.path("arnFormats").properties().forEach(namespace -> {
|
||||
Set<String> unqualified = names(document.path("unqualified").path(namespace.getKey()));
|
||||
@@ -295,8 +325,9 @@ final class RequestResourceMembers {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " gives " + namespace.getKey() + "'s "
|
||||
+ type.getKey() + " no ARN format");
|
||||
}
|
||||
Set<String> integers = names(document.path("integerVariables").path(namespace.getKey()).path(type.getKey()));
|
||||
types.put(type.getKey(), new ArnFormat(format, pattern(format), unqualified.contains(type.getKey()),
|
||||
minted.contains(type.getKey())));
|
||||
minted.contains(type.getKey()), integers.isEmpty() ? null : pattern(format, integers)));
|
||||
});
|
||||
formats.put(namespace.getKey(), Map.copyOf(types));
|
||||
});
|
||||
@@ -730,21 +761,49 @@ final class RequestResourceMembers {
|
||||
return new Member(outer.name(), outer.list(), arnMember(operation, path.substring(dot + 1)), outer.map());
|
||||
}
|
||||
|
||||
/**
|
||||
* What an ARN of the format matches: its literal parts, a partition, any Region and account, and anything for
|
||||
* each other variable but nothing.
|
||||
*/
|
||||
/** Reject integer metadata that does not identify a resource variable in a known ARN format. */
|
||||
private static void validateIntegerVariables(JsonNode integers, JsonNode formats) {
|
||||
if (integers.isMissingNode()) {
|
||||
return;
|
||||
}
|
||||
if (!integers.isObject()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " integerVariables must be an object");
|
||||
}
|
||||
integers.properties().forEach(namespace -> {
|
||||
if (!namespace.getValue().isObject() || !formats.has(namespace.getKey())) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " integerVariables has no formats for " + namespace.getKey());
|
||||
}
|
||||
namespace.getValue().properties().forEach(type -> {
|
||||
String format = formats.path(namespace.getKey()).path(type.getKey()).asText("");
|
||||
Set<String> variables = VARIABLE.matcher(format).results().map(match -> match.group(1))
|
||||
.filter(name -> !Set.of("Partition", "Region", "Account").contains(name))
|
||||
.collect(Collectors.toSet());
|
||||
JsonNode names = type.getValue();
|
||||
if (!names.isArray() || names.isEmpty() || !names.valueStream().allMatch(
|
||||
name -> name.isTextual() && variables.contains(name.asText()))) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " integerVariables names no variables for "
|
||||
+ namespace.getKey() + ":" + type.getKey());
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static Pattern pattern(String format) {
|
||||
return pattern(format, Set.of());
|
||||
}
|
||||
|
||||
private static Pattern pattern(String format, Set<String> integers) {
|
||||
StringBuilder regex = new StringBuilder("^");
|
||||
Matcher variable = VARIABLE.matcher(format);
|
||||
int at = 0;
|
||||
while (variable.find()) {
|
||||
regex.append(Pattern.quote(format.substring(at, variable.start())));
|
||||
regex.append(switch (variable.group(1)) {
|
||||
String value = switch (variable.group(1)) {
|
||||
case "Partition" -> "[^:]+";
|
||||
case "Region", "Account" -> "[^:]*";
|
||||
default -> ".+";
|
||||
});
|
||||
};
|
||||
regex.append(integers.contains(variable.group(1)) ? "(" + value + ")" : value);
|
||||
at = variable.end();
|
||||
}
|
||||
return Pattern.compile(regex.append(Pattern.quote(format.substring(at))).append('$').toString());
|
||||
|
||||
@@ -704,7 +704,7 @@ public final class RequestResources {
|
||||
from.region().isEmpty() ? region : from.region(),
|
||||
from.accountId().isEmpty() ? accountId : from.accountId())), format,
|
||||
resource.types().getFirst(), filling.variables(), null, region), format.awsOwned(), partition,
|
||||
region, accountId, null);
|
||||
region, accountId, stored);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -872,10 +872,18 @@ public final class RequestResources {
|
||||
* the ARN's Region or the request's, whichever its handler acts on. An ARN with no account or Region keeps none,
|
||||
* and one of a type AWS owns ({@code awsOwned}) its account, {@code aws}, which is no caller's. One naming another
|
||||
* account whose resource {@code stored} says the service acts on there (an OAM sink a link is made to, a RAM
|
||||
* invitation sent to the caller) is that resource alone.
|
||||
* invitation sent to the caller) is that resource alone. When the service resolves the complete ARN to a stored
|
||||
* resource in its own coordinates, that stored ARN alone is checked.
|
||||
*/
|
||||
private static void addNamed(Set<String> arns, String arn, boolean awsOwned, String partition, String region,
|
||||
String accountId, StoredResourceArns stored) {
|
||||
if (stored != null) {
|
||||
Optional<String> resolved = stored.resolveStoredArn(arn);
|
||||
if (resolved.isPresent()) {
|
||||
arns.add(resolved.get());
|
||||
return;
|
||||
}
|
||||
}
|
||||
arns.add(arn);
|
||||
if (!AwsArnUtils.isArn(arn)) {
|
||||
return;
|
||||
|
||||
@@ -43,6 +43,15 @@ public interface StoredResourceArns {
|
||||
*/
|
||||
Optional<String> find(String type, List<String> names, String region);
|
||||
|
||||
/**
|
||||
* The resource resolved by an ARN in its own account and Region, without substituting the caller's
|
||||
* coordinates. Empty when the service has no such resource or does not resolve ARN selectors this way.
|
||||
* Used only for operations read by the generated request-resource table.
|
||||
*/
|
||||
default Optional<String> resolveStoredArn(String arn) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARNs of the resources the caller's resource named by {@code value} in {@code member}, as the table writes
|
||||
* the member's path, runs on, is in or reaches in the Region, which a request naming it acts on (the cluster or
|
||||
|
||||
@@ -743,6 +743,19 @@ public class IamActionRegistry {
|
||||
.map(IamRestRoutes.Match::labels).orElse(Map.of());
|
||||
}
|
||||
|
||||
/** Keep the modeled operation when AWS authorizes its route under a different action name. */
|
||||
public String operationForRoute(String credentialScope, String action, ContainerRequestContext ctx) {
|
||||
if (credentialScope == null || action == null) {
|
||||
return action;
|
||||
}
|
||||
String method = ctx.getMethod().toUpperCase();
|
||||
return restRoutes.operation(credentialScope, method, withoutInternalPrefix(ctx, routedPath(ctx)),
|
||||
ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx))
|
||||
.filter(operation -> action.equals(IamAuthorizedActions.actionFor(credentialScope, operation, method)))
|
||||
.map(operation -> credentialScope + ":" + operation)
|
||||
.orElse(action);
|
||||
}
|
||||
|
||||
/** Member locations of a generated alternative route; absent entries keep the operation's model locations. */
|
||||
public static Map<String, String> routeMemberLocations(String operation, ContainerRequestContext ctx) {
|
||||
int separator = operation.indexOf(':');
|
||||
|
||||
@@ -97,7 +97,8 @@ public class LambdaLayerController {
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
LambdaLayerVersion lv = layerService.getLayerVersion(region, layerName, versionNumber);
|
||||
return Response.ok(buildLayerVersionResponse(lv, region, uriInfo)).build();
|
||||
String layerRegion = AwsArnUtils.parse(lv.getLayerVersionArn()).region();
|
||||
return Response.ok(buildLayerVersionResponse(lv, layerRegion, uriInfo)).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
|
||||
@@ -8,6 +8,7 @@ import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.SignedMarkers;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.services.lambda.model.LambdaLayerVersion;
|
||||
import io.floci.aws.services.lambda.zip.DeploymentPackage;
|
||||
import io.floci.aws.services.lambda.zip.ZipExtractor;
|
||||
@@ -26,6 +27,7 @@ import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -33,7 +35,7 @@ import java.util.stream.Collectors;
|
||||
* Business logic for Lambda Layer management.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class LambdaLayerService {
|
||||
public class LambdaLayerService implements StoredResourceArns {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(LambdaLayerService.class);
|
||||
|
||||
@@ -99,6 +101,26 @@ public class LambdaLayerService {
|
||||
this.s3Service = s3Service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "lambda";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
// Layer version selectors supply the complete identifier, so no generated suffix needs a lookup.
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> resolveStoredArn(String arn) {
|
||||
if (!LAYER_VERSION_ARN.matcher(arn).matches()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
LambdaLayerVersion layer = resolveLayerByArn(arn);
|
||||
return layer == null ? Optional.empty() : Optional.of(layer.getLayerVersionArn());
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes a new layer version. Each call with the same layer name creates a new version.
|
||||
*/
|
||||
@@ -211,6 +233,9 @@ public class LambdaLayerService {
|
||||
* Returns information about a specific layer version.
|
||||
*/
|
||||
public LambdaLayerVersion getLayerVersion(String region, String layerName, long versionNumber) {
|
||||
if (AwsArnUtils.looksLikeArn(layerName)) {
|
||||
return getLayerVersionByArn(layerName + ":" + versionNumber);
|
||||
}
|
||||
return layerStore.get(region, layerName, versionNumber)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Layer version " + versionNumber + " for layer " + layerName + " not found.", 404));
|
||||
|
||||
Reference in new issue
Block a user