|  | 
|  | 1 | +local nettle_hmac = require "resty.nettle.hmac" | 
|  | 2 | +local resty_sha256 = require "resty.sha256" | 
|  | 3 | +local to_hex = require("resty.string").to_hex | 
|  | 4 | + | 
|  | 5 | +local escape_uri = ngx.escape_uri | 
|  | 6 | +local gsub = ngx.re.gsub | 
|  | 7 | + | 
|  | 8 | +local AWS_SERVICE = "es" | 
|  | 9 | +local UNSIGNED_HEADERS = { | 
|  | 10 | +  authorization = 1, | 
|  | 11 | +  expect = 1, | 
|  | 12 | +} | 
|  | 13 | + | 
|  | 14 | +local _M = {} | 
|  | 15 | + | 
|  | 16 | +local function hmac(secret_key, value) | 
|  | 17 | +  assert(secret_key) | 
|  | 18 | +  assert(value) | 
|  | 19 | + | 
|  | 20 | +  local hmac_sha256 = nettle_hmac.sha256.new(secret_key) | 
|  | 21 | +  hmac_sha256:update(value) | 
|  | 22 | +  local binary = hmac_sha256:digest() | 
|  | 23 | + | 
|  | 24 | +  return binary | 
|  | 25 | +end | 
|  | 26 | + | 
|  | 27 | +local function sha256_hexdigest(value) | 
|  | 28 | +  local sha256 = resty_sha256:new() | 
|  | 29 | +  sha256:update(value or "") | 
|  | 30 | +  return to_hex(sha256:final()) | 
|  | 31 | +end | 
|  | 32 | + | 
|  | 33 | +local function canonical_header_name(name) | 
|  | 34 | +  return string.lower(name) | 
|  | 35 | +end | 
|  | 36 | + | 
|  | 37 | +local function canonical_header_value(value) | 
|  | 38 | +  return gsub(value, [[\s+]], " ", "jo") | 
|  | 39 | +end | 
|  | 40 | + | 
|  | 41 | +local function escape_uri_component(value) | 
|  | 42 | +  if(value == true) then | 
|  | 43 | +    return "" | 
|  | 44 | +  else | 
|  | 45 | +    return escape_uri(value or "") | 
|  | 46 | +  end | 
|  | 47 | +end | 
|  | 48 | + | 
|  | 49 | +local function get_headers() | 
|  | 50 | +  local headers = {} | 
|  | 51 | + | 
|  | 52 | +  local raw_headers = ngx.req.get_headers() | 
|  | 53 | +  for name, value in pairs(raw_headers) do | 
|  | 54 | +    if type(value) == "table" then | 
|  | 55 | +      for multi_name, multi_value in pairs(value) do | 
|  | 56 | +        table.insert(headers, { | 
|  | 57 | +          name = canonical_header_name(multi_name), | 
|  | 58 | +          value = canonical_header_value(multi_value), | 
|  | 59 | +        }) | 
|  | 60 | +      end | 
|  | 61 | +    else | 
|  | 62 | +      table.insert(headers, { | 
|  | 63 | +        name = canonical_header_name(name), | 
|  | 64 | +        value = canonical_header_value(value), | 
|  | 65 | +      }) | 
|  | 66 | +    end | 
|  | 67 | +  end | 
|  | 68 | + | 
|  | 69 | +  return headers | 
|  | 70 | +end | 
|  | 71 | + | 
|  | 72 | +local function get_canonical_headers(headers) | 
|  | 73 | +  local canonical = {} | 
|  | 74 | +  for _, header in ipairs(headers) do | 
|  | 75 | +    if not UNSIGNED_HEADERS[header.name] then | 
|  | 76 | +      table.insert(canonical, header.name .. ":" .. header.value) | 
|  | 77 | +    end | 
|  | 78 | +  end | 
|  | 79 | + | 
|  | 80 | +  table.sort(canonical) | 
|  | 81 | +  return table.concat(canonical, "\n") | 
|  | 82 | +end | 
|  | 83 | + | 
|  | 84 | +local function get_signed_headers(headers) | 
|  | 85 | +  local signed = {} | 
|  | 86 | +  for _, header in ipairs(headers) do | 
|  | 87 | +    if not UNSIGNED_HEADERS[header.name] then | 
|  | 88 | +      table.insert(signed, header.name) | 
|  | 89 | +    end | 
|  | 90 | +  end | 
|  | 91 | + | 
|  | 92 | +  table.sort(signed) | 
|  | 93 | +  return table.concat(signed, ";") | 
|  | 94 | +end | 
|  | 95 | + | 
|  | 96 | +local function get_canonical_query_string() | 
|  | 97 | +  local canonical = {} | 
|  | 98 | +  local args = ngx.req.get_uri_args() | 
|  | 99 | +  for name, value in pairs(args) do | 
|  | 100 | +    if type(value) == "table" then | 
|  | 101 | +      for multi_name, multi_value in pairs(value) do | 
|  | 102 | +        table.insert(canonical, escape_uri_component(multi_name) .. "=" .. escape_uri_component(multi_value)) | 
|  | 103 | +      end | 
|  | 104 | +    else | 
|  | 105 | +      table.insert(canonical, escape_uri_component(name) .. "=" .. escape_uri_component(value)) | 
|  | 106 | +    end | 
|  | 107 | +  end | 
|  | 108 | + | 
|  | 109 | +  table.sort(canonical) | 
|  | 110 | +  return table.concat(canonical, "&") | 
|  | 111 | +end | 
|  | 112 | + | 
|  | 113 | +local function get_canonical_request(headers, signed_headers, content_sha256) | 
|  | 114 | +  return table.concat({ | 
|  | 115 | +    ngx.var.request_method, | 
|  | 116 | +    gsub(escape_uri(ngx.var.uri), [[%2F]], "/", "ijo"), | 
|  | 117 | +    get_canonical_query_string(), | 
|  | 118 | +    get_canonical_headers(headers) .. "\n", | 
|  | 119 | +    signed_headers, | 
|  | 120 | +    content_sha256, | 
|  | 121 | +  }, "\n") | 
|  | 122 | +end | 
|  | 123 | + | 
|  | 124 | +local function get_credential_scope(aws_region, date) | 
|  | 125 | +  return table.concat({ | 
|  | 126 | +    date, | 
|  | 127 | +    aws_region, | 
|  | 128 | +    AWS_SERVICE, | 
|  | 129 | +    "aws4_request", | 
|  | 130 | +  }, "/") | 
|  | 131 | +end | 
|  | 132 | + | 
|  | 133 | +local function get_string_to_sign(datetime, credential_scope, canonical_request) | 
|  | 134 | +  return table.concat({ | 
|  | 135 | +    "AWS4-HMAC-SHA256", | 
|  | 136 | +    datetime, | 
|  | 137 | +    credential_scope, | 
|  | 138 | +    sha256_hexdigest(canonical_request), | 
|  | 139 | +  }, "\n") | 
|  | 140 | +end | 
|  | 141 | + | 
|  | 142 | +local function get_signature(aws_region, aws_secret_access_key, date, string_to_sign) | 
|  | 143 | +  local k_date = hmac("AWS4" .. aws_secret_access_key, date) | 
|  | 144 | +  local k_region = hmac(k_date, aws_region) | 
|  | 145 | +  local k_service = hmac(k_region, AWS_SERVICE) | 
|  | 146 | +  local k_credentials = hmac(k_service, "aws4_request") | 
|  | 147 | +  return to_hex(hmac(k_credentials, string_to_sign)) | 
|  | 148 | +end | 
|  | 149 | + | 
|  | 150 | +local function get_authorization(aws_access_key_id, credential_scope, signed_headers, signature) | 
|  | 151 | +  return table.concat({ | 
|  | 152 | +    "AWS4-HMAC-SHA256 Credential=" .. aws_access_key_id .. "/" .. credential_scope, | 
|  | 153 | +    "SignedHeaders=" .. signed_headers, | 
|  | 154 | +    "Signature=" .. signature, | 
|  | 155 | +  }, ", ") | 
|  | 156 | +end | 
|  | 157 | + | 
|  | 158 | +function _M.sign_request(aws_region, aws_access_key_id, aws_secret_access_key) | 
|  | 159 | +  local datetime = os.date("!%Y%m%dT%H%M%SZ", ngx.now()) | 
|  | 160 | +  local date = string.sub(datetime, 1, 8) | 
|  | 161 | +  ngx.req.set_header("X-Amz-Date", os.date("!%Y%m%dT%H%M%SZ", ngx.now())) | 
|  | 162 | + | 
|  | 163 | +  ngx.req.read_body() | 
|  | 164 | +  local body = ngx.req.get_body_data() | 
|  | 165 | +  local content_sha256 = sha256_hexdigest(body) | 
|  | 166 | +  ngx.req.set_header("X-Amz-Content-Sha256", content_sha256) | 
|  | 167 | + | 
|  | 168 | +  local headers = get_headers() | 
|  | 169 | +  local signed_headers = get_signed_headers(headers) | 
|  | 170 | +  local credential_scope = get_credential_scope(aws_region, date) | 
|  | 171 | + | 
|  | 172 | +  local canonical_request = get_canonical_request(headers, signed_headers, content_sha256) | 
|  | 173 | +  local string_to_sign = get_string_to_sign(datetime, credential_scope, canonical_request) | 
|  | 174 | +  local signature = get_signature(aws_region, aws_secret_access_key, date, string_to_sign) | 
|  | 175 | +  local authorization = get_authorization(aws_access_key_id, credential_scope, signed_headers, signature) | 
|  | 176 | +  ngx.req.set_header("Authorization", authorization) | 
|  | 177 | +end | 
|  | 178 | + | 
|  | 179 | +return _M | 
0 commit comments