Add support to discover public v4 and v6 addresses on AWS (#3471)

Update github.com/hashicorp/go-discover/provider/aws to support the
'addr_type' option which allows detection of private_v4, public_v4 and
public_v6 addresses.

Fixes #3471
This commit is contained in:
Frank Schroeder 2017-09-25 03:12:30 +02:00
parent 511dc3e95a
commit 9362cbcbc2
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
2 changed files with 53 additions and 9 deletions

View File

@ -23,6 +23,7 @@ func (p *Provider) Help() string {
region: The AWS region. Default to region of instance.
tag_key: The tag key to filter on
tag_value: The tag value to filter on
addr_type: "private_v4", "public_v4" or "public_v6". Defaults to "private_v4".
access_key_id: The AWS access key to use
secret_access_key: The AWS secret access key to use
@ -43,10 +44,21 @@ func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error
region := args["region"]
tagKey := args["tag_key"]
tagValue := args["tag_value"]
addrType := args["addr_type"]
accessKey := args["access_key_id"]
secretKey := args["secret_access_key"]
log.Printf("[DEBUG] discover-aws: Using region=%s tag_key=%s tag_value=%s", region, tagKey, tagValue)
if addrType != "private_v4" && addrType != "public_v4" && addrType != "public_v6" {
l.Printf("[INFO] discover-aws: Address type %s is not supported. Valid values are {private_v4,public_v4,public_v6}. Falling back to 'private_v4'", addrType)
addrType = "private_v4"
}
if addrType == "" {
l.Printf("[DEBUG] discover-aws: Address type not provided. Using 'private_v4'")
addrType = "private_v4"
}
log.Printf("[DEBUG] discover-aws: Using region=%s tag_key=%s tag_value=%s addr_type=%s", region, tagKey, tagValue, addrType)
if accessKey == "" && secretKey == "" {
log.Printf("[DEBUG] discover-aws: No static credentials")
log.Printf("[DEBUG] discover-aws: Using environment variables, shared credentials or instance role")
@ -89,6 +101,10 @@ func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error
Name: aws.String("tag:" + tagKey),
Values: []*string{aws.String(tagValue)},
},
&ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String("running")},
},
},
})
if err != nil {
@ -103,14 +119,42 @@ func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error
id := *inst.InstanceId
l.Printf("[DEBUG] discover-aws: Found instance %s", id)
// Terminated instances don't have the PrivateIpAddress field
if inst.PrivateIpAddress == nil {
l.Printf("[DEBUG] discover-aws: Instance %s has no private ip", id)
continue
}
switch addrType {
case "public_v6":
l.Printf("[DEBUG] discover-aws: Instance %s has %d network interfaces", id, len(inst.NetworkInterfaces))
l.Printf("[INFO] discover-aws: Instance %s has private ip %s", id, *inst.PrivateIpAddress)
addrs = append(addrs, *inst.PrivateIpAddress)
for _, networkinterface := range inst.NetworkInterfaces {
l.Printf("[DEBUG] discover-aws: Checking NetworInterfaceId %s on Instance %s", *networkinterface.NetworkInterfaceId, id)
// Check if instance got any ipv6
if networkinterface.Ipv6Addresses == nil {
l.Printf("[DEBUG] discover-aws: Instance %s has no IPv6 on NetworkInterfaceId %s", id, *networkinterface.NetworkInterfaceId)
continue
}
for _, ipv6address := range networkinterface.Ipv6Addresses {
l.Printf("[INFO] discover-aws: Instance %s has IPv6 %s on NetworkInterfaceId %s", id, *ipv6address.Ipv6Address, *networkinterface.NetworkInterfaceId)
addrs = append(addrs, *ipv6address.Ipv6Address)
}
}
case "public_v4":
if inst.PublicIpAddress == nil {
l.Printf("[DEBUG] discover-aws: Instance %s has no public IPv4", id)
continue
}
l.Printf("[INFO] discover-aws: Instance %s has public ip %s", id, *inst.PublicIpAddress)
addrs = append(addrs, *inst.PublicIpAddress)
default:
// EC2-Classic don't have the PrivateIpAddress field
if inst.PrivateIpAddress == nil {
l.Printf("[DEBUG] discover-aws: Instance %s has no private ip", id)
continue
}
l.Printf("[INFO] discover-aws: Instance %s has private ip %s", id, *inst.PrivateIpAddress)
addrs = append(addrs, *inst.PrivateIpAddress)
}
}
}

2
vendor/vendor.json vendored
View File

@ -26,7 +26,7 @@
{"path":"github.com/hashicorp/go-checkpoint","checksumSHA1":"nd3S1qkFv7zZxA9be0bw4nT0pe0=","revision":"e4b2dc34c0f698ee04750bf2035d8b9384233e1b","revisionTime":"2015-10-22T18:15:14Z"},
{"path":"github.com/hashicorp/go-cleanhttp","checksumSHA1":"b8F628srIitj5p7Y130xc9k0QWs=","revision":"3573b8b52aa7b37b9358d966a898feb387f62437","revisionTime":"2017-02-11T01:34:15Z"},
{"path":"github.com/hashicorp/go-discover","checksumSHA1":"uNoQWG5h2hzWHjaLi376ZXVaCr4=","revision":"21b26b722865b64ae5809a532d2c18f3a3800129","revisionTime":"2017-08-16T16:53:52Z"},
{"path":"github.com/hashicorp/go-discover/provider/aws","checksumSHA1":"6wdwVsYyTqW4ReHsWs4mEwoz1FI=","revision":"21b26b722865b64ae5809a532d2c18f3a3800129","revisionTime":"2017-08-16T16:53:52Z","tree":true},
{"path":"github.com/hashicorp/go-discover/provider/aws","checksumSHA1":"lyPRg8aZKgGiNkMILk/VKwOqMy4=","revision":"25e4565347de14cea0a0e0730374c9fcffa7bab0","revisionTime":"2017-09-25T01:06:15Z","tree":true},
{"path":"github.com/hashicorp/go-discover/provider/azure","checksumSHA1":"r97P32e+VmNMh2vwLkZa1zPEDQU=","revision":"b518491d039b6782035b8881502b4f5e9fcc887b","revisionTime":"2017-08-01T15:32:04Z","tree":true},
{"path":"github.com/hashicorp/go-discover/provider/gce","checksumSHA1":"KC/MepQsQF17904UShiM61jmaEs=","revision":"b518491d039b6782035b8881502b4f5e9fcc887b","revisionTime":"2017-08-01T15:32:04Z","tree":true},
{"path":"github.com/hashicorp/go-discover/provider/softlayer","checksumSHA1":"SIyZ44AHIUTBfI336ACpCeybsLg=","revision":"b518491d039b6782035b8881502b4f5e9fcc887b","revisionTime":"2017-08-01T15:32:04Z","tree":true},