2017-01-05 18:15:21 +01:00
|
|
|
// Copyright 2016 The prometheus-operator Authors
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
package framework
|
|
|
|
|
|
|
|
|
|
import (
|
2017-11-30 16:37:20 +01:00
|
|
|
"bytes"
|
2021-07-23 12:58:54 +05:30
|
|
|
"context"
|
2017-02-24 13:55:52 +01:00
|
|
|
"encoding/json"
|
2017-01-05 18:15:21 +01:00
|
|
|
"fmt"
|
2024-01-29 17:19:20 +01:00
|
|
|
"os"
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
"reflect"
|
2025-11-04 16:28:38 +08:00
|
|
|
"slices"
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
"strings"
|
2017-01-05 18:15:21 +01:00
|
|
|
"time"
|
|
|
|
|
|
2019-06-05 11:43:01 -07:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2017-05-11 14:05:39 +02:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2025-08-01 14:28:47 +02:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2022-04-13 16:08:56 +02:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2017-05-11 14:05:39 +02:00
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2023-08-11 09:42:11 +02:00
|
|
|
"k8s.io/utils/ptr"
|
2017-01-05 18:15:21 +01:00
|
|
|
|
2022-04-13 16:08:56 +02:00
|
|
|
"github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring"
|
2020-08-05 13:13:46 +02:00
|
|
|
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
|
|
|
|
|
"github.com/prometheus-operator/prometheus-operator/pkg/operator"
|
2017-01-05 18:15:21 +01:00
|
|
|
)
|
|
|
|
|
|
2020-05-11 16:17:40 +03:00
|
|
|
const (
|
2024-01-29 17:19:20 +01:00
|
|
|
SECRET = iota
|
|
|
|
|
CONFIGMAP
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
ScrapingTLSSecret = "scraping-tls"
|
|
|
|
|
ServerTLSSecret = "server-tls"
|
|
|
|
|
ServerCASecret = "server-tls-ca"
|
|
|
|
|
|
|
|
|
|
CAKey = "ca.pem"
|
|
|
|
|
CertKey = "cert.pem"
|
|
|
|
|
PrivateKey = "key.pem"
|
2020-05-11 16:17:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Key struct {
|
|
|
|
|
Filename string
|
|
|
|
|
SecretName string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Cert struct {
|
|
|
|
|
Filename string
|
|
|
|
|
ResourceName string
|
|
|
|
|
ResourceType int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PromRemoteWriteTestConfig struct {
|
2024-10-22 17:53:12 +02:00
|
|
|
ClientKey Key
|
|
|
|
|
ClientCert Cert
|
|
|
|
|
CA Cert
|
|
|
|
|
InsecureSkipVerify bool
|
|
|
|
|
RemoteWriteMessageVersion *monitoringv1.RemoteWriteMessageVersion
|
2024-01-29 17:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *Framework) CreateCertificateResources(namespace, certsDir string, prwtc PromRemoteWriteTestConfig) error {
|
|
|
|
|
var (
|
|
|
|
|
clientKey, clientCert, serverKey, serverCert, caCert []byte
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if prwtc.ClientKey.Filename != "" {
|
|
|
|
|
clientKey, err = os.ReadFile(certsDir + prwtc.ClientKey.Filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to load %s: %w", prwtc.ClientKey.Filename, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if prwtc.ClientCert.Filename != "" {
|
|
|
|
|
clientCert, err = os.ReadFile(certsDir + prwtc.ClientCert.Filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to load %s: %w", prwtc.ClientCert.Filename, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if prwtc.CA.Filename != "" {
|
|
|
|
|
caCert, err = os.ReadFile(certsDir + prwtc.CA.Filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to load %s: %w", prwtc.CA.Filename, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serverKey, err = os.ReadFile(certsDir + "ca.key")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to load %s: %w", "ca.key", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serverCert, err = os.ReadFile(certsDir + "ca.crt")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to load %s: %w", "ca.crt", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scrapingKey, err := os.ReadFile(certsDir + "client.key")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to load %s: %w", "client.key", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scrapingCert, err := os.ReadFile(certsDir + "client.crt")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to load %s: %v", "client.crt", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
secrets = map[string]*v1.Secret{}
|
|
|
|
|
configMaps = map[string]*v1.ConfigMap{}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
secrets[ScrapingTLSSecret] = MakeSecretWithCert(namespace, ScrapingTLSSecret, []string{PrivateKey, CertKey, CAKey}, [][]byte{scrapingKey, scrapingCert, serverCert})
|
|
|
|
|
secrets[ServerTLSSecret] = MakeSecretWithCert(namespace, ServerTLSSecret, []string{PrivateKey, CertKey}, [][]byte{serverKey, serverCert})
|
|
|
|
|
secrets[ServerCASecret] = MakeSecretWithCert(namespace, ServerCASecret, []string{CAKey}, [][]byte{serverCert})
|
|
|
|
|
|
|
|
|
|
if len(clientKey) > 0 && len(clientCert) > 0 {
|
|
|
|
|
secrets[prwtc.ClientKey.SecretName] = MakeSecretWithCert(namespace, prwtc.ClientKey.SecretName, []string{PrivateKey}, [][]byte{clientKey})
|
|
|
|
|
|
|
|
|
|
if prwtc.ClientCert.ResourceType == CONFIGMAP {
|
|
|
|
|
configMaps[prwtc.ClientCert.ResourceName] = MakeConfigMapWithCert(namespace, prwtc.ClientCert.ResourceName, "", CertKey, "", nil, clientCert, nil)
|
|
|
|
|
} else {
|
|
|
|
|
if _, found := secrets[prwtc.ClientCert.ResourceName]; found {
|
|
|
|
|
secrets[prwtc.ClientCert.ResourceName].Data[CertKey] = clientCert
|
|
|
|
|
} else {
|
|
|
|
|
secrets[prwtc.ClientCert.ResourceName] = MakeSecretWithCert(namespace, prwtc.ClientCert.ResourceName, []string{CertKey}, [][]byte{clientCert})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(caCert) > 0 {
|
|
|
|
|
if prwtc.CA.ResourceType == CONFIGMAP {
|
|
|
|
|
if _, found := configMaps[prwtc.CA.ResourceName]; found {
|
|
|
|
|
configMaps[prwtc.CA.ResourceName].Data[CAKey] = string(caCert)
|
|
|
|
|
} else {
|
|
|
|
|
configMaps[prwtc.CA.ResourceName] = MakeConfigMapWithCert(namespace, prwtc.CA.ResourceName, "", "", CAKey, nil, nil, caCert)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if _, found := secrets[prwtc.CA.ResourceName]; found {
|
|
|
|
|
secrets[prwtc.CA.ResourceName].Data[CAKey] = caCert
|
|
|
|
|
} else {
|
|
|
|
|
secrets[prwtc.CA.ResourceName] = MakeSecretWithCert(namespace, prwtc.CA.ResourceName, []string{CAKey}, [][]byte{caCert})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for k := range secrets {
|
|
|
|
|
_, err := f.KubeClient.CoreV1().Secrets(namespace).Create(context.Background(), secrets[k], metav1.CreateOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create secret: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for k := range configMaps {
|
|
|
|
|
_, err := f.KubeClient.CoreV1().ConfigMaps(namespace).Create(context.Background(), configMaps[k], metav1.CreateOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create configmap: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2020-05-11 16:17:40 +03:00
|
|
|
}
|
|
|
|
|
|
2017-08-22 12:56:04 +02:00
|
|
|
func (f *Framework) MakeBasicPrometheus(ns, name, group string, replicas int32) *monitoringv1.Prometheus {
|
2024-09-18 18:33:32 +05:30
|
|
|
promVersion := operator.DefaultPrometheusVersion
|
2024-12-03 17:27:56 +05:30
|
|
|
if os.Getenv("TEST_PROMETHEUS_V2") == "true" {
|
|
|
|
|
promVersion = operator.DefaultPrometheusV2
|
2024-09-18 18:33:32 +05:30
|
|
|
}
|
2017-08-22 12:56:04 +02:00
|
|
|
return &monitoringv1.Prometheus{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
pkg/*/statefulset.go: Do not mutate shared object
Users have reported high CPU usage of the Prometheus Operator when
adding an annotation to a Prometheus object. The Operator would update
the respective StatefulSet in an infinite loop.
Whether a given StatefulSet needs updating is determined by the hash of
the inputs needed to generate the StatefulSet, which is calculated and
then attached to the StatefulSet as an annotation. On subsequent
reconciliations this hash is compared to the hash of the new inputs.
The function to build the StatefulSet definition is passed the
Prometheus object. This is done by value, not by reference. This does
not enforce a deep copy but merely a shallow copy. In the build function
the new StatefulSet would inherit the annotation map of the Prometheus
object. Next the input hash would be added to this map, resulting in
both the Statefulset having the hash annotation, as intended, as well as
the Prometheus object (same map, shared as a reference).
On subsequent reconciliations the same Prometheus object is used to
calculate the input hash, this time accidentally containing the has
annotation from the previous run. Even though the actual inputs never
changed, this results in a new hash, thereby updating the StatefulSet,
...
The solution is to deep copy the Prometheus object before using it in
the StatefulSet build function, thereby never mutating the annotations
of the Prometheus object. Same measure is taken for the Alertmanager
StatefulSet build function.
2018-08-13 13:42:41 +02:00
|
|
|
Name: name,
|
|
|
|
|
Namespace: ns,
|
|
|
|
|
Annotations: map[string]string{},
|
2017-08-22 12:56:04 +02:00
|
|
|
},
|
|
|
|
|
Spec: monitoringv1.PrometheusSpec{
|
2022-01-28 10:47:06 +01:00
|
|
|
CommonPrometheusFields: monitoringv1.CommonPrometheusFields{
|
|
|
|
|
Replicas: &replicas,
|
2024-09-18 18:33:32 +05:30
|
|
|
Version: promVersion,
|
2022-01-28 10:47:06 +01:00
|
|
|
ServiceMonitorSelector: &metav1.LabelSelector{
|
|
|
|
|
MatchLabels: map[string]string{
|
|
|
|
|
"group": group,
|
|
|
|
|
},
|
2017-08-22 12:56:04 +02:00
|
|
|
},
|
2022-01-28 10:47:06 +01:00
|
|
|
PodMonitorSelector: &metav1.LabelSelector{
|
|
|
|
|
MatchLabels: map[string]string{
|
|
|
|
|
"group": group,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
ServiceAccountName: "prometheus",
|
|
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
|
Requests: v1.ResourceList{
|
|
|
|
|
v1.ResourceMemory: resource.MustParse("400Mi"),
|
|
|
|
|
},
|
2020-07-27 18:07:57 +02:00
|
|
|
},
|
|
|
|
|
},
|
2018-06-05 13:56:45 +02:00
|
|
|
RuleSelector: &metav1.LabelSelector{
|
2017-03-09 20:08:38 +01:00
|
|
|
MatchLabels: map[string]string{
|
|
|
|
|
"role": "rulefile",
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-01-13 12:14:00 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 17:19:20 +01:00
|
|
|
// AddRemoteWriteWithTLSToPrometheus configures Prometheus to send samples to the remote-write endpoint.
|
|
|
|
|
func (prwtc PromRemoteWriteTestConfig) AddRemoteWriteWithTLSToPrometheus(p *monitoringv1.Prometheus, url string) {
|
2020-05-11 16:17:40 +03:00
|
|
|
p.Spec.RemoteWrite = []monitoringv1.RemoteWriteSpec{{
|
2024-10-22 17:53:12 +02:00
|
|
|
URL: url,
|
|
|
|
|
MessageVersion: prwtc.RemoteWriteMessageVersion,
|
2024-01-29 17:19:20 +01:00
|
|
|
QueueConfig: &monitoringv1.QueueConfig{
|
2024-03-04 23:27:24 +08:00
|
|
|
BatchSendDeadline: (*monitoringv1.Duration)(ptr.To("1s")),
|
2024-01-29 17:19:20 +01:00
|
|
|
},
|
2020-05-11 16:17:40 +03:00
|
|
|
}}
|
|
|
|
|
|
2024-01-29 17:19:20 +01:00
|
|
|
if (prwtc.ClientKey.SecretName == "" || prwtc.ClientCert.ResourceName == "") && prwtc.CA.ResourceName == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-05-11 16:17:40 +03:00
|
|
|
|
2024-01-29 17:19:20 +01:00
|
|
|
p.Spec.RemoteWrite[0].TLSConfig = &monitoringv1.TLSConfig{
|
|
|
|
|
SafeTLSConfig: monitoringv1.SafeTLSConfig{
|
2024-05-07 11:12:55 +02:00
|
|
|
ServerName: ptr.To("caandserver.com"),
|
2024-01-29 17:19:20 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if prwtc.ClientKey.SecretName != "" && prwtc.ClientCert.ResourceName != "" {
|
|
|
|
|
p.Spec.RemoteWrite[0].TLSConfig.KeySecret = &v1.SecretKeySelector{
|
|
|
|
|
LocalObjectReference: v1.LocalObjectReference{
|
|
|
|
|
Name: prwtc.ClientKey.SecretName,
|
2020-08-13 15:24:32 +02:00
|
|
|
},
|
2024-01-29 17:19:20 +01:00
|
|
|
Key: PrivateKey,
|
2020-05-11 16:17:40 +03:00
|
|
|
}
|
2024-01-29 17:19:20 +01:00
|
|
|
p.Spec.RemoteWrite[0].TLSConfig.Cert = monitoringv1.SecretOrConfigMap{}
|
2020-05-11 16:17:40 +03:00
|
|
|
|
2024-01-29 17:19:20 +01:00
|
|
|
if prwtc.ClientCert.ResourceType == SECRET {
|
|
|
|
|
p.Spec.RemoteWrite[0].TLSConfig.Cert.Secret = &v1.SecretKeySelector{
|
2020-05-11 16:17:40 +03:00
|
|
|
LocalObjectReference: v1.LocalObjectReference{
|
2024-01-29 17:19:20 +01:00
|
|
|
Name: prwtc.ClientCert.ResourceName,
|
2020-05-11 16:17:40 +03:00
|
|
|
},
|
2024-01-29 17:19:20 +01:00
|
|
|
Key: CertKey,
|
2020-05-11 16:17:40 +03:00
|
|
|
}
|
2024-01-29 17:19:20 +01:00
|
|
|
} else { //certType == CONFIGMAP
|
|
|
|
|
p.Spec.RemoteWrite[0].TLSConfig.Cert.ConfigMap = &v1.ConfigMapKeySelector{
|
|
|
|
|
LocalObjectReference: v1.LocalObjectReference{
|
|
|
|
|
Name: prwtc.ClientCert.ResourceName,
|
|
|
|
|
},
|
|
|
|
|
Key: CertKey,
|
2020-05-11 16:17:40 +03:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-29 17:19:20 +01:00
|
|
|
}
|
2020-05-11 16:17:40 +03:00
|
|
|
|
2024-01-29 17:19:20 +01:00
|
|
|
switch {
|
|
|
|
|
case prwtc.CA.ResourceName != "":
|
|
|
|
|
p.Spec.RemoteWrite[0].TLSConfig.CA = monitoringv1.SecretOrConfigMap{}
|
|
|
|
|
switch prwtc.CA.ResourceType {
|
|
|
|
|
case SECRET:
|
|
|
|
|
p.Spec.RemoteWrite[0].TLSConfig.CA.Secret = &v1.SecretKeySelector{
|
|
|
|
|
LocalObjectReference: v1.LocalObjectReference{
|
|
|
|
|
Name: prwtc.CA.ResourceName,
|
|
|
|
|
},
|
|
|
|
|
Key: CAKey,
|
|
|
|
|
}
|
|
|
|
|
case CONFIGMAP:
|
|
|
|
|
p.Spec.RemoteWrite[0].TLSConfig.CA.ConfigMap = &v1.ConfigMapKeySelector{
|
|
|
|
|
LocalObjectReference: v1.LocalObjectReference{
|
|
|
|
|
Name: prwtc.CA.ResourceName,
|
|
|
|
|
},
|
|
|
|
|
Key: CAKey,
|
2020-05-11 16:17:40 +03:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-29 17:19:20 +01:00
|
|
|
|
|
|
|
|
case prwtc.InsecureSkipVerify:
|
2024-05-07 11:12:55 +02:00
|
|
|
p.Spec.RemoteWrite[0].TLSConfig.InsecureSkipVerify = ptr.To(true)
|
2020-05-11 16:17:40 +03:00
|
|
|
}
|
2022-03-21 20:26:36 +05:30
|
|
|
}
|
|
|
|
|
|
2024-01-29 17:19:20 +01:00
|
|
|
func (f *Framework) EnableRemoteWriteReceiverWithTLS(p *monitoringv1.Prometheus) {
|
2024-09-23 18:14:54 +05:30
|
|
|
p.Spec.EnableRemoteWriteReceiver = true
|
2024-01-29 17:19:20 +01:00
|
|
|
p.Spec.Web = &monitoringv1.PrometheusWebSpec{
|
|
|
|
|
WebConfigFileFields: monitoringv1.WebConfigFileFields{
|
|
|
|
|
TLSConfig: &monitoringv1.WebTLSConfig{
|
|
|
|
|
ClientCA: monitoringv1.SecretOrConfigMap{
|
|
|
|
|
Secret: &v1.SecretKeySelector{
|
|
|
|
|
LocalObjectReference: v1.LocalObjectReference{
|
|
|
|
|
Name: ServerCASecret,
|
|
|
|
|
},
|
|
|
|
|
Key: CAKey,
|
|
|
|
|
},
|
2022-03-21 20:26:36 +05:30
|
|
|
},
|
2024-01-29 17:19:20 +01:00
|
|
|
Cert: monitoringv1.SecretOrConfigMap{
|
|
|
|
|
Secret: &v1.SecretKeySelector{
|
|
|
|
|
LocalObjectReference: v1.LocalObjectReference{
|
|
|
|
|
Name: ServerTLSSecret,
|
|
|
|
|
},
|
|
|
|
|
Key: CertKey,
|
|
|
|
|
},
|
2022-03-21 20:26:36 +05:30
|
|
|
},
|
2024-01-29 17:19:20 +01:00
|
|
|
KeySecret: v1.SecretKeySelector{
|
|
|
|
|
LocalObjectReference: v1.LocalObjectReference{
|
|
|
|
|
Name: ServerTLSSecret,
|
|
|
|
|
},
|
|
|
|
|
Key: PrivateKey,
|
|
|
|
|
},
|
|
|
|
|
// Liveness/readiness probes don't work when using "RequireAndVerifyClientCert".
|
2024-12-20 11:48:16 +01:00
|
|
|
ClientAuthType: ptr.To("VerifyClientCertIfGiven"),
|
2022-03-21 20:26:36 +05:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2020-05-11 16:17:40 +03:00
|
|
|
}
|
|
|
|
|
|
2017-08-22 12:56:04 +02:00
|
|
|
func (f *Framework) AddAlertingToPrometheus(p *monitoringv1.Prometheus, ns, name string) {
|
2018-03-09 13:32:40 +01:00
|
|
|
p.Spec.Alerting = &monitoringv1.AlertingSpec{
|
2017-08-22 12:56:04 +02:00
|
|
|
Alertmanagers: []monitoringv1.AlertmanagerEndpoints{
|
2018-07-20 17:53:51 +02:00
|
|
|
{
|
2024-07-31 22:07:04 +05:30
|
|
|
Namespace: ptr.To(ns),
|
2017-01-18 19:07:22 +01:00
|
|
|
Name: fmt.Sprintf("alertmanager-%s", name),
|
2017-01-13 12:14:00 +01:00
|
|
|
Port: intstr.FromString("web"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-22 12:56:04 +02:00
|
|
|
func (f *Framework) MakeBasicServiceMonitor(name string) *monitoringv1.ServiceMonitor {
|
|
|
|
|
return &monitoringv1.ServiceMonitor{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: name,
|
|
|
|
|
Labels: map[string]string{
|
|
|
|
|
"group": name,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Spec: monitoringv1.ServiceMonitorSpec{
|
|
|
|
|
Selector: metav1.LabelSelector{
|
|
|
|
|
MatchLabels: map[string]string{
|
|
|
|
|
"group": name,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Endpoints: []monitoringv1.Endpoint{
|
2018-07-20 17:53:51 +02:00
|
|
|
{
|
2025-12-19 13:11:51 +01:00
|
|
|
Port: "web",
|
|
|
|
|
Interval: "30s",
|
2017-08-22 12:56:04 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 17:40:11 +02:00
|
|
|
func (f *Framework) MakeBasicPodMonitor(name string) *monitoringv1.PodMonitor {
|
|
|
|
|
return &monitoringv1.PodMonitor{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: name,
|
|
|
|
|
Labels: map[string]string{
|
|
|
|
|
"group": name,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Spec: monitoringv1.PodMonitorSpec{
|
|
|
|
|
Selector: metav1.LabelSelector{
|
|
|
|
|
MatchLabels: map[string]string{
|
|
|
|
|
"group": name,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
|
|
|
|
|
{
|
2024-11-29 21:12:17 +08:00
|
|
|
Port: ptr.To("web"),
|
2020-07-24 17:40:11 +02:00
|
|
|
Interval: "30s",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:45:42 +01:00
|
|
|
func (f *Framework) MakePrometheusService(name, group string, serviceType v1.ServiceType) *v1.Service {
|
|
|
|
|
service := &v1.Service{
|
2017-05-11 14:05:39 +02:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-01-18 19:07:22 +01:00
|
|
|
Name: fmt.Sprintf("prometheus-%s", name),
|
2017-01-13 12:14:00 +01:00
|
|
|
Labels: map[string]string{
|
|
|
|
|
"group": group,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Spec: v1.ServiceSpec{
|
2017-02-01 13:45:42 +01:00
|
|
|
Type: serviceType,
|
2017-01-13 12:14:00 +01:00
|
|
|
Ports: []v1.ServicePort{
|
2018-07-20 17:53:51 +02:00
|
|
|
{
|
2017-01-13 12:14:00 +01:00
|
|
|
Name: "web",
|
|
|
|
|
Port: 9090,
|
|
|
|
|
TargetPort: intstr.FromString("web"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Selector: map[string]string{
|
|
|
|
|
"prometheus": name,
|
|
|
|
|
},
|
2017-01-05 18:15:21 +01:00
|
|
|
},
|
|
|
|
|
}
|
2017-02-01 13:45:42 +01:00
|
|
|
return service
|
2017-01-05 18:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-14 18:47:15 +02:00
|
|
|
func (f *Framework) MakeThanosQuerierService(name string) *v1.Service {
|
|
|
|
|
service := &v1.Service{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: name,
|
|
|
|
|
},
|
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
|
Ports: []v1.ServicePort{
|
2018-07-20 17:53:51 +02:00
|
|
|
{
|
2025-05-05 16:28:43 +02:00
|
|
|
Name: "web",
|
2018-06-14 18:47:15 +02:00
|
|
|
Port: 10902,
|
|
|
|
|
TargetPort: intstr.FromString("http"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Selector: map[string]string{
|
2025-08-01 14:28:47 +02:00
|
|
|
operator.ApplicationNameLabelKey: "thanos-query",
|
2018-06-14 18:47:15 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return service
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) CreatePrometheusAndWaitUntilReady(ctx context.Context, ns string, p *monitoringv1.Prometheus) (*monitoringv1.Prometheus, error) {
|
|
|
|
|
result, err := f.MonClientV1.Prometheuses(ns).Create(ctx, p, metav1.CreateOptions{})
|
2017-01-05 18:15:21 +01:00
|
|
|
if err != nil {
|
2024-01-29 17:19:20 +01:00
|
|
|
return nil, fmt.Errorf("creating %d Prometheus instances failed (%v): %v", ptr.Deref(p.Spec.Replicas, 1), p.Name, err)
|
2017-01-05 18:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 11:34:55 -03:00
|
|
|
result, err = f.WaitForPrometheusReady(ctx, result, 5*time.Minute)
|
|
|
|
|
if err != nil {
|
2024-01-29 17:19:20 +01:00
|
|
|
return nil, fmt.Errorf("waiting for %d Prometheus instances timed out (%v): %v", ptr.Deref(p.Spec.Replicas, 1), p.Name, err)
|
2017-01-05 18:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-26 17:55:22 +01:00
|
|
|
return result, nil
|
2017-01-05 18:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 11:34:55 -03:00
|
|
|
func (f *Framework) UpdatePrometheusReplicasAndWaitUntilReady(ctx context.Context, name, ns string, replicas int32) (*monitoringv1.Prometheus, error) {
|
2022-04-13 16:08:56 +02:00
|
|
|
return f.PatchPrometheusAndWaitUntilReady(
|
|
|
|
|
ctx,
|
|
|
|
|
name,
|
|
|
|
|
ns,
|
|
|
|
|
monitoringv1.PrometheusSpec{
|
|
|
|
|
CommonPrometheusFields: monitoringv1.CommonPrometheusFields{
|
2024-01-08 17:11:27 -03:00
|
|
|
Replicas: ptr.To(replicas),
|
2022-04-13 16:08:56 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 11:34:55 -03:00
|
|
|
func (f *Framework) ScalePrometheusAndWaitUntilReady(ctx context.Context, name, ns string, shards int32) (*monitoringv1.Prometheus, error) {
|
|
|
|
|
promClient := f.MonClientV1.Prometheuses(ns)
|
|
|
|
|
scale, err := promClient.GetScale(ctx, name, metav1.GetOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to get Prometheus %s/%s scale: %w", ns, name, err)
|
|
|
|
|
}
|
|
|
|
|
scale.Spec.Replicas = shards
|
|
|
|
|
|
|
|
|
|
_, err = promClient.UpdateScale(ctx, name, scale, metav1.UpdateOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to scale Prometheus %s/%s: %w", ns, name, err)
|
|
|
|
|
}
|
|
|
|
|
p, err := promClient.Get(ctx, name, metav1.GetOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to get Prometheus %s/%s: %w", ns, name, err)
|
|
|
|
|
}
|
|
|
|
|
return f.WaitForPrometheusReady(ctx, p, 5*time.Minute)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 16:08:56 +02:00
|
|
|
func (f *Framework) PatchPrometheus(ctx context.Context, name, ns string, spec monitoringv1.PrometheusSpec) (*monitoringv1.Prometheus, error) {
|
|
|
|
|
b, err := json.Marshal(
|
|
|
|
|
&monitoringv1.Prometheus{
|
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
|
Kind: monitoringv1.PrometheusesKind,
|
|
|
|
|
APIVersion: schema.GroupVersion{Group: monitoring.GroupName, Version: monitoringv1.Version}.String(),
|
|
|
|
|
},
|
|
|
|
|
Spec: spec,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
2023-11-17 13:28:48 -03:00
|
|
|
return nil, fmt.Errorf("failed to marshal Prometheus spec: %w", err)
|
2022-04-13 16:08:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p, err := f.MonClientV1.Prometheuses(ns).Patch(
|
|
|
|
|
ctx,
|
|
|
|
|
name,
|
|
|
|
|
types.ApplyPatchType,
|
|
|
|
|
b,
|
|
|
|
|
metav1.PatchOptions{
|
2023-08-11 09:42:11 +02:00
|
|
|
Force: ptr.To(true),
|
2022-04-13 16:08:56 +02:00
|
|
|
FieldManager: "e2e-test",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2017-01-13 12:14:00 +01:00
|
|
|
if err != nil {
|
2018-11-26 17:55:22 +01:00
|
|
|
return nil, err
|
2017-01-13 12:14:00 +01:00
|
|
|
}
|
2022-04-13 16:08:56 +02:00
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *Framework) PatchPrometheusAndWaitUntilReady(ctx context.Context, name, ns string, spec monitoringv1.PrometheusSpec) (*monitoringv1.Prometheus, error) {
|
|
|
|
|
p, err := f.PatchPrometheus(ctx, name, ns, spec)
|
|
|
|
|
if err != nil {
|
2023-11-17 13:28:48 -03:00
|
|
|
return nil, fmt.Errorf("failed to patch Prometheus %s/%s: %w", ns, name, err)
|
2017-01-13 12:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 11:34:55 -03:00
|
|
|
p, err = f.WaitForPrometheusReady(ctx, p, 5*time.Minute)
|
|
|
|
|
if err != nil {
|
2022-04-13 16:08:56 +02:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p, nil
|
2017-01-13 12:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 11:34:55 -03:00
|
|
|
func (f *Framework) WaitForPrometheusReady(ctx context.Context, p *monitoringv1.Prometheus, timeout time.Duration) (*monitoringv1.Prometheus, error) {
|
2022-04-13 16:08:56 +02:00
|
|
|
expected := *p.Spec.Replicas
|
|
|
|
|
if p.Spec.Shards != nil && *p.Spec.Shards > 0 {
|
|
|
|
|
expected = expected * *p.Spec.Shards
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 11:34:55 -03:00
|
|
|
var current *monitoringv1.Prometheus
|
|
|
|
|
var getErr error
|
2023-01-03 16:08:32 +01:00
|
|
|
if err := f.WaitForResourceAvailable(
|
|
|
|
|
ctx,
|
2023-04-28 15:38:26 +02:00
|
|
|
func(ctx context.Context) (resourceStatus, error) {
|
2024-01-11 11:34:55 -03:00
|
|
|
current, getErr = f.MonClientV1.Prometheuses(p.Namespace).Get(ctx, p.Name, metav1.GetOptions{})
|
|
|
|
|
if getErr != nil {
|
|
|
|
|
return resourceStatus{}, getErr
|
2022-09-12 16:29:54 +02:00
|
|
|
}
|
2023-01-03 16:08:32 +01:00
|
|
|
return resourceStatus{
|
|
|
|
|
expectedReplicas: expected,
|
|
|
|
|
generation: current.Generation,
|
|
|
|
|
replicas: current.Status.UpdatedReplicas,
|
|
|
|
|
conditions: current.Status.Conditions,
|
|
|
|
|
}, nil
|
|
|
|
|
},
|
|
|
|
|
timeout,
|
|
|
|
|
); err != nil {
|
2024-01-11 11:34:55 -03:00
|
|
|
return nil, fmt.Errorf("prometheus %v/%v failed to become available: %w", p.Namespace, p.Name, err)
|
2022-04-13 16:08:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 11:34:55 -03:00
|
|
|
return current, nil
|
2017-05-03 11:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-01 14:28:47 +02:00
|
|
|
func listOptionsForPrometheus(name string) metav1.ListOptions {
|
|
|
|
|
return metav1.ListOptions{
|
|
|
|
|
LabelSelector: fields.SelectorFromSet(fields.Set(map[string]string{
|
|
|
|
|
operator.ApplicationNameLabelKey: "prometheus",
|
|
|
|
|
"prometheus": name,
|
|
|
|
|
})).String(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) DeletePrometheusAndWaitUntilGone(ctx context.Context, ns, name string) error {
|
|
|
|
|
_, err := f.MonClientV1.Prometheuses(ns).Get(ctx, name, metav1.GetOptions{})
|
2017-01-13 12:14:00 +01:00
|
|
|
if err != nil {
|
2023-11-17 13:28:48 -03:00
|
|
|
return fmt.Errorf("requesting Prometheus custom resource %v failed: %w", name, err)
|
2017-01-13 12:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
if err := f.MonClientV1.Prometheuses(ns).Delete(ctx, name, metav1.DeleteOptions{}); err != nil {
|
2023-11-17 13:28:48 -03:00
|
|
|
return fmt.Errorf("deleting Prometheus custom resource %v failed: %w", name, err)
|
2017-01-05 18:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-29 21:03:14 +05:30
|
|
|
if err := f.WaitForPodsReady(
|
2021-07-23 12:58:54 +05:30
|
|
|
ctx,
|
2017-05-04 11:14:31 +02:00
|
|
|
ns,
|
2017-04-18 11:24:58 +02:00
|
|
|
f.DefaultTimeout,
|
|
|
|
|
0,
|
2025-08-01 14:28:47 +02:00
|
|
|
listOptionsForPrometheus(name),
|
2017-04-18 11:24:58 +02:00
|
|
|
); err != nil {
|
2023-11-17 13:28:48 -03:00
|
|
|
return fmt.Errorf("waiting for Prometheus custom resource (%s) to vanish timed out: %w", name, err)
|
2017-01-05 18:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-01-13 12:14:00 +01:00
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) WaitForPrometheusRunImageAndReady(ctx context.Context, ns string, p *monitoringv1.Prometheus) error {
|
2025-08-01 14:28:47 +02:00
|
|
|
if err := f.WaitForPodsRunImage(
|
|
|
|
|
ctx,
|
|
|
|
|
ns,
|
|
|
|
|
int(*p.Spec.Replicas),
|
|
|
|
|
promImage(p.Spec.Version),
|
|
|
|
|
listOptionsForPrometheus(p.Name),
|
|
|
|
|
); err != nil {
|
2017-03-09 11:25:29 +01:00
|
|
|
return err
|
|
|
|
|
}
|
2025-08-01 14:28:47 +02:00
|
|
|
|
2021-06-29 21:03:14 +05:30
|
|
|
return f.WaitForPodsReady(
|
2021-07-23 12:58:54 +05:30
|
|
|
ctx,
|
2017-05-04 11:14:31 +02:00
|
|
|
ns,
|
2017-04-18 11:24:58 +02:00
|
|
|
f.DefaultTimeout,
|
|
|
|
|
int(*p.Spec.Replicas),
|
2025-08-01 14:28:47 +02:00
|
|
|
listOptionsForPrometheus(p.Name),
|
2017-04-18 11:24:58 +02:00
|
|
|
)
|
2017-03-09 11:25:29 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-13 12:14:00 +01:00
|
|
|
func promImage(version string) string {
|
|
|
|
|
return fmt.Sprintf("quay.io/prometheus/prometheus:%s", version)
|
|
|
|
|
}
|
2017-02-24 13:55:52 +01:00
|
|
|
|
2020-07-27 14:38:35 +02:00
|
|
|
// WaitForActiveTargets waits for a number of targets to be configured.
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) WaitForActiveTargets(ctx context.Context, ns, svcName string, amount int) error {
|
2017-02-24 13:55:52 +01:00
|
|
|
var targets []*Target
|
|
|
|
|
|
2023-04-28 15:38:26 +02:00
|
|
|
if err := wait.PollUntilContextTimeout(ctx, time.Second, time.Minute*5, false, func(ctx context.Context) (bool, error) {
|
2017-02-24 13:55:52 +01:00
|
|
|
var err error
|
2021-07-23 12:58:54 +05:30
|
|
|
targets, err = f.GetActiveTargets(ctx, ns, svcName)
|
2017-02-24 13:55:52 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(targets) == amount {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
|
}); err != nil {
|
2020-07-27 14:38:35 +02:00
|
|
|
return fmt.Errorf("waiting for active targets timed out. %v of %v active targets found. %v", len(targets), amount, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WaitForHealthyTargets waits for a number of targets to be configured and
|
|
|
|
|
// healthy.
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) WaitForHealthyTargets(ctx context.Context, ns, svcName string, amount int) error {
|
2022-04-13 16:08:56 +02:00
|
|
|
var loopErr error
|
2020-07-27 14:38:35 +02:00
|
|
|
|
2024-01-29 17:19:20 +01:00
|
|
|
err := wait.PollUntilContextTimeout(ctx, time.Second, time.Minute*1, true, func(ctx context.Context) (bool, error) {
|
2022-04-13 16:08:56 +02:00
|
|
|
var targets []*Target
|
|
|
|
|
targets, loopErr = f.GetHealthyTargets(ctx, ns, svcName)
|
|
|
|
|
if loopErr != nil {
|
|
|
|
|
return false, nil
|
2020-07-27 14:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(targets) == amount {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 13:28:48 -03:00
|
|
|
loopErr = fmt.Errorf("expected %d, found %d healthy targets", amount, len(targets))
|
2020-07-27 14:38:35 +02:00
|
|
|
return false, nil
|
2022-04-13 16:08:56 +02:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2024-01-29 17:19:20 +01:00
|
|
|
return fmt.Errorf("%s: waiting for healthy targets failed: %v: %v", svcName, err, loopErr)
|
2017-02-24 13:55:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) WaitForDiscoveryWorking(ctx context.Context, ns, svcName, prometheusName string) error {
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
var loopErr error
|
|
|
|
|
|
2023-04-28 15:38:26 +02:00
|
|
|
err := wait.PollUntilContextTimeout(ctx, time.Second, 5*f.DefaultTimeout, false, func(ctx context.Context) (bool, error) {
|
2025-08-01 14:28:47 +02:00
|
|
|
pods, loopErr := f.KubeClient.CoreV1().Pods(ns).List(ctx, listOptionsForPrometheus(prometheusName))
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
if loopErr != nil {
|
|
|
|
|
return false, loopErr
|
|
|
|
|
}
|
2025-03-31 15:51:44 +08:00
|
|
|
if len(pods.Items) != 1 {
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
podIP := pods.Items[0].Status.PodIP
|
|
|
|
|
expectedTargets := []string{fmt.Sprintf("http://%s:9090/metrics", podIP)}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
activeTargets, loopErr := f.GetActiveTargets(ctx, ns, svcName)
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
if loopErr != nil {
|
|
|
|
|
return false, loopErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if loopErr = assertExpectedTargets(activeTargets, expectedTargets); loopErr != nil {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
working, loopErr := f.basicQueryWorking(ctx, ns, svcName)
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
if loopErr != nil {
|
|
|
|
|
return false, loopErr
|
|
|
|
|
}
|
|
|
|
|
if !working {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("waiting for Prometheus to discover targets failed: %v: %v", err, loopErr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) basicQueryWorking(ctx context.Context, ns, svcName string) (bool, error) {
|
2022-03-21 20:26:36 +05:30
|
|
|
response, err := f.PrometheusSVCGetRequest(ctx, ns, svcName, "http", "/api/v1/query", map[string]string{"query": "up"})
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rq := PrometheusQueryAPIResponse{}
|
|
|
|
|
if err := json.NewDecoder(bytes.NewBuffer(response)).Decode(&rq); err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if rq.Status != "success" && rq.Data.Result[0].Value[1] == "1" {
|
|
|
|
|
fmt.Printf("Query Response not successful.")
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertExpectedTargets(targets []*Target, expectedTargets []string) error {
|
|
|
|
|
existingTargets := []string{}
|
|
|
|
|
|
|
|
|
|
for _, t := range targets {
|
|
|
|
|
existingTargets = append(existingTargets, t.ScrapeURL)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 16:28:38 +08:00
|
|
|
slices.Sort(expectedTargets)
|
|
|
|
|
slices.Sort(existingTargets)
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedTargets, existingTargets) {
|
|
|
|
|
return fmt.Errorf(
|
|
|
|
|
"expected targets %q but got %q", strings.Join(expectedTargets, ","),
|
|
|
|
|
strings.Join(existingTargets, ","),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 20:26:36 +05:30
|
|
|
func (f *Framework) PrometheusSVCGetRequest(ctx context.Context, ns, svcName, scheme, endpoint string, query map[string]string) ([]byte, error) {
|
2017-07-13 14:20:27 +02:00
|
|
|
ProxyGet := f.KubeClient.CoreV1().Services(ns).ProxyGet
|
2022-03-21 20:26:36 +05:30
|
|
|
request := ProxyGet(scheme, svcName, "web", endpoint, query)
|
2021-07-23 12:58:54 +05:30
|
|
|
return request.DoRaw(ctx)
|
2017-07-13 15:20:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) GetActiveTargets(ctx context.Context, ns, svcName string) ([]*Target, error) {
|
2022-03-21 20:26:36 +05:30
|
|
|
response, err := f.PrometheusSVCGetRequest(ctx, ns, svcName, "http", "/api/v1/targets", map[string]string{})
|
2017-02-24 13:55:52 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rt := prometheusTargetAPIResponse{}
|
2017-11-30 16:37:20 +01:00
|
|
|
if err := json.NewDecoder(bytes.NewBuffer(response)).Decode(&rt); err != nil {
|
2017-02-24 13:55:52 +01:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 14:38:35 +02:00
|
|
|
return rt.Data.ActiveTargets, nil
|
2020-07-24 14:32:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) GetHealthyTargets(ctx context.Context, ns, svcName string) ([]*Target, error) {
|
|
|
|
|
targets, err := f.GetActiveTargets(ctx, ns, svcName)
|
2020-07-27 14:38:35 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-07-24 14:32:22 +02:00
|
|
|
|
2020-07-27 14:38:35 +02:00
|
|
|
healthyTargets := make([]*Target, 0, len(targets))
|
2020-07-24 14:32:22 +02:00
|
|
|
for _, target := range targets {
|
|
|
|
|
switch target.Health {
|
|
|
|
|
case healthGood:
|
|
|
|
|
healthyTargets = append(healthyTargets, target)
|
|
|
|
|
case healthBad:
|
2023-11-17 13:28:48 -03:00
|
|
|
return nil, fmt.Errorf("target %q: %s", target.ScrapeURL, target.LastError)
|
2020-07-24 14:32:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return healthyTargets, nil
|
2017-02-24 13:55:52 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 16:11:32 +02:00
|
|
|
// GetPrometheusFiringAlerts returns a slice of alert labels matching the given alert name.
|
|
|
|
|
func (f *Framework) GetPrometheusFiringAlerts(ctx context.Context, ns, svcName, alertName string) ([]map[string]string, error) {
|
|
|
|
|
response, err := f.PrometheusSVCGetRequest(
|
|
|
|
|
ctx,
|
|
|
|
|
ns,
|
|
|
|
|
svcName,
|
|
|
|
|
"http",
|
|
|
|
|
"/api/v1/query",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"query": fmt.Sprintf(`ALERTS{alertname="%v",alertstate="firing"}`, alertName),
|
|
|
|
|
},
|
|
|
|
|
)
|
2018-04-12 21:42:44 +02:00
|
|
|
if err != nil {
|
2025-04-11 16:11:32 +02:00
|
|
|
return nil, err
|
2018-04-12 21:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
q := PrometheusQueryAPIResponse{}
|
2018-04-12 21:42:44 +02:00
|
|
|
if err := json.NewDecoder(bytes.NewBuffer(response)).Decode(&q); err != nil {
|
2025-04-11 16:11:32 +02:00
|
|
|
return nil, err
|
2018-04-12 21:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 16:11:32 +02:00
|
|
|
alerts := make([]map[string]string, len(q.Data.Result))
|
|
|
|
|
for i, res := range q.Data.Result {
|
|
|
|
|
alerts[i] = res.Metric
|
2018-04-12 21:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 16:11:32 +02:00
|
|
|
return alerts, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *Framework) CheckPrometheusFiringAlert(ctx context.Context, ns, svcName, alertName string) error {
|
|
|
|
|
alerts, err := f.GetPrometheusFiringAlerts(ctx, ns, svcName, alertName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(alerts) != 1 {
|
|
|
|
|
return fmt.Errorf("expected 1 query result but got %v", len(alerts))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2018-04-12 21:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-21 20:26:36 +05:30
|
|
|
func (f *Framework) PrometheusQuery(ns, svcName, scheme, query string) ([]PrometheusQueryResult, error) {
|
|
|
|
|
response, err := f.PrometheusSVCGetRequest(context.Background(), ns, svcName, scheme, "/api/v1/query", map[string]string{"query": query})
|
2021-09-13 15:43:09 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
q := PrometheusQueryAPIResponse{}
|
|
|
|
|
if err := json.NewDecoder(bytes.NewBuffer(response)).Decode(&q); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if q.Status != "success" {
|
|
|
|
|
return nil, fmt.Errorf("expecting status to be 'success', got %q instead", q.Status)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return q.Data.Result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:58:54 +05:30
|
|
|
func (f *Framework) WaitForPrometheusFiringAlert(ctx context.Context, ns, svcName, alertName string) error {
|
2018-04-12 21:42:44 +02:00
|
|
|
var loopError error
|
|
|
|
|
|
2025-04-11 16:11:32 +02:00
|
|
|
err := wait.PollUntilContextTimeout(ctx, time.Second, 5*f.DefaultTimeout, true, func(_ context.Context) (bool, error) {
|
|
|
|
|
loopError = f.CheckPrometheusFiringAlert(context.Background(), ns, svcName, alertName)
|
|
|
|
|
if loopError != nil {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
2018-04-12 21:42:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2023-11-17 13:28:48 -03:00
|
|
|
return fmt.Errorf(
|
2018-05-08 09:43:45 +02:00
|
|
|
"waiting for alert '%v' to fire: %v: %v",
|
|
|
|
|
alertName,
|
2021-04-28 14:23:52 +02:00
|
|
|
err,
|
|
|
|
|
loopError,
|
2018-05-08 09:43:45 +02:00
|
|
|
)
|
2018-04-12 21:42:44 +02:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 14:32:22 +02:00
|
|
|
type targetHealth string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
healthGood targetHealth = "up"
|
|
|
|
|
healthBad targetHealth = "down"
|
|
|
|
|
)
|
|
|
|
|
|
2017-02-24 13:55:52 +01:00
|
|
|
type Target struct {
|
2019-09-24 14:41:03 +02:00
|
|
|
ScrapeURL string `json:"scrapeUrl"`
|
|
|
|
|
Labels map[string]string `json:"labels"`
|
2020-07-24 14:32:22 +02:00
|
|
|
LastError string `json:"lastError"`
|
|
|
|
|
Health targetHealth `json:"health"`
|
2017-02-24 13:55:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type targetDiscovery struct {
|
|
|
|
|
ActiveTargets []*Target `json:"activeTargets"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type prometheusTargetAPIResponse struct {
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
Data *targetDiscovery `json:"data"`
|
|
|
|
|
}
|
2018-04-12 21:42:44 +02:00
|
|
|
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
type PrometheusQueryResult struct {
|
2018-04-12 21:42:44 +02:00
|
|
|
Metric map[string]string `json:"metric"`
|
2025-09-01 10:33:41 +01:00
|
|
|
Value []any `json:"value"`
|
2018-04-12 21:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
type PrometheusQueryData struct {
|
|
|
|
|
ResultType string `json:"resultType"`
|
|
|
|
|
Result []PrometheusQueryResult `json:"result"`
|
2018-04-12 21:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
type PrometheusQueryAPIResponse struct {
|
2018-04-12 21:42:44 +02:00
|
|
|
Status string `json:"status"`
|
pkg/prometheus: Enable users to configure bearer token from secret
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.
In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
pkg/prometheus: Add option to deny file system access through service monitors
ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
test/basic-auth-test-app: Add mTLS endpoint
pkg/prometheus: Enable users to configure tls from secret
pkg/prometheus/operator: Validate TLS configs before retrieving assets
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
test: Rename basic-auth-test-app to instrumented-sample-app
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.
test/e2e/prometheus_test: Test ArbitraryFSAccessThroughSM option for tls
The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
pkg/prometheus/operator: Move check for arbitrary fs access into func
2019-03-18 15:56:38 +01:00
|
|
|
Data *PrometheusQueryData `json:"data"`
|
2018-04-12 21:42:44 +02:00
|
|
|
}
|