17
17
18
18
from keycloak import KeycloakAdmin
19
19
from testcontainers .core .container import DockerContainer
20
- from testcontainers .core .waiting_utils import wait_container_is_ready , wait_for_logs
21
-
22
- _DEFAULT_DEV_COMMAND = "start-dev"
20
+ from testcontainers .core .waiting_utils import wait_container_is_ready
23
21
24
22
25
23
class KeycloakContainer (DockerContainer ):
26
24
has_realm_imports = False
25
+ # Since Keycloak v26.0.0
26
+ # See: https://www.keycloak.org/server/all-config#category-bootstrap_admin
27
+ ADMIN_USERNAME_ENVIRONMENT_VARIABLE = "KC_BOOTSTRAP_ADMIN_USERNAME"
28
+ ADMIN_PASSWORD_ENVIRONMENT_VARIABLE = "KC_BOOTSTRAP_ADMIN_PASSWORD"
27
29
28
30
"""
29
31
Keycloak container.
@@ -46,19 +48,19 @@ def __init__(
46
48
password : Optional [str ] = None ,
47
49
port : int = 8080 ,
48
50
management_port : int = 9000 ,
49
- cmd : Optional [str ] = _DEFAULT_DEV_COMMAND ,
51
+ cmd : Optional [str ] = "start-dev" ,
50
52
) -> None :
51
53
super ().__init__ (image = image )
52
- self .username = username or os .environ .get ("KEYCLOAK_ADMIN" , "test" )
53
- self .password = password or os .environ .get ("KEYCLOAK_ADMIN_PASSWORD" , "test" )
54
+ self .username = username or os .environ .get (self . ADMIN_USERNAME_ENVIRONMENT_VARIABLE , "test" )
55
+ self .password = password or os .environ .get (self . ADMIN_PASSWORD_ENVIRONMENT_VARIABLE , "test" )
54
56
self .port = port
55
57
self .management_port = management_port
56
58
self .with_exposed_ports (self .port , self .management_port )
57
59
self .cmd = cmd
58
60
59
61
def _configure (self ) -> None :
60
- self .with_env ("KEYCLOAK_ADMIN" , self .username )
61
- self .with_env ("KEYCLOAK_ADMIN_PASSWORD" , self .password )
62
+ self .with_env (self . ADMIN_USERNAME_ENVIRONMENT_VARIABLE , self .username )
63
+ self .with_env (self . ADMIN_PASSWORD_ENVIRONMENT_VARIABLE , self .password )
62
64
# Enable health checks
63
65
# see: https://www.keycloak.org/server/health#_relevant_options
64
66
self .with_env ("KC_HEALTH_ENABLED" , "true" )
@@ -88,8 +90,6 @@ def _readiness_probe(self) -> None:
88
90
except requests .exceptions .ConnectionError :
89
91
response = requests .get (f"{ self .get_url ()} /health/ready" , timeout = 1 )
90
92
response .raise_for_status ()
91
- if _DEFAULT_DEV_COMMAND in self ._command :
92
- wait_for_logs (self , "Added user .* to realm .*" )
93
93
94
94
def start (self ) -> "KeycloakContainer" :
95
95
super ().start ()
0 commit comments