21552: Require IMDSv2 on ec2 compute instances. 21552-force-imdsv2
authorTom Clegg <tom@curii.com>
Wed, 28 Feb 2024 17:26:03 +0000 (12:26 -0500)
committerTom Clegg <tom@curii.com>
Wed, 28 Feb 2024 17:26:03 +0000 (12:26 -0500)
As described at
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-IMDS-new-instances.html

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

lib/cloud/ec2/ec2.go
lib/cloud/ec2/ec2_test.go

index 07a146d99f080ab8a5294626062d47fc22a803f4..9a3f784b51e77d9d90d214183fb071b1190a39c6 100644 (file)
@@ -251,6 +251,12 @@ func (instanceSet *ec2InstanceSet) Create(
                                ResourceType: aws.String("instance"),
                                Tags:         ec2tags,
                        }},
+               MetadataOptions: &ec2.InstanceMetadataOptionsRequest{
+                       // Require IMDSv2, as described at
+                       // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-IMDS-new-instances.html
+                       HttpEndpoint: aws.String(ec2.InstanceMetadataEndpointStateEnabled),
+                       HttpTokens:   aws.String(ec2.HttpTokensStateRequired),
+               },
                UserData: aws.String(base64.StdEncoding.EncodeToString([]byte("#!/bin/sh\n" + initCommand + "\n"))),
        }
 
index 4b830058963b93cfc508ee1795e65d22d3d70af9..d342f0fb3010ee59e0a12363f8339f76d22b2b91 100644 (file)
@@ -277,6 +277,12 @@ func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
        if *live == "" {
                c.Check(ap.client.(*ec2stub).describeKeyPairsCalls, check.HasLen, 1)
                c.Check(ap.client.(*ec2stub).importKeyPairCalls, check.HasLen, 1)
+
+               runcalls := ap.client.(*ec2stub).runInstancesCalls
+               if c.Check(runcalls, check.HasLen, 1) {
+                       c.Check(runcalls[0].MetadataOptions.HttpEndpoint, check.DeepEquals, aws.String("enabled"))
+                       c.Check(runcalls[0].MetadataOptions.HttpTokens, check.DeepEquals, aws.String("required"))
+               }
        }
 }