Skip to content

Commit

Permalink
Escape slashes from resource names when generating filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Nov 13, 2024
1 parent 1a118b0 commit 26e89e4
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test-clean:
go clean -testcache

test:
go test -v ./cmd/... ./pkg/...
go test -v ./cmd/... ./internal/... ./pkg/...

integration: run-test-image-locally dev
go test -v ./integration/...
Expand Down
4 changes: 3 additions & 1 deletion pkg/grafana/alertgroup-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"

"github.com/grafana/grafana-openapi-client-go/client/provisioning"
Expand All @@ -29,7 +30,8 @@ const (

// ResourceFilePath returns the location on disk where a resource should be updated
func (h *AlertRuleGroupHandler) ResourceFilePath(resource grizzly.Resource, filetype string) string {
return fmt.Sprintf(alertRuleGroupPattern, resource.Name(), filetype)
filename := strings.ReplaceAll(resource.Name(), string(os.PathSeparator), "-")
return fmt.Sprintf(alertRuleGroupPattern, filename, filetype)
}

// Validate checks that the uid format is valid
Expand Down
21 changes: 21 additions & 0 deletions pkg/grafana/alertgroup-handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package grafana

import (
"testing"

"github.com/grafana/grizzly/pkg/grizzly"
"github.com/stretchr/testify/require"
)

func TestAlertRuleGroupHandler_ResourceFilePath(t *testing.T) {
handler := NewAlertRuleGroupHandler(&Provider{})

t.Run("slashes are escaped from filenames", func(t *testing.T) {
req := require.New(t)

resource, err := grizzly.NewResource(handler.APIVersion(), handler.Kind(), "some/alert/group", map[string]interface{}{})
req.NoError(err)

req.Equal("alert-rules/alertRuleGroup-some-alert-group.yaml", handler.ResourceFilePath(resource, "yaml"))
})
}
5 changes: 4 additions & 1 deletion pkg/grafana/datasource-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"

"github.com/go-openapi/runtime"
"github.com/grafana/grafana-openapi-client-go/client/datasources"
Expand All @@ -31,7 +33,8 @@ const (

// ResourceFilePath returns the location on disk where a resource should be updated
func (h *DatasourceHandler) ResourceFilePath(resource grizzly.Resource, filetype string) string {
return fmt.Sprintf(datasourcePattern, resource.Name(), filetype)
filename := strings.ReplaceAll(resource.Name(), string(os.PathSeparator), "-")
return fmt.Sprintf(datasourcePattern, filename, filetype)
}

// Unprepare removes unnecessary elements from a remote resource ready for presentation/comparison
Expand Down
21 changes: 21 additions & 0 deletions pkg/grafana/datasource-handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package grafana

import (
"testing"

"github.com/grafana/grizzly/pkg/grizzly"
"github.com/stretchr/testify/require"
)

func TestDatasourceHandler_ResourceFilePath(t *testing.T) {
handler := NewDatasourceHandler(&Provider{})

t.Run("slashes are escaped from filenames", func(t *testing.T) {
req := require.New(t)

resource, err := grizzly.NewResource(handler.APIVersion(), handler.Kind(), "some/datasource", map[string]interface{}{})
req.NoError(err)

req.Equal("datasources/datasource-some-datasource.yaml", handler.ResourceFilePath(resource, "yaml"))
})
}
4 changes: 3 additions & 1 deletion pkg/mimir/rules-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mimir

import (
"fmt"
"os"
"strings"

"github.com/grafana/grizzly/pkg/grizzly"
Expand Down Expand Up @@ -29,7 +30,8 @@ const (

// ResourceFilePath returns the location on disk where a resource should be updated
func (h *RuleHandler) ResourceFilePath(resource grizzly.Resource, filetype string) string {
return fmt.Sprintf(prometheusRuleGroupPattern, resource.Name(), filetype)
filename := strings.ReplaceAll(resource.Name(), string(os.PathSeparator), "-")
return fmt.Sprintf(prometheusRuleGroupPattern, filename, filetype)
}

// Validate returns the uid of resource
Expand Down
13 changes: 13 additions & 0 deletions pkg/mimir/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ func TestRules(t *testing.T) {
})
}

func TestRuleHandler_ResourceFilePath(t *testing.T) {
handler := NewRuleHandler(&Provider{}, &FakeClient{})

t.Run("slashes are escaped from filenames", func(t *testing.T) {
req := require.New(t)

resource, err := grizzly.NewResource(handler.APIVersion(), handler.Kind(), "some/rule", map[string]interface{}{})
req.NoError(err)

req.Equal("prometheus/rules-some-rule.yaml", handler.ResourceFilePath(resource, "yaml"))
})
}

type FakeClient struct {
hasFile bool
expectedError error
Expand Down
5 changes: 4 additions & 1 deletion pkg/syntheticmonitoring/synthetic-monitoring-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"

"github.com/grafana/grizzly/pkg/grizzly"
Expand Down Expand Up @@ -48,7 +50,8 @@ const (

// ResourceFilePath returns the location on disk where a resource should be updated
func (h *SyntheticMonitoringHandler) ResourceFilePath(resource grizzly.Resource, filetype string) string {
return fmt.Sprintf(syntheticMonitoringPattern, resource.Name(), filetype)
filename := strings.ReplaceAll(resource.Name(), string(os.PathSeparator), "-")
return fmt.Sprintf(syntheticMonitoringPattern, filename, filetype)
}

// Unprepare removes unnecessary elements from a remote resource ready for presentation/comparison
Expand Down
13 changes: 13 additions & 0 deletions pkg/syntheticmonitoring/synthetic-monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,16 @@ func TestSyntheticMonitoringCheckUID(t *testing.T) {
})
}
}

func TestSyntheticMonitoringHandler_ResourceFilePath(t *testing.T) {
handler := NewSyntheticMonitoringHandler(&Provider{})

t.Run("slashes are escaped from filenames", func(t *testing.T) {
req := require.New(t)

resource, err := grizzly.NewResource(handler.APIVersion(), handler.Kind(), "some/check", map[string]interface{}{})
req.NoError(err)

req.Equal("synthetic-monitoring/check-some-check.yaml", handler.ResourceFilePath(resource, "yaml"))
})
}

0 comments on commit 26e89e4

Please sign in to comment.