Debian Codegraph @

lualib/lua_aws.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17
18 --[[[
19 -- @module lua_aws
20 -- This module contains Amazon AWS utility functions
21 --]]
22
23 local N = "aws"
24 --local rspamd_logger = require "rspamd_logger"
25 local T = require "lua_shape.core"
26 local lua_util = require "lua_util"
27 local fun = require "fun"
28 local rspamd_crypto_hash = require "rspamd_cryptobox_hash"
29
30 local exports = {}
31
32 -- Returns a canonical representation of today date
33 local function today_canonical()
34 return os.date('!%Y%m%d')
35 end
36
37 --[[[
38 -- @function lua_aws.aws_date([date_str])
39 -- Returns an aws date header corresponding to the specific date
40 --]]
41 local function aws_date(date_str)
42 if not date_str then
43 date_str = today_canonical()
44 end
45
46 return date_str .. os.date('!T%H%M%SZ')
47 end
48
49 exports.aws_date = aws_date
50
51
52 -- Local cache of the keys to save resources
53 local cached_keys = {}
54
55 local function maybe_get_cached_key(date_str, secret_key, region, service, req_type)
56 local bucket = cached_keys[tonumber(date_str)]
57
58 if not bucket then
59 return nil
60 end
61
62 local elt = bucket[string.format('%s.%s.%s.%s', secret_key, region, service, req_type)]
63 if elt then
64 return elt
65 end
66 end
67
68 local function save_cached_key(date_str, secret_key, region, service, req_type, key)
69 local numdate = tonumber(date_str)
70 -- expire old buckets
71 for k, _ in pairs(cached_keys) do
72 if k < numdate then
73 cached_keys[k] = nil
74 end
75 end
76
77 local bucket = cached_keys[tonumber(date_str)]
78 local idx = string.format('%s.%s.%s.%s', secret_key, region, service, req_type)
79
80 if not bucket then
81 cached_keys[tonumber(date_str)] = {
82 idx = key
83 }
84 else
85 bucket[idx] = key
86 end
87 end
88 --[[[
89 -- @function lua_aws.aws_signing_key([date_str], secret_key, region, [service='s3'], [req_type='aws4_request'])
90 -- Returns a signing key for the specific parameters
91 --]]
92 local function aws_signing_key(date_str, secret_key, region, service, req_type)
93 if not date_str then
94 date_str = today_canonical()
95 end
96
97 if not service then
98 service = 's3'
99 end
100
101 if not req_type then
102 req_type = 'aws4_request'
103 end
104
105 assert(type(secret_key) == 'string')
106 assert(type(region) == 'string')
107
108 local maybe_cached = maybe_get_cached_key(date_str, secret_key, region, service, req_type)
109
110 if maybe_cached then
111 return maybe_cached
112 end
113
114 local hmac1 = rspamd_crypto_hash.create_specific_keyed("AWS4" .. secret_key, "sha256", date_str):bin()
115 local hmac2 = rspamd_crypto_hash.create_specific_keyed(hmac1, "sha256", region):bin()
116 local hmac3 = rspamd_crypto_hash.create_specific_keyed(hmac2, "sha256", service):bin()
117 local final_key = rspamd_crypto_hash.create_specific_keyed(hmac3, "sha256", req_type):bin()
118
119 save_cached_key(date_str, secret_key, region, service, req_type, final_key)
120
121 return final_key
122 end
123
124 exports.aws_signing_key = aws_signing_key
125
126 --[[[
127 -- @function lua_aws.aws_canon_request_hash(method, path, headers_to_sign, hex_hash)
128 -- Returns a hash + list of headers as required to produce signature afterwards
129 --]]
130 local function aws_canon_request_hash(method, uri, headers_to_sign, hex_hash)
131 assert(type(method) == 'string')
132 assert(type(uri) == 'string')
133 assert(type(headers_to_sign) == 'table')
134
135 if not hex_hash then
136 hex_hash = headers_to_sign['x-amz-content-sha256']
137 end
138
139 assert(type(hex_hash) == 'string')
140
141 local sha_ctx = rspamd_crypto_hash.create_specific('sha256')
142
143 lua_util.debugm(N, 'update signature with the method %s',
144 method)
145 sha_ctx:update(method .. '\n')
146 lua_util.debugm(N, 'update signature with the uri %s',
147 uri)
148 sha_ctx:update(uri .. '\n')
149 -- XXX add query string canonicalisation
150 sha_ctx:update('\n')
151 -- Sort auth headers and canonicalise them as requested
152 local hdr_canon = fun.tomap(fun.map(function(k, v)
153 return k:lower(), lua_util.str_trim(v)
154 end, headers_to_sign))
155 local header_names = lua_util.keys(hdr_canon)
156 table.sort(header_names)
157 for _, hn in ipairs(header_names) do
158 local v = hdr_canon[hn]
159 lua_util.debugm(N, 'update signature with the header %s, %s',
160 hn, v)
161 sha_ctx:update(string.format('%s:%s\n', hn, v))
162 end
163 local hdrs_list = table.concat(header_names, ';')
164 lua_util.debugm(N, 'headers list to sign: %s', hdrs_list)
165 sha_ctx:update(string.format('\n%s\n%s', hdrs_list, hex_hash))
166
167 return sha_ctx:hex(), hdrs_list
168 end
169
170 exports.aws_canon_request_hash = aws_canon_request_hash
171
172 local aws_authorization_hdr_args_schema = T.table({
173 date = T.string():with_default(today_canonical),
174 secret_key = T.string(),
175 method = T.string():with_default('GET'),
176 uri = T.string(),
177 region = T.string(),
178 service = T.string():with_default('s3'),
179 req_type = T.string():with_default('aws4_request'),
180 -- headers is a table with string keys and string values (map_of equivalent)
181 headers = T.table({}, { open = true, extra = T.string() }),
182 key_id = T.string(),
183 })
184 --[[[
185 -- @function lua_aws.aws_authorization_hdr(params)
186 -- Produces an authorization header as required by AWS
187 -- Parameters schema:
188 -- {
189 -- date = string or nil (default: today),
190 -- secret_key = string,
191 -- method = string or nil (default: 'GET'),
192 -- uri = string,
193 -- region = string,
194 -- service = string or nil (default: 's3'),
195 -- req_type = string or nil (default: 'aws4_request'),
196 -- headers = table of string->string,
197 -- key_id = string,
198 -- }
199 --]]
200 local function aws_authorization_hdr(tbl, transformed)
201 local res, err
202 if not transformed then
203 res, err = aws_authorization_hdr_args_schema:transform(tbl)
204 assert(res, err)
205 else
206 res = tbl
207 end
208
209 local signing_key = aws_signing_key(res.date, res.secret_key, res.region, res.service,
210 res.req_type)
211 assert(signing_key ~= nil)
212 local signed_sha, signed_hdrs = aws_canon_request_hash(res.method, res.uri,
213 res.headers)
214
215 if not signed_sha then
216 return nil
217 end
218
219 local string_to_sign = string.format('AWS4-HMAC-SHA256\n%s\n%s/%s/%s/%s\n%s',
220 res.headers['x-amz-date'] or aws_date(),
221 res.date, res.region, res.service, res.req_type,
222 signed_sha)
223 lua_util.debugm(N, "string to sign: %s", string_to_sign)
224 local hmac = rspamd_crypto_hash.create_specific_keyed(signing_key, 'sha256', string_to_sign):hex()
225 lua_util.debugm(N, "hmac: %s", hmac)
226 local auth_hdr = string.format('AWS4-HMAC-SHA256 Credential=%s/%s/%s/%s/%s,' ..
227 'SignedHeaders=%s,Signature=%s',
228 res.key_id, res.date, res.region, res.service, res.req_type,
229 signed_hdrs, hmac)
230
231 return auth_hdr
232 end
233
234 exports.aws_authorization_hdr = aws_authorization_hdr
235
236
237
238 --[[[
239 -- @function lua_aws.aws_request_enrich(params, content)
240 -- Produces an authorization header as required by AWS
241 -- Parameters schema (same as aws_authorization_hdr):
242 -- {
243 -- date = string or nil (default: today),
244 -- secret_key = string,
245 -- method = string or nil (default: 'GET'),
246 -- uri = string,
247 -- region = string,
248 -- service = string or nil (default: 's3'),
249 -- req_type = string or nil (default: 'aws4_request'),
250 -- headers = table of string->string,
251 -- key_id = string,
252 -- }
253 -- This method returns new/modified in place table of the headers
254 --]]
255 local function aws_request_enrich(tbl, content)
256 local res, err = aws_authorization_hdr_args_schema:transform(tbl)
257 assert(res, err)
258 local content_sha256 = rspamd_crypto_hash.create_specific('sha256', content):hex()
259 local hdrs = res.headers
260 hdrs['x-amz-content-sha256'] = content_sha256
261 if not hdrs['x-amz-date'] then
262 hdrs['x-amz-date'] = aws_date(res.date)
263 end
264 hdrs['Authorization'] = aws_authorization_hdr(res, true)
265
266 return hdrs
267 end
268
269 exports.aws_request_enrich = aws_request_enrich
270
271 -- A simple tests according to AWS docs to check sanity
272 local test_request_hdrs = {
273 ['Host'] = 'examplebucket.s3.amazonaws.com',
274 ['x-amz-date'] = '20130524T000000Z',
275 ['Range'] = 'bytes=0-9',
276 ['x-amz-content-sha256'] = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
277 }
278
279 assert(aws_canon_request_hash('GET', '/test.txt', test_request_hdrs) ==
280 '7344ae5b7ee6c3e7e6b0fe0640412a37625d1fbfff95c48bbb2dc43964946972')
281
282 assert(aws_authorization_hdr {
283 date = '20130524',
284 region = 'us-east-1',
285 headers = test_request_hdrs,
286 uri = '/test.txt',
287 key_id = 'AKIAIOSFODNN7EXAMPLE',
288 secret_key = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
289 } == 'AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request,' ..
290 'SignedHeaders=host;range;x-amz-content-sha256;x-amz-date,' ..
291 'Signature=f0e8bdb87c964420e857bd35b5d6ed310bd44f0170aba48dd91039c6036bdb41')
292
293 return exports