Skip to content

Commit

Permalink
Validate url for external remote write endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
philipgough committed May 10, 2024
1 parent d7918ae commit 14208a6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ func newAPISpec(c client.Client, mco *mcov1beta2.MultiClusterObservability) (obs
apiSpec.ImagePullPolicy = mcoconfig.GetImagePullPolicy(mco.Spec)
apiSpec.ServiceMonitor = true
if mco.Spec.StorageConfig.WriteStorage != nil {
eps := []mcoutil.RemoteWriteEndpointWithSecret{}
mountSecrets := []string{}
var eps []mcoutil.RemoteWriteEndpointWithSecret
var mountSecrets []string
for _, storageConfig := range mco.Spec.StorageConfig.WriteStorage {
storageSecret := &v1.Secret{}
err := c.Get(context.TODO(), types.NamespacedName{Name: storageConfig.Name,
Expand All @@ -560,6 +560,13 @@ func newAPISpec(c client.Client, mco *mcov1beta2.MultiClusterObservability) (obs
log.Error(err, "Failed to unmarshal data in secret", "name", storageConfig.Name)
return apiSpec, err
}

err = ep.Validate()
if err != nil {
log.Error(err, "Failed to validate data in secret", "name", storageConfig.Name)
return apiSpec, err
}

newEp := &mcoutil.RemoteWriteEndpointWithSecret{
Name: storageConfig.Name,
URL: ep.URL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package util

import (
"fmt"
"net/url"
"path"

"github.com/prometheus/common/config"
Expand Down Expand Up @@ -95,6 +97,23 @@ type RemoteWriteEndpointWithSecret struct {
HttpClientConfig *HTTPClientConfigWithSecret `yaml:"http_client_config,omitempty" json:"http_client_config,omitempty"`
}

// Validate validates the remote write endpoint
func (res *RemoteWriteEndpointWithSecret) Validate() error {
if res.Name == "" {
return fmt.Errorf("name is required for remote write endpoint")
}

if res.URL.String() == "" {
return fmt.Errorf("url is required for remote write endpoint %s", res.Name)
}

if _, err := url.ParseRequestURI(res.URL.String()); err != nil {
return fmt.Errorf("url %s is invalid for remote write endpoint %s: %s", res.URL.String(), res.Name, err)
}

return nil
}

func getMountPath(secretName, key string) string {
return path.Join(MountPath, secretName, key)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package util

import (
"net/url"
"path"
"testing"

"github.com/prometheus/common/config"
)

const (
Expand Down Expand Up @@ -84,3 +87,51 @@ func TestTransform(t *testing.T) {
t.Fatalf("Wrong number of mount secrets: expect 5, get %d", len(names))
}
}

func TestValidateRemoteWriteEndpointWithSecret(t *testing.T) {
testCases := []struct {
name string
endpoint *RemoteWriteEndpointWithSecret
wantErr bool
}{
{
name: "test empty name",
endpoint: &RemoteWriteEndpointWithSecret{Name: "", URL: mustParseURL(t, "https://example.com")},
wantErr: true,
},
{
name: "test missing url",
endpoint: &RemoteWriteEndpointWithSecret{Name: "valid-name", URL: mustParseURL(t, "")},
wantErr: true,
},
{
name: "test invalid url",
endpoint: &RemoteWriteEndpointWithSecret{Name: "valid-name", URL: mustParseURL(t, "invalid-url")},
wantErr: true,
},
{
name: "test happy path",
endpoint: &RemoteWriteEndpointWithSecret{Name: "valid-name", URL: mustParseURL(t, "https://example.com")},
wantErr: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.endpoint.Validate()
if (err != nil) != tc.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tc.wantErr)
return
}
})
}
}

func mustParseURL(t *testing.T, s string) config.URL {
u, err := url.Parse(s)
if err != nil {
t.Fatalf(err.Error())
}

return config.URL{URL: u}
}

0 comments on commit 14208a6

Please sign in to comment.