Skip to content

feat: remove libexpat dependency #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: Test

on: [push, pull_request]

env:
LIBEXPAT_DOWNLOAD_URL: https://github.com/libexpat/libexpat/releases/download/R_2_5_0/expat-2.5.0.tar.gz
on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:

Expand All @@ -16,6 +17,8 @@ jobs:
openresty_version:
- 1.17.8.2
- 1.19.9.1
- 1.21.4.3
- 1.25.3.2

runs-on: ubuntu-latest
container:
Expand All @@ -30,18 +33,6 @@ jobs:
apk add --no-cache curl perl bash wget git perl-dev libarchive-tools nodejs jq
ln -s /usr/bin/bsdtar /usr/bin/tar

- name: Build libexpat
if: matrix.openresty_version == '1.17.8.2'
run: |
mkdir -p /tmp/expat
curl -Ls $LIBEXPAT_DOWNLOAD_URL | tar -xz --strip-components=1 -f - -C /tmp/expat
cd /tmp/expat && ./configure && make && make install

- name: Install libexpat from package manager
if: matrix.openresty_version == '1.19.9.1'
run: |
apk add --no-cache expat-dev

- name: Cache
uses: actions/cache@v2
with:
Expand Down
1 change: 0 additions & 1 deletion lua-resty-aws-dev-1.rockspec.template
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ dependencies = {
"lua-resty-http >= 0.16",
"lua-resty-luasocket ~> 1",
"lua-resty-openssl >= 0.8.17",
"luaexpat >= 1.5.1",
}

