Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Add AWS EC2 metadata sysvars #315

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sysvars/sysvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func Init(ll *logger.Logger, userVars map[string]string) error {
if err := gceVars(sysVars); err != nil {
return err
}
} else {
if err := ec2Vars(sysVars); err != nil {
return fmt.Errorf("utils.SystemVars: error getting ec2 metadata: %v", err)
}
}

for k, v := range userVars {
Expand Down
52 changes: 52 additions & 0 deletions sysvars/sysvars_ec2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sysvars

import (
"fmt"

"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
)

func ec2Vars(sysVars map[string]string) error {
s, err := session.NewSession()
if err != nil {
return fmt.Errorf("ec2Vars: could not create session %v", err)
}
md := ec2metadata.New(s)
// Doing the availability check in module since we need a session
if md.Available() == false {
sysVars["EC2_METADATA_Availabile"] = "false"
return nil
}

id, err := md.GetInstanceIdentityDocument()
if err != nil {
sysVars["EC2_METADATA_Availabile"] = "false"
return fmt.Errorf("ec2Vars: could not get instance document %v", err)
}

sysVars["EC2_METADATA_Availabile"] = "true"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding all the variables by default, I'd start with a few that are useful for probing (also, some of this information may be security risk, e.g. DevpayProductCodes, AccountID).

Can you start with only: AvailabilityZone, Region, InstanceID, PrivateIP and may be ImageID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed sensitive info, added ramdisk and architecture to the list

sysVars["EC2_AvailabilityZone"] = id.AvailabilityZone
sysVars["EC2_PrivateIP"] = id.PrivateIP
sysVars["EC2_Region"] = id.Region
sysVars["EC2_InstanceID"] = id.InstanceID
sysVars["EC2_InstanceType"] = id.InstanceType
sysVars["EC2_ImageID"] = id.ImageID
sysVars["EC2_KernelID"] = id.KernelID
sysVars["EC2_RamdiskID"] = id.RamdiskID
sysVars["EC2_Architecture"] = id.Architecture
return nil
}