-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[metricbeat] [helper] Fix http server helper SSL config #39405
Conversation
This pull request does not have a backport label.
To fixup this pull request, you need to add the backport labels for the needed
|
How I tested this:Prerequisites:
Generate certificates:openssl.cnf
gencerts.sh
mkdir -p certs
OPENSSL_SUBJ="/C=US/ST=California/L=Santa Clara"
OPENSSL_CA="${OPENSSL_SUBJ}/CN=fake-CA"
OPENSSL_SERVER="${OPENSSL_SUBJ}/CN=fake-server"
sh ./genroot.sh "${OPENSSL_CA}"
sh ./genserver.sh "${OPENSSL_SERVER}" genroot.sh
OPENSSL_ROOT_CA=$1
docker run --rm -v $PWD/certs:/certs -it nginx \
openssl genrsa 2048 > certs/root-ca-key.pem
docker run --rm -v $PWD/certs:/certs -it nginx \
openssl req -new -x509 -nodes -days 3600 \
-subj "${OPENSSL_ROOT_CA}" \
-key /certs/root-ca-key.pem -out /certs/root-ca.pem genserver.sh
OPENSSL_SERVER=$1
docker run --rm -v $PWD/certs:/certs -v $PWD/openssl.cnf:/openssl.cnf -it nginx \
openssl req -newkey rsa:2048 -days 3600 -nodes \
-subj "${OPENSSL_SERVER}" \
-keyout /certs/server-key.pem -out /certs/server-req.pem \
-config /openssl.cnf
docker run --rm -v $PWD/certs:/certs -it nginx \
openssl rsa -in /certs/server-key.pem -out /certs/server-key.pem
docker run --rm -v $PWD/certs:/certs -v $PWD/openssl.cnf:/openssl.cnf -it nginx \
openssl x509 -req -in /certs/server-req.pem -days 3600 \
-CA /certs/root-ca.pem -CAkey /certs/root-ca-key.pem \
-set_serial 01 -out /certs/server-cert.pem \
-extensions v3_req -extfile /openssl.cnf
docker run --rm -v $PWD/certs:/certs -it nginx \
openssl verify -CAfile /certs/root-ca.pem /certs/server-cert.pem Execute Prometheus:Run it with the config below. prometheus.yml
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
scrape_configs:
- job_name: 'fake'
static_configs:
- targets: ['localhost:8080']
remote_write:
- url: "https://0.0.0.0:9201/write"
tls_config:
ca_file: /path/to/certs/root-ca.pem
#cert_file: /path/to/certs/server-cert.pem
#key_file: /path/to/certs/server-key.pem Golang app to create some fake prometheus metrics: main.go
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
fakeMetric = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "fake_metric_total",
Help: "This is a fake metric.",
},
)
)
func init() {
prometheus.MustRegister(fakeMetric)
}
func main() {
http.HandleFunc("/increment", func(w http.ResponseWriter, r *http.Request) {
fakeMetric.Inc()
})
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
} Run the go app and make a few curl requests MetricbeatStart metricbeat with this - module: prometheus
metricsets: ["remote_write"]
host: "localhost"
port: "9201"
ssl.enabled: true
# ssl.certificate_authorities: ["/path/to/certs/root-ca.pem"]
ssl.certificate: "/path/to/certs/server-cert.pem"
ssl.key: "/path/to/certs/server-key.pem" In prometheus logs you should see an error like this:
SSL connection to the server started by the |
This pull request is now in conflicts. Could you fix it? 🙏
|
Pinging @elastic/obs-ds-hosted-services (Team:obs-ds-hosted-services) |
f241a0b
to
8e58038
Compare
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.
@gpop63, the changes look good. Could you add a test?
This pull request is now in conflicts. Could you fix it? 🙏
|
Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane) |
Co-authored-by: Tiago Queiroz <me@tiago.life>
|
* add changelog entry * fix TLS config * fix changelog pr id * golangci-lint fixes * mage check * fix http server ssl test * Update metricbeat/helper/server/http/http_test.go Co-authored-by: Tiago Queiroz <me@tiago.life> * fix changelog --------- Co-authored-by: Tiago Queiroz <me@tiago.life> (cherry picked from commit 6d4fbfc)
) * add changelog entry * fix TLS config * fix changelog pr id * golangci-lint fixes * mage check * fix http server ssl test * Update metricbeat/helper/server/http/http_test.go Co-authored-by: Tiago Queiroz <me@tiago.life> * fix changelog --------- Co-authored-by: Tiago Queiroz <me@tiago.life> (cherry picked from commit 6d4fbfc) Co-authored-by: Gabriel Pop <94497545+gpop63@users.noreply.github.com>
Overview
The TLS config used in the metricbeat http server module uses a TLS config suitable for clients (
BuildModuleClientConfig
), not for servers. This makes it impossible to successfully connect to Prometheusremote_write
using TLS sincebad certificate
is always returned.Made some small unrelated changes to make
golangci-lint
happy. Added a reasonableReadHeaderTimeout
of 10 seconds. Replacedioutil
withio
.This was tested with
openssl s_client
and also directly with theremote_write
data stream.The
NewHttpServer
is also used by thehttp
module.Checklist
CHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.Disruptive User Impact
Author's Checklist
How to test this PR locally
Related issues
Use cases
Screenshots
Logs