From d80590597b314287bec83ec0b3f25d4689deaae9 Mon Sep 17 00:00:00 2001 From: subkanthi Date: Sun, 30 Jan 2022 10:05:43 -0500 Subject: [PATCH 1/6] 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 From cfd29585b1342c9816b18257789ee7449ad4f83e Mon Sep 17 00:00:00 2001 From: subkanthi Date: Sun, 30 Jan 2022 10:07:02 -0500 Subject: [PATCH 2/6] Fixed pre-commit errors --- chart/tests/test_elasticsearch_secret.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chart/tests/test_elasticsearch_secret.py b/chart/tests/test_elasticsearch_secret.py index 0acaeaaddb687..639a6066cf078 100644 --- a/chart/tests/test_elasticsearch_secret.py +++ b/chart/tests/test_elasticsearch_secret.py @@ -132,7 +132,7 @@ 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( { From e37f7bd9ed5d357c0559ed5d4e1ded800b822df8 Mon Sep 17 00:00:00 2001 From: subkanthi Date: Sun, 30 Jan 2022 10:05:43 -0500 Subject: [PATCH 3/6] 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 From 8774d8ed0b97e5b4e97079343844d70057367740 Mon Sep 17 00:00:00 2001 From: subkanthi Date: Sun, 30 Jan 2022 10:07:02 -0500 Subject: [PATCH 4/6] Fixed pre-commit errors --- chart/tests/test_elasticsearch_secret.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chart/tests/test_elasticsearch_secret.py b/chart/tests/test_elasticsearch_secret.py index 0acaeaaddb687..639a6066cf078 100644 --- a/chart/tests/test_elasticsearch_secret.py +++ b/chart/tests/test_elasticsearch_secret.py @@ -132,7 +132,7 @@ 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( { From e3618cc6c1c5f0467b4d3242b131ed97ec8cd05c Mon Sep 17 00:00:00 2001 From: subkanthi Date: Thu, 10 Feb 2022 13:19:27 -0500 Subject: [PATCH 5/6] Fixed tests for user/pass elasticsearch secret --- .../secrets/elasticsearch-secret.yaml | 6 +-- chart/tests/test_elasticsearch_secret.py | 45 ++++++++----------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/chart/templates/secrets/elasticsearch-secret.yaml b/chart/templates/secrets/elasticsearch-secret.yaml index 080971c2c756f..e4b348d32beaa 100644 --- a/chart/templates/secrets/elasticsearch-secret.yaml +++ b/chart/templates/secrets/elasticsearch-secret.yaml @@ -33,12 +33,8 @@ metadata: type: Opaque data: {{- with .Values.elasticsearch.connection }} - {{- if .user }} - {{- if .pass }} + {{- 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 }} {{- else }} connection: {{ urlJoin (dict "scheme" (default "http" .scheme) "host" (printf "%s:%s" .host ((default 9200 .port) | toString))) | b64enc | quote }} {{- end }} diff --git a/chart/tests/test_elasticsearch_secret.py b/chart/tests/test_elasticsearch_secret.py index 639a6066cf078..62d7e5cf35875 100644 --- a/chart/tests/test_elasticsearch_secret.py +++ b/chart/tests/test_elasticsearch_secret.py @@ -133,38 +133,29 @@ 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): + @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": {"pass": "password", "host": "elastichostname", "port": 8080}, + "connection": {"host": "elastichostname", "port": 8080, **extra_conn_kwargs}, } } ) - 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 + if not expected_user_info: + assert f"http://elastichostname:8080" == connection + else: + assert f"http://{expected_user_info}@elastichostname:8080" == connection From f7ea58209e5c2aabe5158b475402d723e61e634f Mon Sep 17 00:00:00 2001 From: Kanthi Date: Thu, 10 Feb 2022 14:03:17 -0500 Subject: [PATCH 6/6] Update chart/tests/test_elasticsearch_secret.py removed not required f-string Co-authored-by: Jed Cunningham <66968678+jedcunningham@users.noreply.github.com> --- chart/tests/test_elasticsearch_secret.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chart/tests/test_elasticsearch_secret.py b/chart/tests/test_elasticsearch_secret.py index 62d7e5cf35875..0b7a0f45f6de2 100644 --- a/chart/tests/test_elasticsearch_secret.py +++ b/chart/tests/test_elasticsearch_secret.py @@ -156,6 +156,6 @@ def test_url_generated_when_user_pass_empty_combinations(self, extra_conn_kwargs ) if not expected_user_info: - assert f"http://elastichostname:8080" == connection + assert "http://elastichostname:8080" == connection else: assert f"http://{expected_user_info}@elastichostname:8080" == connection