Skip to content

Commit bc82019

Browse files
feat: support authentication via headers
Signed-off-by: Abhishek Choudhary <[email protected]>
1 parent 7f56ffc commit bc82019

File tree

2 files changed

+94
-9
lines changed

2 files changed

+94
-9
lines changed

apisix/plugins/elasticsearch-logger.lua

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,20 @@ local schema = {
6262
type = "string",
6363
minLength = 1
6464
},
65+
header_name = {
66+
type = "string",
67+
minLength = 1
68+
},
69+
header_value = {
70+
type = "string",
71+
minLength = 1
72+
}
6573
},
66-
required = {"username", "password"},
74+
oneOf = {
75+
{required = {"username", "password"}},
76+
{required = {"header_name", "header_value"}},
77+
{maxProperties = 0},
78+
}
6779
},
6880
timeout = {
6981
type = "integer",
@@ -135,18 +147,38 @@ function _M.check_schema(conf, schema_type)
135147
end
136148

137149

150+
local function fill_auth_info(conf)
151+
local headers = {}
152+
if conf.auth.username ~= nil and conf.auth.password ~= nil then
153+
local authorization = "Basic " .. ngx.encode_base64(
154+
conf.auth.username .. ":" .. conf.auth.password
155+
)
156+
headers["Authorization"] = authorization
157+
return headers
158+
end
159+
if conf.auth.header_name ~= nil and conf.auth.header_value ~= nil then
160+
headers[conf.auth.header_name] = conf.auth.header_value
161+
return headers
162+
end
163+
return nil
164+
end
165+
166+
138167
local function get_es_major_version(uri, conf)
139168
local httpc = http.new()
140169
if not httpc then
141170
return nil, "failed to create http client"
142171
end
172+
143173
local headers = {}
144174
if conf.auth then
145-
local authorization = "Basic " .. ngx.encode_base64(
146-
conf.auth.username .. ":" .. conf.auth.password
147-
)
148-
headers["Authorization"] = authorization
175+
local auth_headers = fill_auth_info(conf)
176+
if not auth_headers then
177+
return nil, "failed to fill auth info"
178+
end
179+
headers = auth_headers
149180
end
181+
150182
httpc:set_timeout(conf.timeout * 1000)
151183
local res, err = httpc:request_uri(uri, {
152184
ssl_verify = conf.ssl_verify,
@@ -230,10 +262,12 @@ local function send_to_elasticsearch(conf, entries)
230262
["Accept"] = "application/vnd.elasticsearch+json"
231263
}
232264
if conf.auth then
233-
local authorization = "Basic " .. ngx.encode_base64(
234-
conf.auth.username .. ":" .. conf.auth.password
235-
)
236-
headers["Authorization"] = authorization
265+
local auth_headers = fill_auth_info(conf)
266+
if auth_headers then
267+
for k, v in pairs(auth_headers) do
268+
headers[k] = v
269+
end
270+
end
237271
end
238272

239273
core.log.info("uri: ", uri, ", body: ", body)

t/plugin/elasticsearch-logger2.t

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,54 @@ location /t {
119119
--- error_log
120120
max pending entries limit exceeded. discarding entry
121121
--- timeout: 5
122+
123+
124+
125+
=== TEST 2: set route with header auth
126+
--- config
127+
location /t {
128+
content_by_lua_block {
129+
local t = require("lib.test_admin").test
130+
local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, {
131+
uri = "/hello",
132+
upstream = {
133+
type = "roundrobin",
134+
nodes = {
135+
["127.0.0.1:1980"] = 1
136+
}
137+
},
138+
plugins = {
139+
["elasticsearch-logger"] = {
140+
endpoint_addr = "http://127.0.0.1:9201",
141+
field = {
142+
index = "services"
143+
},
144+
auth = {
145+
header_name = "Authorization",
146+
header_value = "ZWxhc3RpYzoxMjM0NTY="
147+
},
148+
batch_max_size = 1,
149+
inactive_timeout = 1
150+
}
151+
}
152+
})
153+
154+
if code >= 300 then
155+
ngx.status = code
156+
end
157+
ngx.say(body)
158+
}
159+
}
160+
--- response_body
161+
passed
162+
163+
164+
165+
=== TEST 3: test route (auth success)
166+
--- request
167+
GET /hello
168+
--- wait: 2
169+
--- response_body
170+
hello world
171+
--- error_log
172+
Batch Processor[elasticsearch-logger] successfully processed the entries

0 commit comments

Comments
 (0)