Skip to content

Commit f714f1c

Browse files
selfxpddragosd
authored andcommitted
Added security token service support (#4)
* Added security token service support * Updated Make install to include the sts folder
1 parent 685a0c6 commit f714f1c

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ install: all
2121
$(INSTALL) -d $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/kinesis/
2222
$(INSTALL) -d $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/lambda/
2323
$(INSTALL) -d $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/httpclient/
24+
$(INSTALL) -d $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/sts/
2425
$(INSTALL) src/lua/api-gateway/aws/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/
2526
$(INSTALL) src/lua/api-gateway/aws/httpclient/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/httpclient/
2627
$(INSTALL) src/lua/api-gateway/aws/kms/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/kms/
2728
$(INSTALL) src/lua/api-gateway/aws/sns/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/sns/
2829
$(INSTALL) src/lua/api-gateway/aws/kinesis/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/kinesis/
2930
$(INSTALL) src/lua/api-gateway/aws/lambda/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/lambda/
31+
$(INSTALL) src/lua/api-gateway/aws/sts/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/sts/
3032
# $(INSTALL) src/lua/api-gateway/aws/s3/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/api-gateway/aws/s3/
3133

3234
test-docker:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
local AwsService = require"api-gateway.aws.AwsService"
2+
local cjson = require"cjson"
3+
local error = error
4+
5+
local _M = AwsService:new({ ___super = true })
6+
local super = {
7+
instance = _M,
8+
constructor = _M.constructor
9+
}
10+
11+
function _M.new(self, o)
12+
ngx.log(ngx.DEBUG, "StsService() o=", tostring(o))
13+
local o = o or {}
14+
o.aws_service = "sts"
15+
-- aws_service_name is used in the X-Amz-Target Header: i.e Kinesis_20131202.ListStreams
16+
o.aws_service_name = "STS_20110615"
17+
18+
super.constructor(_M, o)
19+
20+
setmetatable(o, self)
21+
self.__index = self
22+
return o
23+
end
24+
25+
---
26+
-- @param roleARN
27+
-- @param roleSessionName
28+
-- @param policy
29+
-- @param durationSeconds
30+
-- @param externalId
31+
--
32+
function _M:assumeRole(roleARN, roleSessionName, policy, durationSeconds, externalId)
33+
assert(roleARN ~= nil, "Please provide a valid roleARN." )
34+
assert(roleSessionName ~= nil, "Please provide a valid roleSessionName." )
35+
local arguments = {
36+
RoleArn = roleARN,
37+
RoleSessionName = roleSessionName,
38+
Policy = policy,
39+
DurationSeconds = durationSeconds or 3600,
40+
ExternalId = externalId
41+
}
42+
local ok, code, headers, status, body = self:performAction("AssumeRole", arguments, "/", "POST", true)
43+
44+
if (code == ngx.HTTP_OK and body ~= nil) then
45+
return cjson.decode(body), code, headers, status, body
46+
end
47+
return nil, code, headers, status, body
48+
end
49+
50+
return _M
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
use lib 'lib';
3+
use Test::Nginx::Socket::Lua;
4+
use Cwd qw(cwd);
5+
6+
#worker_connections(1014);
7+
#master_process_enabled(1);
8+
#log_level('warn');
9+
10+
repeat_each(1);
11+
12+
plan tests => repeat_each() * (blocks())+2;
13+
14+
my $pwd = cwd();
15+
16+
# try to read the nameservers used by the system resolver:
17+
my @nameservers;
18+
if (open my $in, "/etc/resolv.conf") {
19+
while (<$in>) {
20+
if (/^\s*nameserver\s+(\d+(?:\.\d+){3})(?:\s+|$)/) {
21+
push @nameservers, $1;
22+
if (@nameservers > 10) {
23+
last;
24+
}
25+
}
26+
}
27+
close $in;
28+
}
29+
30+
if (!@nameservers) {
31+
# default to Google's open DNS servers
32+
push @nameservers, "8.8.8.8", "8.8.4.4";
33+
}
34+
35+
36+
warn "Using nameservers: \n@nameservers\n";
37+
38+
our $HttpConfig = <<_EOC_;
39+
# lua_package_path "$pwd/scripts/?.lua;;";
40+
lua_package_path 'src/lua/?.lua;/usr/local/lib/lua/?.lua;;';
41+
42+
client_body_temp_path /tmp/;
43+
proxy_temp_path /tmp/;
44+
fastcgi_temp_path /tmp/;
45+
46+
# lua_package_cpath 'src/lua/?.so;;';
47+
init_by_lua '
48+
local v = require "jit.v"
49+
v.on("$Test::Nginx::Util::ErrLogFile")
50+
require "resty.core"
51+
';
52+
lua_shared_dict shared_cache 1m;
53+
resolver @nameservers;
54+
55+
server {
56+
listen 80;
57+
location / {
58+
proxy_pass http://127.0.0.1:\$TEST_NGINX_PORT/sts-mock;
59+
}
60+
}
61+
62+
_EOC_
63+
64+
#no_diff();
65+
no_long_string();
66+
run_tests();
67+
68+
__DATA__
69+
70+
71+
=== TEST 1: test response of the SecurityTokenService
72+
--- http_config eval: $::HttpConfig
73+
--- config
74+
75+
location = /latest/meta-data/iam/security-credentials/ {
76+
return 200 'test-iam-user';
77+
}
78+
79+
location = /latest/meta-data/iam/security-credentials/test-iam-user {
80+
set_by_lua $expiration '
81+
local offset = os.time() - os.time(os.date("!*t"))
82+
return os.date("%Y-%m-%dT%H:%M:%SZ", os.time() + math.abs(offset) + 20)
83+
';
84+
return 200 '{
85+
"Code" : "Success",
86+
"LastUpdated" : "2014-11-03T01:56:20Z",
87+
"Type" : "AWS-HMAC",
88+
"AccessKeyId" : "TEST_NGINX_AWS_CLIENT_ID",
89+
"SecretAccessKey" : "TEST_NGINX_AWS_SECRET",
90+
"Token" : "TEST_NGINX_AWS_SECURITY_TOKEN",
91+
"Expiration" : "$expiration"
92+
}';
93+
}
94+
95+
location = /sts-mock {
96+
return 200 '
97+
{
98+
"AssumedRoleUser": {
99+
"AssumedRoleId": "AROA3XFRBF535PLBIFPI4:s3-access-example",
100+
"Arn": "arn:aws:sts::123456789012:assumed-role/xaccounts3access/s3-access-example"
101+
},
102+
"Credentials": {
103+
"SecretAccessKey": "9drTJvcXLB89EXAMPLELB8923FB892xMFI",
104+
"SessionToken": "AQoXdzELDDY//////////wEaoAK1wvxJY12r2IrDFT2IvAzTCn3zHoZ7YNtpiQLF0MqZye/qwjzP2iEXAMPLEbw/m3hsj8VBTkPORGvr9jM5sgP+w9IZWZnU+LWhmg+a5fDi2oTGUYcdg9uexQ4mtCHIHfi4citgqZTgco40Yqr4lIlo4V2b2Dyauk0eYFNebHtYlFVgAUj+7Indz3LU0aTWk1WKIjHmmMCIoTkyYp/k7kUG7moeEYKSitwQIi6Gjn+nyzM+PtoA3685ixzv0R7i5rjQi0YE0lf1oeie3bDiNHncmzosRM6SFiPzSvp6h/32xQuZsjcypmwsPSDtTPYcs0+YN/8BRi2/IcrxSpnWEXAMPLEXSDFTAQAM6Dl9zR0tXoybnlrZIwMLlMi1Kcgo5OytwU=",
105+
"Expiration": "2016-03-15T00:05:07Z",
106+
"AccessKeyId": "ASIAJEXAMPLEXEG2JICEA"
107+
}
108+
}
109+
';
110+
}
111+
112+
location /test {
113+
content_by_lua '
114+
local SecurityTokenService = require "api-gateway.aws.sts.SecurityTokenService"
115+
local sts = SecurityTokenService:new({
116+
security_credentials_host = "127.0.0.1",
117+
security_credentials_port = $TEST_NGINX_PORT,
118+
aws_region = "us-east-1",
119+
aws_debug = true, -- print warn level messages on the nginx logs
120+
aws_conn_keepalive = 60000, -- how long to keep the sockets used for AWS alive
121+
aws_conn_pool = 100 -- the connection pool size for sockets used to connect to AWS
122+
})
123+
sts.getAWSHost = function(self)
124+
return "127.0.0.1"
125+
end
126+
127+
sts.performAction = function(self, actionName, arguments, path, http_method, useSSL, timeout, contentType, extra_headers)
128+
-- force useSSL to false
129+
return SecurityTokenService.performAction(self, actionName, arguments, path, http_method, false, timeout, contentType, extra_headers)
130+
end
131+
132+
local response, code, headers, status, body = sts:assumeRole("", "", nil, nil, nil)
133+
ngx.say(":" .. tostring(response.Credentials.AccessKeyId))
134+
';
135+
}
136+
--- request
137+
GET /test
138+
--- response_body_like eval
139+
["ASIAJEXAMPLEXEG2JICEA"]
140+
--- error_code: 200
141+
--- no_error_log
142+
[error]
143+
--- more_headers
144+
X-Test: test

0 commit comments

Comments
 (0)