add default address/DNS check/add for interface, not just whole instance

This commit is contained in:
Chris Marchesi committed 2021-12-06 11:40:44 -08:00
1 parent 13469e3594
commit 7a07ac42c0
2 files changed
+44 -21

No files matched your search

+29 -21
View File
@@ -568,6 +568,15 @@ func awsInstanceToHost(instance *ec2.Instance) (*pb.ListHostsResponseHost, error
return nil, errors.New("response integrity error: interface entry is nil")
}
// Populate default IP addresses/DNS name similar to how we do
// for the entire instance.
if aws.StringValue(iface.PrivateIpAddress) != "" && !stringInSlice(result.IpAddresses, aws.StringValue(iface.PrivateIpAddress)) {
result.IpAddresses = append(result.IpAddresses, aws.StringValue(iface.PrivateIpAddress))
}
if aws.StringValue(iface.PrivateDnsName) != "" && !stringInSlice(result.DnsNames, aws.StringValue(iface.PrivateDnsName)) {
result.DnsNames = append(result.DnsNames, aws.StringValue(iface.PrivateDnsName))
}
// Iterate through the private IP addresses and log the
// information.
for _, addr := range iface.PrivateIpAddresses {
@@ -575,31 +584,20 @@ 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/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))
}
// Add private address/dns name if they have not been added yet
if aws.StringValue(addr.PrivateIpAddress) != "" && !stringInSlice(result.IpAddresses, 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))
}
if aws.StringValue(addr.PrivateDnsName) != "" && !stringInSlice(result.DnsNames, aws.StringValue(addr.PrivateDnsName)) {
result.DnsNames = append(result.DnsNames, aws.StringValue(addr.PrivateDnsName))
}
// 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))
}
// Add public address/dns name if they have not been added yet
if addr.Association != nil && aws.StringValue(addr.Association.PublicIp) != "" && !stringInSlice(result.IpAddresses, 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))
}
if addr.Association != nil && aws.StringValue(addr.Association.PublicDnsName) != "" && !stringInSlice(result.DnsNames, aws.StringValue(addr.Association.PublicDnsName)) {
result.DnsNames = append(result.DnsNames, aws.StringValue(addr.Association.PublicDnsName))
}
}
@@ -618,3 +616,13 @@ func awsInstanceToHost(instance *ec2.Instance) (*pb.ListHostsResponseHost, error
// Done
return result, nil
}
func stringInSlice(s []string, x string) bool {
for _, y := range s {
if x == y {
return true
}
}
return false
}
+15
View File
@@ -1385,6 +1385,8 @@ func TestAwsInstanceToHost(t *testing.T) {
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
@@ -1412,6 +1414,8 @@ func TestAwsInstanceToHost(t *testing.T) {
PrivateDnsName: aws.String("test.example.internal"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
PrivateIpAddress: aws.String("10.0.0.1"),
@@ -1437,6 +1441,8 @@ func TestAwsInstanceToHost(t *testing.T) {
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddress: aws.String("10.0.0.2"),
PrivateDnsName: aws.String("test2.example.internal"),
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
PrivateIpAddress: aws.String("10.0.0.2"),
@@ -1445,6 +1451,8 @@ func TestAwsInstanceToHost(t *testing.T) {
},
},
&ec2.InstanceNetworkInterface{
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
@@ -1474,6 +1482,8 @@ func TestAwsInstanceToHost(t *testing.T) {
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddress: aws.String("10.0.0.2"),
PrivateDnsName: aws.String("test2.example.internal"),
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
@@ -1486,6 +1496,8 @@ func TestAwsInstanceToHost(t *testing.T) {
},
},
&ec2.InstanceNetworkInterface{
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
@@ -1515,6 +1527,8 @@ func TestAwsInstanceToHost(t *testing.T) {
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{
@@ -1548,6 +1562,7 @@ func TestAwsInstanceToHost(t *testing.T) {
PublicDnsName: aws.String("test.example.com"),
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
&ec2.InstanceNetworkInterface{
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
&ec2.InstancePrivateIpAddress{
Association: &ec2.InstanceNetworkInterfaceAssociation{