From 46e1c66f45e360f3ad1c20c227046e04c1a6e933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Bia=C5=82o=C5=84?= Date: Tue, 24 Oct 2023 16:24:55 +0200 Subject: [PATCH] Backend/S3: Add support for overriding EC2 Metadata Service endpoint (#767) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Białoń --- CHANGELOG.md | 1 + internal/backend/remote-state/s3/backend.go | 22 +++++++++++-------- .../docs/language/settings/backends/s3.mdx | 2 ++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65998d411c..ad2c0e1571 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ S3 BACKEND: * Adds support for account whitelisting using the `forbidden_account_ids` and `allowed_account_ids` arguments. ([#699](https://github.com/opentofu/opentofu/issues/699)) * Adds the `custom_ca_bundle` argument. ([#689](https://github.com/opentofu/opentofu/issues/689)) * Adds support for the `sts_region` argument. ([#695](https://github.com/opentofu/opentofu/issues/695)) +* Adds support for `ec2_metadata_service_endpoint` and `ec2_metadata_service_endpoint_mode` arguments to enable overriding the EC2 metadata service (IMDS) endpoint. ([#693](https://github.com/opentofu/opentofu/issues/693)) ## Previous Releases diff --git a/internal/backend/remote-state/s3/backend.go b/internal/backend/remote-state/s3/backend.go index 875063af57..9a5cce462a 100644 --- a/internal/backend/remote-state/s3/backend.go +++ b/internal/backend/remote-state/s3/backend.go @@ -240,6 +240,16 @@ func (b *Backend) ConfigSchema(context.Context) *configschema.Block { Optional: true, Description: "File containing custom root and intermediate certificates. Can also be configured using the `AWS_CA_BUNDLE` environment variable.", }, + "ec2_metadata_service_endpoint": { + Type: cty.String, + Optional: true, + Description: "The endpoint of IMDS.", + }, + "ec2_metadata_service_endpoint_mode": { + Type: cty.String, + Optional: true, + Description: "The endpoint mode of IMDS. Valid values: IPv4, IPv6.", + }, "assume_role": { NestedType: &configschema.Object{ Nesting: configschema.NestingSingle, @@ -593,7 +603,9 @@ func (b *Backend) Configure(ctx context.Context, obj cty.Value) tfdiags.Diagnost {Name: "APN", Version: "1.0"}, {Name: httpclient.DefaultApplicationName, Version: version.String()}, }, - CustomCABundle: stringAttrDefaultEnvVar(obj, "custom_ca_bundle", "AWS_CA_BUNDLE"), + CustomCABundle: stringAttrDefaultEnvVar(obj, "custom_ca_bundle", "AWS_CA_BUNDLE"), + EC2MetadataServiceEndpoint: stringAttrDefaultEnvVar(obj, "ec2_metadata_service_endpoint", "AWS_EC2_METADATA_SERVICE_ENDPOINT"), + EC2MetadataServiceEndpointMode: stringAttrDefaultEnvVar(obj, "ec2_metadata_service_endpoint_mode", "AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE"), } if val, ok := boolAttrOk(obj, "use_legacy_workflow"); ok { @@ -614,14 +626,6 @@ func (b *Backend) Configure(ctx context.Context, obj cty.Value) tfdiags.Diagnost cfg.SharedCredentialsFiles = []string{val} } - if val, ok := boolAttrOk(obj, "skip_metadata_api_check"); ok { - if val { - cfg.EC2MetadataServiceEnableState = imds.ClientDisabled - } else { - cfg.EC2MetadataServiceEnableState = imds.ClientEnabled - } - } - if value := obj.GetAttr("assume_role"); !value.IsNull() { cfg.AssumeRole = configureNestedAssumeRole(obj) } else if value := obj.GetAttr("role_arn"); !value.IsNull() { diff --git a/website/docs/language/settings/backends/s3.mdx b/website/docs/language/settings/backends/s3.mdx index c48e1163b3..3bc38fe100 100644 --- a/website/docs/language/settings/backends/s3.mdx +++ b/website/docs/language/settings/backends/s3.mdx @@ -175,6 +175,8 @@ The following configuration is optional: * `forbidden_account_ids` (Optional): A list of prohibited AWS account IDs to prevent unintentional disruption of a live environment. This option conflicts with `allowed_account_ids`. * `use_legacy_workflow` - (Optional) Prefer environment variables for legacy authentication; default is 'true.' This method doesn't match AWS CLI or SDK authentication and will be removed in the future. * `custom_ca_bundle` - File containing custom root and intermediate certificates. Can also be configured using the `AWS_CA_BUNDLE` environment variable. +* `ec2_metadata_service_endpoint` - Address of the EC2 metadata service (IMDS) endpoint to use. This can also be sourced from the `AWS_EC2_METADATA_SERVICE_ENDPOINT` environment variable. +* `ec2_metadata_service_endpoint_mode` - Mode to use in communicating with the metadata service. Valid values are `IPv4` and `IPv6`. This can also be sourced from the `AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE` environment variable. #### Assume Role Configuration