Skip to content

Commit b568361

Browse files
authored
Merge pull request #4274 from wyike/disable-imdsv2
Set IMDSv2 as optional
2 parents 11b3269 + 2bca14c commit b568361

11 files changed

+36
-25
lines changed

api/v1beta2/awsmachinetemplate_webhook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ func TestAWSMachineTemplateValidateUpdate(t *testing.T) {
127127
InstanceType: "test",
128128
InstanceMetadataOptions: &InstanceMetadataOptions{
129129
HTTPEndpoint: InstanceMetadataEndpointStateEnabled,
130-
HTTPPutResponseHopLimit: 2,
131-
HTTPTokens: HTTPTokensStateRequired,
130+
HTTPPutResponseHopLimit: 1,
131+
HTTPTokens: HTTPTokensStateOptional,
132132
InstanceMetadataTags: InstanceMetadataEndpointStateDisabled,
133133
},
134134
},

api/v1beta2/types.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,10 @@ type InstanceMetadataOptions struct {
264264
// always returns the version 2.0 credentials; the version 1.0 credentials are
265265
// not available.
266266
//
267-
// Default: required
267+
// Default: optional
268+
//
268269
// +kubebuilder:validation:Enum:=optional;required
269-
// +kubebuilder:default=required
270+
// +kubebuilder:default=optional
270271
HTTPTokens HTTPTokensState `json:"httpTokens,omitempty"`
271272

272273
// Set to enabled to allow access to instance tags from the instance metadata.
@@ -275,6 +276,7 @@ type InstanceMetadataOptions struct {
275276
// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#work-with-tags-in-IMDS).
276277
//
277278
// Default: disabled
279+
//
278280
// +kubebuilder:validation:Enum:=enabled;disabled
279281
// +kubebuilder:default=disabled
280282
InstanceMetadataTags InstanceMetadataState `json:"instanceMetadataTags,omitempty"`
@@ -285,10 +287,10 @@ func (obj *InstanceMetadataOptions) SetDefaults() {
285287
obj.HTTPEndpoint = InstanceMetadataEndpointStateEnabled
286288
}
287289
if obj.HTTPPutResponseHopLimit == 0 {
288-
obj.HTTPPutResponseHopLimit = 2 // Defaults to 2 in container environment
290+
obj.HTTPPutResponseHopLimit = 1
289291
}
290292
if obj.HTTPTokens == "" {
291-
obj.HTTPTokens = HTTPTokensStateRequired // Defaults to IMDSv2
293+
obj.HTTPTokens = HTTPTokensStateOptional // Defaults to IMDSv1
292294
}
293295
if obj.InstanceMetadataTags == "" {
294296
obj.InstanceMetadataTags = InstanceMetadataEndpointStateDisabled

config/crd/bases/controlplane.cluster.x-k8s.io_awsmanagedcontrolplanes.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ spec:
866866
minimum: 1
867867
type: integer
868868
httpTokens:
869-
default: required
869+
default: optional
870870
description: "The state of token usage for your instance metadata
871871
requests. \n If the state is optional, you can choose to
872872
retrieve instance metadata with or without a session token
@@ -878,7 +878,7 @@ spec:
878878
with any instance metadata retrieval requests. In this state,
879879
retrieving the IAM role credentials always returns the version
880880
2.0 credentials; the version 1.0 credentials are not available.
881-
\n Default: required"
881+
\n Default: optional"
882882
enum:
883883
- optional
884884
- required
@@ -2288,7 +2288,7 @@ spec:
22882288
minimum: 1
22892289
type: integer
22902290
httpTokens:
2291-
default: required
2291+
default: optional
22922292
description: "The state of token usage for your instance metadata
22932293
requests. \n If the state is optional, you can choose to
22942294
retrieve instance metadata with or without a session token
@@ -2300,7 +2300,7 @@ spec:
23002300
with any instance metadata retrieval requests. In this state,
23012301
retrieving the IAM role credentials always returns the version
23022302
2.0 credentials; the version 1.0 credentials are not available.
2303-
\n Default: required"
2303+
\n Default: optional"
23042304
enum:
23052305
- optional
23062306
- required

config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusters.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ spec:
13581358
minimum: 1
13591359
type: integer
13601360
httpTokens:
1361-
default: required
1361+
default: optional
13621362
description: "The state of token usage for your instance metadata
13631363
requests. \n If the state is optional, you can choose to
13641364
retrieve instance metadata with or without a session token
@@ -1370,7 +1370,7 @@ spec:
13701370
with any instance metadata retrieval requests. In this state,
13711371
retrieving the IAM role credentials always returns the version
13721372
2.0 credentials; the version 1.0 credentials are not available.
1373-
\n Default: required"
1373+
\n Default: optional"
13741374
enum:
13751375
- optional
13761376
- required

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachines.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ spec:
688688
minimum: 1
689689
type: integer
690690
httpTokens:
691-
default: required
691+
default: optional
692692
description: "The state of token usage for your instance metadata
693693
requests. \n If the state is optional, you can choose to retrieve
694694
instance metadata with or without a session token on your request.
@@ -699,7 +699,7 @@ spec:
699699
you must send a session token with any instance metadata retrieval
700700
requests. In this state, retrieving the IAM role credentials
701701
always returns the version 2.0 credentials; the version 1.0
702-
credentials are not available. \n Default: required"
702+
credentials are not available. \n Default: optional"
703703
enum:
704704
- optional
705705
- required

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachinetemplates.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ spec:
637637
minimum: 1
638638
type: integer
639639
httpTokens:
640-
default: required
640+
default: optional
641641
description: "The state of token usage for your instance
642642
metadata requests. \n If the state is optional, you
643643
can choose to retrieve instance metadata with or without
@@ -650,7 +650,7 @@ spec:
650650
metadata retrieval requests. In this state, retrieving
651651
the IAM role credentials always returns the version
652652
2.0 credentials; the version 1.0 credentials are not
653-
available. \n Default: required"
653+
available. \n Default: optional"
654654
enum:
655655
- optional
656656
- required

controllers/awsmachine_controller_unit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,8 +2551,8 @@ func TestAWSMachineReconcilerReconcileDefaultsToLoadBalancerTypeClassic(t *testi
25512551
},
25522552
MetadataOptions: &ec2.InstanceMetadataOptionsResponse{
25532553
HttpEndpoint: aws.String(string(infrav1.InstanceMetadataEndpointStateEnabled)),
2554-
HttpPutResponseHopLimit: aws.Int64(2),
2555-
HttpTokens: aws.String(string(infrav1.HTTPTokensStateRequired)),
2554+
HttpPutResponseHopLimit: aws.Int64(1),
2555+
HttpTokens: aws.String(string(infrav1.HTTPTokensStateOptional)),
25562556
InstanceMetadataTags: aws.String(string(infrav1.InstanceMetadataEndpointStateDisabled)),
25572557
},
25582558
},

docs/book/src/topics/instance-metadata.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ Instance metadata is data about your instance that you can use to configure or m
55
* Instance Metadata Service Version 1 (IMDSv1) – a request/response method
66
* Instance Metadata Service Version 2 (IMDSv2) – a session-oriented method
77

8-
CAPA defaults to IMDSv2 when creating instances, as it provides a [better level of security](https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service/).
9-
CAPA defaults to 2 hot limit when creating instances with IMDSv2, as it is recommended in container environment according to [AWS document](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html#imds-considerations).
8+
CAPA defaults to use IMDSv2 as optional property when creating instances.
9+
10+
CAPA expose options to configure IMDSv2 as required when creating instances, as it provides a [better level of security](https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service/).
1011

1112
It is possible to configure the instance metadata options using the field called `instanceMetadataOptions` in the `AWSMachineTemplate`.
1213

@@ -22,11 +23,16 @@ spec:
2223
spec:
2324
instanceMetadataOptions:
2425
httpEndpoint: enabled
25-
httpPutResponseHopLimit: 2
26-
httpTokens: required
26+
httpPutResponseHopLimit: 1
27+
httpTokens: optional
2728
instanceMetadataTags: disabled
2829
```
2930
30-
To use IMDSv1, simply set `httpTokens` value to `optional` (in other words, set the use of IMDSv2 to optional).
31+
To use IMDSv2, simply set `httpTokens` value to `required` (in other words, set the use of IMDSv2 to required).
32+
To use IMDSv2, please also set `httpPutResponseHopLimit` value to `2`, as it is recommended in container environment according to [AWS document](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html#imds-considerations).
3133

3234
See [the CLI command reference](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/modify-instance-metadata-options.html) for more information.
35+
36+
Before you decide to use IMDSv2 for the cluster instances, please make sure all your applications are compatible to IMDSv2.
37+
38+
See the [transition guide](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-metadata-transition-to-version-2.html#recommended-path-for-requiring-imdsv2) for more information.

pkg/cloud/services/ec2/instances.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, use
236236
input.Tenancy = scope.AWSMachine.Spec.Tenancy
237237

238238
s.scope.Debug("Running instance", "machine-role", scope.Role())
239+
s.scope.Debug("Running instance with instance metadata options", "metadata options", input.InstanceMetadataOptions)
239240
out, err := s.runInstance(scope.Role(), input)
240241
if err != nil {
241242
// Only record the failure event if the error is not related to failed dependencies.
@@ -981,6 +982,7 @@ func (s *Service) ModifyInstanceMetadataOptions(instanceID string, options *infr
981982
InstanceId: aws.String(instanceID),
982983
}
983984

985+
s.scope.Info("Updating instance metadata options", "instance id", instanceID, "options", input)
984986
if _, err := s.EC2Client.ModifyInstanceMetadataOptions(input); err != nil {
985987
return err
986988
}

test/e2e/suites/unmanaged/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ func assertInstanceMetadataOptions(instanceID string, expected infrav1.InstanceM
594594
metadataOptions := result.Reservations[0].Instances[0].MetadataOptions
595595
Expect(metadataOptions).ToNot(BeNil())
596596

597-
Expect(metadataOptions.HttpTokens).To(HaveValue(Equal(string(expected.HTTPTokens)))) // IMDSv2 enabled
597+
Expect(metadataOptions.HttpTokens).To(HaveValue(Equal(string(expected.HTTPTokens))))
598598
Expect(metadataOptions.HttpEndpoint).To(HaveValue(Equal(string(expected.HTTPEndpoint))))
599599
Expect(metadataOptions.InstanceMetadataTags).To(HaveValue(Equal(string(expected.InstanceMetadataTags))))
600600
Expect(metadataOptions.HttpPutResponseHopLimit).To(HaveValue(Equal(expected.HTTPPutResponseHopLimit)))

0 commit comments

Comments
 (0)