mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
9db3bc926e
commit
3ce1a5b0ca
@ -39,12 +39,19 @@ func (params *urlBuilder) buildResourceURI() (*string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
metricNamespaceArray := strings.Split(*metricNamespace, "/")
|
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
|
var resourceNameArray []string
|
||||||
if params.ResourceName != nil && *params.ResourceName != "" {
|
if params.ResourceName != nil && *params.ResourceName != "" {
|
||||||
resourceNameArray = strings.Split(*params.ResourceName, "/")
|
resourceNameArray = strings.Split(*params.ResourceName, "/")
|
||||||
}
|
}
|
||||||
provider := metricNamespaceArray[0]
|
|
||||||
metricNamespaceArray = metricNamespaceArray[1:]
|
|
||||||
|
|
||||||
if strings.HasPrefix(strings.ToLower(*metricNamespace), "microsoft.storage/storageaccounts/") &&
|
if strings.HasPrefix(strings.ToLower(*metricNamespace), "microsoft.storage/storageaccounts/") &&
|
||||||
params.ResourceName != nil &&
|
params.ResourceName != nil &&
|
||||||
@ -66,7 +73,11 @@ func (params *urlBuilder) buildResourceURI() (*string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, namespace := range metricNamespaceArray {
|
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, "/")
|
resourceURI := strings.Join(urlArray, "/")
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"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")
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user