From 6bcb791bc525bfbd7e7a49aba826a6a9375b1447 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sat, 10 Apr 2021 02:44:40 -0500 Subject: [PATCH] fix ping path from docker labels, clarify docs about path (not url) for the destination --- README.md | 6 +++--- app/discovery/discovery.go | 2 ++ app/discovery/provider/docker.go | 2 +- app/discovery/provider/docker_test.go | 4 ++-- docker-compose.yml | 8 ++++++++ 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d91afd1..fed9f69 100644 --- a/README.md +++ b/README.md @@ -58,14 +58,14 @@ This is a dynamic provider and file change will be applied automatically. ### Docker -Docker provider works with no extra configuration and by default redirects all requests like `https://server/api//(.*)` to the internal IP of given container and the exposed port. Only active (running) containers will be detected. +Docker provider works with no extra configuration and by default redirects all requests like `https://server/api//(.*)` to the internal IP of the given container and the exposed port. Only active (running) containers will be detected. This default can be changed with labels: - `reproxy.server` - server (hostname) to match - `reproxy.route` - source route (location) -- `reproxy.dest` - destination URL -- `reproxy.ping` - ping url for the destination container +- `reproxy.dest` - destination path. Note: this is not full url, but just the path which will be appended to container's ip:port +- `reproxy.ping` - ping path for the destination container. By default all containers with exposed port will be considered as routing destinations. There are 2 ways to restrict it: diff --git a/app/discovery/discovery.go b/app/discovery/discovery.go index 77bf7f9..a1493e4 100644 --- a/app/discovery/discovery.go +++ b/app/discovery/discovery.go @@ -137,6 +137,8 @@ func (s *Service) mergeLists() (res []URLMapper) { func (s *Service) extendRule(m URLMapper) URLMapper { src := m.SrcMatch.String() + + // TODO: Probably should be ok in practice but we better figure a nicer way to do it if strings.Contains(m.Dst, "$1") || strings.Contains(src, "(") || !strings.HasSuffix(src, "/") { return m } diff --git a/app/discovery/provider/docker.go b/app/discovery/provider/docker.go index e55dcb8..d7cf817 100644 --- a/app/discovery/provider/docker.go +++ b/app/discovery/provider/docker.go @@ -88,7 +88,7 @@ func (d *Docker) List() ([]discovery.URLMapper, error) { srcRegex, err := regexp.Compile(srcURL) if v, ok := c.Labels["reproxy.ping"]; ok { - pingURL = v + pingURL = fmt.Sprintf("http://%s:%d%s", c.IP, c.Port, v) } if err != nil { diff --git a/app/discovery/provider/docker_test.go b/app/discovery/provider/docker_test.go index e5da43d..bed5847 100644 --- a/app/discovery/provider/docker_test.go +++ b/app/discovery/provider/docker_test.go @@ -22,7 +22,7 @@ func TestDocker_List(t *testing.T) { {PrivatePort: 12345}, }, Labels: map[string]string{"reproxy.route": "^/api/123/(.*)", "reproxy.dest": "/blah/$1", - "reproxy.server": "example.com", "reproxy.ping": "http://localhost/ping"}, + "reproxy.server": "example.com", "reproxy.ping": "/ping"}, }, {Names: []string{"c2"}, State: "running", Networks: dc.NetworkList{ @@ -53,7 +53,7 @@ func TestDocker_List(t *testing.T) { assert.Equal(t, "^/api/123/(.*)", res[0].SrcMatch.String()) assert.Equal(t, "http://127.0.0.2:12345/blah/$1", res[0].Dst) assert.Equal(t, "example.com", res[0].Server) - assert.Equal(t, "http://localhost/ping", res[0].PingURL) + assert.Equal(t, "http://127.0.0.2:12345/ping", res[0].PingURL) assert.Equal(t, "^/api/c2/(.*)", res[1].SrcMatch.String()) assert.Equal(t, "http://127.0.0.3:12346/$1", res[1].Dst) diff --git a/docker-compose.yml b/docker-compose.yml index 3436acb..8d7141b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -28,3 +28,11 @@ services: ports: - "9092" command: python3 -m http.server 9092 + + whoami: + image: 'containous/whoami' + container_name: whoami + labels: + reproxy.server: '*' + reproxy.route: '^/whoami/(.*)' + reproxy.dest: '/$$1' \ No newline at end of file