mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): authorize launch template names against stored resource ARNs (#8850)
This commit is contained in:
1 parent
9c8734b41f
commit
f511cd8a44
8 files changed
+481
-10
No files matched your search
@@ -0,0 +1,29 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import jakarta.enterprise.context.RequestScoped;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** Keeps name resolution stable between IAM evaluation and the operation in the same request. */
|
||||
@RequestScoped
|
||||
public class Ec2LaunchTemplateSelections {
|
||||
|
||||
private record Key(String account, String region, String name) {
|
||||
}
|
||||
|
||||
/** A null id records that the name did not exist when IAM evaluated it. */
|
||||
record Selection(String id) {
|
||||
}
|
||||
|
||||
private final Map<Key, Selection> selections = new HashMap<>();
|
||||
|
||||
Selection resolve(String account, String region, String name, Supplier<String> lookup) {
|
||||
return selections.computeIfAbsent(new Key(account, region, name), ignored -> new Selection(lookup.get()));
|
||||
}
|
||||
|
||||
Selection get(String account, String region, String name) {
|
||||
return selections.get(new Key(account, region, name));
|
||||
}
|
||||
}
|
||||
@@ -170,6 +170,9 @@ import static io.floci.aws.services.ec2.Ec2Ids.randomHex;
|
||||
@ApplicationScoped
|
||||
public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettable {
|
||||
|
||||
// CDI Instance is qualified because EC2 also has an Instance model.
|
||||
private jakarta.enterprise.inject.Instance<Ec2LaunchTemplateSelections> launchTemplateSelections;
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(Ec2Service.class);
|
||||
|
||||
/**
|
||||
@@ -608,7 +611,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
jakarta.enterprise.inject.Instance<Ec2InstanceLaunchListener> instanceLaunchListenersInstance,
|
||||
Ec2EbsEncryptionService ebsEncryptionService,
|
||||
jakarta.enterprise.inject.Instance<VpcDependencyProvider> vpcDependencyProviders,
|
||||
jakarta.enterprise.inject.Instance<ResourceGroupsService> resourceGroupsServices) {
|
||||
jakarta.enterprise.inject.Instance<ResourceGroupsService> resourceGroupsServices,
|
||||
jakarta.enterprise.inject.Instance<Ec2LaunchTemplateSelections> launchTemplateSelections) {
|
||||
this(config, containerManager, portForwardManager, amiImageResolver, imageCatalog,
|
||||
instanceTypeCatalog, storageFactory, requestContextInstance, iamService, volumeBlockDeviceManager);
|
||||
this.vpcNetworkManager = vpcNetworkManager;
|
||||
@@ -618,6 +622,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
this.ebsEncryptionService = ebsEncryptionService;
|
||||
this.vpcDependencyProviders = vpcDependencyProviders;
|
||||
this.resourceGroupsServices = resourceGroupsServices;
|
||||
this.launchTemplateSelections = launchTemplateSelections;
|
||||
}
|
||||
|
||||
public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,
|
||||
@@ -10408,6 +10413,37 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return matchingLaunchTemplates(region, ids, names, filters);
|
||||
}
|
||||
|
||||
/** The caller's stored launch-template ARN, without creating defaults or changing the template. */
|
||||
Optional<String> findLaunchTemplateArn(String region, String name) {
|
||||
String id;
|
||||
try {
|
||||
id = launchTemplateSelections == null ? launchTemplateIdByName(region, name)
|
||||
: launchTemplateSelections.get().resolve(callerAccountId(), region, name,
|
||||
() -> launchTemplateIdByName(region, name)).id();
|
||||
} catch (ContextNotActiveException expected) {
|
||||
LOG.debug("No active request context for launch-template authorization");
|
||||
id = launchTemplateIdByName(region, name);
|
||||
}
|
||||
return Optional.ofNullable(id).map(found -> AwsArnUtils.Arn.of("ec2", region, callerAccountId(),
|
||||
"launch-template/" + found).toString());
|
||||
}
|
||||
|
||||
private String launchTemplateIdByName(String region, String name) {
|
||||
return launchTemplates.scan(k -> true).stream()
|
||||
.filter(template -> region.equals(template.getRegion()) && name.equals(template.getLaunchTemplateName()))
|
||||
.map(LaunchTemplate::getLaunchTemplateId).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
private Ec2LaunchTemplateSelections.Selection selectedLaunchTemplate(String region, String name) {
|
||||
try {
|
||||
return launchTemplateSelections == null ? null
|
||||
: launchTemplateSelections.get().get(callerAccountId(), region, name);
|
||||
} catch (ContextNotActiveException expected) {
|
||||
LOG.debug("No active request context for launch-template selection");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private List<LaunchTemplate> matchingLaunchTemplates(String region, List<String> ids, List<String> names,
|
||||
List<Map.Entry<String, List<String>>> filters) {
|
||||
ensureDefaultResources(region);
|
||||
@@ -10620,9 +10656,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return launchTemplate;
|
||||
}
|
||||
} else if (name != null && !name.isBlank()) {
|
||||
return launchTemplates.scan(k -> true).stream()
|
||||
.filter(lt -> lt.getRegion().equals(region) && name.equals(lt.getLaunchTemplateName()))
|
||||
.findFirst()
|
||||
Ec2LaunchTemplateSelections.Selection selected = selectedLaunchTemplate(region, name);
|
||||
String selectedId = selected == null ? launchTemplateIdByName(region, name) : selected.id();
|
||||
return (selectedId == null ? Optional.<LaunchTemplate>empty() : launchTemplates.get(key(region, selectedId)))
|
||||
.orElseThrow(() -> new AwsException("InvalidLaunchTemplateName.NotFoundException",
|
||||
"The specified launch template does not exist.", 400));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Resolves the caller's launch-template names to their stored ID-based ARNs. */
|
||||
@ApplicationScoped
|
||||
public class Ec2StoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private final Ec2Service ec2;
|
||||
|
||||
@Inject
|
||||
public Ec2StoredResourceArns(Ec2Service ec2) {
|
||||
this.ec2 = ec2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "ec2";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return "launch-template".equals(type) && names.size() == 1
|
||||
? ec2.findLaunchTemplateArn(region, names.getFirst()) : Optional.empty();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user