plugin: add DNS names

This adds DNS names to the output for host queries.

Hosts are discovered in private, then public order, first at the
top-level, and then through attached network interfaces. This mirrors
the discovery order of IP addresses.
This commit is contained in:
Chris Marchesi committed 2021-12-03 13:24:48 -08:00
1 parent 88e5224f69
commit 13469e3594
2 files changed
+61 -15

No files matched your search

+26 -10
View File
@@ -543,16 +543,22 @@ func awsInstanceToHost(instance *ec2.Instance) (*pb.ListHostsResponseHost, error
// External ID is the instance ID.
result.ExternalId = aws.StringValue(instance.InstanceId)
// First IP address is always the private IP address field if it's
// populated
// First IP address/dns name are always the private fields if they
// are populated
if aws.StringValue(instance.PrivateIpAddress) != "" {
result.IpAddresses = append(result.IpAddresses, aws.StringValue(instance.PrivateIpAddress))
}
if aws.StringValue(instance.PrivateDnsName) != "" {
result.DnsNames = append(result.DnsNames, aws.StringValue(instance.PrivateDnsName))
}
// Public IP address is next.
// Public IP address/dns names are next
if aws.StringValue(instance.PublicIpAddress) != "" {
result.IpAddresses = append(result.IpAddresses, aws.StringValue(instance.PublicIpAddress))
}
if aws.StringValue(instance.PublicDnsName) != "" {
result.DnsNames = append(result.DnsNames, aws.StringValue(instance.PublicDnsName))
}
// Now go through all of the interfaces and log the IP address of
// every interface.
@@ -569,22 +575,32 @@ func awsInstanceToHost(instance *ec2.Instance) (*pb.ListHostsResponseHost, error
return nil, errors.New("response integrity error: interface address entry is nil")
}
// Check to see if the PrivateIpAddress matches the one
// reported at the top-level of the instance (let's call it the
// "default address"). If it doesn't, add it.
// Check to see if the PrivateIpAddress/PrivateDnsName fields
// match the ones reported at the top-level of the instance.
// If they don't, add them.
if aws.StringValue(addr.PrivateIpAddress) != "" {
if aws.StringValue(instance.PrivateIpAddress) != aws.StringValue(addr.PrivateIpAddress) {
result.IpAddresses = append(result.IpAddresses, aws.StringValue(addr.PrivateIpAddress))
}
}
if aws.StringValue(addr.PrivateDnsName) != "" {
if aws.StringValue(instance.PrivateDnsName) != aws.StringValue(addr.PrivateDnsName) {
result.DnsNames = append(result.DnsNames, aws.StringValue(addr.PrivateDnsName))
}
}
// Do the same for the default public IP address and the
// public association on the interface.
if addr.Association != nil && addr.Association.PublicIp != nil && aws.StringValue(addr.Association.PublicIp) != "" {
if instance.PublicIpAddress != nil && aws.StringValue(instance.PublicIpAddress) != aws.StringValue(addr.Association.PublicIp) {
// Do the same for the top-level public IP address/DNS name and
// the public association on the interface.
if addr.Association != nil && aws.StringValue(addr.Association.PublicIp) != "" {
if aws.StringValue(instance.PublicIpAddress) != aws.StringValue(addr.Association.PublicIp) {
result.IpAddresses = append(result.IpAddresses, aws.StringValue(addr.Association.PublicIp))
}
}
if addr.Association != nil && aws.StringValue(addr.Association.PublicDnsName) != "" {
if aws.StringValue(instance.PublicDnsName) != aws.StringValue(addr.Association.PublicDnsName) {
result.DnsNames = append(result.DnsNames, aws.StringValue(addr.Association.PublicDnsName))
}
}
}
// Add the IPv6 addresses.
+35 -5
View File
@@ -1380,15 +1380,19 @@ func TestAwsInstanceToHost(t *testing.T) {
instance: &ec2.Instance{
InstanceId: aws.String("foobar"),
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PublicIpAddress: aws.String("1.1.1.1"),
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
PublicIp: aws.String("1.1.1.1"),
PublicIp: aws.String("1.1.1.1"),
PublicDnsName: aws.String("test.example.com"),
},
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
},
},
},
@@ -1397,6 +1401,7 @@ func TestAwsInstanceToHost(t *testing.T) {
expected: &pb.ListHostsResponseHost{
ExternalId: "foobar",
IpAddresses: []string{"10.0.0.1", "1.1.1.1"},
DnsNames: []string{"test.example.internal", "test.example.com"},
},
},
{
@@ -1404,11 +1409,13 @@ func TestAwsInstanceToHost(t *testing.T) {
instance: &ec2.Instance{
InstanceId: aws.String("foobar"),
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
},
},
},
@@ -1417,6 +1424,7 @@ func TestAwsInstanceToHost(t *testing.T) {
expected: &pb.ListHostsResponseHost{
ExternalId: "foobar",
IpAddresses: []string{"10.0.0.1"},
DnsNames: []string{"test.example.internal"},
},
},
{
@@ -1424,12 +1432,15 @@ func TestAwsInstanceToHost(t *testing.T) {
instance: &ec2.Instance{
InstanceId: aws.String("foobar"),
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PublicIpAddress: aws.String("1.1.1.1"),
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
PrivateIpAddress: aws.String("10.0.0.2"),
PrivateDnsName: aws.String("test2.example.internal"),
},
},
},
@@ -1437,9 +1448,11 @@ func TestAwsInstanceToHost(t *testing.T) {
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
PublicIp: aws.String("1.1.1.1"),
PublicIp: aws.String("1.1.1.1"),
PublicDnsName: aws.String("test.example.com"),
},
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
},
},
},
@@ -1448,6 +1461,7 @@ func TestAwsInstanceToHost(t *testing.T) {
expected: &pb.ListHostsResponseHost{
ExternalId: "foobar",
IpAddresses: []string{"10.0.0.1", "1.1.1.1", "10.0.0.2"},
DnsNames: []string{"test.example.internal", "test.example.com", "test2.example.internal"},
},
},
{
@@ -1455,15 +1469,19 @@ func TestAwsInstanceToHost(t *testing.T) {
instance: &ec2.Instance{
InstanceId: aws.String("foobar"),
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PublicIpAddress: aws.String("1.1.1.1"),
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
PublicIp: aws.String("1.1.1.2"),
PublicIp: aws.String("1.1.1.2"),
PublicDnsName: aws.String("test2.example.com"),
},
PrivateIpAddress: aws.String("10.0.0.2"),
PrivateDnsName: aws.String("test2.example.internal"),
},
},
},
@@ -1471,9 +1489,11 @@ func TestAwsInstanceToHost(t *testing.T) {
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
PublicIp: aws.String("1.1.1.1"),
PublicIp: aws.String("1.1.1.1"),
PublicDnsName: aws.String("test.example.com"),
},
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
},
},
},
@@ -1482,6 +1502,7 @@ func TestAwsInstanceToHost(t *testing.T) {
expected: &pb.ListHostsResponseHost{
ExternalId: "foobar",
IpAddresses: []string{"10.0.0.1", "1.1.1.1", "10.0.0.2", "1.1.1.2"},
DnsNames: []string{"test.example.internal", "test.example.com", "test2.example.internal", "test2.example.com"},
},
},
{
@@ -1489,18 +1510,23 @@ func TestAwsInstanceToHost(t *testing.T) {
instance: &ec2.Instance{
InstanceId: aws.String("foobar"),
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PublicIpAddress: aws.String("1.1.1.1"),
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
PublicIp: aws.String("1.1.1.1"),
PublicIp: aws.String("1.1.1.1"),
PublicDnsName: aws.String("test.example.com"),
},
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
},
&ec2.InstancePrivateIpAddress{
PrivateIpAddress: aws.String("10.0.0.2"),
PrivateDnsName: aws.String("test2.example.internal"),
},
},
},
@@ -1509,6 +1535,7 @@ func TestAwsInstanceToHost(t *testing.T) {
expected: &pb.ListHostsResponseHost{
ExternalId: "foobar",
IpAddresses: []string{"10.0.0.1", "1.1.1.1", "10.0.0.2"},
DnsNames: []string{"test.example.internal", "test.example.com", "test2.example.internal"},
},
},
{
@@ -1516,7 +1543,9 @@ func TestAwsInstanceToHost(t *testing.T) {
instance: &ec2.Instance{
InstanceId: aws.String("foobar"),
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PublicIpAddress: aws.String("1.1.1.1"),
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
@@ -1537,6 +1566,7 @@ func TestAwsInstanceToHost(t *testing.T) {
expected: &pb.ListHostsResponseHost{
ExternalId: "foobar",
IpAddresses: []string{"10.0.0.1", "1.1.1.1", "some::fake::address"},
DnsNames: []string{"test.example.internal", "test.example.com"},
},
},
}