Skip to content

Commit f31b3cf

Browse files
committed
tmp
1 parent e7b8a8f commit f31b3cf

File tree

4 files changed

+133
-2
lines changed

4 files changed

+133
-2
lines changed

a.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
setmetatable(_G, nil)
2+
local AWS = require("resty.aws")
3+
local AWS_global_config = require("resty.aws.config").global
4+
local aws = AWS { region = AWS_global_config.region }
5+
local cache = aws:ElastiCache()
6+
local redis = require "resty.redis"
7+
8+
local hostname = "test-cache-ppl9c2.serverless.apne1.cache.amazonaws.com"
9+
local cachename = "test-cache"
10+
local port = 6379
11+
local name = "nik-test"
12+
13+
local signer = cache:Signer { -- create a signer instance
14+
hostname = cachename,
15+
port = port,
16+
username = name,
17+
is_serverless = true,
18+
region = nil, -- will be inherited from `aws`
19+
credentials = nil, -- will be inherited from `aws`
20+
}
21+
22+
-- use the 'signer' to generate the token, whilst overriding some options
23+
local auth_token, err = signer:getAuthToken()
24+
25+
if err then
26+
ngx.log(ngx.ERR, "Failed to build auth token: ", err)
27+
return
28+
end
29+
print(auth_token)
30+
31+
local red = redis:new()
32+
--red:set_timeouts(1000, 1000, 1000)
33+
34+
local ok, err = red:connect(hostname, port, { ssl = true })
35+
if not ok then
36+
print("failed to connect: ", err)
37+
return
38+
end
39+
40+
local res, err = red:auth(name, auth_token)
41+
if not res then
42+
print("failed to authenticate: ", err)
43+
return
44+
end
45+
46+
print("OK")

lua-resty-aws-dev-1.rockspec.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ build = {
4444
["resty.aws.request.signatures.v4"] = "src/resty/aws/request/signatures/v4.lua",
4545
["resty.aws.request.signatures.presign"] = "src/resty/aws/request/signatures/presign.lua",
4646
["resty.aws.request.signatures.none"] = "src/resty/aws/request/signatures/none.lua",
47-
["resty.aws.service.rds.signer"] = "src/resty/aws/service/rds/signer.lua",
47+
["resty.aws.service.rds.signer"] = "src/resty/aws/service/rds/signer.lua",
48+
["resty.aws.service.elasticache.signer"] = "src/resty/aws/service/elasticache/signer.lua",
4849
["resty.aws.credentials.Credentials"] = "src/resty/aws/credentials/Credentials.lua",
4950
["resty.aws.credentials.ChainableTemporaryCredentials"] = "src/resty/aws/credentials/ChainableTemporaryCredentials.lua",
5051
["resty.aws.credentials.CredentialProviderChain"] = "src/resty/aws/credentials/CredentialProviderChain.lua",

src/resty/aws/init.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,19 @@ function AWS:new(config)
498498
service_config[k] = service_config[k] or v
499499
end
500500

501+
local signer
502+
if service_id == "RDS" then
503+
signer = require("resty.aws.service.rds.signer")
504+
elseif service_id == "ElastiCache" then
505+
signer = require("resty.aws.service.elasticache.signer")
506+
end
507+
501508
local service_instance = {
502509
aws = aws_instance,
503510
config = service_config,
504511
api = api,
505512
-- Add service specific methods:
506-
Signer = (service_id == "RDS") and require("resty.aws.service.rds.signer") or nil
513+
Signer = signer
507514
}
508515

509516
AWS.configureEndpoint(service_instance)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--- Signer class for Elasticache tokens for Valkey and Redis OSS access.
2+
3+
-- Elasticache services created will get a `Signer` method to create an instance.
4+
-- The `Signer` will inherit its configuration from the `AWS` instance.
5+
6+
local httpc = require("resty.luasocket.http")
7+
local presign_awsv4_request = require("resty.aws.request.signatures.presign")
8+
9+
local ELASTICACHE_IAM_AUTH_EXPIRE_TIME = 15 * 60
10+
11+
local function getAuthToken(self, opts) --endpoint, region, username, is_serverless)
12+
opts = setmetatable(opts or {}, { __index = self.config }) -- lookup missing params in inherited config
13+
14+
local region = assert(opts.region, "parameter 'region' not set")
15+
local hostname = assert(opts.hostname, "parameter 'hostname' not set")
16+
local port = assert(opts.port, "parameter 'port' not set")
17+
local username = assert(opts.username, "parameter 'username' not set")
18+
19+
local endpoint = hostname
20+
if endpoint:sub(1,7) ~= "http://" then
21+
endpoint = "http://" .. endpoint
22+
end
23+
24+
local query_args = "Action=connect"
25+
if opts.is_serverless then
26+
query_args = query_args .. "&ResourceType=ServerlessCache"
27+
end
28+
29+
local query_args = query_args .. "&User=" .. username
30+
31+
local canonical_request_url = endpoint .. "/?" .. query_args
32+
local scheme, host, port, path, query = unpack(httpc:parse_uri(canonical_request_url, false))
33+
local req_data = {
34+
method = "GET",
35+
scheme = scheme,
36+
tls = scheme == "https",
37+
host = host,
38+
port = port,
39+
path = path,
40+
query = query,
41+
headers = {
42+
["Host"] = host,
43+
},
44+
}
45+
46+
local presigned_request, err = presign_awsv4_request(self.config, req_data, opts.signingName, region, ELASTICACHE_IAM_AUTH_EXPIRE_TIME)
47+
if err then
48+
return nil, err
49+
end
50+
51+
return presigned_request.host .. presigned_request.path .. "?" .. presigned_request.query
52+
end
53+
54+
55+
-- signature: intended to be a method on the Elasticache service object, cache_instance == self in that case
56+
return function(cache_instance, config)
57+
local token_instance = {
58+
config = {},
59+
getAuthToken = getAuthToken, -- injected method for token generation
60+
}
61+
62+
-- first copy the inherited config elements NOTE: inherits from AWS, not the cache_instance!!!
63+
for k,v in pairs(cache_instance.aws.config) do
64+
token_instance.config[k] = v
65+
end
66+
67+
-- service specifics
68+
token_instance.config.signatureVersion = "v4"
69+
token_instance.config.signingName = "elasticache"
70+
71+
-- then add/overwrite with provided config
72+
for k,v in pairs(config or {}) do
73+
token_instance.config[k] = v
74+
end
75+
76+
return token_instance
77+
end

0 commit comments

Comments
 (0)