mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
feat(cloudformation): provision signing profiles and code signing configs (#7546)
This commit is contained in:
1 parent
ee038af772
commit
b35e51374c
22 files changed
+1173
-35
No files matched your search
+3
@@ -101,6 +101,9 @@ public final class CfnRollback {
|
||||
*/
|
||||
public static final String EVENT_INVOKE_CONFIG_SNAPSHOT_ATTR = "__FlociEventInvokeConfigSnapshot";
|
||||
|
||||
/** The code signing config an in-place update changed, as stored, so a failed stack update can put it back. */
|
||||
public static final String CODE_SIGNING_CONFIG_UPDATE_SNAPSHOT_ATTR = "__FlociCodeSigningConfigUpdateSnapshot";
|
||||
|
||||
/** The prior WAFv2 logging configuration for an in-place update that may need rollback. */
|
||||
public static final String WAF_LOGGING_UPDATE_SNAPSHOT_ATTR = "__FlociWafLoggingUpdateSnapshot";
|
||||
|
||||
|
||||
+21
@@ -141,6 +141,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
applyLambdaReservedConcurrency(region, func, desired);
|
||||
applyLambdaCodeSigningConfig(ctx, func, desired);
|
||||
|
||||
r.setPhysicalId(desired.functionName());
|
||||
r.getAttributes().put("Arn", func.getFunctionArn());
|
||||
@@ -281,6 +282,10 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (!tags.isEmpty()) {
|
||||
createRequest.put("Tags", tags);
|
||||
}
|
||||
String codeSigningConfigArn = ctx.resolveOptional(props, "CodeSigningConfigArn");
|
||||
if (codeSigningConfigArn != null) {
|
||||
createRequest.put("CodeSigningConfigArn", codeSigningConfigArn);
|
||||
}
|
||||
Integer reservedConcurrentExecutions = null;
|
||||
String reserved = ctx.resolveOptional(props, "ReservedConcurrentExecutions");
|
||||
if (reserved != null) {
|
||||
@@ -459,6 +464,22 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches or replaces the template's code signing config, and detaches one only when the previous
|
||||
* template declared it: a config attached outside the stack stays, as CloudFormation leaves it.
|
||||
*/
|
||||
private void applyLambdaCodeSigningConfig(ProvisionContext ctx, LambdaFunction fn, LambdaDesiredState desired) {
|
||||
Object codeSigningConfigArn = desired.createRequest().get("CodeSigningConfigArn");
|
||||
if (codeSigningConfigArn == null) {
|
||||
if (ctx.previouslyDeclared("CodeSigningConfigArn") && fn.getCodeSigningConfigArn() != null) {
|
||||
lambdaService.deleteFunctionCodeSigningConfig(ctx.region(), fn.getFunctionName());
|
||||
}
|
||||
} else if (!codeSigningConfigArn.equals(fn.getCodeSigningConfigArn())) {
|
||||
lambdaService.attachFunctionCodeSigningConfig(ctx.region(), fn.getFunctionName(),
|
||||
codeSigningConfigArn.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The form LambdaService stores a hot-reload host path in: normalized, so {@code /a/../b/}
|
||||
* and {@code /b} are the same mount, and rendered as the POSIX path Docker receives
|
||||
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaCodeSigningConfigService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.CodeSigningConfig;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Provisions {@code AWS::Lambda::CodeSigningConfig}. {@code Ref} is the config's ARN. Every property
|
||||
* updates in place, and an update applies the template's whole state, so a dropped description or
|
||||
* policy returns to its default. A rolled-back update puts the prior settings back. Deleting a config
|
||||
* a function still uses is refused, as Lambda's DeleteCodeSigningConfig refuses it.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class LambdaCodeSigningConfigCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private final LambdaCodeSigningConfigService configs;
|
||||
private final LambdaService lambdaService;
|
||||
|
||||
@Inject
|
||||
public LambdaCodeSigningConfigCfnProvisioner(LambdaCodeSigningConfigService configs, LambdaService lambdaService) {
|
||||
this.configs = configs;
|
||||
this.lambdaService = lambdaService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of("AWS::Lambda::CodeSigningConfig");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
r.getAttributes().remove(CfnRollback.CODE_SIGNING_CONFIG_UPDATE_SNAPSHOT_ATTR);
|
||||
Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
|
||||
JsonNode properties = props == null ? MAPPER.missingNode() : props;
|
||||
String description = Objects.requireNonNullElse(ctx.resolveOptional(properties, "Description"), "");
|
||||
CodeSigningConfig.AllowedPublishers publishers = new CodeSigningConfig.AllowedPublishers();
|
||||
publishers.setSigningProfileVersionArns(ctx.resolveStringList(
|
||||
ctx.engine().resolveNode(properties.path("AllowedPublishers")), "SigningProfileVersionArns"));
|
||||
CodeSigningConfig.CodeSigningPolicies policies = new CodeSigningConfig.CodeSigningPolicies();
|
||||
policies.setUntrustedArtifactOnDeployment(Objects.requireNonNullElse(ctx.resolveOptional(
|
||||
ctx.engine().resolveNode(properties.path("CodeSigningPolicies")), "UntrustedArtifactOnDeployment"),
|
||||
"Warn"));
|
||||
|
||||
// A dispatcher stub from before this provisioner existed is not an ARN and names no config.
|
||||
String prior = ctx.priorPhysicalId();
|
||||
CodeSigningConfig config = ctx.isUpdate() && AwsArnUtils.isArn(prior)
|
||||
? configs.withConfigLock(prior, () -> {
|
||||
r.getAttributes().put(CfnRollback.CODE_SIGNING_CONFIG_UPDATE_SNAPSHOT_ATTR,
|
||||
MAPPER.valueToTree(configs.get(ctx.region(), prior)).toString());
|
||||
return configs.update(ctx.region(), prior, description, publishers, policies);
|
||||
})
|
||||
: configs.create(ctx.region(), ctx.accountId(), description, publishers, policies);
|
||||
r.setPhysicalId(config.getCodeSigningConfigArn());
|
||||
r.getAttributes().put("CodeSigningConfigArn", config.getCodeSigningConfigArn());
|
||||
r.getAttributes().put("CodeSigningConfigId", config.getCodeSigningConfigId());
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasReplacementUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.hasReplacement(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String updateCleanupPhysicalId(StackResource resource) {
|
||||
return ReplacementCleanup.cleanupPhysicalId(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateCleanupResult completeUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.complete(resource, this::delete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearUpdate(StackResource resource) {
|
||||
ReplacementCleanup.clear(resource);
|
||||
}
|
||||
|
||||
/** A stub migration deletes the config it created; an in-place update puts the prior settings back. */
|
||||
@Override
|
||||
public boolean rollbackUpdate(StackResource resource) {
|
||||
if (ReplacementCleanup.rollback(resource, this::delete)) {
|
||||
return true;
|
||||
}
|
||||
JsonNode snapshot = CfnRollback.updateSnapshot(resource, CfnRollback.CODE_SIGNING_CONFIG_UPDATE_SNAPSHOT_ATTR);
|
||||
if (snapshot == null) {
|
||||
return true;
|
||||
}
|
||||
CodeSigningConfig prior = MAPPER.convertValue(snapshot, CodeSigningConfig.class);
|
||||
String arn = resource.getPhysicalId();
|
||||
String region = AwsArnUtils.parse(arn).region();
|
||||
configs.withConfigLock(arn, () -> configs.update(region, arn, prior.getDescription(),
|
||||
prior.getAllowedPublishers(), prior.getCodeSigningPolicies()));
|
||||
resource.getAttributes().remove(CfnRollback.CODE_SIGNING_CONFIG_UPDATE_SNAPSHOT_ATTR);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
if (!AwsArnUtils.isArn(physicalId)) {
|
||||
return;
|
||||
}
|
||||
String accountId = AwsArnUtils.parse(physicalId).accountId();
|
||||
CfnDeletes.safeDelete("Lambda code signing config", physicalId,
|
||||
() -> lambdaService.deleteCodeSigningConfig(region, accountId, physicalId),
|
||||
"ResourceNotFoundException");
|
||||
}
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Provisions {@code AWS::Signer::SigningProfile}. Floci has no Signer API, so a profile is only the
|
||||
* identifiers CloudFormation hands the template: {@code Ref} is the profile ARN, and the attributes
|
||||
* carry the name, a version and the version ARN a code signing config lists. Every property but
|
||||
* {@code Tags} is create-only: an update that keeps them keeps the profile, one that changes them
|
||||
* mints a new one, and one that would keep an explicit name is refused, as CloudFormation refuses
|
||||
* replacing any custom-named resource. A replaced or deleted profile leaves nothing behind to clean
|
||||
* up, and a rolled-back replacement points the resource back to the prior profile.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class SignerCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final String TYPE = "AWS::Signer::SigningProfile";
|
||||
private static final List<String> PLATFORMS = List.of("AWSLambda-SHA384-ECDSA", "Notation-OCI-SHA384-ECDSA");
|
||||
private static final Pattern PROFILE_NAME = Pattern.compile("[0-9a-zA-Z_]{2,64}");
|
||||
private static final Map<String, Integer> MAX_VALIDITY = Map.of("DAYS", 4105, "MONTHS", 135, "YEARS", 11);
|
||||
private static final String ALPHANUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
/** The create-only properties the profile was minted from, so an update can tell whether it is replaced. */
|
||||
private static final String CREATE_ONLY_ATTR = "FlociSigningProfileCreateOnly";
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of(TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
|
||||
String platformId = ctx.resolveOptional(props, "PlatformId");
|
||||
if (platformId == null || !PLATFORMS.contains(platformId)) {
|
||||
throw new AwsException("ValidationError",
|
||||
TYPE + " PlatformId must be one of " + PLATFORMS + ", got " + platformId, 400);
|
||||
}
|
||||
String explicitName = ctx.resolveOptional(props, "ProfileName");
|
||||
if (explicitName != null && !PROFILE_NAME.matcher(explicitName).matches()) {
|
||||
throw new AwsException("ValidationError",
|
||||
TYPE + " ProfileName must be 2 to 64 characters from [0-9a-zA-Z_], got " + explicitName, 400);
|
||||
}
|
||||
JsonNode period = ctx.engine().resolveNodeOmittingNoValue(props.path("SignatureValidityPeriod"));
|
||||
validatePeriod(period, ctx);
|
||||
String createOnly = platformId + "|" + Objects.toString(explicitName, "") + "|"
|
||||
+ period.path("Type").asText() + "|" + period.path("Value").asText();
|
||||
if (!createOnly.equals(r.getAttributes().get(CREATE_ONLY_ATTR))) {
|
||||
if (ctx.isUpdate() && explicitName != null && explicitName.equals(r.getAttributes().get("ProfileName"))) {
|
||||
throw new AwsException("ValidationError",
|
||||
"CloudFormation cannot update a stack when a custom-named resource requires "
|
||||
+ "replacing. Rename " + explicitName + " and update the stack again.", 400);
|
||||
}
|
||||
mint(r, ctx, explicitName, createOnly);
|
||||
}
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
}
|
||||
|
||||
/**
|
||||
* Signer's checks on a supplied validity period, in its order and with its messages: a known unit,
|
||||
* then a value above zero and at most the unit's cap. An omitted period keeps Signer's default.
|
||||
*/
|
||||
private static void validatePeriod(JsonNode period, ProvisionContext ctx) {
|
||||
if (period.isMissingNode()) {
|
||||
return;
|
||||
}
|
||||
String type = period.path("Type").asText(null);
|
||||
if (type == null || !MAX_VALIDITY.containsKey(type)) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Signature validity period type must be one of [DAYS, MONTHS, YEARS]", 400);
|
||||
}
|
||||
Integer value = ctx.resolveOptionalInt(period, "Value");
|
||||
if (value == null || value < 1) {
|
||||
throw new AwsException("ValidationError", "Signature validity period value must be greater than 0.", 400);
|
||||
}
|
||||
if (value > MAX_VALIDITY.get(type)) {
|
||||
throw new AwsException("ValidationError", "Signature validity period value for " + type
|
||||
+ " must be less than or equal to " + MAX_VALIDITY.get(type), 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void mint(StackResource r, ProvisionContext ctx, String explicitName, String createOnly) {
|
||||
String name = explicitName != null ? explicitName
|
||||
: r.getLogicalId().substring(0, Math.min(r.getLogicalId().length(), 51)) + "_" + random(12);
|
||||
String arn = AwsArnUtils.Arn.of("signer", ctx.region(), ctx.accountId(), "/signing-profiles/" + name)
|
||||
.toString();
|
||||
String version = random(10);
|
||||
r.setPhysicalId(arn);
|
||||
r.getAttributes().put("Arn", arn);
|
||||
r.getAttributes().put("ProfileName", name);
|
||||
r.getAttributes().put("ProfileVersion", version);
|
||||
r.getAttributes().put("ProfileVersionArn", arn + "/" + version);
|
||||
r.getAttributes().put(CREATE_ONLY_ATTR, createOnly);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasReplacementUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.hasReplacement(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String updateCleanupPhysicalId(StackResource resource) {
|
||||
return ReplacementCleanup.cleanupPhysicalId(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateCleanupResult completeUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.complete(resource, this::delete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearUpdate(StackResource resource) {
|
||||
ReplacementCleanup.clear(resource);
|
||||
}
|
||||
|
||||
/** A replacement points back to the prior profile; a tags-only update changed nothing Floci holds. */
|
||||
@Override
|
||||
public boolean rollbackUpdate(StackResource resource) {
|
||||
ReplacementCleanup.rollback(resource, this::delete);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static String random(int length) {
|
||||
ThreadLocalRandom random = ThreadLocalRandom.current();
|
||||
StringBuilder out = new StringBuilder(length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
out.append(ALPHANUMERIC.charAt(random.nextInt(ALPHANUMERIC.length())));
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
+2
-12
@@ -130,18 +130,8 @@ public class LambdaCodeSigningConfigFunctionsController {
|
||||
@PathParam("codeSigningConfigArn") String codeSigningConfigArn) {
|
||||
LambdaCodeSigningConfigService.validateArn(codeSigningConfigArn);
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
String accountId = regionResolver.getAccountId();
|
||||
return service.withConfigLock(codeSigningConfigArn, () -> {
|
||||
service.get(region, accountId, codeSigningConfigArn);
|
||||
PaginatedResult<String> attached = lambdaService.listFunctionsByCodeSigningConfig(
|
||||
region, accountId, codeSigningConfigArn, 1, null);
|
||||
if (!attached.items().isEmpty()) {
|
||||
throw new AwsException("ResourceConflictException",
|
||||
"The code signing configuration is still attached to a function.", 409);
|
||||
}
|
||||
service.delete(region, codeSigningConfigArn);
|
||||
return Response.noContent().build();
|
||||
});
|
||||
lambdaService.deleteCodeSigningConfig(region, regionResolver.getAccountId(), codeSigningConfigArn);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
+5
-12
@@ -37,17 +37,14 @@ import jakarta.ws.rs.core.Response;
|
||||
public class LambdaCodeSigningController {
|
||||
|
||||
private final LambdaService lambdaService;
|
||||
private final LambdaCodeSigningConfigService configService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public LambdaCodeSigningController(LambdaService lambdaService,
|
||||
LambdaCodeSigningConfigService configService,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper) {
|
||||
this.lambdaService = lambdaService;
|
||||
this.configService = configService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
@@ -74,16 +71,12 @@ public class LambdaCodeSigningController {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
String arn = body == null || !body.hasNonNull("CodeSigningConfigArn")
|
||||
? null : body.get("CodeSigningConfigArn").asText();
|
||||
LambdaCodeSigningConfigService.validateArn(arn);
|
||||
return configService.withConfigLock(arn, () -> {
|
||||
configService.requireForAttachment(region, regionResolver.getAccountId(), arn);
|
||||
LambdaFunction fn = lambdaService.putFunctionCodeSigningConfig(region, functionName, arn);
|
||||
LambdaFunction fn = lambdaService.attachFunctionCodeSigningConfig(region, functionName, arn);
|
||||
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
root.put("CodeSigningConfigArn", arn);
|
||||
root.put("FunctionName", fn.getFunctionName());
|
||||
return Response.ok(root).build();
|
||||
});
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
root.put("CodeSigningConfigArn", arn);
|
||||
root.put("FunctionName", fn.getFunctionName());
|
||||
return Response.ok(root).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
|
||||
@@ -736,6 +736,42 @@ public class LambdaService implements ResourceProvider {
|
||||
return updateFunctionCodeSigningConfig(region, functionName, codeSigningConfigArn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and attaches a code-signing configuration under the configuration's lock, as
|
||||
* PutFunctionCodeSigningConfig does.
|
||||
*/
|
||||
public LambdaFunction attachFunctionCodeSigningConfig(String region, String functionName,
|
||||
String codeSigningConfigArn) {
|
||||
if (codeSigningConfigService == null) {
|
||||
throw new IllegalStateException("Lambda code-signing configuration service is unavailable");
|
||||
}
|
||||
LambdaCodeSigningConfigService.validateArn(codeSigningConfigArn);
|
||||
return codeSigningConfigService.withConfigLock(codeSigningConfigArn, () -> {
|
||||
codeSigningConfigService.requireForAttachment(region, regionResolver.getAccountId(), codeSigningConfigArn);
|
||||
return putFunctionCodeSigningConfig(region, functionName, codeSigningConfigArn);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a code-signing configuration under the configuration's lock, refusing one a function
|
||||
* still uses, as DeleteCodeSigningConfig does.
|
||||
*/
|
||||
public void deleteCodeSigningConfig(String region, String accountId, String codeSigningConfigArn) {
|
||||
if (codeSigningConfigService == null) {
|
||||
throw new IllegalStateException("Lambda code-signing configuration service is unavailable");
|
||||
}
|
||||
codeSigningConfigService.withConfigLock(codeSigningConfigArn, () -> {
|
||||
codeSigningConfigService.get(region, accountId, codeSigningConfigArn);
|
||||
if (!listFunctionsByCodeSigningConfig(region, accountId, codeSigningConfigArn, 1, null)
|
||||
.items().isEmpty()) {
|
||||
throw new AwsException("ResourceConflictException",
|
||||
"The code signing configuration is still attached to a function.", 409);
|
||||
}
|
||||
codeSigningConfigService.delete(region, codeSigningConfigArn);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/** Removes a code-signing configuration from the function's {@code $LATEST}. */
|
||||
public void deleteFunctionCodeSigningConfig(String region, String functionName) {
|
||||
updateFunctionCodeSigningConfig(region, functionName, null);
|
||||
|
||||
Reference in new issue
Block a user