From e37f7bd9ed5d357c0559ed5d4e1ded800b822df8 Mon Sep 17 00:00:00 2001 From: subkanthi Date: Sun, 30 Jan 2022 10:05:43 -0500 Subject: [PATCH] Fix helm chart for elasticsearch secret when username/password fields are empty --- .../secrets/elasticsearch-secret.yaml | 12 +++++-- chart/tests/test_elasticsearch_secret.py | 36 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/chart/templates/secrets/elasticsearch-secret.yaml b/chart/templates/secrets/elasticsearch-secret.yaml index 8f031374d652b..080971c2c756f 100644 --- a/chart/templates/secrets/elasticsearch-secret.yaml +++ b/chart/templates/secrets/elasticsearch-secret.yaml @@ -33,6 +33,14 @@ metadata: type: Opaque data: {{- with .Values.elasticsearch.connection }} - connection: {{ urlJoin (dict "scheme" (default "http" .scheme) "userinfo" (printf "%s:%s" (.user | urlquery) (.pass | urlquery)) "host" (printf "%s:%s" .host ((default 9200 .port) | toString) ) ) | b64enc | quote }} - {{- end }} + {{- if .user }} + {{- if .pass }} + connection: {{ urlJoin (dict "scheme" (default "http" .scheme) "userinfo" (printf "%s:%s" (.user | urlquery) (.pass | urlquery)) "host" (printf "%s:%s" .host ((default 9200 .port) | toString) ) ) | b64enc | quote }} + {{- else}} + connection: {{ urlJoin (dict "scheme" (default "http" .scheme) "host" (printf "%s:%s" .host ((default 9200 .port) | toString))) | b64enc | quote }} + {{- end }} + {{- else }} + connection: {{ urlJoin (dict "scheme" (default "http" .scheme) "host" (printf "%s:%s" .host ((default 9200 .port) | toString))) | b64enc | quote }} + {{- end }} +{{- end }} {{- end }} diff --git a/chart/tests/test_elasticsearch_secret.py b/chart/tests/test_elasticsearch_secret.py index a304a7b95c9a3..0acaeaaddb687 100644 --- a/chart/tests/test_elasticsearch_secret.py +++ b/chart/tests/test_elasticsearch_secret.py @@ -132,3 +132,39 @@ def test_should_generate_secret_with_specified_schemes(self, scheme): ) assert f"{scheme}://username:password@elastichostname:9200" == connection + + def test_url_generated_when_user_is_empty(self): + connection = self._get_connection( + { + "elasticsearch": { + "enabled": True, + "connection": {"pass": "password", "host": "elastichostname", "port": 8080}, + } + } + ) + + assert "http://elastichostname:8080" == connection + + def test_url_generated_when_password_is_empty(self): + connection = self._get_connection( + { + "elasticsearch": { + "enabled": True, + "connection": {"user": "admin", "host": "elastichostname", "port": 8080}, + } + } + ) + + assert "http://elastichostname:8080" == connection + + def test_url_generated_with_valid_user_password(self): + connection = self._get_connection( + { + "elasticsearch": { + "enabled": True, + "connection": {"user": "admin", "pass": "pass", "host": "elastichostname", "port": 8080}, + } + } + ) + + assert "http://admin:pass@elastichostname:8080" == connection