@@ -129,8 +129,8 @@ func TestUserWorkloadMonitoringAlerting(t *testing.T) {
129
129
f : assertUserWorkloadRules ,
130
130
},
131
131
{
132
- name : "assert tenancy model is enforced for rules" ,
133
- f : assertTenancyForRules ,
132
+ name : "assert tenancy model is enforced for rules and alerts " ,
133
+ f : assertTenancyForRulesAndAlerts ,
134
134
},
135
135
{
136
136
name : "assert prometheus is not deployed in user namespace" ,
@@ -664,44 +664,46 @@ func assertTenancyForMetrics(t *testing.T) {
664
664
})
665
665
}
666
666
667
- // Check that the account doesn't have to access the rules endpoint.
668
- err = framework .Poll (5 * time .Second , time .Minute , func () error {
669
- // The tenancy port (9092) is only exposed in-cluster so we need to use
670
- // port forwarding to access kube-rbac-proxy.
671
- host , cleanUp , err := f .ForwardPort (t , f .Ns , "thanos-querier" , 9092 )
672
- if err != nil {
673
- t .Fatal (err )
674
- }
675
- defer cleanUp ()
667
+ // Check that the account doesn't have to access the rules and alerts endpoint.
668
+ for _ , path := range []string {"/api/v1/rules" , "/api/v1/alerts" } {
669
+ err = framework .Poll (5 * time .Second , time .Minute , func () error {
670
+ // The tenancy port (9092) is only exposed in-cluster so we need to use
671
+ // port forwarding to access kube-rbac-proxy.
672
+ host , cleanUp , err := f .ForwardPort (t , f .Ns , "thanos-querier" , 9092 )
673
+ if err != nil {
674
+ t .Fatal (err )
675
+ }
676
+ defer cleanUp ()
676
677
677
- client := framework .NewPrometheusClient (
678
- host ,
679
- token ,
680
- & framework.QueryParameterInjector {
681
- Name : "namespace" ,
682
- Value : userWorkloadTestNs ,
683
- },
684
- )
678
+ client := framework .NewPrometheusClient (
679
+ host ,
680
+ token ,
681
+ & framework.QueryParameterInjector {
682
+ Name : "namespace" ,
683
+ Value : userWorkloadTestNs ,
684
+ },
685
+ )
685
686
686
- resp , err := client .Do ("GET" , "/api/v1/rules" , nil )
687
- if err != nil {
688
- return err
689
- }
690
- defer resp .Body .Close ()
687
+ resp , err := client .Do ("GET" , path , nil )
688
+ if err != nil {
689
+ return err
690
+ }
691
+ defer resp .Body .Close ()
691
692
692
- b , err := io .ReadAll (resp .Body )
693
- if err != nil {
694
- return err
695
- }
693
+ b , err := io .ReadAll (resp .Body )
694
+ if err != nil {
695
+ return err
696
+ }
696
697
697
- if resp .StatusCode / 100 == 2 {
698
- return fmt .Errorf ("expected request to be rejected, but got status code %d (%s)" , resp .StatusCode , framework .ClampMax (b ))
699
- }
698
+ if resp .StatusCode / 100 == 2 {
699
+ return fmt .Errorf ("expected request to be rejected, but got status code %d (%s)" , resp .StatusCode , framework .ClampMax (b ))
700
+ }
700
701
701
- return nil
702
- })
703
- if err != nil {
704
- t .Fatalf ("the account has access to the rules endpoint of Thanos querier: %v" , err )
702
+ return nil
703
+ })
704
+ if err != nil {
705
+ t .Fatalf ("the account has access to the %q endpoint of Thanos querier: %v" , path , err )
706
+ }
705
707
}
706
708
707
709
for _ , tc := range []struct {
@@ -821,8 +823,8 @@ func assertTenancyForMetrics(t *testing.T) {
821
823
}
822
824
}
823
825
824
- // assertTenancyForRules ensures that a tenant can access rules from her namespace (and only from this one).
825
- func assertTenancyForRules (t * testing.T ) {
826
+ // assertTenancyForRulesAndAlerts ensures that a tenant can access rules and alerts from her namespace (and only from this one).
827
+ func assertTenancyForRulesAndAlerts (t * testing.T ) {
826
828
const testAccount = "test-rules"
827
829
828
830
_ , err := f .CreateServiceAccount (userWorkloadTestNs , testAccount )
@@ -951,6 +953,27 @@ func assertTenancyForRules(t *testing.T) {
951
953
t .Fatalf ("failed to query rules from Thanos querier: %v" , err )
952
954
}
953
955
956
+ err = framework .Poll (5 * time .Second , time .Minute , func () error {
957
+ resp , err := client .Do ("GET" , "/api/v1/alerts" , nil )
958
+ if err != nil {
959
+ return err
960
+ }
961
+ defer resp .Body .Close ()
962
+
963
+ b , err := io .ReadAll (resp .Body )
964
+ if err != nil {
965
+ return err
966
+ }
967
+
968
+ if resp .StatusCode != http .StatusOK {
969
+ return fmt .Errorf ("unexpected status code response, want %d, got %d (%s)" , http .StatusOK , resp .StatusCode , framework .ClampMax (b ))
970
+ }
971
+ return nil
972
+ })
973
+ if err != nil {
974
+ t .Fatalf ("failed to query alerts from Thanos querier: %v" , err )
975
+ }
976
+
954
977
// Check that the account doesn't have to access the query endpoints.
955
978
for _ , path := range []string {"/api/v1/range?query=up" , "/api/v1/query_range?query=up&start=0&end=0&step=1s" } {
956
979
err = framework .Poll (5 * time .Second , time .Minute , func () error {
0 commit comments