-
Notifications
You must be signed in to change notification settings - Fork 170
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
Policy: Camel proxy #1193
Merged
Merged
Policy: Camel proxy #1193
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Camel proxy policy | ||
|
||
This policy allows users to define a camel proxy where the traffic will be send | ||
over the defined proxy, the example traffic flow is the following: | ||
|
||
``` | ||
,-. | ||
`-' | ||
/|\ | ||
| ,-------. ,---------. ,----------. | ||
/ \ |Apicast| | CAMEL | |APIBackend| | ||
User `---+---' `----+----' `----------' | ||
| GET /resource | | | | ||
| --------------->| | | | ||
| | | | | ||
| | Get /resource | | | ||
| |------------------>| | | ||
| | | | | ||
| | | Get /resource/ | | ||
| | | - - - - - - - - - >| | ||
| | | | | ||
| | | response | | ||
| | |<- - - - - - - - - -| | ||
| | | | | ||
| | response | | | ||
| |<------------------| | | ||
| | | | | ||
| | | | | ||
| <---------------| | | | ||
User ,---+---. ,----+----. ,----------. | ||
,-. |Apicast| | CAMEL | |APIBackend| | ||
`-' `-------' `---------' `----------' | ||
/|\ | ||
| | ||
/ \ | ||
``` | ||
|
||
|
||
## Configuration | ||
|
||
``` | ||
"policy_chain": [ | ||
{ | ||
"name": "apicast.policy.apicast" | ||
}, | ||
{ | ||
"name": "apicast.policy.camel", | ||
"configuration": { | ||
"all_proxy": "http://192.168.15.103:8888/", | ||
"https_proxy": "https://192.168.15.103:8888/", | ||
"http_proxy": "https://192.168.15.103:8888/" | ||
} | ||
} | ||
] | ||
``` | ||
|
||
- If http_proxy or https_proxy is not defined the all_proxy will be taken. | ||
|
||
## Caveats | ||
|
||
- This policy will disable all load-balancing policies and traffic will be | ||
always send to the proxy. | ||
- In case of HTTP_PROXY, HTTPS_PROXY or ALL_PROXY parameters are defined, this | ||
policy will overwrite those values. | ||
- Proxy connection does not support authentication, if you need auth, please use | ||
headers policy. | ||
|
||
|
||
## Example Use case | ||
|
||
This policy was designed to be able to apply more fined grained policies and | ||
transformation using Apache Camel. | ||
|
||
An example project can be found | ||
[here](https://github.com/zregvart/camel-netty-proxy). This project is an HTTP | ||
Proxy that transforms to uppercase all the response body given by the API | ||
backend. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"$schema": "http://apicast.io/policy-v1/schema#manifest#", | ||
"name": "Camel Service", | ||
"summary": "Adds an Camel proxy to the service.", | ||
"description": [ | ||
"With this policy all the traffic for this service will be routed accross ", | ||
"the defined proxy" | ||
], | ||
"version": "builtin", | ||
"configuration": { | ||
"type": "object", | ||
"properties": { | ||
"all_proxy": { | ||
"description": "Defines a HTTP proxy to be used for connecting to services if a protocol-specific proxy is not specified. Authentication is not supported.", | ||
"type": "string" | ||
}, | ||
"https_proxy": { | ||
"description": "Defines a HTTPS proxy to be used for connecting to HTTPS services. Authentication is not supported", | ||
"type": "string" | ||
}, | ||
"http_proxy": { | ||
"description": "Defines a HTTP proxy to be used for connecting to HTTP services. Authentication is not supported", | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
local policy = require('apicast.policy') | ||
local _M = policy.new('http_proxy', 'builtin') | ||
|
||
local resty_url = require 'resty.url' | ||
local ipairs = ipairs | ||
|
||
local new = _M.new | ||
|
||
local proxies = {"http", "https"} | ||
|
||
function _M.new(config) | ||
local self = new(config) | ||
self.proxies = {} | ||
|
||
if config.all_proxy then | ||
local err | ||
self.all_proxy, err = resty_url.parse(config.all_proxy) | ||
if err then | ||
ngx.log(ngx.WARN, "All proxy '", config.all_proxy, "' is not correctly defined, err:", err) | ||
end | ||
end | ||
|
||
for _, proto in ipairs(proxies) do | ||
local val, err = resty_url.parse(config[string.format("%s_proxy", proto)]) | ||
if err then | ||
ngx.log(ngx.WARN, proto, " proxy is not correctly defined, err: ", err) | ||
end | ||
self.proxies[proto] = val or self.all_proxy | ||
end | ||
return self | ||
end | ||
|
||
local function find_proxy(self, scheme) | ||
return self.proxies[scheme] | ||
end | ||
|
||
function _M:access(context) | ||
local upstream = context.get_upstream() | ||
if not upstream then | ||
return | ||
end | ||
|
||
upstream:set_skip_https_connect_on_proxy() | ||
end | ||
|
||
function _M:export() | ||
-- This get_http_proxy function will be called in upstream just in case if a | ||
-- proxy is defined. | ||
return { | ||
get_http_proxy = function(uri) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be good to document here who expects this function to be in the context. |
||
if not uri.scheme then | ||
return nil | ||
end | ||
return find_proxy(self, uri.scheme) | ||
end | ||
} | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
return require("camel") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
local camel_policy = require('apicast.policy.camel') | ||
local resty_url = require 'resty.url' | ||
|
||
describe('Camel policy', function() | ||
local all_proxy_val = "http://all.com" | ||
local http_proxy_val = "http://plain.com" | ||
local https_proxy_val = "http://secure.com" | ||
|
||
local http_uri = {scheme="http"} | ||
local https_uri = {scheme="https"} | ||
|
||
it("http[s] proxies are defined if all_proxy is in there", function() | ||
local proxy = camel_policy.new({ | ||
all_proxy = all_proxy_val | ||
}) | ||
local callback = proxy:export() | ||
|
||
assert.same(callback.get_http_proxy(http_uri), resty_url.parse(all_proxy_val)) | ||
assert.same(callback.get_http_proxy(https_uri), resty_url.parse(all_proxy_val)) | ||
end) | ||
|
||
it("all_proxy does not overwrite http/https proxies", function() | ||
local proxy = camel_policy.new({ | ||
all_proxy = all_proxy_val, | ||
http_proxy = http_proxy_val, | ||
https_proxy = https_proxy_val | ||
}) | ||
local callback = proxy:export() | ||
|
||
assert.same(callback.get_http_proxy(http_uri), resty_url.parse(http_proxy_val)) | ||
assert.same(callback.get_http_proxy(https_uri), resty_url.parse(https_proxy_val)) | ||
end) | ||
|
||
it("empty config return all nil", function() | ||
local proxy = camel_policy.new({}) | ||
local callback = proxy:export() | ||
|
||
assert.is_nil(callback.get_http_proxy(https_uri)) | ||
assert.is_nil(callback.get_http_proxy(http_uri)) | ||
end) | ||
|
||
describe("get_http_proxy callback", function() | ||
local callback = camel_policy.new({ | ||
all_proxy = all_proxy_val | ||
}):export() | ||
|
||
it("Valid protocol", function() | ||
|
||
local result = callback.get_http_proxy( | ||
resty_url.parse("http://google.com")) | ||
assert.same(result, resty_url.parse(all_proxy_val)) | ||
end) | ||
|
||
it("invalid protocol", function() | ||
local result = callback:get_http_proxy( | ||
{}, {scheme="invalid"}) | ||
assert.is_nil(result) | ||
end) | ||
|
||
end) | ||
end) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is the best name for the policy. There's nothing camel-specific, it's valid for any proxy, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TLS is only for camel (With termination) and the policy name is suggested to be like that:
https://issues.redhat.com/browse/THREESCALE-4867