-
Notifications
You must be signed in to change notification settings - Fork 4.8k
NO-JIRA: chore(extended/prometheus): reduce targets-auth skip list and handle mTLS #31372
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
Open
machine424
wants to merge
1
commit into
openshift:main
Choose a base branch
from
machine424:promauth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−51
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,6 @@ import ( | |
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "regexp" | ||
| "runtime" | ||
| "slices" | ||
|
|
@@ -56,24 +55,11 @@ type TelemeterClientConfig struct { | |
| Enabled *bool `json:"enabled"` | ||
| } | ||
|
|
||
| // Set $MONITORING_AUTH_TEST_NAMESPACE to focus on the targets from a single namespace | ||
| var namespaceUnderTest = os.Getenv("MONITORING_AUTH_TEST_NAMESPACE") | ||
|
|
||
| var _ = g.Describe("[sig-instrumentation][Late] Platform Prometheus targets", func() { | ||
| defer g.GinkgoRecover() | ||
| var ( | ||
| oc = exutil.NewCLIWithPodSecurityLevel("prometheus", admissionapi.LevelBaseline) | ||
| prometheusURL, bearerToken string | ||
|
|
||
| // TODO: remove the namespace when the bug is fixed. | ||
| namespacesToSkip = sets.New[string]("openshift-marketplace", // https://issues.redhat.com/browse/OCPBUGS-59763 | ||
| "openshift-image-registry", // https://issues.redhat.com/browse/OCPBUGS-59767 | ||
| "openshift-operator-lifecycle-manager", // https://issues.redhat.com/browse/OCPBUGS-59768 | ||
| "openshift-cluster-samples-operator", // https://issues.redhat.com/browse/OCPBUGS-59769 | ||
| "openshift-cluster-csi-drivers", // https://issues.redhat.com/browse/OCPBUGS-60159 | ||
| "openshift-cluster-node-tuning-operator", // https://issues.redhat.com/browse/OCPBUGS-60258 | ||
| "openshift-etcd", // https://issues.redhat.com/browse/OCPBUGS-60263 | ||
| ) | ||
| ) | ||
|
|
||
| g.BeforeEach(func(ctx g.SpecContext) { | ||
|
|
@@ -91,33 +77,27 @@ var _ = g.Describe("[sig-instrumentation][Late] Platform Prometheus targets", fu | |
| o.Expect(err).NotTo(o.HaveOccurred(), "Get public url of prometheus") | ||
| bearerToken, err = helper.RequestPrometheusServiceAccountAPIToken(ctx, oc) | ||
| o.Expect(err).NotTo(o.HaveOccurred(), "Request prometheus service account API token") | ||
|
|
||
| if namespacesToSkip.Has(namespaceUnderTest) { | ||
| e2e.Logf("The namespace %s is not skipped because $MONITORING_AUTH_TEST_NAMESPACE is set to it", namespaceUnderTest) | ||
| namespacesToSkip.Delete(namespaceUnderTest) | ||
| } | ||
| }) | ||
|
|
||
| g.It("should not be accessible without auth [Serial]", func() { | ||
| expectedStatusCodes := sets.New(http.StatusUnauthorized, http.StatusForbidden) | ||
| // TODO: remove the namespace when the bug is fixed. | ||
| namespacesToSkip := []string{ | ||
| "openshift-image-registry", // https://issues.redhat.com/browse/OCPBUGS-59767 | ||
| "openshift-cluster-samples-operator", // https://issues.redhat.com/browse/OCPBUGS-59769 | ||
| } | ||
|
|
||
| g.By("checking that targets reject the requests with 401 or 403") | ||
| expectedStatusCodes := []int{http.StatusUnauthorized, http.StatusForbidden} | ||
|
|
||
| // With network policies bypassed, targets should still reject unauthenticated requests. | ||
| g.By("deploying network policies to allow test pods through existing ones") | ||
| ports := []networkPolicyTarget{ | ||
| {Namespace: "openshift-dns", Port: 9154}, | ||
| {Namespace: "openshift-dns-operator", Port: 9393}, | ||
| {Namespace: "openshift-ingress-operator", Port: 9393}, | ||
| } | ||
| networkPolicies := BuildNetworkPolicies(oc.Namespace(), ports) | ||
| for _, networkPolicy := range networkPolicies { | ||
| policies, err := oc.AdminKubeClient().NetworkingV1().NetworkPolicies(networkPolicy.Namespace).List(context.Background(), metav1.ListOptions{}) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the goal is to ensure auth is present even if NPs are bypassed. |
||
| o.Expect(err).NotTo(o.HaveOccurred(), fmt.Sprintf("List networkpolicies in %s", networkPolicy.Namespace)) | ||
| // If there are no network policies in the target namespace, the | ||
| // test pods should already have access. No need to install | ||
| // additional network policies. | ||
| if len(policies.Items) == 0 { | ||
| continue | ||
| } | ||
| _, err = oc.AdminKubeClient().NetworkingV1().NetworkPolicies(networkPolicy.Namespace).Create(context.Background(), &networkPolicy, metav1.CreateOptions{}) | ||
| _, err := oc.AdminKubeClient().NetworkingV1().NetworkPolicies(networkPolicy.Namespace).Create(context.Background(), &networkPolicy, metav1.CreateOptions{}) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| o.Expect(err).NotTo(o.HaveOccurred(), fmt.Sprintf("Create networkpolicy %s/%s", networkPolicy.Namespace, networkPolicy.Name)) | ||
| } | ||
| defer func() { | ||
|
|
@@ -129,6 +109,8 @@ var _ = g.Describe("[sig-instrumentation][Late] Platform Prometheus targets", fu | |
| o.Expect(err).NotTo(o.HaveOccurred(), fmt.Sprintf("Delete networkpolicy %s/%s", networkPolicy.Namespace, networkPolicy.Name)) | ||
| } | ||
| }() | ||
|
|
||
| g.By("checking that targets reject the requests with 401 or 403") | ||
| execPod := exutil.CreateExecPodOrFail(oc.AdminKubeClient(), oc.Namespace(), "execpod-targets-authorization") | ||
| defer func() { | ||
| err := oc.AdminKubeClient().CoreV1().Pods(execPod.Namespace).Delete(context.Background(), execPod.Name, *metav1.NewDeleteOptions(1)) | ||
|
|
@@ -161,13 +143,19 @@ var _ = g.Describe("[sig-instrumentation][Late] Platform Prometheus targets", fu | |
| eg.Go(func() error { | ||
| targetNs, targetJob, targetPod, targetScrapeURL := target.Labels["namespace"], target.Labels["job"], target.Labels["pod"], target.ScrapeUrl | ||
| o.Expect(targetNs).NotTo(o.BeEmpty()) | ||
| if namespaceUnderTest != "" && targetNs != namespaceUnderTest { | ||
| return nil | ||
| } | ||
|
|
||
| scrapeErr := wait.PollUntilContextTimeout(context.Background(), 10*time.Second, 5*time.Minute, true, func(context.Context) (bool, error) { | ||
| statusCode, err := helper.URLStatusCodeExecViaPod(execPod.Namespace, execPod.Name, targetScrapeURL) | ||
| e2e.Logf("scraping target %s of pod %s/%s/%s without auth returned %d, err: %v (skip=%t)", targetScrapeURL, targetNs, targetJob, targetPod, statusCode, err, namespacesToSkip.Has(targetNs)) | ||
| if expectedStatusCodes.Has(statusCode) { | ||
| statusCode, curlErrMsg, err := helper.CurlExecViaPod(execPod.Namespace, execPod.Name, targetScrapeURL) | ||
| e2e.Logf("scraping target %s of pod %s/%s/%s without auth returned %d, curlErrMsg: %q, err: %v (skip=%t)", targetScrapeURL, targetNs, targetJob, targetPod, statusCode, curlErrMsg, err, slices.Contains(namespacesToSkip, targetNs)) | ||
| if slices.Contains(expectedStatusCodes, statusCode) { | ||
| return true, nil | ||
| } | ||
|
|
||
| // Some targets enforce auth via mTLS (client certificates). | ||
| // We assume bad certificates will be rejected. | ||
| // https://issues.redhat.com/browse/OCPBUGS-60263 (etcd) | ||
| // https://issues.redhat.com/browse/OCPBUGS-59763 (marketplace) | ||
| if err != nil && statusCode == 0 && strings.Contains(curlErrMsg, "certificate required") { | ||
| return true, nil | ||
| } | ||
|
|
||
|
|
@@ -178,16 +166,16 @@ var _ = g.Describe("[sig-instrumentation][Late] Platform Prometheus targets", fu | |
| statusCode == http.StatusTooManyRequests { | ||
| return false, nil | ||
| } | ||
| return false, fmt.Errorf("expecting status code %v but returned %d", expectedStatusCodes.UnsortedList(), statusCode) | ||
| return false, fmt.Errorf("expecting status code %v but returned %d (curlErrMsg: %q)", expectedStatusCodes, statusCode, curlErrMsg) | ||
| }) | ||
|
|
||
| // Ignoring targets that Prometheus no longer scrapes or fails to scrape. | ||
| // These may be leftovers from earlier tests. | ||
| // Reference: https://issues.redhat.com/browse/OCPBUGS-61193 | ||
| if scrapeErr != nil && !namespacesToSkip.Has(targetNs) { | ||
| if scrapeErr != nil && !slices.Contains(namespacesToSkip, targetNs) { | ||
| targets, err := promTargets() | ||
| if err != nil { | ||
| e2e.Logf("refreshing state of target %s of pod %s/%s/%s failed, err: %v (skip=%t)", targetScrapeURL, targetNs, targetJob, targetPod, err, namespacesToSkip.Has(targetNs)) | ||
| e2e.Logf("refreshing state of target %s of pod %s/%s/%s failed, err: %v (skip=%t)", targetScrapeURL, targetNs, targetJob, targetPod, err, slices.Contains(namespacesToSkip, targetNs)) | ||
| targets = initialPromTargets | ||
| } | ||
| idx := slices.IndexFunc(targets.Data.ActiveTargets, func(t prometheusTarget) bool { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a short comment why this is no longer needed can be helpful to future readers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can leave the comment here:
this is no longer needed and was making the logic hard to read.