Skip to content

Commit 935693e

Browse files
fix(opensearch): add support for admin_password in >= 2.12 (#697)
Opensearch has made an initial admin password mandatory since version 2.12 as can be read in the [changelogs](https://github.com/opensearch-project/opensearch-build/blob/main/release-notes/opensearch-release-notes-2.12.0.md). The open search module in test containers-python does not pass in this additional flag and current compatibility with open search >= 2.12 is broken . This MR adds compatibility with open search >= 2.12 by adding the inidial_admin_password flag to the docker environment --------- Co-authored-by: David Ankin <[email protected]>
1 parent 8c0cdbc commit 935693e

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

modules/opensearch/testcontainers/opensearch/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import suppress
2+
13
from opensearchpy import OpenSearch
24
from opensearchpy.exceptions import ConnectionError, TransportError
35
from urllib3.exceptions import ProtocolError
@@ -35,25 +37,37 @@ def __init__(
3537
image: str = "opensearchproject/opensearch:2.4.0",
3638
port: int = 9200,
3739
security_enabled: bool = False,
40+
initial_admin_password: str = "admin",
3841
**kwargs,
3942
) -> None:
4043
"""
4144
Args:
4245
image: Docker image to use for the container.
4346
port: Port to expose on the container.
4447
security_enabled: :code:`False` disables the security plugin in OpenSearch.
48+
initial_admin_password: set the password for opensearch, For OpenSearch versions 2.12 and
49+
later, you must set the initial admin password as seen in the documentation,
50+
https://opensearch.org/docs/latest/security/configuration/demo-configuration/#setting-up-a-custom-admin-password
4551
"""
4652
raise_for_deprecated_parameter(kwargs, "port_to_expose", "port")
4753
super().__init__(image, **kwargs)
4854
self.port = port
4955
self.security_enabled = security_enabled
56+
self.initial_admin_password = initial_admin_password
5057

5158
self.with_exposed_ports(self.port)
5259
self.with_env("discovery.type", "single-node")
5360
self.with_env("plugins.security.disabled", "false" if security_enabled else "true")
61+
if self._supports_initial_admin_password(str(image)):
62+
self.with_env("OPENSEARCH_INITIAL_ADMIN_PASSWORD", self.initial_admin_password)
5463
if security_enabled:
5564
self.with_env("plugins.security.allow_default_init_securityindex", "true")
5665

66+
def _supports_initial_admin_password(self, image: str) -> bool:
67+
with suppress(Exception):
68+
return [int(n) for n in image.split(":")[-1].split(".")] >= [int(n) for n in "2.12.0".split(".")]
69+
return False
70+
5771
def get_config(self) -> dict:
5872
"""This method returns the configuration of the OpenSearch container,
5973
including the host, port, username, and password.
@@ -66,7 +80,7 @@ def get_config(self) -> dict:
6680
"host": self.get_container_host_ip(),
6781
"port": self.get_exposed_port(self.port),
6882
"username": "admin",
69-
"password": "admin",
83+
"password": self.initial_admin_password,
7084
}
7185

7286
def get_client(self, verify_certs: bool = False, **kwargs) -> OpenSearch:

modules/opensearch/tests/test_opensearch.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
from testcontainers.opensearch import OpenSearchContainer
22

3+
import pytest
4+
5+
6+
@pytest.fixture(autouse=True)
7+
def disable_logging():
8+
import logging
9+
import warnings
10+
11+
warnings.filterwarnings("ignore")
12+
logging.getLogger("opensearch").setLevel(logging.CRITICAL)
13+
14+
yield
15+
warnings.resetwarnings()
16+
logging.getLogger("opensearch").setLevel(logging.NOTSET)
17+
318

419
def test_docker_run_opensearch():
520
with OpenSearchContainer() as opensearch:
@@ -25,6 +40,14 @@ def test_docker_run_opensearch_v1_with_security():
2540
assert client.cluster.health()["status"] == "green"
2641

2742

43+
def test_docker_run_opensearch_v2_12():
44+
with OpenSearchContainer(
45+
image="opensearchproject/opensearch:2.12.0", initial_admin_password="Testing!#345"
46+
) as opensearch:
47+
client = opensearch.get_client()
48+
assert client.cluster.health()["status"] == "green"
49+
50+
2851
def test_search():
2952
with OpenSearchContainer() as opensearch:
3053
client = opensearch.get_client()

0 commit comments

Comments
 (0)