Skip to content

Commit

Permalink
fix: validate proxyURL for AlertmanagerConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
  • Loading branch information
simonpasquier committed May 17, 2024
1 parent 35340a6 commit 400c2c5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/apis/monitoring/v1beta1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v1beta1
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -61,6 +62,12 @@ func (hc *HTTPConfig) Validate() error {
}
}

if hc.ProxyURL != "" {
if _, err := url.Parse(hc.ProxyURL); err != nil {
return fmt.Errorf("invalid proxyURL: %w", err)
}
}

return nil
}

Expand Down
61 changes: 61 additions & 0 deletions pkg/apis/monitoring/v1beta1/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,69 @@ package v1beta1
import (
"reflect"
"testing"

monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
v1 "k8s.io/api/core/v1"
)

func TestHTTPClientConfigValidate(t *testing.T) {
for _, tc := range []struct {
name string
in *HTTPConfig
fail bool
}{
{
name: "nil",
},
{
name: "empty",
in: &HTTPConfig{},
},
{
name: "duplicate basic-auth and auth",
in: &HTTPConfig{
Authorization: &monitoringv1.SafeAuthorization{
Credentials: &v1.SecretKeySelector{},
},
BasicAuth: &monitoringv1.BasicAuth{},
},
fail: true,
},
{
name: "duplicate basic-auth and oauth2",
in: &HTTPConfig{
OAuth2: &monitoringv1.OAuth2{},
BasicAuth: &monitoringv1.BasicAuth{},
},
fail: true,
},
{
name: "invalid Proxy URL",
in: &HTTPConfig{
ProxyURL: "://example.com",
},
fail: true,
},
} {
t.Run(tc.name, func(t *testing.T) {

err := tc.in.Validate()
if tc.fail {
if err == nil {
t.Fatal("expecting error, got nil")
}

return
}

if err != nil {
t.Fatalf("expecting no error, got %q", err)
}
})
}

}

func TestTimeRange_Parse(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit 400c2c5

Please sign in to comment.