Skip to content

Commit 55b1dd2

Browse files
fix: timeout risk in usages of lua-resty-aws (#12070)
1 parent 0a06828 commit 55b1dd2

5 files changed

+32
-2
lines changed

apisix/plugins/ai-aws-content-moderation.lua

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
-- See the License for the specific language governing permissions and
1515
-- limitations under the License.
1616
--
17+
require("resty.aws.config") -- to read env vars before initing aws module
18+
1719
local core = require("apisix.core")
18-
local aws_instance = require("resty.aws")()
20+
local aws = require("resty.aws")
21+
local aws_instance
22+
1923
local http = require("resty.http")
2024
local fetch_secrets = require("apisix.secret").fetch_secrets
2125

@@ -96,6 +100,9 @@ function _M.rewrite(conf, ctx)
96100

97101
local comprehend = conf.comprehend
98102

103+
if not aws_instance then
104+
aws_instance = aws()
105+
end
99106
local credentials = aws_instance:Credentials({
100107
accessKeyId = comprehend.access_key_id,
101108
secretAccessKey = comprehend.secret_access_key,

apisix/secret/aws.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
--
1717

1818
--- AWS Tools.
19+
require("resty.aws.config") -- to read env vars before initing aws module
20+
1921
local core = require("apisix.core")
2022
local http = require("resty.http")
2123
local aws = require("resty.aws")
24+
local aws_instance
2225

2326
local sub = core.string.sub
2427
local find = core.string.find
@@ -51,7 +54,9 @@ local _M = {
5154
}
5255

5356
local function make_request_to_aws(conf, key)
54-
local aws_instance = aws()
57+
if not aws_instance then
58+
aws_instance = aws()
59+
end
5560

5661
local region = conf.region
5762

t/plugin/ai-aws-content-moderation-secrets.t

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ add_block_preprocessor(sub {
3333
$block->set_value("request", "GET /t");
3434
}
3535

36+
my $main_config = $block->main_config // <<_EOC_;
37+
env AWS_REGION=us-east-1;
38+
_EOC_
39+
40+
$block->set_value("main_config", $main_config);
41+
3642
my $http_config = $block->http_config // <<_EOC_;
3743
server {
3844
listen 2668;

t/plugin/ai-aws-content-moderation.t

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ add_block_preprocessor(sub {
3030
$block->set_value("request", "GET /t");
3131
}
3232

33+
my $main_config = $block->main_config // <<_EOC_;
34+
env AWS_REGION=us-east-1;
35+
_EOC_
36+
37+
$block->set_value("main_config", $main_config);
38+
3339
my $http_config = $block->http_config // <<_EOC_;
3440
server {
3541
listen 2668;

t/plugin/ai-aws-content-moderation2.t

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ no_root_location();
2626
add_block_preprocessor(sub {
2727
my ($block) = @_;
2828

29+
my $main_config = $block->main_config // <<_EOC_;
30+
env AWS_REGION=us-east-1;
31+
_EOC_
32+
33+
$block->set_value("main_config", $main_config);
34+
2935
if (!defined $block->request) {
3036
$block->set_value("request", "GET /t");
3137
}

0 commit comments

Comments
 (0)