Skip to content

Commit bc7cfaa

Browse files
author
Arvind Iyengar
committed
Add flags for metadata options
1 parent 7c4e432 commit bc7cfaa

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

drivers/amazonec2/amazonec2.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ var (
7373
errorNoSubnetsFound = errors.New("The desired subnet could not be located in this region. Is '--amazonec2-subnet-id' or AWS_SUBNET_ID configured correctly?")
7474
errorDisableSSLWithoutCustomEndpoint = errors.New("using --amazonec2-insecure-transport also requires --amazonec2-endpoint")
7575
errorReadingUserData = errors.New("unable to read --amazonec2-userdata file")
76+
errorInvalidValueForHTTPToken = errors.New("httpToken must be either optional or required")
77+
errorInvalidValueForHTTPEndpoint = errors.New("httpEndpoint must be either enabled or disabled")
7678
)
7779

7880
type Driver struct {
@@ -128,6 +130,10 @@ type Driver struct {
128130
EncryptEbsVolume bool
129131
spotInstanceRequestId string
130132
kmsKeyId *string
133+
134+
// Metadata Options
135+
HttpEndpoint string
136+
HttpTokens string
131137
}
132138

133139
type clientFactory interface {
@@ -304,6 +310,16 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
304310
Usage: "Custom KMS key using the AWS Managed CMK",
305311
EnvVar: "AWS_KMS_KEY",
306312
},
313+
mcnflag.StringFlag{
314+
Name: "amazonec2-http-endpoint",
315+
Usage: "Enables or disables the HTTP metadata endpoint on your instances",
316+
EnvVar: "AWS_HTTP_ENDPOINT",
317+
},
318+
mcnflag.StringFlag{
319+
Name: "amazonec2-http-tokens",
320+
Usage: "The state of token usage for your instance metadata requests.",
321+
EnvVar: "AWS_HTTP_TOKENS",
322+
},
307323
}
308324
}
309325

@@ -403,6 +419,22 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
403419
d.UserDataFile = flags.String("amazonec2-userdata")
404420
d.EncryptEbsVolume = flags.Bool("amazonec2-encrypt-ebs-volume")
405421

422+
httpEndpoint := flags.String("amazonec2-http-endpoint")
423+
if httpEndpoint != "" {
424+
if httpEndpoint != "disabled" && httpEndpoint != "enabled" {
425+
return errorInvalidValueForHTTPEndpoint
426+
}
427+
d.HttpEndpoint = httpEndpoint
428+
}
429+
430+
httpTokens := flags.String("amazonec2-http-tokens")
431+
if httpTokens != "" {
432+
if httpTokens != "optional" && httpTokens != "required" {
433+
return errorInvalidValueForHTTPToken
434+
}
435+
d.HttpTokens = httpTokens
436+
}
437+
406438
kmskeyid := flags.String("amazonec2-kms-key")
407439
if kmskeyid != "" {
408440
d.kmsKeyId = aws.String(kmskeyid)
@@ -752,6 +784,17 @@ func (d *Driver) innerCreate() error {
752784

753785
d.waitForInstance()
754786

787+
if d.HttpEndpoint != "" || d.HttpTokens != "" {
788+
_, err := d.getClient().ModifyInstanceMetadataOptions(&ec2.ModifyInstanceMetadataOptionsInput{
789+
InstanceId: aws.String(d.InstanceId),
790+
HttpEndpoint: aws.String(d.HttpEndpoint),
791+
HttpTokens: aws.String(d.HttpTokens),
792+
})
793+
if err != nil {
794+
return fmt.Errorf("Error modifying instance metadata options for instance: %s", err)
795+
}
796+
}
797+
755798
log.Debugf("created instance ID %s, IP address %s, Private IP address %s",
756799
d.InstanceId,
757800
d.IPAddress,

drivers/amazonec2/ec2client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ type Ec2Client interface {
99

1010
CreateTags(input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error)
1111

12+
ModifyInstanceMetadataOptions(input *ec2.ModifyInstanceMetadataOptionsInput) (*ec2.ModifyInstanceMetadataOptionsOutput, error)
13+
1214
//SecurityGroup
1315

1416
CreateSecurityGroup(input *ec2.CreateSecurityGroupInput) (*ec2.CreateSecurityGroupOutput, error)

0 commit comments

Comments
 (0)