Skip to content

Commit

Permalink
Fix EC2MetadataInstanceInfo ENI calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSirenko committed Jun 17, 2024
1 parent 73b4399 commit 2e778b4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/cloud/metadata/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ 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))

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
7 changes: 7 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (

var (
isAlphanumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString
isMACAddressRegex = regexp.MustCompile(`(?s)([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 +138,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
35 changes: 35 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,41 @@ 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,
},
}
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 2e778b4

Please sign in to comment.