Skip to content
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

Support ping endpoint #456

Merged
merged 6 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ log_debug: <bool> | default = false [optional]
# Whether to ignore security warnings
hack_me_please: <bool> | default = false [optional]

# Allow ping server
allow_ping: <bool> | default = false [optional]

# Named list of cache configurations
caches:
- <cache_config> ...
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type Config struct {

ConnectionPool ConnectionPool `yaml:"connection_pool,omitempty"`

// Allow to proxy ping requests
AllowPing bool `yaml:"allow_ping,omitempty"`

networkReg map[string]Networks

// Catches all undefined fields
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"golang.org/x/crypto/acme/autocert"
)

const pingEndpoint string = "/ping"

var (
configFile = flag.String("config", "", "Proxy configuration filename")
version = flag.Bool("version", false, "Prints current version and exits")
Expand All @@ -35,6 +37,7 @@ var (
allowedNetworksHTTPS atomic.Value
allowedNetworksMetrics atomic.Value
proxyHandler atomic.Value
allowPing atomic.Bool
)

func main() {
Expand Down Expand Up @@ -237,8 +240,15 @@ func serveHTTP(rw http.ResponseWriter, r *http.Request) {
}
proxy.refreshCacheMetrics()
promHandler.ServeHTTP(rw, r)
case "/", "/query":
case "/", "/query", pingEndpoint:
var err error

if r.URL.Path == pingEndpoint && !allowPing.Load() {
err = fmt.Errorf("ping is not allowed")
respondWith(rw, err, http.StatusForbidden)
return
}

// nolint:forcetypeassert // We will cover this by tests as we control what is stored.
proxyHandler := proxyHandler.Load().(*ProxyHandler)
r.RemoteAddr = proxyHandler.GetRemoteAddr(r)
Expand Down Expand Up @@ -296,6 +306,7 @@ func applyConfig(cfg *config.Config) error {
allowedNetworksHTTPS.Store(&cfg.Server.HTTPS.AllowedNetworks)
allowedNetworksMetrics.Store(&cfg.Server.Metrics.AllowedNetworks)
proxyHandler.Store(NewProxyHandler(&cfg.Server.Proxy))
allowPing.Store(cfg.AllowPing)
log.SetDebug(cfg.LogDebug)
log.Infof("Loaded config:\n%s", cfg)

Expand Down
38 changes: 38 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,44 @@ func TestServe(t *testing.T) {
},
startTLS,
},
{
"http ping request",
"testdata/http.ping.yml",
func(t *testing.T) {
httpGet(t, "http://127.0.0.1:9090/ping", http.StatusOK)
},
startHTTP,
},
{
"https ping request",
"testdata/https.ping.yml",
func(t *testing.T) {
req, err := http.NewRequest("GET", "https://127.0.0.1:9090/ping", nil)
checkErr(t, err)
req.Close = true
req.SetBasicAuth("default", "qwerty")

resp, err := tlsClient.Do(req)
checkErr(t, err)
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d; expected: %d", resp.StatusCode, http.StatusOK)
}
},
startTLS,
},
{
"http ping request is not allowed",
"testdata/http.yml",
func(t *testing.T) {
resp := httpGet(t, "http://127.0.0.1:9090/ping", http.StatusForbidden)
expected := "ping is not allowed\n"
checkResponse(t, resp.Body, expected)
resp.Body.Close()
},
startHTTP,
},
}

// Wait until CHServer starts.
Expand Down
7 changes: 7 additions & 0 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ func (s *scope) decorateRequest(req *http.Request) (*http.Request, url.Values) {
// Make new params to purify URL.
params := make(url.Values)

// pass ping request
if req.RequestURI == pingEndpoint {
req.URL.Scheme = s.host.Scheme()
req.URL.Host = s.host.Host()
return req, req.URL.Query()
}

// Set user params
if s.user.params != nil {
for _, param := range s.user.params.params {
Expand Down
15 changes: 15 additions & 0 deletions testdata/http.ping.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
allow_ping: true

server:
http:
listen_addr: ":9090"
allowed_networks: ["127.0.0.1/24"]

users:
- name: "default"
to_cluster: "default"
to_user: "default"

clusters:
- name: "default"
nodes: ["127.0.0.1:18124"]
17 changes: 17 additions & 0 deletions testdata/https.ping.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
allow_ping: true

server:
https:
listen_addr: ":9090"
cert_file: "testdata/example.com.cert"
key_file: "testdata/example.com.key"

users:
- name: "default"
password: "qwerty"
to_cluster: "default"
to_user: "default"

clusters:
- name: "default"
nodes: ["127.0.0.1:18124"]
Loading