如何将秘密传递给testContainers?

huangapple go评论175阅读模式
英文:

How to pass secrets to testContainers?

问题

以下是您要翻译的部分:

  1. 我有以下用于本地开发的Docker Compose文件:
  2. version: '3.4'
  3. networks:
  4. mynetwork:
  5. services:
  6. samba:
  7. image: instantlinux/samba-dc:latest
  8. container_name: samba-dc
  9. cap_add:
  10. - CAP_SYS_ADMIN
  11. hostname: my.org
  12. environment:
  13. DOMAIN_ACTION: provision
  14. REALM: my.org
  15. volumes:
  16. - etc:/etc/samba
  17. - lib:/var/lib/samba
  18. ports:
  19. - "53:53"
  20. - "53:53/udp"
  21. - "88:88"
  22. - "88:88/udp"
  23. - "389:389"
  24. secrets:
  25. - samba-admin-password
  26. volumes:
  27. etc:
  28. lib:
  29. secrets:
  30. samba-admin-password:
  31. file: secrets.yaml
  32. 现在我尝试使用testContainers来实施集成测试:
  33. @Testcontainers
  34. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  35. @ActiveProfiles("test")
  36. ....
  37. init {
  38. try {
  39. val ldapContainer =
  40. GenericContainer("instantlinux/samba-dc:latest")
  41. .withEnv("DOMAIN_ACTION", "provision")
  42. .withEnv("REALM", "my.company")
  43. .withEnv("ADMIN_PASSWORD_SECRET", "samba-admin-password")
  44. .withExposedPorts(53, 88, 389)
  45. ldapContainer.start()
  46. print("Containers has started")
  47. } catch (e: Exception) {
  48. e.printStackTrace()
  49. }
  50. }
  51. 但是当我尝试运行它时,我收到以下错误:
  52. Container startup failed for image instantlinux/samba-dc:latest
  53. ....
  54. rg.testcontainers.containers.GenericContainer expected the predicate to return <true> but it returned <false> for input of <InspectContainerResponse(args=[], config=ContainerConfig(attachStderr=false, attachStdin=false, attachStdout=false, cmd=null, domainName=, entrypoint=[/usr/local/bin/entrypoint.sh], env=[DOMAIN_ACTION=provision, ADMIN_PASSWORD_SECRET=samba-admin-password, REALM=my.company, PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin, ALLOW_DNS_UPDATES=secure, BIND_INTERFACES_ONLY=yes, DOMAIN_LOGONS=yes, DOMAIN_MASTER=no, INTERFACES=lo eth0, LOG_LEVEL=1, MODEL=standard, NETBIOS_NAME=, SERVER_STRING=Samba Domain Controller, TZ=UTC, WINBIND_USE_DEFAULT_DOMAIN=yes, WORKGROUP=AD], exposedPorts=.....
  55. ...
  56. 17:01:51.548 [Test worker] ERROR tc.instantlinux/samba-dc:latest -- Log output from the failed container:
  57. Set timezone
  58. Cannot read secret $ADMIN_PASSWORD_SECRET in /run/secrets
  59. 看起来我必须以某种方式配置秘密,但我看不到如何做到。
  1. 更新1
  2. -
  3. 秘密文件如下:
  4. ```yaml
  5. kind: Secret
  6. apiVersion: v1
  7. metadata:
  8. name: samba-admin-password
  9. data:
  10. ADMIN_PASSWORD_SECRET: superpassword

更新2

根据VonC的答案,我创建了以下示例:

  1. @Testcontainers
  2. @SpringBootTest(webEnvironment = RANDOM_PORT)
  3. @ActiveProfiles("test")
  4. class TestContainersBase {
  5. @Test
  6. fun test() {
  7. val mapper = ObjectMapper(YAMLFactory())
  8. val secretPathOnHost = "C:\\work\\MyApp\\docker\\secrets.yaml"
  9. val secretsFile = File(secretPathOnHost)
  10. val secretsData: Map<String, Any> = mapper.readValue(secretsFile, object: TypeReference<Map<String, Any>>() {})
  11. // 从解析的数据中提取秘密
  12. val adminPassword = (secretsData["data"] as Map<*, *>)!!["ADMIN_PASSWORD_SECRET"] as String?
  13. val secretPathInContainer = "/run/secrets/samba-admin-password";
  14. // 创建并启动容器
  15. val ldapContainer = GenericContainer("instantlinux/samba-dc:latest")
  16. .withEnv("DOMAIN_ACTION", "provision")
  17. .withEnv("REALM", "my.company")
  18. .withEnv("ADMIN_PASSWORD_SECRET", adminPassword) // 将提取的秘密设置为环境变量
  19. .withExposedPorts(53, 88, 389)
  20. .withFileSystemBind(secretPathOnHost, secretPathInContainer, BindMode.READ_ONLY);
  21. ldapContainer.start()
  22. print("qwerty")
  23. Thread.sleep(100000000)
  24. }
  25. }
  1. 在应用程序日志中,我看到:
  2. 2023-08-21T13:38:50.555+03:00 INFO 15136 --- [ Test worker] o.t.utility.ImageNameSubstitutor : Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
  3. 2023-08-21T13:38:51.739+03:00 INFO 15136 --- [ Test worker] o.t.d.DockerClientProviderStrategy : Loaded org.testcontainers.dockerclient.NpipeSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
  4. 2023-08-21T13:38:52.779+03:00 INFO 15136 --- [ Test worker] o.t.d.DockerClientProviderStrategy : Found Docker environment with local Npipe socket (npipe:////./pipe/docker_engine)
  5. 2023-08-21T13:38:52.784+03:00 INFO 15136 --- [ Test worker] org.testcontainers.DockerClientFactory : Docker host IP address is localhost
  6. 2023-08-21T13:38:52.814+03:00 INFO 15136 --- [ Test worker] org.testcontainers.DockerClientFactory : Connected to docker:
  7. Server Version: 20.10.21
  8. API Version: 1.41
  9. Operating System: Docker Desktop
  10. Total Memory: 38292 MB
  11. 2023-08-21T13:38:52.889+03:00 INFO 15136 --- [ Test worker] tc.testcontainers/ryuk:0.4.0 : Creating container for image: testcontainers/ryuk:0.4.0
  12. 2023-08-21T13:38:53.928+03:00 INFO 15136 --- [ Test worker] o.t.utility.RegistryAuthLocator : Credential helper/store (docker-credential-desktop) does not have credentials for https://index.docker.io/v1/
  13. 2023-08-21T13:38:54.201+03:00 INFO 15136 --- [ Test
  14. <details>
  15. <summary>英文:</summary>
  16. I have following docker-compose file for local development:
  17. version: &#39;3.4&#39;
  18. networks:
  19. mynetwork:
  20. services:
  21. samba:
  22. image: instantlinux/samba-dc:latest
  23. container_name: samba-dc
  24. cap_add:
  25. - CAP_SYS_ADMIN
  26. hostname: my.org
  27. environment:
  28. DOMAIN_ACTION: provision
  29. REALM: my.org
  30. volumes:
  31. - etc:/etc/samba
  32. - lib:/var/lib/samba
  33. ports:
  34. - &quot;53:53&quot;
  35. - &quot;53:53/udp&quot;
  36. - &quot;88:88&quot;
  37. - &quot;88:88/udp&quot;
  38. - &quot;389:389&quot;
  39. secrets:
  40. - samba-admin-password
  41. volumes:
  42. etc:
  43. lib:
  44. secrets:
  45. samba-admin-password:
  46. file: secrets.yaml
  47. Now I try to implement integration tests using testContainers for that purpose:
  48. @Testcontainers
  49. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  50. @ActiveProfiles(&quot;test&quot;)
  51. ....
  52. init {
  53. try {
  54. val ldapContainer =
  55. GenericContainer(&quot;instantlinux/samba-dc:latest&quot;)
  56. .withEnv(&quot;DOMAIN_ACTION&quot;, &quot;provision&quot;)
  57. .withEnv(&quot;REALM&quot;, &quot;my.company&quot;)
  58. .withEnv(&quot;ADMIN_PASSWORD_SECRET&quot;, &quot;samba-admin-password&quot;)
  59. .withExposedPorts(53, 88, 389)
  60. ldapContainer.start()
  61. print(&quot;Containers has started&quot;)
  62. } catch (e: Exception) {
  63. e.printStackTrace()
  64. }
  65. }
  66. But when I try to run it I receive an error:
  67. Container startup failed for image instantlinux/samba-dc:latest
  68. ....
  69. rg.testcontainers.containers.GenericContainer expected the predicate to return &lt;true&gt; but it returned &lt;false&gt; for input of &lt;InspectContainerResponse(args=[], config=ContainerConfig(attachStderr=false, attachStdin=false, attachStdout=false, cmd=null, domainName=, entrypoint=[/usr/local/bin/entrypoint.sh], env=[DOMAIN_ACTION=provision, ADMIN_PASSWORD_SECRET=samba-admin-password, REALM=my.company, PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin, ALLOW_DNS_UPDATES=secure, BIND_INTERFACES_ONLY=yes, DOMAIN_LOGONS=yes, DOMAIN_MASTER=no, INTERFACES=lo eth0, LOG_LEVEL=1, MODEL=standard, NETBIOS_NAME=, SERVER_STRING=Samba Domain Controller, TZ=UTC, WINBIND_USE_DEFAULT_DOMAIN=yes, WORKGROUP=AD], exposedPorts=....
  70. ...
  71. 17:01:51.548 [Test worker] ERROR tc.instantlinux/samba-dc:latest -- Log output from the failed container:
  72. Set timezone
  73. Cannot read secret $ADMIN_PASSWORD_SECRET in /run/secrets
  74. Looks like I have to configure secrets somehow but I don&#39; see a way how to acheve it.
  75. Update 1
  76. -
  77. Secret file looks like this:

kind: Secret
apiVersion: v1
metadata:
name: samba-admin-password
data:
ADMIN_PASSWORD_SECRET: superpassword

  1. Update 2
  2. -
  3. Based on VonC answer I&#39;ve created the example:
  4. @Testcontainers
  5. @SpringBootTest(webEnvironment = RANDOM_PORT)
  6. @ActiveProfiles(&quot;test&quot;)
  7. class TestContainersBase {
  8. @Test
  9. fun test() {
  10. val mapper = ObjectMapper(YAMLFactory())
  11. val secretPathOnHost = &quot;C:\\work\\MyApp\\docker\\secrets.yaml&quot;
  12. val secretsFile = File(secretPathOnHost)
  13. val secretsData: Map&lt;String, Any&gt; = mapper.readValue(secretsFile, object: TypeReference&lt;Map&lt;String, Any&gt;&gt;(){})
  14. // Extract the secret from the parsed data
  15. val adminPassword = (secretsData[&quot;data&quot;] as Map&lt;*,*&gt;?)!![&quot;ADMIN_PASSWORD_SECRET&quot;] as String?
  16. val secretPathInContainer = &quot;/run/secrets/samba-admin-password&quot;;
  17. // Create and start the container
  18. val ldapContainer = GenericContainer(&quot;instantlinux/samba-dc:latest&quot;)
  19. .withEnv(&quot;DOMAIN_ACTION&quot;, &quot;provision&quot;)
  20. .withEnv(&quot;REALM&quot;, &quot;my.company&quot;)
  21. .withEnv(&quot;ADMIN_PASSWORD_SECRET&quot;, adminPassword) // Set the extracted secret as an environment variable
  22. .withExposedPorts(53, 88, 389)
  23. .withFileSystemBind(secretPathOnHost, secretPathInContainer, BindMode.READ_ONLY);
  24. ldapContainer.start()
  25. print(&quot;qwerty&quot;)
  26. Thread.sleep(100000000)
  27. }
  28. }
  29. In app logs I see:
  30. 2023-08-21T13:38:50.555+03:00 INFO 15136 --- [ Test worker] o.t.utility.ImageNameSubstitutor : Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of &#39;ConfigurationFileImageNameSubstitutor&#39; and &#39;PrefixingImageNameSubstitutor&#39;)
  31. 2023-08-21T13:38:51.739+03:00 INFO 15136 --- [ Test worker] o.t.d.DockerClientProviderStrategy : Loaded org.testcontainers.dockerclient.NpipeSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
  32. 2023-08-21T13:38:52.779+03:00 INFO 15136 --- [ Test worker] o.t.d.DockerClientProviderStrategy : Found Docker environment with local Npipe socket (npipe:////./pipe/docker_engine)
  33. 2023-08-21T13:38:52.784+03:00 INFO 15136 --- [ Test worker] org.testcontainers.DockerClientFactory : Docker host IP address is localhost
  34. 2023-08-21T13:38:52.814+03:00 INFO 15136 --- [ Test worker] org.testcontainers.DockerClientFactory : Connected to docker:
  35. Server Version: 20.10.21
  36. API Version: 1.41
  37. Operating System: Docker Desktop
  38. Total Memory: 38292 MB
  39. 2023-08-21T13:38:52.889+03:00 INFO 15136 --- [ Test worker] tc.testcontainers/ryuk:0.4.0 : Creating container for image: testcontainers/ryuk:0.4.0
  40. 2023-08-21T13:38:53.928+03:00 INFO 15136 --- [ Test worker] o.t.utility.RegistryAuthLocator : Credential helper/store (docker-credential-desktop) does not have credentials for https://index.docker.io/v1/
  41. 2023-08-21T13:38:54.201+03:00 INFO 15136 --- [ Test worker] tc.testcontainers/ryuk:0.4.0 : Container testcontainers/ryuk:0.4.0 is starting: b4a10e2647f83d6fc404644fb09edabf930e987e2c5d138eb3d1b9414b1400ac
  42. 2023-08-21T13:38:55.320+03:00 INFO 15136 --- [ Test worker] tc.testcontainers/ryuk:0.4.0 : Container testcontainers/ryuk:0.4.0 started in PT2.488268S
  43. 2023-08-21T13:38:55.330+03:00 INFO 15136 --- [ Test worker] o.t.utility.RyukResourceReaper : Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
  44. 2023-08-21T13:38:55.330+03:00 INFO 15136 --- [ Test worker] org.testcontainers.DockerClientFactory : Checking the system...
  45. 2023-08-21T13:38:55.332+03:00 INFO 15136 --- [ Test worker] org.testcontainers.DockerClientFactory : ?? Docker server version should be at least 1.6.0
  46. 2023-08-21T13:38:55.334+03:00 INFO 15136 --- [ Test worker] tc.instantlinux/samba-dc:latest : Creating container for image: instantlinux/samba-dc:latest
  47. 2023-08-21T13:38:56.834+03:00 INFO 15136 --- [ Test worker] tc.instantlinux/samba-dc:latest : Container instantlinux/samba-dc:latest is starting: 496246f47398809c3a7327b0c73a9b7d7fbe6440865b1cad4c124849f6069acb
  48. 2023-08-21T13:39:07.361+03:00 WARN 15136 --- [ntainers-wait-0] .c.w.i.InternalCommandPortListeningCheck : An exception while executing the internal check: Container.ExecResult(exitCode=137, stdout=, stderr=/bin/sh: /bin/bash: not found
  49. /bin/sh: /bin/bash: not found
  50. /bin/sh: /bin/bash: not found
  51. /bin/sh: /bin/bash: not found
  52. /bin/sh: /bin/bash: not found
  53. /bin/sh: /bin/bash: not found
  54. /bin/sh: /bin/bash: not found
  55. /bin/sh: /bin/bash: not found
  56. /bin/sh: /bin/bash: not found
  57. /bin/sh: /bin/bash: not found
  58. /bin/sh: /bin/bash: not found
  59. /bin/sh: /bin/bash: not found
  60. /bin/sh: /bin/bash: not found
  61. /bin/sh: /bin/bash: not found
  62. /bin/sh: /bin/bash: not found
  63. /bin/sh: /bin/bash: not found
  64. /bin/sh: /bin/bash: not found
  65. /bin/sh: /bin/bash: not found
  66. /bin/sh: /bin/bash: not found
  67. /bin/sh: /bin/bash: not found
  68. /bin/sh: /bin/bash: not found
  69. /bin/sh: /bin/bash: not found
  70. /bin/sh: /bin/bash: not found
  71. /bin/sh: /bin/bash: not found
  72. /bin/sh: /bin/bash: not found
  73. /bin/sh: /bin/bash: not found
  74. /bin/sh: /bin/bash: not found
  75. /bin/sh: /bin/bash: not found
  76. /bin/sh: /bin/bash: not found
  77. /bin/sh: /bin/bash: not found
  78. /bin/sh: /bin/bash: not found
  79. /bin/sh: /bin/bash: not found
  80. /bin/sh: /bin/bash: not found
  81. /bin/sh: /bin/bash: not found
  82. /bin/sh: /bin/bash: not found
  83. /bin/sh: /bin/bash: not found
  84. /bin/sh: /bin/bash: not found
  85. /bin/sh: /bin/bash: not found
  86. /bin/sh: /bin/bash: not found
  87. /bin/sh: /bin/bash: not found
  88. /bin/sh: /bin/bash: not found
  89. /bin/sh: /bin/bash: not found
  90. /bin/sh: /bin/bash: not found
  91. /bin/sh: /bin/bash: not found
  92. /bin/sh: /bin/bash: not found
  93. /bin/sh: /bin/bash: not found
  94. /bin/sh: /bin/bash: not found
  95. /bin/sh: /bin/bash: not found
  96. /bin/sh: /bin/bash: not found
  97. /bin/sh: /bin/bash: not found
  98. /bin/sh: /bin/bash: not found
  99. /bin/sh: /bin/bash: not found
  100. /bin/sh: /bin/bash: not found
  101. /bin/sh: /bin/bash: not found
  102. /bin/sh: /bin/bash: not found
  103. /bin/sh: /bin/bash: not found
  104. /bin/sh: /bin/bash: not found
  105. /bin/sh: /bin/bash: not found
  106. /bin/sh: /bin/bash: not found
  107. /bin/sh: /bin/bash: not found
  108. /bin/sh: /bin/bash: not found
  109. /bin/sh: /bin/bash: not found
  110. /bin/sh: /bin/bash: not found
  111. /bin/sh: /bin/bash: not found
  112. /bin/sh: /bin/bash: not found
  113. /bin/sh: /bin/bash: not found
  114. /bin/sh: /bin/bash: not found
  115. /bin/sh: /bin/bash: not found
  116. /bin/sh: /bin/bash: not found
  117. /bin/sh: /bin/bash: not found
  118. /bin/sh: /bin/bash: not found
  119. /bin/sh: /bin/bash: not found
  120. )
  121. 2023-08-21T13:39:07.367+03:00 INFO 15136 --- [ Test worker] tc.instantlinux/samba-dc:latest : Container instantlinux/samba-dc:latest started in PT12.0305603S
  122. In docker desktop:
  123. [![enter image description here][1]][1]
  124. And the first container(based on ports I think that it is Samba) logs:
  125. 2023-08-21 13:38:58 Set timezone
  126. 2023-08-21 13:38:59 INFO 2023-08-21 10:38:59,067 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #2108: Looking up IPv4 addresses
  127. 2023-08-21 13:38:59 INFO 2023-08-21 10:38:59,068 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #2125: Looking up IPv6 addresses
  128. 2023-08-21 13:38:59 WARNING 2023-08-21 10:38:59,068 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #2132: No IPv6 address will be assigned
  129. 2023-08-21 13:38:59 INFO 2023-08-21 10:38:59,721 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #2274: Setting up share.ldb
  130. 2023-08-21 13:38:59 INFO 2023-08-21 10:38:59,874 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #2278: Setting up secrets.ldb
  131. 2023-08-21 13:38:59 INFO 2023-08-21 10:38:59,936 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #2283: Setting up the registry
  132. 2023-08-21 13:39:00 INFO 2023-08-21 10:39:00,304 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #2286: Setting up the privileges database
  133. 2023-08-21 13:39:00 INFO 2023-08-21 10:39:00,466 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #2289: Setting up idmap db
  134. 2023-08-21 13:39:00 INFO 2023-08-21 10:39:00,555 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #2296: Setting up SAM db
  135. 2023-08-21 13:39:00 INFO 2023-08-21 10:39:00,573 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #880: Setting up sam.ldb partitions and settings
  136. 2023-08-21 13:39:00 INFO 2023-08-21 10:39:00,574 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #892: Setting up sam.ldb rootDSE
  137. 2023-08-21 13:39:00 INFO 2023-08-21 10:39:00,591 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1305: Pre-loading the Samba 4 and AD schema
  138. 2023-08-21 13:39:00 Unable to determine the DomainSID, can not enforce uniqueness constraint on local domainSIDs
  139. 2023-08-21 13:39:00
  140. 2023-08-21 13:39:00 INFO 2023-08-21 10:39:00,638 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1383: Adding DomainDN: DC=my,DC=company
  141. 2023-08-21 13:39:00 INFO 2023-08-21 10:39:00,659 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1415: Adding configuration container
  142. 2023-08-21 13:39:00 INFO 2023-08-21 10:39:00,678 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1430: Setting up sam.ldb schema
  143. 2023-08-21 13:39:03 INFO 2023-08-21 10:39:03,229 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1448: Setting up sam.ldb configuration data
  144. 2023-08-21 13:39:03 INFO 2023-08-21 10:39:03,356 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1489: Setting up display specifiers
  145. 2023-08-21 13:39:05 INFO 2023-08-21 10:39:05,522 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1497: Modifying display specifiers and extended rights
  146. 2023-08-21 13:39:05 INFO 2023-08-21 10:39:05,569 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1504: Adding users container
  147. 2023-08-21 13:39:05 INFO 2023-08-21 10:39:05,570 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1510: Modifying users container
  148. 2023-08-21 13:39:05 INFO 2023-08-21 10:39:05,571 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1513: Adding computers container
  149. 2023-08-21 13:39:05 INFO 2023-08-21 10:39:05,572 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1519: Modifying computers container
  150. 2023-08-21 13:39:05 INFO 2023-08-21 10:39:05,574 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1523: Setting up sam.ldb data
  151. 2023-08-21 13:39:05 INFO 2023-08-21 10:39:05,728 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1553: Setting up well known security principals
  152. 2023-08-21 13:39:05 INFO 2023-08-21 10:39:05,770 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1567: Setting up sam.ldb users and groups
  153. 2023-08-21 13:39:05 INFO 2023-08-21 10:39:05,993 pid:18 /usr/lib/python3.10/site-packages/samba/provision/__init__.py #1575: Setting up self join
  154. 2023-08-21 13:39:06 Repacking database from v1 to v2 format (first record CN=Structural-Object-Class,CN=Schema,CN=Configuration,DC=my,DC=company)
  155. 2023-08-21 13:39:06 Repack: re-packed 10000 records so far
  156. 2023-08-21 13:39:06 Repacking database from v1 to v2 format (first record CN=nTDSSiteSettings-Display,CN=406,CN=DisplaySpecifiers,CN=Configuration,DC=my,DC=company)
  157. 2023-08-21 13:39:06 Repacking database from v1 to v2 format (first record CN=ObjectMoveTable,CN=FileLinks,CN=System,DC=my,DC=company)
  158. 2023-08-21 13:39:07 set_nt_acl_no_snum: fset_nt_acl returned NT_STATUS_ACCESS_DENIED.
  159. 2023-08-21 13:39:07 ERROR(runtime): uncaught exception - (3221225506, &#39;{Access Denied} A process has requested access to an object but has not been granted those access rights.&#39;)
  160. 2023-08-21 13:39:07 File &quot;/usr/lib/python3.10/site-packages/samba/netcmd/__init__.py&quot;, line 186, in _run
  161. 2023-08-21 13:39:07 return self.run(*args, **kwargs)
  162. 2023-08-21 13:39:07 File &quot;/usr/lib/python3.10/site-packages/samba/netcmd/domain.py&quot;, line 493, in run
  163. 2023-08-21 13:39:07 result = provision(self.logger,
  164. 2023-08-21 13:39:07 File &quot;/usr/lib/python3.10/site-packages/samba/provision/__init__.py&quot;, line 2325, in provision
  165. 2023-08-21 13:39:07 provision_fill(samdb, secrets_ldb, logger, names, paths,
  166. 2023-08-21 13:39:07 File &quot;/usr/lib/python3.10/site-packages/samba/provision/__init__.py&quot;, line 1965, in provision_fill
  167. 2023-08-21 13:39:07 setsysvolacl(samdb, paths.netlogon, paths.sysvol, paths.root_uid,
  168. 2023-08-21 13:39:07 File &quot;/usr/lib/python3.10/site-packages/samba/provision/__init__.py&quot;, line 1742, in setsysvolacl
  169. 2023-08-21 13:39:07 _setntacl(sysvol)
  170. 2023-08-21 13:39:07 File &quot;/usr/lib/python3.10/site-packages/samba/provision/__init__.py&quot;, line 1736, in _setntacl
  171. 2023-08-21 13:39:07 return setntacl(
  172. 2023-08-21 13:39:07 File &quot;/usr/lib/python3.10/site-packages/samba/ntacls.py&quot;, line 228, in setntacl
  173. 2023-08-21 13:39:07 smbd.set_nt_acl(
  174. **docker ps**
  175. PS C:\work\myApp\docker&gt; docker ps
  176. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  177. 5541e9f96005 testcontainers/ryuk:0.4.0 &quot;/bin/ryuk&quot; 24 seconds ago Up 23 seconds 0.0.0.0:64762-&gt;8080/tcp testcontainers-ryuk-6d02415f-7042-4de8-bf0d-a1be71ea5172
  178. PS C:\work\myApp\docker&gt;
  179. [1]: https://i.stack.imgur.com/2WJiX.png
  180. </details>
  181. # 答案1
  182. **得分**: 3
  183. [Testcontainers](https://testcontainers.com/) 看起来不直接支持 Docker Compose 的 secrets。唯一关于 secret 的概念是在使用 [HashiCorp Vault Module](https://java.testcontainers.org/modules/vault/) 时的 `.withSecretInVault()`。
  184. 在你的情况下,你可以尝试使用卷来模拟一个 secret:这是一个解决方法,将你的 secrets 绑定挂载到容器中的期望路径。
  185. ```java
  186. val secretPathOnHost = &quot;/path/to/your/secrets.yaml&quot;;
  187. val secretPathInContainer = &quot;/run/secrets/samba-admin-password&quot;;
  188. val ldapContainer = GenericContainer(&quot;instantlinux/samba-dc:latest&quot;)
  189. .withEnv(&quot;DOMAIN_ACTION&quot;, &quot;provision&quot;)
  190. .withEnv(&quot;REALM&quot;, &quot;my.company&quot;)
  191. .withEnv(&quot;ADMIN_PASSWORD_SECRET&quot;, &quot;samba-admin-password&quot;)
  192. .withExposedPorts(53, 88, 389)
  193. .withFileSystemBind(secretPathOnHost, secretPathInContainer, BindMode.READ_ONLY);
  194. ldapContainer.start();

请将 /path/to/your/secrets.yaml 替换为主机上 secrets.yaml 文件的绝对路径。

注意:通过卷来模拟 secrets 意味着该 secret 在你的主机系统上以明文文件的形式可用,所以确保正确管理其权限和访问。这对于本地开发和测试可能没问题,但在类似于生产的环境中可能不太理想。

而且... 永远不要将 secrets 或 secret 路径提交到源代码控制中。


由于 secret 文件如下:

  1. kind: Secret
  2. apiVersion: v1
  3. metadata:
  4. name: samba-admin-password
  5. data:
  6. ADMIN_PASSWORD_SECRET: superpassword

鉴于这种格式,类似于 Kubernetes secret,你需要在测试设置中解析该文件,然后将 secret 设置为容器的环境变量。

使用 testcontainers 和 Jackson 库进行 YAML 解析:

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
  3. import java.io.File;
  4. import java.util.Map;
  5. // ...
  6. // 加载并解析 secrets.yaml 文件
  7. ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
  8. File secretsFile = new File(&quot;/path/to/your/secrets.yaml&quot;);
  9. Map&lt;String, Object&gt; secretsData = mapper.readValue(secretsFile, Map.class);
  10. // 从解析的数据中提取 secret
  11. String adminPassword = (String) ((Map) secretsData.get(&quot;data&quot;)).get(&quot;ADMIN_PASSWORD_SECRET&quot;);
  12. // 创建并启动容器
  13. val ldapContainer = GenericContainer(&quot;instantlinux/samba-dc:latest&quot;)
  14. .withEnv(&quot;DOMAIN_ACTION&quot;, &quot;provision&quot;)
  15. .withEnv(&quot;REALM&quot;, &quot;my.company&quot;)
  16. .withEnv(&quot;ADMIN_PASSWORD_SECRET&quot;, adminPassword) // 将提取的 secret 设置为环境变量
  17. .withExposedPorts(53, 88, 389);
  18. ldapContainer.start();

这种方法涉及将 secret 读入你的 Java 应用程序,然后将其作为环境变量传递给容器。


从 OP 提供的示例中,看起来是在 Spring Boot 应用程序中使用 Testcontainers 启动的 Samba 容器。容器似乎启动正确,因为你可以看到 Samba 的初始化日志。

主要问题是 samba 容器在启动后立即关闭(应用程序在 Thread sleep 中挂起)。看起来根本原因可能在于 samba 日志中找到:

2023-08-21 13:39:07 ERROR(runtime): uncaught exception - (3221225506, &#39;{Access Denied} A process has requested access to an object but has not been granted those access rights.&#39;)

由于你正在使用 Docker,请记住 Samba 容器将有自己的用户系统。如果你挂载了卷,可能需要调整用户或组 ID 以与主机系统匹配。

在容器的入口点中添加 id -als -alrth /path/to/your/secrets.yaml 命令,以查看你在容器内是谁,以及你如何看待挂载的文件系统。
检查任何挂载到容器内的卷的所有权和权限。容器内部的 UID 和 GID 可能与外部不同,导致权限问题。

如果在 Docker 容器内运行 Samba,请确保根据需要提供所有必要的权限,使用 --cap-add

检查你的 Samba 配置 (smb.conf)。确保所定义的共享和路径具有正确的权限。还要检查是否有任何 valid usersread listwrite list 指令,并确保列出的用户具有适当的权限。

如果你运行的系统启用了 SELinux,这可能会导致权限问题。你可以将 SELinux 临时设置为宽松模式,以查看是否解决了问题:

  1. sudo setenforce 0

如果这解决了问题,你需要创建适当的 SELinux 策略或调整与 Samba 相关的文件和目录的上下文。

英文:

Testcontainers does not seem to support Docker Compose's secrets directly. The only notion of secret is when using a HashiCorp Vault Module .withSecretInVault().

In your case, you can try and use a volume to emulate a secret: it is a workaround, a volume to bind-mount your secrets into the container at the expected path.

  1. val secretPathOnHost = &quot;/path/to/your/secrets.yaml&quot;;
  2. val secretPathInContainer = &quot;/run/secrets/samba-admin-password&quot;;
  3. val ldapContainer = GenericContainer(&quot;instantlinux/samba-dc:latest&quot;)
  4. .withEnv(&quot;DOMAIN_ACTION&quot;, &quot;provision&quot;)
  5. .withEnv(&quot;REALM&quot;, &quot;my.company&quot;)
  6. .withEnv(&quot;ADMIN_PASSWORD_SECRET&quot;, &quot;samba-admin-password&quot;)
  7. .withExposedPorts(53, 88, 389)
  8. .withFileSystemBind(secretPathOnHost, secretPathInContainer, BindMode.READ_ONLY);
  9. ldapContainer.start();

Do replace /path/to/your/secrets.yaml with the absolute path to your secrets.yaml file on the host machine.

Note: Emulating secrets as volumes means the secret is available as a plaintext file on your host system, so make sure you manage its permissions and access properly. That might be fine for local development and testing, but might not be ideal for production-like environments.

And... never commit secrets or secret paths to source control.


Since the secret file is:

  1. kind: Secret
  2. apiVersion: v1
  3. metadata:
  4. name: samba-admin-password
  5. data:
  6. ADMIN_PASSWORD_SECRET: superpassword

Given this format, which is similar to a Kubernetes secret, you will need to parse the file in your test setup and then set the secret as an environment variable to the container.

Using testcontainers and the Jackson library for YAML parsing:

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
  3. import java.io.File;
  4. import java.util.Map;
  5. // ...
  6. // Load and parse the secrets.yaml file
  7. ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
  8. File secretsFile = new File(&quot;/path/to/your/secrets.yaml&quot;);
  9. Map&lt;String, Object&gt; secretsData = mapper.readValue(secretsFile, Map.class);
  10. // Extract the secret from the parsed data
  11. String adminPassword = (String) ((Map) secretsData.get(&quot;data&quot;)).get(&quot;ADMIN_PASSWORD_SECRET&quot;);
  12. // Create and start the container
  13. val ldapContainer = GenericContainer(&quot;instantlinux/samba-dc:latest&quot;)
  14. .withEnv(&quot;DOMAIN_ACTION&quot;, &quot;provision&quot;)
  15. .withEnv(&quot;REALM&quot;, &quot;my.company&quot;)
  16. .withEnv(&quot;ADMIN_PASSWORD_SECRET&quot;, adminPassword) // Set the extracted secret as an environment variable
  17. .withExposedPorts(53, 88, 389);
  18. ldapContainer.start();

That method involves reading the secret into your Java application, which you then pass as an environment variable to the container.


From the example provided by the OP, it looks like a Samba container using Testcontainers in a Spring Boot application. The container seems to be starting correctly as you can see the initialization logs of Samba.

> The main problem that samba container shutdown immediately after start(application is hanging on in Thread sleep). Looks like root cause could be find in samba logs:
>
> 2023-08-21 13:39:07 ERROR(runtime): uncaught exception - (3221225506, &#39;{Access Denied} A process has requested access to an object but has not been granted those access rights.&#39;)

Since you are using Docker, remember that the Samba container will have its own user system. You may need to adjust the user or group IDs to match your host system if you are mounting volumes.

Add in your container entry point the id -a and ls -alrth /path/to/your/secrets.yaml commands to see who you are inside the container, and how you see the mounted file system.
Check the ownership and permissions of any volumes you have mounted into the container. The UID and GID inside the container might differ from those outside, leading to permission issues.

If you are running Samba inside a Docker container, ensure that you have provided all necessary capabilities using --cap-add if needed.

Review your Samba configuration (smb.conf). Ensure that the shares and paths defined have the correct permissions.
Also, check for any valid users, read list, or write list directives and ensure that the users listed have the appropriate permissions.

And if you are running a system with SELinux enabled, this can cause permission issues. You can temporarily set SELinux to permissive mode to see if it resolves the issue:

  1. sudo setenforce 0

If this resolves the issue, you will need to create the appropriate SELinux policies or adjust the context for the Samba-related files and directories.

huangapple
  • 本文由 发表于 2023年8月10日 22:08:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定