diff --git a/chart/templates/secrets/elasticsearch-secret.yaml b/chart/templates/secrets/elasticsearch-secret.yaml index 8f031374d652b..e4b348d32beaa 100644 --- a/chart/templates/secrets/elasticsearch-secret.yaml +++ b/chart/templates/secrets/elasticsearch-secret.yaml @@ -33,6 +33,10 @@ 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 and .user .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 }} +{{- end }} {{- end }} diff --git a/chart/tests/test_elasticsearch_secret.py b/chart/tests/test_elasticsearch_secret.py index a304a7b95c9a3..0b7a0f45f6de2 100644 --- a/chart/tests/test_elasticsearch_secret.py +++ b/chart/tests/test_elasticsearch_secret.py @@ -132,3 +132,30 @@ def test_should_generate_secret_with_specified_schemes(self, scheme): ) assert f"{scheme}://username:password@elastichostname:9200" == connection + + @parameterized.expand( + [ + # When both user and password are empty. + ({}, ""), + # When password is empty + ({"user": "admin"}, ""), + # When user is empty + ({"pass": "password"}, ""), + # Valid username/password + ({"user": "admin", "pass": "password"}, "admin:password"), + ], + ) + def test_url_generated_when_user_pass_empty_combinations(self, extra_conn_kwargs, expected_user_info): + connection = self._get_connection( + { + "elasticsearch": { + "enabled": True, + "connection": {"host": "elastichostname", "port": 8080, **extra_conn_kwargs}, + } + } + ) + + if not expected_user_info: + assert "http://elastichostname:8080" == connection + else: + assert f"http://{expected_user_info}@elastichostname:8080" == connection