mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor: read MWAA, Lambda, Amazon MQ and Cognito Regions with regionOf (#8938)
* refactor: read MWAA, Lambda, Amazon MQ and Cognito Regions with regionOf
This commit is contained in:
1 parent
987a2e581b
commit
e3b7fd286c
21 files changed
+98
-92
No files matched your search
@@ -326,7 +326,7 @@ public class AmazonMqService implements ResourceProvider {
|
||||
|
||||
/** A broker's key in its own Region, which its ARN has always named. */
|
||||
private String brokerKey(Broker broker) {
|
||||
return brokerKey(broker.region(regionResolver.getDefaultRegion()), broker.getBrokerId());
|
||||
return brokerKey(regionResolver.regionOf(broker.getBrokerArn()), broker.getBrokerId());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -183,9 +183,9 @@ public class RabbitMqManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** The broker's Region, {@link Broker#region} with the configured default Region. */
|
||||
/** The Region the broker's ARN names, and the default Region for a broker recorded without one. */
|
||||
private String brokerRegion(Broker broker) {
|
||||
return broker.region(regionResolver.getDefaultRegion());
|
||||
return regionResolver.regionOf(broker.getBrokerArn());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,6 @@ package io.floci.aws.services.amazonmq.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
@@ -139,12 +138,6 @@ public class Broker {
|
||||
public String getBrokerArn() { return brokerArn; }
|
||||
public void setBrokerArn(String brokerArn) { this.brokerArn = brokerArn; }
|
||||
|
||||
/**
|
||||
* The Region the broker's ARN names; a broker recorded without one counts as {@code defaultRegion}'s.
|
||||
* Not a getter, so Jackson neither persists nor returns it.
|
||||
*/
|
||||
public String region(String defaultRegion) { return AwsArnUtils.regionOrDefault(brokerArn, defaultRegion); }
|
||||
|
||||
public String getBrokerName() { return brokerName; }
|
||||
public void setBrokerName(String brokerName) { this.brokerName = brokerName; }
|
||||
|
||||
|
||||
@@ -270,7 +270,7 @@ public class CognitoService implements ResourceProvider, StoredResourceArns {
|
||||
lambdaService,
|
||||
acmService,
|
||||
new VerificationCodeService(storageFactory, clock),
|
||||
new CognitoMessageDispatcher(sesService, snsService, regionResolver.getDefaultRegion()),
|
||||
new CognitoMessageDispatcher(sesService, snsService, regionResolver),
|
||||
certificateManager,
|
||||
clock,
|
||||
emulatorConfig.services().cognito().enforceTokenValidityLimits(),
|
||||
|
||||
+7
-6
@@ -2,6 +2,7 @@ package io.floci.aws.services.cognito.verification;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.DomainNames;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.services.cognito.CognitoHtml;
|
||||
import io.floci.aws.services.cognito.model.CognitoUser;
|
||||
import io.floci.aws.services.cognito.model.UserPool;
|
||||
@@ -75,13 +76,13 @@ public final class CognitoMessageDispatcher {
|
||||
|
||||
private final SesService ses;
|
||||
private final SnsService sns;
|
||||
private final String defaultRegion;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
/** {@code defaultRegion} serves a pool whose ARN names no region; a pool otherwise sends from its own. */
|
||||
public CognitoMessageDispatcher(SesService ses, SnsService sns, String defaultRegion) {
|
||||
/** A pool sends from the Region its ARN names, and from {@code regionResolver}'s default when it names none. */
|
||||
public CognitoMessageDispatcher(SesService ses, SnsService sns, RegionResolver regionResolver) {
|
||||
this.ses = ses;
|
||||
this.sns = sns;
|
||||
this.defaultRegion = defaultRegion;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
public void dispatch(UserPool pool, CognitoUser user, VerificationCode.Purpose purpose,
|
||||
@@ -130,7 +131,7 @@ public final class CognitoMessageDispatcher {
|
||||
String phone = user.getAttributes().get("phone_number");
|
||||
|
||||
List<String> mediums = resolveDeliveryMediums(deliveryMediums, email, phone);
|
||||
String region = AwsArnUtils.regionOrDefault(pool.getArn(), defaultRegion);
|
||||
String region = regionResolver.regionOf(pool.getArn());
|
||||
|
||||
for (String medium : mediums) {
|
||||
if ("EMAIL".equalsIgnoreCase(medium) && email != null) {
|
||||
@@ -181,7 +182,7 @@ public final class CognitoMessageDispatcher {
|
||||
String defaultBody = temporaryPassword == null ? DEFAULT_PASSWORDLESS_INVITE_BODY : DEFAULT_INVITE_BODY;
|
||||
String email = invitationRecipient(user, deliveryMediums, "EMAIL");
|
||||
String phone = invitationRecipient(user, deliveryMediums, "SMS");
|
||||
String region = AwsArnUtils.regionOrDefault(pool.getArn(), defaultRegion);
|
||||
String region = regionResolver.regionOf(pool.getArn());
|
||||
|
||||
List<Runnable> sends = new ArrayList<>();
|
||||
if (email != null) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.docker.ContainerBuilder;
|
||||
import io.floci.aws.core.common.docker.ContainerLifecycleManager;
|
||||
import io.floci.aws.core.common.docker.ContainerLogStreamer;
|
||||
@@ -125,6 +126,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
private final RuntimeApiServerFactory runtimeApiServerFactory;
|
||||
private final DockerHostResolver dockerHostResolver;
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
private final EcrRegistryManager ecrRegistryManager;
|
||||
private final LambdaLayerService layerService;
|
||||
private final LaunchedContainerAwsEnv awsEnv;
|
||||
@@ -143,6 +145,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
RuntimeApiServerFactory runtimeApiServerFactory,
|
||||
DockerHostResolver dockerHostResolver,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver,
|
||||
EcrRegistryManager ecrRegistryManager,
|
||||
LambdaLayerService layerService,
|
||||
LaunchedContainerAwsEnv awsEnv,
|
||||
@@ -156,6 +159,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
this.runtimeApiServerFactory = runtimeApiServerFactory;
|
||||
this.dockerHostResolver = dockerHostResolver;
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
this.ecrRegistryManager = ecrRegistryManager;
|
||||
this.layerService = layerService;
|
||||
this.awsEnv = awsEnv;
|
||||
@@ -262,7 +266,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
String cwLogGroup = LambdaLogDestinations.logGroup(fn);
|
||||
String cwLogStream = LOG_STREAM_DATE_FMT.format(LocalDate.now()) + "/"
|
||||
+ LambdaLogDestinations.logStreamSuffix(fn, shortId);
|
||||
String lambdaRegion = extractRegionFromArn(fn.getFunctionArn(), config.defaultRegion());
|
||||
String lambdaRegion = regionResolver.regionOf(fn.getFunctionArn());
|
||||
String lambdaAccountId = AwsArnUtils.accountOrDefault(fn.getFunctionArn(), config.defaultAccountId());
|
||||
|
||||
List<String> env = LambdaRuntimeEnv.of(fn, runtimeApiEndpoint, cwLogGroup, cwLogStream);
|
||||
@@ -1591,10 +1595,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
}
|
||||
}
|
||||
|
||||
private static String extractRegionFromArn(String arn, String defaultRegion) {
|
||||
return AwsArnUtils.regionOrDefault(arn, defaultRegion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a TAR archive from all files in {@code sourceDir}, streaming to {@code out}.
|
||||
* Uses GNU long-name extension (via Commons Compress) so file paths of any length
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.github.dockerjava.api.model.ContainerNetwork;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.Base64Url;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.docker.ContainerBuilder;
|
||||
import io.floci.aws.core.common.docker.ContainerDetector;
|
||||
import io.floci.aws.core.common.docker.ContainerExec;
|
||||
@@ -94,6 +95,7 @@ public class MwaaEnvironmentManager {
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final ContainerDetector containerDetector;
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
private final LaunchedContainerAwsEnv awsEnv;
|
||||
private final Ec2Service ec2Service;
|
||||
/** The VPC address each Airflow container holds, returned once the container is gone. */
|
||||
@@ -104,12 +106,14 @@ public class MwaaEnvironmentManager {
|
||||
ContainerLifecycleManager lifecycleManager,
|
||||
ContainerDetector containerDetector,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver,
|
||||
LaunchedContainerAwsEnv awsEnv,
|
||||
Ec2Service ec2Service) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.containerDetector = containerDetector;
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
this.awsEnv = awsEnv;
|
||||
this.ec2Service = ec2Service;
|
||||
this.vpcLeases = new VpcContainerLeases(ec2Service, "MWAA environment",
|
||||
@@ -134,7 +138,7 @@ public class MwaaEnvironmentManager {
|
||||
String dbPassword = generateSecret(24);
|
||||
environment.setDbPassword(dbPassword);
|
||||
|
||||
String dbContainerName = dbContainerName(config, environment);
|
||||
String dbContainerName = dbContainerName(environment);
|
||||
String dbVolume = dbContainerName;
|
||||
lifecycleManager.removeIfExists(dbContainerName);
|
||||
lifecycleManager.ensureVolume(dbVolume);
|
||||
@@ -172,7 +176,7 @@ public class MwaaEnvironmentManager {
|
||||
void startAirflowContainer(Environment environment, String airflowVersion, String dbIp, String dbPassword,
|
||||
byte[] startupScriptContent, byte[] requirementsContent) {
|
||||
String name = environment.getName();
|
||||
String airflowContainerName = airflowContainerName(config, environment);
|
||||
String airflowContainerName = airflowContainerName(environment);
|
||||
String dagsVolume = airflowContainerName + "-dags";
|
||||
String logsVolume = airflowContainerName + "-logs";
|
||||
|
||||
@@ -383,8 +387,8 @@ public class MwaaEnvironmentManager {
|
||||
if (environment.getDbContainerId() != null) {
|
||||
lifecycleManager.stopAndRemove(environment.getDbContainerId(), null);
|
||||
}
|
||||
String airflowContainerName = airflowContainerName(config, environment);
|
||||
lifecycleManager.removeVolume(dbContainerName(config, environment));
|
||||
String airflowContainerName = airflowContainerName(environment);
|
||||
lifecycleManager.removeVolume(dbContainerName(environment));
|
||||
lifecycleManager.removeVolume(airflowContainerName + "-dags");
|
||||
lifecycleManager.removeVolume(airflowContainerName + "-logs");
|
||||
LOG.infov("Stopped MWAA containers for environment {0}", name);
|
||||
@@ -419,7 +423,7 @@ public class MwaaEnvironmentManager {
|
||||
Collection<ContainerNetwork> networks;
|
||||
try {
|
||||
InspectContainerResponse inspect = lifecycleManager.getDockerClient()
|
||||
.inspectContainerCmd(airflowContainerName(config, environment)).exec();
|
||||
.inspectContainerCmd(airflowContainerName(environment)).exec();
|
||||
containerId = inspect.getId();
|
||||
networks = inspect.getNetworkSettings() == null || inspect.getNetworkSettings().getNetworks() == null
|
||||
? List.of() : inspect.getNetworkSettings().getNetworks().values();
|
||||
@@ -602,21 +606,17 @@ public class MwaaEnvironmentManager {
|
||||
|
||||
/** Routed through {@link ContainerStorageHelper} so multiple Floci instances sharing one Docker
|
||||
* daemon (via {@code FLOCI_DOCKER_RESOURCE_NAMESPACE}) don't collide, same as every other
|
||||
* Docker-backed service (EKS, RDS, ...). {@code config} may be {@code null} — the helper treats
|
||||
* that as "no namespace configured" and applies only the {@code floci-aws-} prefix. */
|
||||
static String dbContainerName(EmulatorConfig config, Environment environment) {
|
||||
return ContainerStorageHelper.dockerName(config, "mwaa-" + environmentIdentity(config, environment) + "-db");
|
||||
* Docker-backed service (EKS, RDS, ...). */
|
||||
String dbContainerName(Environment environment) {
|
||||
return ContainerStorageHelper.dockerName(config, "mwaa-" + environmentIdentity(environment) + "-db");
|
||||
}
|
||||
|
||||
static String airflowContainerName(EmulatorConfig config, Environment environment) {
|
||||
return ContainerStorageHelper.dockerName(config,
|
||||
"mwaa-" + environmentIdentity(config, environment) + "-airflow");
|
||||
String airflowContainerName(Environment environment) {
|
||||
return ContainerStorageHelper.dockerName(config, "mwaa-" + environmentIdentity(environment) + "-airflow");
|
||||
}
|
||||
|
||||
private static String environmentIdentity(EmulatorConfig config, Environment environment) {
|
||||
String defaultRegion = config != null ? config.defaultRegion() : null;
|
||||
return environmentAccount(environment) + "." + environmentRegion(environment, defaultRegion) + "."
|
||||
+ environment.getName();
|
||||
private String environmentIdentity(Environment environment) {
|
||||
return environmentAccount(environment) + "." + regionOf(environment) + "." + environment.getName();
|
||||
}
|
||||
|
||||
static String environmentAccount(Environment environment) {
|
||||
@@ -625,13 +625,9 @@ public class MwaaEnvironmentManager {
|
||||
: AwsArnUtils.accountOrDefault(environment.getArn(), "000000000000");
|
||||
}
|
||||
|
||||
/** The Region the environment's ARN names, and the default Region for one without an ARN. */
|
||||
private String regionOf(Environment environment) {
|
||||
return environmentRegion(environment, config.defaultRegion());
|
||||
}
|
||||
|
||||
/** The region the environment's ARN names; {@code defaultRegion} answers for one without an ARN. */
|
||||
static String environmentRegion(Environment environment, String defaultRegion) {
|
||||
return AwsArnUtils.regionOrDefault(environment.getArn(), defaultRegion);
|
||||
return regionResolver.regionOf(environment.getArn());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -893,7 +893,7 @@ public class MwaaService implements TagHandler, VpcDependencyProvider {
|
||||
}
|
||||
|
||||
private String environmentRegion(Environment environment) {
|
||||
return MwaaEnvironmentManager.environmentRegion(environment, regionResolver.getDefaultRegion());
|
||||
return regionResolver.regionOf(environment.getArn());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user