feat(ssm): answer the public Amazon Linux AMI parameters (#3173)

* feat(ssm): answer AWS's public Amazon Linux AMI parameters

GetParameter, GetParameters and GetParametersByPath resolve the
documented /aws/service/ami-amazon-linux-latest/ names from the EC2
image catalog, the way AWS seeds them in every account. They are
read-only and belong to no account, so DescribeParameters and the
parameter history leave them out.

Ported from the SSM half of lex00/floci#116.

* fix(ssm): reject reserved parameter names and list public ones under ancestors

PutParameter refuses names prefixed with aws or ssm, with or without a
leading slash and in any case, with the ValidationException AWS
returns. That closes the gap where a stored value could shadow a
public AMI parameter. GetParametersByPath applies the same path and
Recursive rules to public names as to stored ones, so a recursive
query on /aws or /aws/service lists them.

* fix(ssm): reserve only the aws and ssm path segments

The reserved-name check keyed on a bare prefix, which also refused the
names CloudFormation generates for an AWS::SSM::Parameter in a stack
whose name starts with aws or ssm. It now rejects a name whose first
path segment is aws or ssm, with or without a leading slash and in any
case, and accepts names such as ssm-auto-stack-Param-ABC.
This commit is contained in:
lex00 authored and GitHub committed 2026-09-07 17:05:19 -04:00
1 parent 9eb4fc82cd
commit 8142457f9b
6 files changed
+401 -15

No files matched your search

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ec2;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -49,6 +50,22 @@ public class Ec2ImageCatalog {
return Optional.ofNullable(loaded().imagesByIdOrAlias.get(imageId));
}
/**
* Looks up the image published under one of AWS's public SSM parameter names, such as
* {@code /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64}.
*/
public Optional<CatalogImage> findByPublicParameterName(String parameterName) {
if (parameterName == null || parameterName.isBlank()) {
return Optional.empty();
}
return Optional.ofNullable(loaded().imagesByPublicParameterName.get(parameterName));
}
/** Every public SSM parameter name the catalog answers, in catalog order. */
public List<String> publicParameterNames() {
return List.copyOf(loaded().imagesByPublicParameterName.keySet());
}
private Loaded loaded() {
Loaded result = loaded;
if (result == null) {
@@ -67,6 +84,7 @@ public class Ec2ImageCatalog {
private final String defaultDockerImage;
private final List<CatalogImage> images;
private final Map<String, CatalogImage> imagesByIdOrAlias;
private final Map<String, CatalogImage> imagesByPublicParameterName;
Loaded(Catalog catalog) {
this.defaultDockerImage = require(catalog.defaultDockerImage, "defaultDockerImage");
@@ -75,6 +93,7 @@ public class Ec2ImageCatalog {
throw new IllegalStateException("EC2 image catalog has no images: " + CATALOG_RESOURCE_NAME);
}
this.imagesByIdOrAlias = indexImages(this.images);
this.imagesByPublicParameterName = indexPublicParameterNames(this.images);
}
}
@@ -107,6 +126,20 @@ public class Ec2ImageCatalog {
return Map.copyOf(index);
}
private static Map<String, CatalogImage> indexPublicParameterNames(List<CatalogImage> images) {
Map<String, CatalogImage> index = new LinkedHashMap<>();
for (CatalogImage image : images) {
for (String parameterName : image.publicParameterNames()) {
CatalogImage previous = index.putIfAbsent(parameterName, image);
if (previous != null) {
throw new IllegalStateException(
"Duplicate EC2 image catalog public parameter name: " + parameterName);
}
}
}
return Collections.unmodifiableMap(index);
}
private static void addIndexEntry(Map<String, CatalogImage> index, String imageIdOrAlias, CatalogImage image) {
CatalogImage previous = index.putIfAbsent(imageIdOrAlias, image);
if (previous != null) {
@@ -133,6 +166,7 @@ public class Ec2ImageCatalog {
public static final class CatalogImage {
public String imageId;
public List<String> aliases = List.of();
public List<String> publicParameterNames = List.of();
public String dockerImage;
public String name;
public String description;
@@ -160,6 +194,14 @@ public class Ec2ImageCatalog {
return aliases == null ? List.of() : List.copyOf(aliases);
}
/**
* The public SSM parameter names AWS publishes this image under. AWS seeds them in every
* account with no setup, so SSM answers a read of one of these names from the catalog.
*/
public List<String> publicParameterNames() {
return publicParameterNames == null ? List.of() : List.copyOf(publicParameterNames);
}
public List<String> idsAndAliases() {
List<String> ids = new ArrayList<>();
ids.add(imageId);
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.Ec2ImageCatalog;
import io.github.hectorvent.floci.services.ssm.model.Parameter;
import io.github.hectorvent.floci.services.ssm.model.ParameterHistory;
import io.github.hectorvent.floci.services.ssm.model.PatchBaselineIdentity;
@@ -31,6 +32,8 @@ public class SsmService implements ResourceProvider {
private static final Logger LOG = Logger.getLogger(SsmService.class);
private static final String PUBLIC_PARAMETER_PREFIX = "/aws/service/";
/**
* Account-default values for the service settings floci models. AWS rejects
* unknown setting ids with ServiceSettingNotFound; so do we.
@@ -50,9 +53,11 @@ public class SsmService implements ResourceProvider {
private final StorageBackend<String, SsmAssociation> associationStore;
private final int maxParameterHistory;
private final RegionResolver regionResolver;
private final Ec2ImageCatalog imageCatalog;
@Inject
public SsmService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver) {
public SsmService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver,
Ec2ImageCatalog imageCatalog) {
this(
storageFactory.create("ssm", "ssm-parameters.json",
new TypeReference<>() {
@@ -73,7 +78,8 @@ public class SsmService implements ResourceProvider {
new TypeReference<>() {
}),
config.services().ssm().maxParameterHistory(),
regionResolver
regionResolver,
imageCatalog
);
}
@@ -117,6 +123,22 @@ public class SsmService implements ResourceProvider {
StorageBackend<String, ServiceSetting> serviceSettingStore,
StorageBackend<String, SsmAssociation> associationStore,
int maxParameterHistory, RegionResolver regionResolver) {
this(parameterStore, historyStore, documentPermissionStore, documentStore,
serviceSettingStore, associationStore, maxParameterHistory, regionResolver, null);
}
/**
* Package-private constructor for testing without CDI. A null image catalog means no
* public parameters are answered.
*/
SsmService(StorageBackend<String, Parameter> parameterStore,
StorageBackend<String, List<ParameterHistory>> historyStore,
StorageBackend<String, List<String>> documentPermissionStore,
StorageBackend<String, SsmDocument> documentStore,
StorageBackend<String, ServiceSetting> serviceSettingStore,
StorageBackend<String, SsmAssociation> associationStore,
int maxParameterHistory, RegionResolver regionResolver,
Ec2ImageCatalog imageCatalog) {
this.parameterStore = parameterStore;
this.historyStore = historyStore;
this.documentPermissionStore = documentPermissionStore;
@@ -125,6 +147,7 @@ public class SsmService implements ResourceProvider {
this.associationStore = associationStore;
this.maxParameterHistory = maxParameterHistory;
this.regionResolver = regionResolver;
this.imageCatalog = imageCatalog;
}
/**
@@ -132,6 +155,7 @@ public class SsmService implements ResourceProvider {
* Returns the version number.
*/
public long putParameter(String name, String value, String type, String description, boolean overwrite, String region) {
rejectReservedName(name);
String storageKey = regionKey(region, name);
Parameter existing = parameterStore.get(storageKey).orElse(null);
@@ -157,8 +181,7 @@ public class SsmService implements ResourceProvider {
}
public Parameter getParameter(String name, String region) {
String storageKey = regionKey(region, name);
return parameterStore.get(storageKey)
return findParameter(name, region)
.orElseThrow(() -> new AwsException("ParameterNotFound",
"Parameter " + name + " not found.", 400));
}
@@ -166,29 +189,88 @@ public class SsmService implements ResourceProvider {
public List<Parameter> getParameters(List<String> names, String region) {
List<Parameter> result = new ArrayList<>();
for (String name : names) {
parameterStore.get(regionKey(region, name)).ifPresent(result::add);
findParameter(name, region).ifPresent(result::add);
}
return result;
}
/**
* AWS reserves the {@code aws} and {@code ssm} namespaces, with or without a leading slash
* and regardless of case, so an account can never write over a public parameter. The rule
* keys on the first path segment rather than a bare prefix, so a name such as
* {@code ssm-auto-stack-Param-ABC}, which CloudFormation generates for a stack whose name
* starts with ssm, is still accepted.
*/
private static void rejectReservedName(String name) {
String bare = name == null ? "" : name.startsWith("/") ? name.substring(1) : name;
String lower = bare.toLowerCase(Locale.ROOT);
if (lower.equals("aws") || lower.equals("ssm")
|| lower.startsWith("aws/") || lower.startsWith("ssm/")) {
throw new AwsException("ValidationException",
"Parameter name: can't be prefixed with \"aws\" or \"ssm\" (case-insensitive). "
+ "If formed as a path, it can consist of sub-paths divided by slash symbol; "
+ "each sub-path can be formed as a mix of letters, numbers and the following "
+ "3 symbols .-_", 400);
}
}
private Optional<Parameter> findParameter(String name, String region) {
Optional<Parameter> stored = parameterStore.get(regionKey(region, name));
if (stored.isPresent()) {
return stored;
}
return publicParameter(name, region);
}
/**
* AWS publishes read-only AMI id parameters under {@code /aws/service/} in every account
* with no setup, so a read of one of those names is answered from the EC2 image catalog.
* Nothing is written: like on AWS the parameter is not the account's own, so it never
* shows up in DescribeParameters or GetParameterHistory. Any other name under the prefix
* is still ParameterNotFound.
*/
private Optional<Parameter> publicParameter(String name, String region) {
if (imageCatalog == null || name == null || !name.startsWith(PUBLIC_PARAMETER_PREFIX)) {
return Optional.empty();
}
return imageCatalog.findByPublicParameterName(name).map(image -> {
Parameter parameter = new Parameter(name, image.imageId, "String");
parameter.setArn("arn:aws:ssm:" + region + "::parameter" + name);
parameter.setLastModifiedDate(Instant.parse(image.creationDate));
return parameter;
});
}
public List<Parameter> getParametersByPath(String path, boolean recursive, String region) {
String normalizedPath = path.endsWith("/") ? path : path + "/";
String prefix = region + "::";
return parameterStore.scan(key -> {
List<Parameter> result = new ArrayList<>(parameterStore.scan(key -> {
if (!key.startsWith(prefix)) {
return false;
}
String paramName = key.substring(prefix.length());
if (!paramName.startsWith(normalizedPath)) {
return false;
return underPath(key.substring(prefix.length()), normalizedPath, recursive);
}));
if (imageCatalog != null) {
// The public names answer to the same path and Recursive rules as stored ones, so a
// recursive query on an ancestor such as /aws lists them too.
for (String name : imageCatalog.publicParameterNames()) {
if (underPath(name, normalizedPath, recursive)) {
publicParameter(name, region).ifPresent(result::add);
}
}
if (recursive) {
return true;
}
String remainder = paramName.substring(normalizedPath.length());
return !remainder.contains("/");
});
}
return result;
}
private static boolean underPath(String paramName, String normalizedPath, boolean recursive) {
if (!paramName.startsWith(normalizedPath)) {
return false;
}
if (recursive) {
return true;
}
return !paramName.substring(normalizedPath.length()).contains("/");
}
public void deleteParameter(String name, String region) {