-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instance lookup by IP and hostname via private IP addresses on attach…
…ed NIC (#54) * Merges Attribute and Tag filters for defining matchers for non-tag values * Adds an --attribute flag to session calls
- Loading branch information
Nate Catelli
authored
Sep 24, 2021
1 parent
1e6dda1
commit d7241cb
Showing
15 changed files
with
274 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package resolver | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface" | ||
) | ||
|
||
// Provides a interface for resolving an instance to a single ec2 instance. | ||
type InstanceResolver interface { | ||
ResolveToInstanceId(client ec2iface.EC2API) ([]string, error) | ||
} | ||
|
||
/// HostnameResolver attempts to resolve a fqdn or IP to a corresponding instance ID. | ||
type HostnameResolver struct { | ||
addrs []string | ||
} | ||
|
||
func NewHostnameResolver(addrs []string) *HostnameResolver { | ||
return &HostnameResolver{ | ||
addrs: addrs, | ||
} | ||
} | ||
|
||
func (hr *HostnameResolver) ResolveToInstanceId(client ec2iface.EC2API) (output []string, err error) { | ||
ips := make([]*string, 1) | ||
for _, addr := range hr.addrs { | ||
ip, err := resolveToFirst(addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to resolve hostname to %v to ip", hr.addrs) | ||
} | ||
|
||
ipString := ip.String() | ||
ips = append(ips, &ipString) | ||
|
||
} | ||
|
||
ipFilter := &ec2.Filter{} | ||
ipFilter.SetName("addresses.private-ip-address").SetValues(ips) | ||
|
||
dniInput := &ec2.DescribeNetworkInterfacesInput{} | ||
dniInput.SetFilters([]*ec2.Filter{ipFilter}) | ||
|
||
describeNetworkInterfacesPager := func(page *ec2.DescribeNetworkInterfacesOutput, lastPage bool) bool { | ||
for _, nic := range page.NetworkInterfaces { | ||
output = append(output, *nic.Attachment.InstanceId) | ||
} | ||
|
||
// If it's not the last page, continue | ||
return !lastPage | ||
} | ||
|
||
// Fetch all the instances described | ||
if err = client.DescribeNetworkInterfacesPages(dniInput, describeNetworkInterfacesPager); err != nil { | ||
return nil, fmt.Errorf("could not describe network interfaces\n%v", err) | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
func resolveToFirst(addr string) (net.IP, error) { | ||
if ip := net.ParseIP(addr); ip != nil { | ||
return ip, nil | ||
} else if ips, err := net.LookupIP(addr); err == nil { | ||
if len(ips[0]) > 0 { | ||
return ips[0], nil | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("no IP address found for %s", addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package resolver | ||
|
||
import ( | ||
"io/ioutil" | ||
"net" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockedEC2 struct { | ||
ec2iface.EC2API | ||
DescribeNetworkInterfacesOutput []*ec2.DescribeNetworkInterfacesOutput | ||
} | ||
|
||
func (c *mockedEC2) DescribeNetworkInterfacesPages(input *ec2.DescribeNetworkInterfacesInput, fn func(*ec2.DescribeNetworkInterfacesOutput, bool) bool) error { | ||
totalPages := len(c.DescribeNetworkInterfacesOutput) | ||
for i, output := range c.DescribeNetworkInterfacesOutput { | ||
isLastPage := (i == (totalPages - 1)) | ||
if breakLoop := fn(output, isLastPage); breakLoop { | ||
break | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
var ( | ||
exampleInstanceId = "i-1234567890abcdef0" | ||
examplePrivateIpAddress = "127.0.0.1" | ||
singleResponseDescribeNetworkInterfacesOutput = ec2.DescribeNetworkInterfacesOutput{ | ||
NetworkInterfaces: []*ec2.NetworkInterface{ | ||
{ | ||
Attachment: &ec2.NetworkInterfaceAttachment{ | ||
InstanceId: &exampleInstanceId, | ||
}, | ||
PrivateIpAddress: &examplePrivateIpAddress, | ||
}, | ||
}, | ||
NextToken: nil, | ||
} | ||
) | ||
|
||
func TestHostnameResolver(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
logger := logrus.New() | ||
logger.SetOutput(ioutil.Discard) | ||
|
||
t.Run("test passed ip causes a short circuit", func(t *testing.T) { | ||
validIp, err := resolveToFirst(examplePrivateIpAddress) | ||
assert.Nil(err) | ||
assert.NotNil(validIp) | ||
assert.EqualValues(validIp, net.ParseIP(examplePrivateIpAddress)) | ||
}) | ||
|
||
t.Run("test passed passed hostname resolves to an IP", func(t *testing.T) { | ||
validIp, err := resolveToFirst("example.com") | ||
assert.Nil(err) | ||
assert.NotNil(validIp) | ||
}) | ||
|
||
t.Run("test mock resolver returns a valid instance id", func(t *testing.T) { | ||
mockClient := &mockedEC2{DescribeNetworkInterfacesOutput: []*ec2.DescribeNetworkInterfacesOutput{&singleResponseDescribeNetworkInterfacesOutput}} | ||
testResolver := NewHostnameResolver([]string{examplePrivateIpAddress}) | ||
|
||
resp, err := testResolver.ResolveToInstanceId(mockClient) | ||
assert.Nil(err) | ||
assert.EqualValues(resp, []string{exampleInstanceId}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.