Skip to content

Commit

Permalink
Add CORS header support to promxy
Browse files Browse the repository at this point in the history
This adds the same CORS support that upstream prometheus has.

NOTE: this changes the default to send the CORS headers (to match upstream prometheus); if you wish to have the previous behavior set `web.cors.origin` = ""

Fixes #344
  • Loading branch information
jacksontj committed Oct 6, 2020
1 parent 9ef7b37 commit 2b1fd3f
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cmd/promxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"path"
"regexp"

"k8s.io/klog"

Expand Down Expand Up @@ -38,6 +39,7 @@ import (
"github.com/prometheus/prometheus/discovery"
sd_config "github.com/prometheus/prometheus/discovery/config"
"github.com/prometheus/prometheus/notifier"
"github.com/prometheus/prometheus/pkg/relabel"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/rules"
"github.com/prometheus/prometheus/scrape"
Expand Down Expand Up @@ -69,7 +71,8 @@ type cliOpts struct {
LogLevel string `long:"log-level" description:"Log level" default:"info"`
LogFormat string `long:"log-format" description:"Log format(text|json)" default:"text"`

WebReadTimeout time.Duration `long:"web.read-timeout" description:"Maximum duration before timing out read of the request, and closing idle connections." default:"5m"`
WebCORSOriginRegex string `long:"web.cors.origin" description:"Regex for CORS origin. It is fully anchored." default:".*"`
WebReadTimeout time.Duration `long:"web.read-timeout" description:"Maximum duration before timing out read of the request, and closing idle connections." default:"5m"`

ExternalURL string `long:"web.external-url" description:"The URL under which Prometheus is externally reachable (for example, if Prometheus is served via a reverse proxy). Used for generating relative and absolute links back to Prometheus itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Prometheus. If omitted, relevant URL components will be derived automatically."`
EnableLifecycle bool `long:"web.enable-lifecycle" description:"Enable shutdown and reload via HTTP request."`
Expand Down Expand Up @@ -336,6 +339,11 @@ func main() {
},
}

webOptions.CORSOrigin, err = compileCORSRegexString(opts.WebCORSOriginRegex)
if err != nil {
logrus.Fatalf("Erorr parsing CORS regex: %v", err)
}

if externalUrl != nil && externalUrl.Path != "" {
webOptions.RoutePrefix = externalUrl.Path
}
Expand Down Expand Up @@ -532,3 +540,12 @@ func computeExternalURL(u, listenAddr string) (*url.URL, error) {

return eu, nil
}

// compileCORSRegexString compiles given string and adds anchors
func compileCORSRegexString(s string) (*regexp.Regexp, error) {
r, err := relabel.NewRegexp(s)
if err != nil {
return nil, err
}
return r.Regexp, nil
}

0 comments on commit 2b1fd3f

Please sign in to comment.