diff --git a/assets/apis/example.json b/assets/apis/example.json index 4cc7e5a48..508e73d6a 100644 --- a/assets/apis/example.json +++ b/assets/apis/example.json @@ -4,7 +4,12 @@ "proxy" : { "preserve_host" : false, "listen_path" : "/example/*", - "upstream_url" : "http://localhost:9089/hello-world", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://localhost:9089/hello-world"} + ] + }, "strip_path" : false, "append_path" : false, "methods" : ["GET"] diff --git a/assets/auth/auth.json b/assets/auth/auth.json index 7933c688a..2860d91e5 100644 --- a/assets/auth/auth.json +++ b/assets/auth/auth.json @@ -4,7 +4,12 @@ "token" : { "preserve_host" : false, "listen_path" : "/auth/token", - "upstream_url" : "http://localhost:9088/token", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://localhost:9088/token"} + ] + }, "strip_path" : true, "append_path" : false, "enable_load_balancing" : false, diff --git a/docs/config/proxy.md b/docs/config/proxy.md index f1e7c4a86..e2a440dbb 100644 --- a/docs/config/proxy.md +++ b/docs/config/proxy.md @@ -4,7 +4,7 @@ |-----------------------|----------------------------------------------------------------------------------------| | preserve_hosts | Enable the [preserve host](/docs/proxy/preserve_host_property.md) definition | | listen_path | Defines the [endpoint](/docs/proxy/request_uri.md) that will be exposed in Janus | -| upstream_url | Defines the OAuth provider's [endpoint](/docs/proxy/upstream_url.md) for the request | +| upstreams | Defines the OAuth provider's [endpoints](/docs/proxy/upstreams.md) for the request | | strip_path | Enable the [strip URI](/docs/proxy/strip_uri_property.md) rule on this proxy | | enable_load_balancing | Enable load balancing for this proxy | | methods | Defines which [methods](/docs/proxy/request_http_method.md) are enabled for this proxy | diff --git a/docs/proxy/append_uri_property.md b/docs/proxy/append_uri_property.md index bf11243ca..7f78d7c3d 100644 --- a/docs/proxy/append_uri_property.md +++ b/docs/proxy/append_uri_property.md @@ -1,6 +1,6 @@ ##### The `append_path` property -You might also want to always append the `listen_path` to the upstream `upstream_url`. +You might also want to always append the `listen_path` to the elected upstream target. To do so, use the `append_path` boolean property by configuring an API like this: ```json @@ -9,7 +9,12 @@ To do so, use the `append_path` boolean property by configuring an API like this "proxy": { "append_path" : true, "listen_path": "/service/*", - "upstream_url": "http://my-api.com/example", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com/example"} + ] + }, } } ``` diff --git a/docs/proxy/preserve_host_property.md b/docs/proxy/preserve_host_property.md index a47f0f607..b2ec50ae4 100644 --- a/docs/proxy/preserve_host_property.md +++ b/docs/proxy/preserve_host_property.md @@ -1,6 +1,6 @@ #### The `preserve_host` property -When proxying, Janus's default behavior is to set the upstream request's Host header to the hostname of the API's `upstream_url` property. The `preserve_host` field accepts a boolean flag instructing Janus not to do so. +When proxying, Janus's default behavior is to set the upstream request's Host header to the hostname of the API's elected upstream from the`upstreams.targets` property. The `preserve_host` field accepts a boolean flag instructing Janus not to do so. For example, when the `preserve_host` property is not changed and an API is configured like this: @@ -10,7 +10,12 @@ For example, when the `preserve_host` property is not changed and an API is conf "hosts": ["service.com"], "proxy": { "listen_path": "/foo/*", - "upstream_url": "http://my-api.com", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com"} + ] + }, "methods": ["GET"] } } @@ -23,7 +28,7 @@ GET / HTTP/1.1 Host: service.com ``` -Janus would extract the Host header value from the the hostname of the API's `upstream_url` field, and would send the following request to your upstream service: +Janus would extract the Host header value from the the hostname of the API's elected upstream from the `upstreams.target` field, and would send the following request to your upstream service: ```http GET / HTTP/1.1 @@ -38,7 +43,12 @@ However, by explicitly configuring your API with `preserve_host=true`: "hosts": ["example.com", "service.com"], "proxy": { "listen_path": "/foo/*", - "upstream_url": "http://my-api.com", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com"} + ] + }, "methods": ["GET"], "preserve_host": true } diff --git a/docs/proxy/request_http_method.md b/docs/proxy/request_http_method.md index 5912063ec..4e387f3e0 100644 --- a/docs/proxy/request_http_method.md +++ b/docs/proxy/request_http_method.md @@ -14,7 +14,12 @@ routing via `GET` and `HEAD` HTTP methods: "proxy": { "strip_path" : true, "listen_path": "/hello/*", - "upstream_url": "http://my-api.com", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com"} + ] + }, "methods": ["GET", "HEAD"] } } diff --git a/docs/proxy/request_uri.md b/docs/proxy/request_uri.md index 1f4146ccc..65a1079bf 100644 --- a/docs/proxy/request_uri.md +++ b/docs/proxy/request_uri.md @@ -12,7 +12,12 @@ For example, in an API configured like this: "name": "My API", "proxy": { "listen_path": "/hello/*", - "upstream_url": "http://my-api.com", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com"} + ] + }, "methods": ["GET"], } } diff --git a/docs/proxy/routing_capabilities.md b/docs/proxy/routing_capabilities.md index 08caa218a..047722081 100644 --- a/docs/proxy/routing_capabilities.md +++ b/docs/proxy/routing_capabilities.md @@ -13,7 +13,12 @@ Let's go through a few examples. Consider an API configured like this: "hosts": ["example.com", "service.com"], "proxy": { "listen_path": "/foo/*", - "upstream_url": "http://my-api.com", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com"} + ] + }, "methods": ["GET"] } } @@ -67,7 +72,12 @@ Now that we understand how the hosts, uris, and methods properties work together "hosts": ["example.com", "service.com"], "proxy": { "listen_path": "/foo/*", - "upstream_url": "http://my-api.com", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com"} + ] + }, "insecure_skip_verify": true, "methods": ["GET"] } diff --git a/docs/proxy/routing_priorities.md b/docs/proxy/routing_priorities.md index f0b4689bb..e3e724fde 100644 --- a/docs/proxy/routing_priorities.md +++ b/docs/proxy/routing_priorities.md @@ -16,7 +16,12 @@ For example, two APIs are configured like this: "name": "API 1", "proxy": { "listen_path": "/", - "upstream_url": "http://my-api.com", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com"} + ] + }, "hosts": ["example.com"] } }, @@ -24,7 +29,12 @@ For example, two APIs are configured like this: "name": "API 2", "proxy": { "listen_path": "/", - "upstream_url": "http://my-api.com", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com"} + ] + }, "hosts": ["example.com"], "methods": ["POST"] } diff --git a/docs/proxy/strip_uri_property.md b/docs/proxy/strip_uri_property.md index d006b9dc0..87c26e40a 100644 --- a/docs/proxy/strip_uri_property.md +++ b/docs/proxy/strip_uri_property.md @@ -10,7 +10,12 @@ property by configuring an API like this: "proxy": { "strip_path" : true, "listen_path": "/service/*", - "upstream_url": "http://my-api.com", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://my-api.com"} + ] + }, "methods": ["GET"] } } diff --git a/docs/quick_start/add_auth.md b/docs/quick_start/add_auth.md index cdbcd2774..5258671f4 100644 --- a/docs/quick_start/add_auth.md +++ b/docs/quick_start/add_auth.md @@ -29,7 +29,13 @@ Lets create a file with the oAuth2 configuration called `auth.json`: "oauth_endpoints" : { "token" : { "listen_path" : "/auth/token", - "upstream_url" : "http://auth-service:8080/token", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://auth-service1:8080/token"}, + {"target": "http://auth-service2:8080/token"}, + ] + }, "strip_path" : true, "append_path" : false, "methods" : ["POST"] diff --git a/docs/quick_start/add_endpoint.md b/docs/quick_start/add_endpoint.md index 084692709..5763f0794 100644 --- a/docs/quick_start/add_endpoint.md +++ b/docs/quick_start/add_endpoint.md @@ -14,7 +14,12 @@ Create an `example.json` file containing your endpoint configuration: "active" : true, "proxy" : { "listen_path" : "/example/*", - "upstream_url" : "http://www.mocky.io/v2/595625d22900008702cd71e8", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://www.mocky.io/v2/595625d22900008702cd71e8"} + ] + }, "methods" : ["GET"] } } @@ -54,6 +59,6 @@ http -v GET http://localhost:8080/example curl -vX "GET" http://localhost:8080/example {%- endcodetabs %} -A successful response means Janus is now forwarding requests made to `http://localhost:8000` to the `upstream_url` we configured in step #1, and is forwarding the response back to us. +A successful response means Janus is now forwarding requests made to `http://localhost:8000` to the elected upstream target (chosen by the load balancer) wex configured in step #1, and is forwarding the response back to us. [Next](add_plugins.md) we'll learn how to add plugins to our endpoint. diff --git a/docs/quick_start/file_system.md b/docs/quick_start/file_system.md index 511224fd5..99904a3ab 100644 --- a/docs/quick_start/file_system.md +++ b/docs/quick_start/file_system.md @@ -29,7 +29,7 @@ If you access `http://localhost:8080/example` you should something like: ``` A successful response means Janus is now forwarding requests made to -`http://localhost:8080` to the `upstream_url` we configured in step #1, +`http://localhost:8080` to the elected upstream target (chosen by the load balancer) we configured in step #1, and is forwarding the response back to us. ## Understanding the directory structure diff --git a/examples/front-proxy-auth/apis/example.json b/examples/front-proxy-auth/apis/example.json index 17cdbaadb..7588a7ee4 100644 --- a/examples/front-proxy-auth/apis/example.json +++ b/examples/front-proxy-auth/apis/example.json @@ -4,7 +4,12 @@ "proxy" : { "preserve_host" : false, "listen_path" : "/example/*", - "upstream_url" : "http://service1:8080/", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://service1:8080/"} + ] + }, "strip_path" : false, "append_path" : false, "methods" : ["GET"] diff --git a/examples/front-proxy-auth/auth/auth.json b/examples/front-proxy-auth/auth/auth.json index b33bb1268..0c49f28ba 100644 --- a/examples/front-proxy-auth/auth/auth.json +++ b/examples/front-proxy-auth/auth/auth.json @@ -4,7 +4,12 @@ "token" : { "preserve_host" : false, "listen_path" : "/auth/token", - "upstream_url" : "http://auth-service:8080/token", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://auth-service:8080/token"} + ] + }, "strip_path" : true, "append_path" : false, "enable_load_balancing" : false, diff --git a/examples/front-proxy-cluster/apis/example.json b/examples/front-proxy-cluster/apis/example.json index ed775a7c8..674f8a64f 100644 --- a/examples/front-proxy-cluster/apis/example.json +++ b/examples/front-proxy-cluster/apis/example.json @@ -4,7 +4,12 @@ "proxy" : { "preserve_host" : false, "listen_path" : "/example/*", - "upstream_url" : "http://service1:8080/", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://service1:8080/"} + ] + }, "strip_path" : false, "append_path" : false, "methods" : ["GET"] diff --git a/examples/front-proxy-mongo/apis/example.json b/examples/front-proxy-mongo/apis/example.json index bac415233..433984a22 100644 --- a/examples/front-proxy-mongo/apis/example.json +++ b/examples/front-proxy-mongo/apis/example.json @@ -5,7 +5,12 @@ "proxy" : { "preserve_host" : false, "listen_path" : "/example/*", - "upstream_url" : "http://service1:8080/", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://service1:8080/"} + ] + }, "strip_path" : false, "append_path" : false, "methods" : ["GET"] diff --git a/examples/front-proxy/apis/example.json b/examples/front-proxy/apis/example.json index ed775a7c8..674f8a64f 100644 --- a/examples/front-proxy/apis/example.json +++ b/examples/front-proxy/apis/example.json @@ -4,7 +4,12 @@ "proxy" : { "preserve_host" : false, "listen_path" : "/example/*", - "upstream_url" : "http://service1:8080/", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://service1:8080/"} + ] + }, "strip_path" : false, "append_path" : false, "methods" : ["GET"] diff --git a/examples/janus-tls/apis/example.json b/examples/janus-tls/apis/example.json index ed775a7c8..674f8a64f 100644 --- a/examples/janus-tls/apis/example.json +++ b/examples/janus-tls/apis/example.json @@ -4,7 +4,12 @@ "proxy" : { "preserve_host" : false, "listen_path" : "/example/*", - "upstream_url" : "http://service1:8080/", + "upstreams" : { + "balancing": "roundrobin", + "targets": [ + {"target": "http://service1:8080/"} + ] + }, "strip_path" : false, "append_path" : false, "methods" : ["GET"] diff --git a/pkg/plugin/oauth2/mongodb_repository.go b/pkg/plugin/oauth2/mongodb_repository.go index 90aa9dcc4..549e25231 100644 --- a/pkg/plugin/oauth2/mongodb_repository.go +++ b/pkg/plugin/oauth2/mongodb_repository.go @@ -108,7 +108,7 @@ func (r *MongoRepository) FindByTokenURL(url url.URL) (*OAuth, error) { session, coll := r.getSession() defer session.Close() - err := coll.Find(bson.M{"oauth_endpoints.token.upstream_url": url.String()}).One(&result) + err := coll.Find(bson.M{"oauth_endpoints.token.upstreams.targets.target": url.String()}).One(&result) return result, err } diff --git a/pkg/proxy/definition.go b/pkg/proxy/definition.go index dd0c4a196..b4f433e43 100644 --- a/pkg/proxy/definition.go +++ b/pkg/proxy/definition.go @@ -40,6 +40,9 @@ func NewDefinition() *Definition { return &Definition{ Methods: make([]string, 0), Hosts: make([]string, 0), + Upstreams: &Upstreams{ + Targets: make([]*Target, 0), + }, } } diff --git a/pkg/proxy/definition_test.go b/pkg/proxy/definition_test.go index ca7f2a947..598556fbc 100644 --- a/pkg/proxy/definition_test.go +++ b/pkg/proxy/definition_test.go @@ -42,16 +42,13 @@ func TestInvalidTargetURLValidation(t *testing.T) { } func TestRouteToJSON(t *testing.T) { - definition := Definition{ - Methods: make([]string, 0), - Hosts: make([]string, 0), - } - route := NewRoute(&definition) + definition := NewDefinition() + route := NewRoute(definition) json, err := route.JSONMarshal() assert.NoError(t, err) assert.JSONEq( t, - `{"proxy": {"insecure_skip_verify": false, "append_path":false, "enable_load_balancing":false, "methods":[], "hosts":[], "preserve_host":false, "listen_path":"", "upstream_url":"", "strip_path":false}}`, + `{"proxy": {"insecure_skip_verify": false, "append_path":false, "enable_load_balancing":false, "methods":[], "hosts":[], "preserve_host":false, "listen_path":"", "upstream_url":"", "strip_path":false, "upstreams": {"balancing": "", "targets": [] }}}`, string(json), ) }