mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ssm): add DescribePatchBaselines and GetDefaultPatchBaseline (#1404)
Return AWS-owned predefined patch baselines (one default per operating system) so Terraform's aws_ssm_patch_baseline data source resolves. Supports DescribePatchBaselines filters OWNER/OPERATING_SYSTEM/NAME_PREFIX and GetDefaultPatchBaseline. Read-only reference data; no customer-owned baselines. Refs #956
This commit is contained in:
1 parent
f3f481cb4d
commit
be9e61d788
5 files changed
+221
No files matched your search
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.services.ssm.model.CommandInvocation;
|
||||
import io.github.hectorvent.floci.services.ssm.model.InstanceInformation;
|
||||
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;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -57,6 +58,9 @@ public class SsmJsonHandler {
|
||||
case "ListCommandInvocations" -> handleListCommandInvocations(request, region);
|
||||
case "CancelCommand" -> handleCancelCommand(request, region);
|
||||
case "DescribeInstanceInformation" -> handleDescribeInstanceInformation(request, region);
|
||||
// Patch Manager (read-only: AWS-owned predefined baselines)
|
||||
case "DescribePatchBaselines" -> handleDescribePatchBaselines(request, region);
|
||||
case "GetDefaultPatchBaseline" -> handleGetDefaultPatchBaseline(request, region);
|
||||
// Agent registration (internal, not in public SDK)
|
||||
case "UpdateInstanceInformation" -> handleUpdateInstanceInformation(request, region);
|
||||
default -> Response.status(400)
|
||||
@@ -200,6 +204,47 @@ public class SsmJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleDescribePatchBaselines(JsonNode request, String region) {
|
||||
Map<String, List<String>> filters = new HashMap<>();
|
||||
JsonNode filtersNode = request.path("Filters");
|
||||
if (filtersNode.isArray()) {
|
||||
for (JsonNode f : filtersNode) {
|
||||
String key = f.path("Key").asText("");
|
||||
List<String> values = new ArrayList<>();
|
||||
f.path("Values").forEach(v -> values.add(v.asText()));
|
||||
if (!key.isEmpty()) {
|
||||
filters.put(key, values);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<PatchBaselineIdentity> baselines = ssmService.describePatchBaselines(filters);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode identities = objectMapper.createArrayNode();
|
||||
for (PatchBaselineIdentity b : baselines) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("BaselineId", b.baselineId());
|
||||
node.put("BaselineName", b.baselineName());
|
||||
node.put("OperatingSystem", b.operatingSystem());
|
||||
node.put("BaselineDescription", b.baselineDescription());
|
||||
node.put("DefaultBaseline", b.defaultBaseline());
|
||||
identities.add(node);
|
||||
}
|
||||
response.set("BaselineIdentities", identities);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleGetDefaultPatchBaseline(JsonNode request, String region) {
|
||||
String operatingSystem = request.has("OperatingSystem") ? request.path("OperatingSystem").asText() : null;
|
||||
String baselineId = ssmService.getDefaultPatchBaseline(operatingSystem);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("BaselineId", baselineId);
|
||||
response.put("OperatingSystem", (operatingSystem == null || operatingSystem.isBlank()) ? "WINDOWS" : operatingSystem);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleLabelParameterVersion(JsonNode request, String region) {
|
||||
String name = request.path("Name").asText();
|
||||
long parameterVersion = request.path("ParameterVersion").asLong();
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
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;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -241,6 +242,82 @@ public class SsmService {
|
||||
LOG.debugv("Removed tags from parameter: {0}", resourceId);
|
||||
}
|
||||
|
||||
// ──────────────────────────── Patch Baselines ────────────────────────────
|
||||
// AWS provides a fixed set of AWS-owned predefined patch baselines (one default per operating
|
||||
// system). These are static reference data, not customer state, so they live in-memory only.
|
||||
|
||||
private static final List<PatchBaselineIdentity> PREDEFINED_BASELINES = buildPredefinedBaselines();
|
||||
|
||||
private static List<PatchBaselineIdentity> buildPredefinedBaselines() {
|
||||
String[][] defs = {
|
||||
{"WINDOWS", "AWS-DefaultPatchBaseline", "Windows"},
|
||||
{"AMAZON_LINUX", "AWS-AmazonLinuxDefaultPatchBaseline", "Amazon Linux"},
|
||||
{"AMAZON_LINUX_2", "AWS-AmazonLinux2DefaultPatchBaseline", "Amazon Linux 2"},
|
||||
{"AMAZON_LINUX_2022", "AWS-AmazonLinux2022DefaultPatchBaseline", "Amazon Linux 2022"},
|
||||
{"AMAZON_LINUX_2023", "AWS-AmazonLinux2023DefaultPatchBaseline", "Amazon Linux 2023"},
|
||||
{"UBUNTU", "AWS-UbuntuDefaultPatchBaseline", "Ubuntu"},
|
||||
{"REDHAT_ENTERPRISE_LINUX", "AWS-RedHatDefaultPatchBaseline", "Red Hat Enterprise Linux"},
|
||||
{"SUSE", "AWS-SuseDefaultPatchBaseline", "SUSE Linux Enterprise Server"},
|
||||
{"CENTOS", "AWS-CentOSDefaultPatchBaseline", "CentOS"},
|
||||
{"ORACLE_LINUX", "AWS-OracleLinuxDefaultPatchBaseline", "Oracle Linux"},
|
||||
{"DEBIAN", "AWS-DebianDefaultPatchBaseline", "Debian Server"},
|
||||
{"MACOS", "AWS-MacOSDefaultPatchBaseline", "macOS"},
|
||||
{"RASPBIAN", "AWS-RaspbianDefaultPatchBaseline", "Raspbian"},
|
||||
{"ROCKY_LINUX", "AWS-RockyLinuxDefaultPatchBaseline", "Rocky Linux"},
|
||||
{"ALMA_LINUX", "AWS-AlmaLinuxDefaultPatchBaseline", "AlmaLinux"},
|
||||
};
|
||||
List<PatchBaselineIdentity> baselines = new ArrayList<>();
|
||||
for (String[] def : defs) {
|
||||
String os = def[0];
|
||||
String name = def[1];
|
||||
String description = "Default Patch Baseline for " + def[2] + " Provided by AWS.";
|
||||
baselines.add(new PatchBaselineIdentity(stableBaselineId(name), name, os, description, true));
|
||||
}
|
||||
return List.copyOf(baselines);
|
||||
}
|
||||
|
||||
/** Deterministic AWS-style baseline id (pb-<17 hex>) derived from the baseline name. */
|
||||
private static String stableBaselineId(String name) {
|
||||
long h = 1125899906842597L;
|
||||
for (int i = 0; i < name.length(); i++) {
|
||||
h = 31 * h + name.charAt(i);
|
||||
}
|
||||
String hex = String.format("%016x", h & 0x0FFFFFFFFFFFFFFFL);
|
||||
return "pb-0" + hex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return AWS-owned predefined patch baselines matching the given DescribePatchBaselines filters
|
||||
* (supported keys: OWNER, OPERATING_SYSTEM, NAME_PREFIX). There are no customer-owned baselines.
|
||||
*/
|
||||
public List<PatchBaselineIdentity> describePatchBaselines(Map<String, List<String>> filters) {
|
||||
List<String> owners = filters.getOrDefault("OWNER", List.of());
|
||||
// OWNER=Self matches only customer-owned baselines, of which there are none.
|
||||
if (!owners.isEmpty() && !owners.contains("AWS") && !owners.contains("All")) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
List<String> operatingSystems = filters.getOrDefault("OPERATING_SYSTEM", List.of());
|
||||
List<String> namePrefixes = filters.getOrDefault("NAME_PREFIX", List.of());
|
||||
|
||||
return PREDEFINED_BASELINES.stream()
|
||||
.filter(b -> operatingSystems.isEmpty() || operatingSystems.contains(b.operatingSystem()))
|
||||
.filter(b -> namePrefixes.isEmpty()
|
||||
|| namePrefixes.stream().anyMatch(prefix -> b.baselineName().startsWith(prefix)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/** Return the default patch baseline id for an operating system (defaults to WINDOWS). */
|
||||
public String getDefaultPatchBaseline(String operatingSystem) {
|
||||
String os = (operatingSystem == null || operatingSystem.isBlank()) ? "WINDOWS" : operatingSystem;
|
||||
return PREDEFINED_BASELINES.stream()
|
||||
.filter(b -> b.operatingSystem().equals(os))
|
||||
.findFirst()
|
||||
.map(PatchBaselineIdentity::baselineId)
|
||||
.orElseThrow(() -> new AwsException("DoesNotExistException",
|
||||
"No default patch baseline exists for operating system " + os, 400));
|
||||
}
|
||||
|
||||
private static String regionKey(String region, String name) {
|
||||
return region + "::" + name;
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package io.github.hectorvent.floci.services.ssm.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/**
|
||||
* Identity of an AWS-owned predefined patch baseline, as returned by DescribePatchBaselines.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public record PatchBaselineIdentity(String baselineId,
|
||||
String baselineName,
|
||||
String operatingSystem,
|
||||
String baselineDescription,
|
||||
boolean defaultBaseline) {
|
||||
}
|
||||
Reference in new issue
Block a user