mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): serve placement/availability-zone-id from instance metadata (#8405)
IMDS answered /latest/meta-data/placement/availability-zone but not availability-zone-id, which AWS serves and scripts read to pick zone-specific resources. It now returns the zone's ID, the same one DescribeSubnets and DescribeAvailabilityZones report for that zone.
This commit is contained in:
1 parent
b18feb39d1
commit
2fa6deb0d8
3 files changed
+35
-4
No files matched your search
@@ -310,6 +310,7 @@ User data is returned as the original base64-decoded bytes, without decompressin
|
||||
| `GET /latest/meta-data/mac` | MAC address of first ENI |
|
||||
| `GET /latest/meta-data/security-groups` | Security group names |
|
||||
| `GET /latest/meta-data/placement/availability-zone` | AZ |
|
||||
| `GET /latest/meta-data/placement/availability-zone-id` | AZ ID (for example `use1-az1`), the same ID `DescribeSubnets` reports for the zone |
|
||||
| `GET /latest/meta-data/placement/region` | Region |
|
||||
| `GET /latest/meta-data/iam/info` | IAM instance profile info |
|
||||
| `GET /latest/meta-data/iam/security-credentials/` | Role name list |
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AvailabilityZoneIds;
|
||||
import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.services.ec2.model.GroupIdentifier;
|
||||
import io.floci.aws.services.ec2.model.Instance;
|
||||
@@ -180,10 +181,10 @@ public class Ec2MetadataServer {
|
||||
router.get("/latest/meta-data/hostname").handler(ctx -> handleText(ctx, inst -> inst.getPrivateDnsName()));
|
||||
router.get("/latest/meta-data/mac").handler(ctx -> handleMac(ctx));
|
||||
router.get("/latest/meta-data/security-groups").handler(ctx -> handleSecurityGroups(ctx));
|
||||
router.get("/latest/meta-data/placement/availability-zone").handler(ctx -> handleText(ctx, inst ->
|
||||
inst.getPlacement() != null
|
||||
? inst.getPlacement().getAvailabilityZone()
|
||||
: config.defaultAvailabilityZone()));
|
||||
router.get("/latest/meta-data/placement/availability-zone").handler(ctx -> handleText(ctx,
|
||||
this::availabilityZone));
|
||||
router.get("/latest/meta-data/placement/availability-zone-id").handler(ctx -> handleText(ctx, inst ->
|
||||
AvailabilityZoneIds.forZoneName(inst.getRegion(), availabilityZone(inst))));
|
||||
router.get("/latest/meta-data/placement/region").handler(ctx -> handleText(ctx, inst -> inst.getRegion()));
|
||||
router.get("/latest/meta-data/iam/info").handler(ctx -> handleIamInfo(ctx));
|
||||
router.get("/latest/meta-data/iam/security-credentials/").handler(ctx -> handleCredentialsList(ctx));
|
||||
@@ -450,6 +451,11 @@ public class Ec2MetadataServer {
|
||||
ctx.response().putHeader("content-type", "application/json").end(credentialsJson(result.get()));
|
||||
}
|
||||
|
||||
/** The instance's zone; one without a placement is in the configured default zone. */
|
||||
private String availabilityZone(Instance inst) {
|
||||
return inst.getPlacement() != null ? inst.getPlacement().getAvailabilityZone() : config.defaultAvailabilityZone();
|
||||
}
|
||||
|
||||
/** The account that launched the instance; instances persisted before it was recorded belong to the default one. */
|
||||
private String ownerAccount(Instance inst) {
|
||||
return inst.getOwnerId() != null ? inst.getOwnerId() : config.defaultAccountId();
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.services.ec2.model.Placement;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/** The {@code placement/} metadata an instance reads about its own zone. */
|
||||
class Ec2MetadataPlacementTest {
|
||||
|
||||
@Test
|
||||
void availabilityZoneIdIsTheIdOfTheInstancesZone() throws Exception {
|
||||
try (Ec2MetadataTestFixture imds = new Ec2MetadataTestFixture()) {
|
||||
imds.instance().setRegion("us-west-2");
|
||||
imds.instance().setPlacement(new Placement("us-west-2b"));
|
||||
imds.start();
|
||||
String token = imds.putToken("60").body();
|
||||
|
||||
assertEquals("us-west-2b", imds.get("/latest/meta-data/placement/availability-zone", token).body());
|
||||
assertEquals("usw2-az2", imds.get("/latest/meta-data/placement/availability-zone-id", token).body());
|
||||
assertEquals("usw2-az2", imds.get("/latest/meta-data/placement/availability-zone-id/", token).body());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user