Skip to content

Commit

Permalink
Load backends from env vars
Browse files Browse the repository at this point in the history
As we only have a small number of backends that change infrequently,
their URL configuration can be handled by environment variables. This
allows us to remove a dependency from using the Mongo Database to fetch
this information and allows us to using the Content Store for route
information.

Router will use any environment variable with the prefix "BACKEND_URL_",
where the key suffix is the backend id and the value is the URL.
  • Loading branch information
theseanything committed Oct 29, 2024
1 parent 2e101c4 commit 7a11d55
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 116 deletions.
22 changes: 14 additions & 8 deletions integration_tests/performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"net/http/httptest"
"os"
"time"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -22,15 +23,15 @@ var _ = Describe("Performance", func() {
BeforeEach(func() {
backend1 = startSimpleBackend("backend 1")
backend2 = startSimpleBackend("backend 2")
addBackend("backend-1", backend1.URL)
addBackend("backend-2", backend2.URL)
os.Setenv("BACKEND_URL_backend-1", backend1.URL)
os.Setenv("BACKEND_URL_backend-2", backend2.URL)
addRoute("/one", NewBackendRoute("backend-1"))
addRoute("/two", NewBackendRoute("backend-2"))
reloadRoutes(apiPort)
})
AfterEach(func() {
backend1.Close()
backend2.Close()
os.Unsetenv("BACKEND_URL_backend-1")
os.Unsetenv("BACKEND_URL_backend-2")
})

It("Router should not cause errors or much latency", func() {
Expand Down Expand Up @@ -60,7 +61,8 @@ var _ = Describe("Performance", func() {
It("Router should not cause errors or much latency", func() {
slowBackend := startTarpitBackend(time.Second)
defer slowBackend.Close()
addBackend("backend-slow", slowBackend.URL)
os.Setenv("BACKEND_URL_backend-slow", slowBackend.URL)
defer os.Unsetenv("BACKEND_URL_backend-slow")
addRoute("/slow", NewBackendRoute("backend-slow"))
reloadRoutes(apiPort)

Expand All @@ -73,7 +75,9 @@ var _ = Describe("Performance", func() {

Describe("with one downed backend hit separately", func() {
It("Router should not cause errors or much latency", func() {
addBackend("backend-down", "http://127.0.0.1:3162/")
os.Setenv("BACKEND_URL_backend-down", "http://127.0.0.1:3162/")
defer os.Unsetenv("BACKEND_URL_backend-down")

addRoute("/down", NewBackendRoute("backend-down"))
reloadRoutes(apiPort)

Expand All @@ -98,13 +102,15 @@ var _ = Describe("Performance", func() {
BeforeEach(func() {
backend1 = startTarpitBackend(time.Second)
backend2 = startTarpitBackend(time.Second)
addBackend("backend-1", backend1.URL)
addBackend("backend-2", backend2.URL)
os.Setenv("BACKEND_URL_backend-1", backend1.URL)
os.Setenv("BACKEND_URL_backend-2", backend2.URL)
addRoute("/one", NewBackendRoute("backend-1"))
addRoute("/two", NewBackendRoute("backend-2"))
reloadRoutes(apiPort)
})
AfterEach(func() {
os.Unsetenv("BACKEND_URL_backend-1")
os.Unsetenv("BACKEND_URL_backend-2")
backend1.Close()
backend2.Close()
})
Expand Down
30 changes: 21 additions & 9 deletions integration_tests/proxy_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http/httptest"
"net/textproto"
"net/url"
"os"
"strings"
"time"

Expand All @@ -19,7 +20,9 @@ var _ = Describe("Functioning as a reverse proxy", func() {

Describe("connecting to the backend", func() {
It("should return a 502 if the connection to the backend is refused", func() {
addBackend("not-running", "http://127.0.0.1:3164/")
os.Setenv("BACKEND_URL_not-running", "http://127.0.0.1:3164/")
defer os.Unsetenv("BACKEND_URL_not-running")

addRoute("/not-running", NewBackendRoute("not-running"))
reloadRoutes(apiPort)

Expand All @@ -45,7 +48,9 @@ var _ = Describe("Functioning as a reverse proxy", func() {
Expect(err).NotTo(HaveOccurred())
defer stopRouter(3167)

addBackend("black-hole", "http://240.0.0.0:1234/")
os.Setenv("BACKEND_URL_black-hole", "http://240.0.0.0:1234/")
defer os.Unsetenv("BACKEND_URL_black-hole")

addRoute("/should-time-out", NewBackendRoute("black-hole"))
reloadRoutes(3166)

Expand Down Expand Up @@ -78,14 +83,16 @@ var _ = Describe("Functioning as a reverse proxy", func() {
Expect(err).NotTo(HaveOccurred())
tarpit1 = startTarpitBackend(time.Second)
tarpit2 = startTarpitBackend(100*time.Millisecond, 500*time.Millisecond)
addBackend("tarpit1", tarpit1.URL)
addBackend("tarpit2", tarpit2.URL)
os.Setenv("BACKEND_URL_tarpit1", tarpit1.URL)
os.Setenv("BACKEND_URL_tarpit2", tarpit2.URL)
addRoute("/tarpit1", NewBackendRoute("tarpit1"))
addRoute("/tarpit2", NewBackendRoute("tarpit2"))
reloadRoutes(3166)
})

AfterEach(func() {
os.Unsetenv("BACKEND_URL_tarpit1")
os.Unsetenv("BACKEND_URL_tarpit2")
tarpit1.Close()
tarpit2.Close()
stopRouter(3167)
Expand Down Expand Up @@ -119,12 +126,13 @@ var _ = Describe("Functioning as a reverse proxy", func() {
Describe("header handling", func() {
BeforeEach(func() {
recorder = startRecordingBackend()
addBackend("backend", recorder.URL())
os.Setenv("BACKEND_URL_backend", recorder.URL())
addRoute("/foo", NewBackendRoute("backend", "prefix"))
reloadRoutes(apiPort)
})

AfterEach(func() {
os.Unsetenv("BACKEND_URL_backend")
recorder.Close()
})

Expand Down Expand Up @@ -243,12 +251,13 @@ var _ = Describe("Functioning as a reverse proxy", func() {
Describe("request verb, path, query and body handling", func() {
BeforeEach(func() {
recorder = startRecordingBackend()
addBackend("backend", recorder.URL())
os.Setenv("BACKEND_URL_backend", recorder.URL())
addRoute("/foo", NewBackendRoute("backend", "prefix"))
reloadRoutes(apiPort)
})

AfterEach(func() {
os.Unsetenv("BACKEND_URL_backend")
recorder.Close()
})

Expand Down Expand Up @@ -299,12 +308,13 @@ var _ = Describe("Functioning as a reverse proxy", func() {
Describe("handling a backend with a non '/' path", func() {
BeforeEach(func() {
recorder = startRecordingBackend()
addBackend("backend", recorder.URL()+"/something")
os.Setenv("BACKEND_URL_backend", recorder.URL()+"/something")
addRoute("/foo/bar", NewBackendRoute("backend", "prefix"))
reloadRoutes(apiPort)
})

AfterEach(func() {
os.Unsetenv("BACKEND_URL_backend")
recorder.Close()
})

Expand All @@ -330,12 +340,13 @@ var _ = Describe("Functioning as a reverse proxy", func() {
Describe("handling HTTP/1.0 requests", func() {
BeforeEach(func() {
recorder = startRecordingBackend()
addBackend("backend", recorder.URL())
os.Setenv("BACKEND_URL_backend", recorder.URL())
addRoute("/foo", NewBackendRoute("backend", "prefix"))
reloadRoutes(apiPort)
})

AfterEach(func() {
os.Unsetenv("BACKEND_URL_backend")
recorder.Close()
})

Expand Down Expand Up @@ -365,12 +376,13 @@ var _ = Describe("Functioning as a reverse proxy", func() {
err := startRouter(3167, 3166, []string{"ROUTER_TLS_SKIP_VERIFY=1"})
Expect(err).NotTo(HaveOccurred())
recorder = startRecordingTLSBackend()
addBackend("backend", recorder.URL())
os.Setenv("BACKEND_URL_backend", recorder.URL())
addRoute("/foo", NewBackendRoute("backend", "prefix"))
reloadRoutes(3166)
})

AfterEach(func() {
os.Unsetenv("BACKEND_URL_backend")
recorder.Close()
stopRouter(3167)
})
Expand Down
4 changes: 3 additions & 1 deletion integration_tests/redirect_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration

import (
"os"
"time"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -223,13 +224,14 @@ var _ = Describe("Redirection", func() {

BeforeEach(func() {
recorder = startRecordingBackend()
addBackend("be", recorder.URL())
os.Setenv("BACKEND_URL_be", recorder.URL())
addRoute("/guidance/keeping-a-pet-pig-or-micropig", NewBackendRoute("be", "exact"))
addRoute("/GUIDANCE/keeping-a-pet-pig-or-micropig", NewBackendRoute("be", "exact"))
reloadRoutes(apiPort)
})

AfterEach(func() {
os.Unsetenv("BACKEND_URL_be")
recorder.Close()
})

Expand Down
6 changes: 0 additions & 6 deletions integration_tests/route_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"

// revive:disable:dot-imports
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -91,11 +90,6 @@ func initRouteHelper() error {
return nil
}

func addBackend(id, url string) {
err := routerDB.C("backends").Insert(bson.M{"backend_id": id, "backend_url": url})
Expect(err).NotTo(HaveOccurred())
}

func addRoute(path string, route Route) {
route.IncomingPath = path

Expand Down
39 changes: 5 additions & 34 deletions integration_tests/route_loading_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package integration

import (
"fmt"
"net/http/httptest"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -12,16 +12,17 @@ var _ = Describe("loading routes from the db", func() {
var (
backend1 *httptest.Server
backend2 *httptest.Server
backend3 *httptest.Server
)

BeforeEach(func() {
backend1 = startSimpleBackend("backend 1")
backend2 = startSimpleBackend("backend 2")
addBackend("backend-1", backend1.URL)
addBackend("backend-2", backend2.URL)
os.Setenv("BACKEND_URL_backend-1", backend1.URL)
os.Setenv("BACKEND_URL_backend-2", backend2.URL)
})
AfterEach(func() {
os.Unsetenv("BACKEND_URL_backend-1")
os.Unsetenv("BACKEND_URL_backend-2")
backend1.Close()
backend2.Close()
})
Expand Down Expand Up @@ -73,34 +74,4 @@ var _ = Describe("loading routes from the db", func() {
Expect(readBody(resp)).To(Equal("backend 1"))
})
})

Context("a backend an env var overriding the backend_url", func() {
BeforeEach(func() {
// This tests the behaviour of backend.ParseURL overriding the backend_url
// provided in the DB with the value of an env var
blackHole := "240.0.0.0/foo"
backend3 = startSimpleBackend("backend 3")
addBackend("backend-3", blackHole)

stopRouter(routerPort)
err := startRouter(routerPort, apiPort, []string{fmt.Sprintf("BACKEND_URL_backend-3=%s", backend3.URL)})
Expect(err).NotTo(HaveOccurred())

addRoute("/oof", NewBackendRoute("backend-3"))
reloadRoutes(apiPort)
})

AfterEach(func() {
stopRouter(routerPort)
err := startRouter(routerPort, apiPort, nil)
Expect(err).NotTo(HaveOccurred())
backend3.Close()
})

It("should send requests to the backend_url provided in the env var", func() {
resp := routerRequest(routerPort, "/oof")
Expect(resp.StatusCode).To(Equal(200))
Expect(readBody(resp)).To(Equal("backend 3"))
})
})
})
Loading

0 comments on commit 7a11d55

Please sign in to comment.