@@ -14,6 +14,8 @@ import (
1414
1515 g "github.com/onsi/ginkgo/v2"
1616 o "github.com/onsi/gomega"
17+ prometheusoperatorv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
18+ prometheusoperatorv1client "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/typed/monitoring/v1"
1719 promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
1820 dto "github.com/prometheus/client_model/go"
1921 "github.com/prometheus/common/expfmt"
@@ -49,6 +51,76 @@ type TelemeterClientConfig struct {
4951 Enabled * bool `json:"enabled"`
5052}
5153
54+ var _ = g .Describe ("[sig-instrumentation][Late] OpenShift service monitors [apigroup:image.openshift.io]" , func () {
55+ defer g .GinkgoRecover ()
56+ var (
57+ oc = exutil .NewCLIWithoutNamespace ("prometheus" )
58+ prometheusURL , bearerToken string
59+ )
60+
61+ g .BeforeEach (func (ctx g.SpecContext ) {
62+ var err error
63+ prometheusURL , err = helper .PrometheusRouteURL (ctx , oc )
64+ o .Expect (err ).NotTo (o .HaveOccurred (), "Get public url of prometheus" )
65+ bearerToken , err = helper .RequestPrometheusServiceAccountAPIToken (ctx , oc )
66+ o .Expect (err ).NotTo (o .HaveOccurred (), "Request prometheus service account API token" )
67+ })
68+
69+ g .It ("should not be accessible without authorization" , func () {
70+ var errs []error
71+ client , err := prometheusoperatorv1client .NewForConfig (oc .AdminConfig ())
72+ o .Expect (err ).NotTo (o .HaveOccurred (), "Create monitoring client" )
73+
74+ g .By ("verifying all service monitors are configured with authorization" )
75+ serviceMonitorList , err := client .ServiceMonitors ("" ).List (context .Background (), metav1.ListOptions {})
76+ o .Expect (err ).NotTo (o .HaveOccurred (), "List service monitors" )
77+
78+ for _ , sm := range serviceMonitorList .Items {
79+ // we do not check service monitors for user-workload-monitoring
80+ if ! strings .HasPrefix (sm .Namespace , "openshift-" ) {
81+ continue
82+ }
83+ if ! authorizationConfigured (sm ) {
84+ errs = append (errs , fmt .Errorf ("service monitor %s/%s has no authorization" , sm .Namespace , sm .Name ))
85+ }
86+ }
87+
88+ g .By ("verifying all targets returns 401 or 403 without authorization" )
89+ contents , err := helper .GetURLWithToken (helper .MustJoinUrlPath (prometheusURL , "api/v1/targets" ), bearerToken )
90+ o .Expect (err ).NotTo (o .HaveOccurred ())
91+
92+ targets := & prometheusTargets {}
93+ err = json .Unmarshal ([]byte (contents ), targets )
94+ o .Expect (err ).NotTo (o .HaveOccurred ())
95+
96+ for _ , target := range targets .Data .ActiveTargets {
97+ ns := target .Labels ["namespace" ]
98+ if ! strings .HasPrefix (ns , "openshift-" ) {
99+ continue
100+ }
101+ err = helper .ExpectURLStatusCodeExecViaPod ("openshift-monitoring" , "prometheus-k8s-0" , target .ScrapeUrl , 401 , 403 )
102+ if err != nil {
103+ errs = append (errs , fmt .Errorf ("the scaple url %s for namespace %s is accessible without authorization: %w" , target .ScrapeUrl , ns , err ))
104+ }
105+ }
106+ o .Expect (errs ).To (o .BeEmpty ())
107+ })
108+
109+ })
110+
111+ // authorizationConfigured returns true if the service monitor is configured with authorization
112+ func authorizationConfigured (sm * prometheusoperatorv1.ServiceMonitor ) bool {
113+ if sm == nil {
114+ return false
115+ }
116+ for _ , e := range sm .Spec .Endpoints {
117+ if e .BasicAuth != nil || e .OAuth2 != nil || e .Authorization != nil || e .BearerTokenFile != "" || e .BearerTokenSecret != nil {
118+ return true
119+ }
120+ }
121+ return false
122+ }
123+
52124var _ = g .Describe ("[sig-instrumentation][Late] OpenShift alerting rules [apigroup:image.openshift.io]" , func () {
53125 defer g .GinkgoRecover ()
54126
0 commit comments