Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:validate proxyURL for alertManagerCfg (#6466) #6481

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/apis/monitoring/v1alpha1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v1alpha1
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 err
}
}

return nil
}

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

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

func TestTimeRange_Parse(t *testing.T) {
Expand Down Expand Up @@ -390,3 +393,61 @@ func TestYearRange_Parse(t *testing.T) {
})
}
}

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)
}
})
}

}
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 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,6 +17,9 @@ package v1beta1
import (
"reflect"
"testing"

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

func TestTimeRange_Parse(t *testing.T) {
Expand Down Expand Up @@ -390,3 +393,61 @@ func TestYearRange_Parse(t *testing.T) {
})
}
}

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)
}
})
}

}