build = {
Expand Down
88 changes: 4 additions & 84 deletions spec/02-requests/02-build_request_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local cjson = require "cjson"
local pl_stringx = require "pl.stringx"

describe("operations protocol", function()

Expand Down Expand Up @@ -37,10 +36,6 @@ describe("operations protocol", function()
},
input = {
type = "structure",
locationName = "mainXmlElement", -- only for rest-xml protocol
xmlNamespace = { -- only for rest-xml protocol
uri = "cool-name-space"
},
required = {
"RoleArn",
"RoleSessionName"
Expand Down Expand Up @@ -206,6 +201,7 @@ describe("operations protocol", function()
local request = build_request(operation, config, params)
assert.same({
headers = {
["Accept"] = 'application/json',
["X-Sooper-Secret"] = "towel",
["X-Amz-Target"] = "sts.AssumeRole",
["Host"] = "sts.amazonaws.com",
Expand Down Expand Up @@ -239,6 +235,7 @@ describe("operations protocol", function()

assert.same({
headers = {
["Accept"] = 'application/json',
["X-Sooper-Secret"] = "towel",
["Content-Length"] = 172,
["Content-Type"] = "application/x-amz-json-1.0",
Expand Down Expand Up @@ -279,6 +276,7 @@ describe("operations protocol", function()

assert.same({
headers = {
["Accept"] = 'application/json',
["X-Sooper-Secret"] = "towel",
["Content-Length"] = 172,
["Content-Type"] = "application/x-amz-json-1.0",
Expand Down Expand Up @@ -314,6 +312,7 @@ describe("operations protocol", function()

assert.same({
headers = {
["Accept"] = 'application/json',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever and this would be much better!

["Content-Length"] = 4,
["X-Amz-Target"] = "s3.PutObject",
["Host"] = "s3.amazonaws.com",
Expand All @@ -328,85 +327,6 @@ describe("operations protocol", function()
end)


it("rest-xml: querystring, uri, header and body params", function()

config.protocol = "rest-xml"

local request = build_request(operation, config, params)
if request and request.body then
-- cannot reliably compare non-canonicalized json, so decode to Lua table
local body_lines = pl_stringx.splitlines(request.body)
for i, line in ipairs(body_lines) do
body_lines[i] = pl_stringx.strip(line, ' ')
end
request.body = assert(require("pl.xml").parse(table.concat(body_lines, "")))
local to_lua = function(t)
-- convert LOM to comparable Lua table
for i, v in ipairs(t) do
if type(v) == "table" and v.tag then
t[v.tag] = v
v.tag = nil
t[i] = nil
if type(v.attr) == "table" and not next(v.attr) then
-- delete empty attr table
v.attr = nil
end
end
end
end
to_lua(request.body)
to_lua(request.body.someSubStructure)
end

assert.same({
headers = {
["X-Sooper-Secret"] = "towel",
["Content-Length"] = 456,
["Content-Type"] = "application/xml",
["X-Amz-Target"] = "sts.AssumeRole",
["Host"] = "sts.amazonaws.com",
},
method = 'POST',
path = '/hello%20world/42',
host = 'sts.amazonaws.com',
port = 443,
body = {
RoleArn = {
[1] = 'hello' },
RoleSessionName = {
[1] = 'world' },
BinaryData = {
[1] = binary_data },
attr = {
[1] = 'xmlns',
xmlns = 'cool-name-space' },
someSubStructure = {
hello = {
[1] = 'the default hello thinghy' },
world = {
[1] = 'the default world thinghy' } },
subList = {
[1] = {
[1] = '1',
attr = {},
tag = 'listELement' },
[2] = {
[1] = '2',
attr = {},
tag = 'listELement' },
[3] = {
[1] = '3',
attr = {},
tag = 'listELement' } },
tag = 'mainXmlElement' },
query = {
UserId = "Arthur Dent",
nice = '',
}
}, request)
end)


pending("ec2: querystring, uri, header and body params", function()

config.protocol = "ec2"
Expand Down
21 changes: 14 additions & 7 deletions spec/02-requests/03-execute_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ describe("request execution", function()

it("tls defaults to true", function ()
local config = {
region = "us-east-1"
region = "us-east-1",
protocol = "json",
}

config.credentials = Credentials:new({
Expand All @@ -76,7 +77,8 @@ describe("request execution", function()

it("support configuring tls false", function ()
local config = {
region = "us-east-1"
region = "us-east-1",
protocol = "json",
}

config.credentials = Credentials:new({
Expand All @@ -103,7 +105,8 @@ describe("request execution", function()

it("support configuring ssl verify false", function ()
local config = {
region = "us-east-1"
region = "us-east-1",
protocol = "json"
}

config.credentials = Credentials:new({
Expand All @@ -129,7 +132,8 @@ describe("request execution", function()

it("support configure timeout", function ()
local config = {
region = "us-east-1"
region = "us-east-1",
protocol = "json",
}

config.credentials = Credentials:new({
Expand All @@ -155,7 +159,8 @@ describe("request execution", function()

it("support configure keepalive idle timeout", function ()
local config = {
region = "us-east-1"
region = "us-east-1",
protocol = "json",
}

config.credentials = Credentials:new({
Expand All @@ -181,7 +186,8 @@ describe("request execution", function()

it("support set proxy options", function ()
local config = {
region = "us-east-1"
region = "us-east-1",
protocol = "json",
}

config.credentials = Credentials:new({
Expand Down Expand Up @@ -217,7 +223,8 @@ describe("request execution", function()

it("decoded json body should have array metatable", function ()
local config = {
region = "us-east-1"
region = "us-east-1",
protocol = "json",
}

config.credentials = Credentials:new({
Expand Down
22 changes: 10 additions & 12 deletions src/resty/aws/credentials/ChainableTemporaryCredentials.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- ChainableTemporaryCredentials class.
-- @classmod ChainableTemporaryCredentials

local lom = require("lxp.lom")
local cjson = require("cjson")


-- Create class
Expand Down Expand Up @@ -117,19 +117,17 @@ function ChainableTemporaryCredentials:refresh()
return nil, "request for token returned invalid body: " .. err
end

local resp_body_lom, err = lom.parse(response.body)
if not resp_body_lom then
local data, err = cjson.decode(response.body)
if not data then
return nil, "failed to parse response body: " .. err
end

local cred_lom = lom.find_elem(lom.find_elem(resp_body_lom, "AssumeRoleResult"), "Credentials")

local AccessKeyId = lom.find_elem(cred_lom, "AccessKeyId")[1]
local SecretAccessKey = lom.find_elem(cred_lom, "SecretAccessKey")[1]
local SessionToken = lom.find_elem(cred_lom, "SessionToken")[1]
local Expiration = lom.find_elem(cred_lom, "Expiration")[1]

self:set(AccessKeyId, SecretAccessKey, SessionToken, Expiration)
local credentials = data.AssumeRoleResponse.AssumeRoleResult.Credentials
self:set(
credentials.AccessKeyId,
credentials.SecretAccessKey,
credentials.SessionToken,
credentials.Expiration
)

return true
end
Expand Down
22 changes: 10 additions & 12 deletions src/resty/aws/credentials/TokenFileWebIdentityCredentials.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- @classmod TokenFileWebIdentityCredentials

local readfile = require("pl.utils").readfile
local lom = require("lxp.lom")
local cjson = require("cjson")

local aws_config = require("resty.aws.config")

Expand Down Expand Up @@ -79,19 +79,17 @@ function TokenFileWebIdentityCredentials:refresh()
return nil, "request for token returned invalid body: " .. err
end

local resp_body_lom, err = lom.parse(response.body)
if not resp_body_lom then
local data, err = cjson.decode(response.body)
if not data then
return nil, "failed to parse response body: " .. err
end

local cred_lom = lom.find_elem(lom.find_elem(resp_body_lom, "AssumeRoleWithWebIdentityResult"), "Credentials")

local AccessKeyId = lom.find_elem(cred_lom, "AccessKeyId")[1]
local SecretAccessKey = lom.find_elem(cred_lom, "SecretAccessKey")[1]
local SessionToken = lom.find_elem(cred_lom, "SessionToken")[1]
local Expiration = lom.find_elem(cred_lom, "Expiration")[1]

self:set(AccessKeyId, SecretAccessKey, SessionToken, Expiration)
local credentials = data.AssumeRoleWithWebIdentityResponse.AssumeRoleWithWebIdentityResult.Credentials
self:set(
credentials.AccessKeyId,
credentials.SecretAccessKey,
credentials.SessionToken,
credentials.Expiration
)

return true
end
Expand Down
Loading
Loading