Skip to content

fix(*): lazily initialize structures to avoid c-boundary errors on require #87

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 2 commits into from
Sep 19, 2023
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
11 changes: 10 additions & 1 deletion src/resty/aws/credentials/CredentialProviderChain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ CredentialProviderChain.__index = CredentialProviderChain
local aws_config = require("resty.aws.config")


CredentialProviderChain.defaultProviders = {} do
CredentialProviderChain.defaultProviders = {}


local function initialize()
-- while not everything is implemented this will load what we do have without
-- failing on what is missing. Will auto pick up newly added classes afterwards.
local function add_if_exists(name, opts)
Expand Down Expand Up @@ -41,6 +44,8 @@ CredentialProviderChain.defaultProviders = {} do
else
add_if_exists("EC2MetadataCredentials")
end

initialize = nil
end

--- Constructor, inherits from `Credentials`.
Expand All @@ -65,6 +70,10 @@ end
-- @param opt options table, additional fields to the `Credentials` class:
-- @param opt.providers array of `Credentials` objects or functions (functions must return a `Credentials` object)
function CredentialProviderChain:new(opts)
if initialize then
initialize()
end

local self = Super:new(opts) -- override 'self' to be the new object/class
setmetatable(self, CredentialProviderChain)

Expand Down
13 changes: 10 additions & 3 deletions src/resty/aws/credentials/RemoteCredentials.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ local http = require "resty.luasocket.http"
local json = require "cjson"


local FullUri do
-- construct the URL
local FullUri


local function initialize()
-- construct the URL
local function makeset(t)
for i = 1, #t do
t[t[i]] = true
Expand Down Expand Up @@ -73,8 +75,9 @@ local FullUri do
FullUri.port = FullUri.port or
({ http = 80, https = 443 })[FullUri.scheme]
end
end

initialize = nil
end


-- Create class
Expand All @@ -96,6 +99,10 @@ end
-- updates credentials.
-- @return success, or nil+err
function RemoteCredentials:refresh()
if initialize then
initialize()
end

if not FullUri then
return nil, "No URI environment variables found for RemoteCredentials"
end
Expand Down
4 changes: 2 additions & 2 deletions src/resty/aws/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local tablex = require("pl.tablex")
local lookup_helper = function(self, key) -- signature to match __index meta-method
if type(key) == "string" then
local lckey = key:lower()
for k,v in pairs(self) do
for k in pairs(self) do
if type(k) == "string" and k:lower() == lckey then
error(("key '%s' not found, did you mean '%s'?"):format(key, k), 2)
end
Expand Down Expand Up @@ -193,7 +193,7 @@ do
-- @param service service-instance being created; field `aws` is the aws instance,
-- `config` is the service instance config, `api` the service api.
function AWS.configureEndpoint(service)
for i, key in ipairs(derivedKeys(service)) do
for _, key in ipairs(derivedKeys(service)) do

local region_rule_config = aws_config.region.rules[key] --> contains regions templates
if type(region_rule_config) == 'string' then
Expand Down
2 changes: 1 addition & 1 deletion src/resty/aws/request/validate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ local validators do
local list_checks do
local function get_length(t) -- gets length of an array (with holes)
local size = 0
for k,v in pairs(t) do
for k in pairs(t) do
if type(k) ~= "number" then
return nil, "list contains non-numeric indices"
end
Expand Down