From ccc2ddb5fc0c9a35a36f834033430501aa0476f3 Mon Sep 17 00:00:00 2001 From: Chris Doherty Date: Thu, 18 Jan 2024 16:00:32 -0600 Subject: [PATCH] Patch incorrect local IPv4 address When requesting the EC2 /local-ipv4 endpoint the first address was always being returned irrespective of whether a private address existed on the Hardware resource. Signed-off-by: Chris Doherty --- internal/backend/kubernetes/backend.go | 6 +-- internal/backend/kubernetes/backend_test.go | 60 +++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/internal/backend/kubernetes/backend.go b/internal/backend/kubernetes/backend.go index a08bcb0b..dca33eb1 100644 --- a/internal/backend/kubernetes/backend.go +++ b/internal/backend/kubernetes/backend.go @@ -186,17 +186,17 @@ func toEC2Instance(hw tinkv1.Hardware) ec2.Instance { for _, ip := range hw.Spec.Metadata.Instance.Ips { // Public IPv4 if ip.Family == 4 && ip.Public && i.Metadata.PublicIPv4 == "" { - i.Metadata.PublicIPv4 = hw.Spec.Metadata.Instance.Ips[0].Address + i.Metadata.PublicIPv4 = ip.Address } // Private IPv4 if ip.Family == 4 && !ip.Public && i.Metadata.LocalIPv4 == "" { - i.Metadata.LocalIPv4 = hw.Spec.Metadata.Instance.Ips[0].Address + i.Metadata.LocalIPv4 = ip.Address } // Public IPv6 if ip.Family == 6 && i.Metadata.PublicIPv6 == "" { - i.Metadata.PublicIPv6 = hw.Spec.Metadata.Instance.Ips[0].Address + i.Metadata.PublicIPv6 = ip.Address } } } diff --git a/internal/backend/kubernetes/backend_test.go b/internal/backend/kubernetes/backend_test.go index 38d8e438..0aaa3ad1 100644 --- a/internal/backend/kubernetes/backend_test.go +++ b/internal/backend/kubernetes/backend_test.go @@ -251,6 +251,66 @@ func TestGetEC2Instance(t *testing.T) { }, }, }, + { + Name: "PublicThenPrivateIPv4s", + Hardware: tinkv1.Hardware{ + Spec: tinkv1.HardwareSpec{ + Metadata: &tinkv1.HardwareMetadata{ + Instance: &tinkv1.MetadataInstance{ + Ips: []*tinkv1.MetadataInstanceIP{ + { + Address: "10.10.10.10", + Family: 4, + Public: true, + }, + { + Address: "172.15.0.1", + Family: 4, + // Zero value is false but we want to be explicit. + Public: false, + }, + }, + }, + }, + }, + }, + ExpectedInstance: ec2.Instance{ + Metadata: ec2.Metadata{ + PublicIPv4: "10.10.10.10", + LocalIPv4: "172.15.0.1", + }, + }, + }, + { + Name: "PrivateThenPublicIPv4s", + Hardware: tinkv1.Hardware{ + Spec: tinkv1.HardwareSpec{ + Metadata: &tinkv1.HardwareMetadata{ + Instance: &tinkv1.MetadataInstance{ + Ips: []*tinkv1.MetadataInstanceIP{ + { + Address: "10.10.10.10", + Family: 4, + // Zero value is false but we want to be explicit. + Public: false, + }, + { + Address: "172.15.0.1", + Family: 4, + Public: true, + }, + }, + }, + }, + }, + }, + ExpectedInstance: ec2.Instance{ + Metadata: ec2.Metadata{ + PublicIPv4: "172.15.0.1", + LocalIPv4: "10.10.10.10", + }, + }, + }, { Name: "PublicIPv6", Hardware: tinkv1.Hardware{