AzureMonitor: Fix out of bounds error when accessing metricNamespaceArray and resourceNameArray in buildResourceURI (#89222)

Fix out of range error when accessing metricNamespaceArray in buildResourceURI
This commit is contained in:
Adam Yeats 2024-07-25 11:19:15 +01:00 committed by GitHub
parent 9db3bc926e
commit 3ce1a5b0ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 72 additions and 3 deletions

View File

@ -39,12 +39,19 @@ func (params *urlBuilder) buildResourceURI() (*string, error) {
}
metricNamespaceArray := strings.Split(*metricNamespace, "/")
provider := ""
if len(metricNamespaceArray) > 1 {
provider = metricNamespaceArray[0]
metricNamespaceArray = metricNamespaceArray[1:]
} else {
return nil, fmt.Errorf("metricNamespace is not in the correct format")
}
var resourceNameArray []string
if params.ResourceName != nil && *params.ResourceName != "" {
resourceNameArray = strings.Split(*params.ResourceName, "/")
}
provider := metricNamespaceArray[0]
metricNamespaceArray = metricNamespaceArray[1:]
if strings.HasPrefix(strings.ToLower(*metricNamespace), "microsoft.storage/storageaccounts/") &&
params.ResourceName != nil &&
@ -66,7 +73,11 @@ func (params *urlBuilder) buildResourceURI() (*string, error) {
}
for i, namespace := range metricNamespaceArray {
urlArray = append(urlArray, namespace, resourceNameArray[i])
if i < len(resourceNameArray) {
urlArray = append(urlArray, namespace, resourceNameArray[i])
} else {
return nil, fmt.Errorf("resourceNameArray does not have enough elements")
}
}
resourceURI := strings.Join(urlArray, "/")

View File

@ -1,6 +1,7 @@
package metrics
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
@ -145,5 +146,62 @@ func TestBuildResourceURI(t *testing.T) {
}
}
})
t.Run("provider extraction from metricNamespaceArray", func(t *testing.T) {
ub := &urlBuilder{
DefaultSubscription: strPtr("default-sub"),
MetricNamespace: strPtr("provider1/service1"),
ResourceGroup: strPtr("rg"),
ResourceName: strPtr("rn1/rn2/rn3"),
}
expectedProvider := "provider1"
uri, err := ub.buildResourceURI()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if uri == nil {
t.Fatalf("Expected non-nil uri")
}
if !strings.Contains(*uri, expectedProvider) {
t.Errorf("Expected provider %v in uri %v", expectedProvider, *uri)
}
})
t.Run("when metricNamespace is not in the correct format", func(t *testing.T) {
ub := &urlBuilder{
DefaultSubscription: strPtr("default-sub"),
MetricNamespace: strPtr("invalidformat"),
}
_, err := ub.buildResourceURI()
if err == nil || err.Error() != "metricNamespace is not in the correct format" {
t.Errorf("Expected error: metricNamespace is not in the correct format")
}
})
t.Run("when resourceNameArray index out of range", func(t *testing.T) {
ub := &urlBuilder{
DefaultSubscription: strPtr("default-sub"),
MetricNamespace: strPtr("provider1/service1"),
ResourceName: strPtr("rn1/rn2/rn3"),
}
_, err := ub.buildResourceURI()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
ub = &urlBuilder{
DefaultSubscription: strPtr("default-sub"),
MetricNamespace: strPtr("provider1/service1/service2"),
ResourceName: strPtr(""),
}
_, err = ub.buildResourceURI()
if err == nil || err.Error() != "resourceNameArray does not have enough elements" {
t.Errorf("Expected error: resourceNameArray does not have enough elements")
}
})
})
}