From d5aa012fd01318c7319499cf40f783d2c3adff06 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Fri, 15 May 2015 12:47:50 +0200 Subject: [PATCH] Make HTTP basic auth configurable for scrape targets. --- config/config.go | 10 +++++++++- config/config_test.go | 6 +++++- config/testdata/conf.good.yml | 6 +++++- retrieval/target.go | 3 +++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 87fe01595a..17137bb674 100644 --- a/config/config.go +++ b/config/config.go @@ -182,6 +182,8 @@ type DefaultedScrapeConfig struct { MetricsPath string `yaml:"metrics_path,omitempty"` // The URL scheme with which to fetch metrics from targets. Scheme string `yaml:"scheme,omitempty"` + // The HTTP basic authentication credentials for the targets. + BasicAuth *BasicAuth `yaml:"basic_auth"` // List of labeled target groups for this job. TargetGroups []*TargetGroup `yaml:"target_groups,omitempty"` @@ -191,7 +193,13 @@ type DefaultedScrapeConfig struct { RelabelConfigs []*RelabelConfig `yaml:"relabel_configs,omitempty"` } -// A labeled group of targets to scrape for a job. +// BasicAuth contains basic HTTP authentication credentials. +type BasicAuth struct { + Username string `yaml:"username"` + Password string `yaml:"password"` +} + +// TargetGroup is a set of targets with a common label set. type TargetGroup struct { // Targets is a list of targets identified by a label set. Each target is // uniquely identifiable in the group by its address label. diff --git a/config/config_test.go b/config/config_test.go index 792ad83c12..0b7aabd43a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -69,8 +69,12 @@ var expectedConf = &Config{DefaultedConfig{ ScrapeInterval: Duration(50 * time.Second), ScrapeTimeout: Duration(5 * time.Second), + BasicAuth: &BasicAuth{ + Username: "admin", + Password: "password", + }, MetricsPath: "/my_path", - Scheme: "http", + Scheme: "https", DNSSDConfigs: []*DNSSDConfig{ {DefaultedDNSSDConfig{ diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index a1ff4c192d..943901edff 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -40,11 +40,15 @@ scrape_configs: - job_name: service-x + basic_auth: + username: admin + password: password + scrape_interval: 50s scrape_timeout: 5s metrics_path: /my_path - # scheme defaults to 'http'. + scheme: https dns_sd_configs: - refresh_interval: 15s diff --git a/retrieval/target.go b/retrieval/target.go index 73ce5faf7c..2f40ec132f 100644 --- a/retrieval/target.go +++ b/retrieval/target.go @@ -192,6 +192,9 @@ func (t *target) Update(cfg *config.ScrapeConfig, baseLabels clientmodel.LabelSe t.url.Scheme = cfg.Scheme t.url.Path = string(baseLabels[clientmodel.MetricsPathLabel]) + if cfg.BasicAuth != nil { + t.url.User = url.UserPassword(cfg.BasicAuth.Username, cfg.BasicAuth.Password) + } t.scrapeInterval = time.Duration(cfg.ScrapeInterval) t.deadline = time.Duration(cfg.ScrapeTimeout)