Skip to content

Commit

Permalink
track deployment usage by new User-Agent header
Browse files Browse the repository at this point in the history
make apicast work with user agent
  • Loading branch information
mikz committed Jan 11, 2017
1 parent c8923ac commit 8d05976
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
18 changes: 15 additions & 3 deletions apicast/conf.d/apicast.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
set_by_lua $deployment 'return os.getenv("THREESCALE_DEPLOYMENT_ENV");';
set_by_lua $user_agent 'return require("user_agent")()';
set_by_lua_block $deployment {
local user_agent = require('user_agent')
return user_agent.platform() .. '+' .. user_agent.deployment()
}

# TODO: enable in the future when we support SSL
# ssl_certificate_by_lua_block { require('module').call() }
Expand All @@ -17,7 +21,8 @@ location = /threescale_authrep {
proxy_http_version 1.1;
proxy_pass $backend_endpoint$path;
proxy_set_header Host "$backend_host";
proxy_set_header X-3scale-User-Agent "nginx$deployment";
proxy_set_header User-Agent "$user_agent";
proxy_set_header X-3scale-User-Agent "$deployment";
proxy_set_header X-3scale-Version "$version";
proxy_set_header Connection "";

Expand Down Expand Up @@ -88,6 +93,9 @@ location = /_threescale/oauth_store_token {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host "$backend_host";
proxy_set_header User-Agent "$user_agent";
proxy_set_header X-3scale-User-Agent "$deployment";
proxy_set_header X-3scale-Version "$version";

proxy_pass $backend_endpoint/services/$service_id/oauth_access_tokens.xml?$backend_authentication_type=$backend_authentication_value;
}
Expand All @@ -98,14 +106,18 @@ location = /_threescale/check_credentials {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host "$backend_host";
proxy_set_header User-Agent "$user_agent";
proxy_set_header X-3scale-User-Agent "$deployment";
proxy_set_header X-3scale-Version "$version";

proxy_pass $backend_endpoint/transactions/oauth_authorize.xml?$backend_authentication_type=$backend_authentication_value&service_id=$service_id&$args;
}

location = /threescale_oauth_authrep {
internal;
proxy_set_header Host "$backend_host";
proxy_set_header X-3scale-User-Agent "nginx$deployment";
proxy_set_header User-Agent "$user_agent";
proxy_set_header X-3scale-User-Agent "$deployment";
proxy_set_header X-3scale-Version "$version";
proxy_set_header X-3scale-OAuth2-Grant-Type "authorization_code";

Expand Down
6 changes: 5 additions & 1 deletion apicast/src/apicast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ local tonumber = tonumber
local math = math
local getenv = os.getenv
local reload_config = util.env_enabled('APICAST_RELOAD_CONFIG')
local user_agent = require('user_agent')

local _M = {
_VERSION = '0.1'
_VERSION = '2.0',
_NAME = 'APIcast'
}

local missing_configuration = getenv('APICAST_MISSING_CONFIGURATION') or 'log'
Expand All @@ -28,6 +30,8 @@ local function handle_missing_configuration(err)
end

function _M.init()
user_agent.cache()

math.randomseed(ngx.now())
-- First calls to math.random after a randomseed tend to be similar; discard them
for _=1,3 do math.random() end
Expand Down
3 changes: 3 additions & 0 deletions apicast/src/configuration_loader/remote_v1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local resty_url = require 'resty.url'
local http = require "resty.http"
local configuration = require 'configuration'
local util = require 'util'
local user_agent = require 'user_agent'

local _M = {
version = '0.1'
Expand Down Expand Up @@ -90,6 +91,8 @@ function _M.download(endpoint, _)
headers['Authorization'] = "Basic " .. ngx.encode_base64(concat({ user or '', pass or '' }, ':'))
end

headers['User-Agent'] = user_agent()

-- TODO: this does not fully implement HTTP spec, it first should send
-- request without Authentication and then send it after gettting 401

Expand Down

0 comments on commit 8d05976

Please sign in to comment.