From 8cbbfbdd8867ca0206d67ca20ae40763a5616304 Mon Sep 17 00:00:00 2001 From: bencoxx Date: Mon, 20 Nov 2023 16:17:46 +0100 Subject: [PATCH 1/5] Add connectionstring to client hosts config --- README.md | 1 + src/elasticsearch/Settings.lua | 4 ++++ src/elasticsearch/connection/Connection.lua | 9 +++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ce73bfa..bb8ae4a 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ The complete documetation is [here](http://elasticsearch-lua.readthedocs.io/). hosts = { { -- Ignoring any of the following hosts parameters is allowed. -- The default shall be set + connectionstring = "", protocol = "http", host = "localhost", port = 9200, diff --git a/src/elasticsearch/Settings.lua b/src/elasticsearch/Settings.lua index f1b2ebe..d020f0c 100644 --- a/src/elasticsearch/Settings.lua +++ b/src/elasticsearch/Settings.lua @@ -27,6 +27,7 @@ local Settings = {} -- Initial seed of hosts Settings.hosts = { { + connectionstring = "", protocol = "http", host = "localhost", port = 9200, @@ -131,6 +132,7 @@ function Settings:setParameters() -- Checking hosts if self.user_hosts ~= nil then local default_host = { + connectionstring = self.hosts[1].connectionstring, protocol = self.hosts[1].protocol, host = self.hosts[1].host, port = self.hosts[1].port, @@ -142,6 +144,7 @@ function Settings:setParameters() self.hosts = {} for i, v in pairs(self.user_hosts) do self.hosts[i] = { + connectionstring = default_host.connectionstring, protocol = default_host.protocol, host = default_host.host, port = default_host.port, @@ -172,6 +175,7 @@ function Settings:setConnectionSettings() self.connections = {} for key, host in pairs(self.hosts) do table.insert(self.connections, Connection:new{ + connectionstring = host.connectionstring, protocol = host.protocol, host = host.host, port = host.port, diff --git a/src/elasticsearch/connection/Connection.lua b/src/elasticsearch/connection/Connection.lua index 70b5d5a..107dfc2 100644 --- a/src/elasticsearch/connection/Connection.lua +++ b/src/elasticsearch/connection/Connection.lua @@ -16,7 +16,8 @@ local Connection = {} ------------------------------------------------------------------------------- -- Declaring instance variables ------------------------------------------------------------------------------- - +-- The FULL connection string should be use +Connection.connectionstring = nil -- The protocol of the connection Connection.protocol = nil -- The host where the connection should be made @@ -83,7 +84,7 @@ function Connection:request(method, uri, params, body, timeout) request.source = ltn12.source.string(body) end -- Adding auth to request - if self.username ~= nil and self.password ~= nil then + if self.username ~= "" and self.password ~= "" then local authStr, foo = mime.b64(self.username .. ':' .. self.password) request.headers['Authorization'] = 'Basic ' .. authStr end @@ -171,6 +172,10 @@ function Connection:buildURI(uri, params) port = self.port, path = uri } + if self.connectionstring ~= "" then + urlComponents = url.parse(self.connectionstring) + urlComponents.path = uri + end if params ~= nil then urlComponents.query = self:buildQuery(params) end From 664e083dffb5d6e673403081a9dc0710bc3e5278 Mon Sep 17 00:00:00 2001 From: BencoXX Date: Thu, 17 Oct 2024 13:01:10 +0200 Subject: [PATCH 2/5] Update README.md Co-authored-by: Neil Cook --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bb8ae4a..8b3b8d5 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ The complete documetation is [here](http://elasticsearch-lua.readthedocs.io/). hosts = { { -- Ignoring any of the following hosts parameters is allowed. -- The default shall be set - connectionstring = "", + connectionstring = "", -- Ignore host, port and protocol and parse this string instead protocol = "http", host = "localhost", port = 9200, From c4579568397961c595865e444726ae5a3fd37e77 Mon Sep 17 00:00:00 2001 From: bencoxx Date: Sat, 19 Oct 2024 09:30:10 +0200 Subject: [PATCH 3/5] Enhance code stability by adding nil checks in conditions --- src/elasticsearch/connection/Connection.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/elasticsearch/connection/Connection.lua b/src/elasticsearch/connection/Connection.lua index 107dfc2..4260193 100644 --- a/src/elasticsearch/connection/Connection.lua +++ b/src/elasticsearch/connection/Connection.lua @@ -84,7 +84,7 @@ function Connection:request(method, uri, params, body, timeout) request.source = ltn12.source.string(body) end -- Adding auth to request - if self.username ~= "" and self.password ~= "" then + if self.username and self.username ~= "" and self.password and self.password ~= "" then local authStr, foo = mime.b64(self.username .. ':' .. self.password) request.headers['Authorization'] = 'Basic ' .. authStr end @@ -172,7 +172,7 @@ function Connection:buildURI(uri, params) port = self.port, path = uri } - if self.connectionstring ~= "" then + if self.connectionstring and self.connectionstring ~= "" then urlComponents = url.parse(self.connectionstring) urlComponents.path = uri end From 0fe49faa2fadf9f57548fcd6fc3f75358fcdf8a3 Mon Sep 17 00:00:00 2001 From: bencoxx Date: Sat, 19 Oct 2024 10:14:25 +0200 Subject: [PATCH 4/5] Testing test suite --- .vscode/settings.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c9c37bb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "github-actions.workflows.pinned.workflows": [ + ".github/workflows/test-all.yml" + ] +} \ No newline at end of file From a289db55d86b749bdecc26e50b1ca95b40082d3e Mon Sep 17 00:00:00 2001 From: bencoxx Date: Sat, 19 Oct 2024 11:10:48 +0200 Subject: [PATCH 5/5] Add connectionstring property test --- .github/workflows/test-all.yml | 3 +++ tests/connection/ConnectionTest.lua | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/.github/workflows/test-all.yml b/.github/workflows/test-all.yml index 5c02d99..6af5dfb 100644 --- a/.github/workflows/test-all.yml +++ b/.github/workflows/test-all.yml @@ -17,6 +17,7 @@ jobs: ES_PASSWORD: "Changeme12456!" ES_TEST_PROTOCOL: http ES_TEST_PORT: 9200 + ES_TEST_CONNECTIONSTRING: "http://localhost:9200" CACERT_PATH: ELASTIC_CREDS: PROTOCOL: HTTP @@ -61,6 +62,7 @@ jobs: env: ES_TEST_PROTOCOL: https ES_TEST_PORT: 19200 + ES_TEST_CONNECTIONSTRING: "https://localhost:19200" ELASTIC_CREDS: -u ${{env.ES_USERNAME}}:${{env.ES_PASSWORD}} PROTOCOL: https run: cd tests && luarocks path >lpath.sh && source lpath.sh && lua run-tests.lua @@ -68,6 +70,7 @@ jobs: if: ${{ matrix.elastic == 'os2' }} env: ES_TEST_PORT: 29200 + ES_TEST_CONNECTIONSTRING: "http://localhost:29200" run: cd tests && luarocks path >lpath.sh && source lpath.sh && lua run-tests.lua - name: run the tests for el7 if: ${{ matrix.elastic == 'el7' }} diff --git a/tests/connection/ConnectionTest.lua b/tests/connection/ConnectionTest.lua index b533ad3..4ec710c 100644 --- a/tests/connection/ConnectionTest.lua +++ b/tests/connection/ConnectionTest.lua @@ -66,6 +66,20 @@ function buildURITest() con.protocol .. "://" .. con.host .. ":" .. con.port .. "/path?b=b_s&a=a_s" == url) url = con:buildURI("/path", {a="a_s"}) assert_not_equal(con.protocol .. "://" .. con.host .. ":" .. con.port .. "/path?a=a_s&b=b_s", url) + local connectionstring = os.getenv("ES_TEST_CONNECTIONSTRING") + if connectionstring == nil then connectionstring = "http://localhost:9200" end + local con = Connection:new{ + connectionstring = connectionstring, + username = username, + password = password, + pingTimeout = 1, + logger = logger, + requestEngine = "LuaSocket" + } + local url = con:buildURI("/path", {a="a_s", b="b_s"}) + assert_true(con.connectionstring .. "/path?a=a_s&b=b_s" == url or con.connectionstring .. "/path?b=b_s&a=a_s" == url) + url = con:buildURI("/path", {a="a_s"}) + assert_not_equal(con.connectionstring .. "/path?a=a_s&b=b_s", url) end -- Testing the request function