feat(plugin): Support extracting AWS instance name (#21)

This commit is contained in:
Hugo authored and GitHub committed 2023-04-17 11:00:12 +01:00
1 parent adf0c99659
commit c235f0a72f
4 files changed
+137 -165

No files matched your search

+18 -4
View File
@@ -21,10 +21,16 @@ import (
"google.golang.org/protobuf/types/known/structpb"
)
// rotationWaitTimeout controls the time we wait for credential rotation to
// succeed. This is important to ensure that rotated credentials can be used
// right away.
const rotationWaitTimeout = time.Second * 30
const (
// rotationWaitTimeout controls the time we wait for credential rotation to
// succeed. This is important to ensure that rotated credentials can be used
// right away.
rotationWaitTimeout = time.Second * 30
// instanceNameTagKey denotes the special AWS tag key that contains an EC2
// instance's name. This field is to be matched literally.
instanceNameTagKey = "Name"
)
// AwsPlugin implements the HostPluginServiceServer interface for the
// AWS plugin.
@@ -596,6 +602,14 @@ func awsInstanceToHost(instance *ec2.Instance) (*pb.ListHostsResponseHost, error
// External ID is the instance ID.
result.ExternalId = aws.StringValue(instance.InstanceId)
// External Name is the AWS "Name" tag, if it exists.
for _, t := range instance.Tags {
if aws.StringValue(t.Key) == instanceNameTagKey {
result.ExternalName = aws.StringValue(t.Value)
break
}
}
// First IP address/dns name are always the private fields if they
// are populated
if aws.StringValue(instance.PrivateIpAddress) != "" {
+37
View File
@@ -1769,6 +1769,43 @@ func TestAwsInstanceToHost(t *testing.T) {
DnsNames: []string{"test.example.internal", "test.example.com"},
},
},
{
name: "good, single IP w/public addr and external name",
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"),
Tags: []*ec2.Tag{
{Key: aws.String("Name"), Value: aws.String("test-instance-actual-name")}, // The tag name is "Name", not "name".
{Key: aws.String("name"), Value: aws.String("test-instance-fake-name")},
{Key: aws.String("contains-Name"), Value: aws.String("test-instance-contains-name")},
},
NetworkInterfaces: []*ec2.InstanceNetworkInterface{
{
PrivateIpAddress: aws.String("10.0.0.1"),
PrivateDnsName: aws.String("test.example.internal"),
PrivateIpAddresses: []*ec2.InstancePrivateIpAddress{
{
Association: &ec2.InstanceNetworkInterfaceAssociation{
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"),
},
},
},
},
},
expected: &pb.ListHostsResponseHost{
ExternalId: "foobar",
ExternalName: "test-instance-actual-name",
IpAddresses: []string{"10.0.0.1", "1.1.1.1"},
DnsNames: []string{"test.example.internal", "test.example.com"},
},
},
}
for _, tc := range cases {