Skip to content

Commit

Permalink
Merge pull request #2065 from AndrewSirenko/eni-fix
Browse files Browse the repository at this point in the history
Fix EC2MetadataInstanceInfo ENI calculation
  • Loading branch information
k8s-ci-robot authored Jun 20, 2024
2 parents 73b4399 + b452b51 commit 2ace110
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pkg/cloud/metadata/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func EC2MetadataInstanceInfo(svc EC2Metadata, regionFromSession string) (*Metada
if err != nil {
return nil, fmt.Errorf("could not read ENIs metadata content: %w", err)
}
attachedENIs := strings.Count(string(enis), "\n")
attachedENIs := util.CountMACAddresses(string(enis))
klog.V(4).InfoS("Number of attached ENIs", "attachedENIs", attachedENIs)

blockDevMappings := 0
if !util.IsSBE(doc.Region) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/cloud/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestNewMetadataService(t *testing.T) {
},
}, nil)
mockEC2Metadata.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: EnisEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(strings.NewReader("01:23:45:67:89:ab\n")),
Content: io.NopCloser(strings.NewReader("01:23:45:67:89:ab")),
}, nil)
mockEC2Metadata.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: BlockDevicesEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(strings.NewReader("ebs\nebs\n")),
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestEC2MetadataInstanceInfo(t *testing.T) {
},
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: EnisEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(strings.NewReader("eni-1\neni-2\n")),
Content: io.NopCloser(strings.NewReader("eni-1\neni-2")),
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: BlockDevicesEndpoint}).Return(nil, errors.New("failed to get block device mappings metadata"))
},
Expand All @@ -275,7 +275,7 @@ func TestEC2MetadataInstanceInfo(t *testing.T) {
},
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: EnisEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(strings.NewReader("eni-1\neni-2\n")),
Content: io.NopCloser(strings.NewReader("01:23:45:67:89:ab\n02:23:45:67:89:ab")),
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: BlockDevicesEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(errReader{}),
Expand All @@ -295,7 +295,7 @@ func TestEC2MetadataInstanceInfo(t *testing.T) {
},
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: EnisEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(strings.NewReader("eni-1\neni-2\n")),
Content: io.NopCloser(strings.NewReader("01:23:45:67:89:ab\n02:23:45:67:89:ab")),
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: BlockDevicesEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(strings.NewReader("ebs\nebs\n")),
Expand Down Expand Up @@ -332,7 +332,7 @@ func TestEC2MetadataInstanceInfo(t *testing.T) {
},
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: EnisEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(strings.NewReader("eni-1\neni-2\n")),
Content: io.NopCloser(strings.NewReader("01:23:45:67:89:ab\n02:23:45:67:89:ab")),
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: BlockDevicesEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(strings.NewReader("ebs\nebs\n")),
Expand Down Expand Up @@ -362,7 +362,7 @@ func TestEC2MetadataInstanceInfo(t *testing.T) {
},
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: EnisEndpoint}).Return(&imds.GetMetadataOutput{
Content: io.NopCloser(strings.NewReader("eni-1\neni-2\n")),
Content: io.NopCloser(strings.NewReader("01:23:45:67:89:ab\n02:23:45:67:89:ab")),
}, nil)
m.EXPECT().GetMetadata(gomock.Any(), &imds.GetMetadataInput{Path: OutpostArnEndpoint}).Return(nil, errors.New("404 - Not Found"))
},
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const (

var (
isAlphanumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString
// MAC Address Regex Source: https://stackoverflow.com/a/4260512
isMACAddressRegex = regexp.MustCompile(`([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})`)
)

// RoundUpBytes rounds up the volume size in bytes up to multiplications of GiB
Expand Down Expand Up @@ -137,6 +139,12 @@ func StringIsAlphanumeric(s string) bool {
return isAlphanumericRegex(s)
}

// CountMACAddresses returns the amount of MAC addresses within a string
func CountMACAddresses(s string) int {
matches := isMACAddressRegex.FindAllStringIndex(s, -1)
return len(matches)
}

// NormalizeWindowsPath normalizes a Windows path
func NormalizeWindowsPath(path string) string {
normalizedPath := strings.Replace(path, "/", "\\", -1)
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,46 @@ func TestIsAlphanumeric(t *testing.T) {
}
}

func TestCountMACAddresses(t *testing.T) {
testCases := []struct {
name string
testString string
expResult int
}{
{
name: "success with newline at end",
testString: "0e:1c:7d:81:2b:19/\n0e:8c:22:a2:16:ef/\n",
expResult: 2,
},
{
name: "success with no newline",
testString: "0e:1c:7d:81:2b:19/\n0e:8c:22:a2:16:ef/sh-4.2$",
expResult: 2,
},
{
name: "success with no addresses",
testString: "00:::00/sh-4.2$",
expResult: 0,
},
{
name: "success with hard case",
testString: "Zé:1c:7d:81:2b:19/\n23:123:22:a2:16:ef/ff\n:/:sh-4.2$",
expResult: 0,
},
{
name: "success with carriage returns and beginning newline",
testString: "\r\n0e:1c:7d:81:2b:19/\r\n0e:8c:22:a2:16:ef/\r\n0e:8c:22:a2:16:ef/sh-4.2$",
expResult: 3,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := CountMACAddresses(tc.testString)
assert.Equalf(t, tc.expResult, res, "Wrong value returned for CountMACAddresses. Expected %d for string %s, got %d", tc.expResult, tc.testString, res)
})
}
}

type TestRequest struct {
Name string
Secrets map[string]string
Expand Down

0 comments on commit 2ace110

Please sign in to comment.