Skip to content

Commit c1507f5

Browse files
authored
aws/ec2metadata: Add support for AWS_EC2_METADATA_DISABLED env var (#128)
Adds support for the new cross SDK, AWS_EC2_METADATA_DISABLED environment variable. When this environment variable is set. The SDK's EC2 Metadata Client will not attempt to make requests. All requests made with the EC2 Metadata Client will fail.
1 parent 5e8e36f commit c1507f5

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

aws/ec2metadata/service.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
// Package ec2metadata provides the client for making API calls to the
22
// EC2 Metadata service.
3+
//
4+
// This package's client can be disabled completely by setting the environment
5+
// variable "AWS_EC2_METADATA_DISABLED=true". This environment variable set to
6+
// true instructs the SDK to disable the EC2 Metadata client. The client cannot
7+
// be used while the environemnt variable is set to true, (case insensitive).
38
package ec2metadata
49

510
import (
611
"bytes"
712
"errors"
813
"io"
914
"net/http"
15+
"os"
16+
"strings"
1017

1118
"github.com/aws/aws-sdk-go-v2/aws"
1219
"github.com/aws/aws-sdk-go-v2/aws/awserr"
20+
"github.com/aws/aws-sdk-go-v2/aws/defaults"
1321
)
1422

1523
// ServiceName is the name of the service.
1624
const ServiceName = "ec2metadata"
25+
const disableServiceEnvVar = "AWS_EC2_METADATA_DISABLED"
1726

1827
// A EC2Metadata is an EC2 Metadata service Client.
1928
type EC2Metadata struct {
@@ -42,6 +51,21 @@ func New(config aws.Config) *EC2Metadata {
4251
svc.Handlers.Validate.Clear()
4352
svc.Handlers.Validate.PushBack(validateEndpointHandler)
4453

54+
// Disable the EC2 Metadata service if the environment variable is set.
55+
// This shortcirctes the service's functionality to always fail to send
56+
// requests.
57+
if strings.ToLower(os.Getenv(disableServiceEnvVar)) == "true" {
58+
svc.Handlers.Send.SwapNamed(aws.NamedHandler{
59+
Name: defaults.SendHandler.Name,
60+
Fn: func(r *aws.Request) {
61+
r.Error = awserr.New(
62+
aws.CanceledErrorCode,
63+
"EC2 IMDS access disabled via "+disableServiceEnvVar+" env var",
64+
nil)
65+
},
66+
})
67+
}
68+
4569
return svc
4670
}
4771

aws/ec2metadata/service_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ec2metadata_test
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
8+
"github.com/aws/aws-sdk-go-v2/aws/awserr"
9+
"github.com/aws/aws-sdk-go-v2/aws/ec2metadata"
10+
"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"
11+
"github.com/aws/aws-sdk-go/aws/request"
12+
"github.com/aws/aws-sdk-go/awstesting"
13+
)
14+
15+
func TestClientDisableIMDS(t *testing.T) {
16+
env := awstesting.StashEnv()
17+
defer awstesting.PopEnv(env)
18+
19+
os.Setenv("AWS_EC2_METADATA_DISABLED", "true")
20+
21+
svc := ec2metadata.New(unit.Config())
22+
resp, err := svc.Region()
23+
if err == nil {
24+
t.Fatalf("expect error, got none")
25+
}
26+
if len(resp) != 0 {
27+
t.Errorf("expect no response, got %v", resp)
28+
}
29+
30+
aerr := err.(awserr.Error)
31+
if e, a := request.CanceledErrorCode, aerr.Code(); e != a {
32+
t.Errorf("expect %v error code, got %v", e, a)
33+
}
34+
if e, a := "AWS_EC2_METADATA_DISABLED", aerr.Message(); !strings.Contains(a, e) {
35+
t.Errorf("expect %v in error message, got %v", e, a)
36+
}
37+
}

0 commit comments

Comments
 (0)