Skip to content
Open
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
17 changes: 13 additions & 4 deletions apisix/plugins/elasticsearch-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local http = require("resty.http")
local log_util = require("apisix.utils.log-util")
local bp_manager_mod = require("apisix.utils.batch-processor-manager")
local plugin = require("apisix.plugin")
local fetch_secrets = require("apisix.secret").fetch_secrets
local ngx = ngx
local str_format = core.string.format
local math_random = math.random
Expand Down Expand Up @@ -220,17 +221,22 @@ local function fetch_and_update_es_version(conf)
if conf._version then
return
end

-- resolve secrets & env vars
local conf_resolved = fetch_secrets(conf, true)

local selected_endpoint_addr
if conf.endpoint_addr then
selected_endpoint_addr = conf.endpoint_addr
selected_endpoint_addr = conf_resolved.endpoint_addr
else
selected_endpoint_addr = conf.endpoint_addrs[math_random(#conf.endpoint_addrs)]
selected_endpoint_addr = conf_resolved.endpoint_addrs[math_random(#conf_resolved.endpoint_addrs)]
end
local major_version, err = get_es_major_version(selected_endpoint_addr, conf)
local major_version, err = get_es_major_version(selected_endpoint_addr, conf_resolved)
if err then
core.log.error("failed to get Elasticsearch version: ", err)
return
end

conf._version = major_version
end

Expand All @@ -240,7 +246,10 @@ local function send_to_elasticsearch(conf, entries)
if not httpc then
return false, str_format("create http error: %s", err)
end
fetch_and_update_es_version(conf)

-- resolve secrets & env vars
conf = fetch_secrets(conf, true)

local selected_endpoint_addr
if conf.endpoint_addr then
selected_endpoint_addr = conf.endpoint_addr
Expand Down
55 changes: 55 additions & 0 deletions t/plugin/elasticsearch-logger.t
Original file line number Diff line number Diff line change
Expand Up @@ -992,3 +992,58 @@ GET /hello
hello world
--- error_log
Batch Processor[elasticsearch-logger] successfully processed the entries



=== TEST 27: set route (auth.password from env variable via $env://)
--- main_config
env ES_PASSWORD=123456;
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, {
uri = "/hello",
upstream = {
type = "roundrobin",
nodes = {
["127.0.0.1:1980"] = 1
}
},
plugins = {
["elasticsearch-logger"] = {
endpoint_addr = "http://127.0.0.1:9201",
field = {
index = "services"
},
auth = {
username = "elastic",
password = "$env://ES_PASSWORD"
},
batch_max_size = 1,
inactive_timeout = 1
}
}
})

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed



=== TEST 28: test route (auth.password from env variable, auth success)
--- main_config
env ES_PASSWORD=123456;
--- request
GET /hello
--- wait: 2
--- response_body
hello world
--- error_log
Batch Processor[elasticsearch-logger] successfully processed the entries
Loading