4
4
import shutil
5
5
import socket
6
6
import subprocess
7
- from tempfile import TemporaryDirectory
8
- from base64 import b64encode
9
7
import time
10
- import bcrypt
8
+ from base64 import b64encode
11
9
from pathlib import Path
10
+ from tempfile import TemporaryDirectory
12
11
12
+ import bcrypt
13
13
import pytest
14
14
import requests
15
15
@@ -87,7 +87,9 @@ def registry(host_ip):
87
87
port = get_free_port ()
88
88
username = "user"
89
89
password = secrets .token_hex (16 )
90
- bcrypted_pw = bcrypt .hashpw (password .encode ("utf-8" ), bcrypt .gensalt (rounds = 12 )).decode ("utf-8" )
90
+ bcrypted_pw = bcrypt .hashpw (
91
+ password .encode ("utf-8" ), bcrypt .gensalt (rounds = 12 )
92
+ ).decode ("utf-8" )
91
93
92
94
# We put our password here, and mount it into the container.
93
95
# put it in current dir than in /tmp because on macos, current dir is likely to
@@ -100,12 +102,22 @@ def registry(host_ip):
100
102
registry_image = "registry:3.0.0-rc.3"
101
103
subprocess .check_call (["docker" , "pull" , registry_image ])
102
104
103
- cmd = ["docker" , "run" , "--rm" ,
104
- "-e" , "REGISTRY_AUTH=htpasswd" ,
105
- "-e" , "REGISTRY_AUTH_HTPASSWD_REALM=basic" ,
106
- "-e" , "REGISTRY_AUTH_HTPASSWD_PATH=/opt/htpasswd/htpasswd.conf" ,
107
- "--mount" , f"type=bind,src={ htpasswd_dir } ,dst=/opt/htpasswd" ,
108
- "-p" , f"{ port } :5000" , registry_image ]
105
+ cmd = [
106
+ "docker" ,
107
+ "run" ,
108
+ "--rm" ,
109
+ "-e" ,
110
+ "REGISTRY_AUTH=htpasswd" ,
111
+ "-e" ,
112
+ "REGISTRY_AUTH_HTPASSWD_REALM=basic" ,
113
+ "-e" ,
114
+ "REGISTRY_AUTH_HTPASSWD_PATH=/opt/htpasswd/htpasswd.conf" ,
115
+ "--mount" ,
116
+ f"type=bind,src={ htpasswd_dir } ,dst=/opt/htpasswd" ,
117
+ "-p" ,
118
+ f"{ port } :5000" ,
119
+ registry_image ,
120
+ ]
109
121
proc = subprocess .Popen (cmd )
110
122
health_url = f"http://{ host_ip } :{ port } /v2"
111
123
# Wait for the registry to actually come up
@@ -144,19 +156,24 @@ def test_registry_explicit_creds(registry, dind):
144
156
os .environ ["DOCKER_HOST" ] = docker_host
145
157
os .environ ["DOCKER_CERT_PATH" ] = str (cert_dir / "client" )
146
158
os .environ ["DOCKER_TLS_VERIFY" ] = "1"
147
- os .environ ["CONTAINER_ENGINE_REGISTRY_CREDENTIALS" ] = json .dumps ({
148
- "registry" : f"http://{ registry_host } " ,
149
- "username" : username ,
150
- "password" : password
151
- })
159
+ os .environ ["CONTAINER_ENGINE_REGISTRY_CREDENTIALS" ] = json .dumps (
160
+ {
161
+ "registry" : f"http://{ registry_host } " ,
162
+ "username" : username ,
163
+ "password" : password ,
164
+ }
165
+ )
152
166
r2d .start ()
153
167
154
-
155
- proc = subprocess .run (["docker" , "manifest" , "inspect" , "--insecure" , image_name ])
168
+ proc = subprocess .run (
169
+ ["docker" , "manifest" , "inspect" , "--insecure" , image_name ]
170
+ )
156
171
assert proc .returncode == 0
157
172
158
173
# Validate that we didn't leak our registry creds into existing docker config
159
- docker_config_path = Path (os .environ .get ("DOCKER_CONFIG" , "~/.docker/config.json" )).expanduser ()
174
+ docker_config_path = Path (
175
+ os .environ .get ("DOCKER_CONFIG" , "~/.docker/config.json" )
176
+ ).expanduser ()
160
177
if docker_config_path .exists ():
161
178
# Just check that our randomly generated password is not in this file
162
179
# Can this cause a conflict? Sure, if there's a different randomly generated password in here
@@ -184,15 +201,26 @@ def test_registry_no_explicit_creds(registry, dind):
184
201
os .environ ["DOCKER_CERT_PATH" ] = str (cert_dir / "client" )
185
202
os .environ ["DOCKER_TLS_VERIFY" ] = "1"
186
203
with TemporaryDirectory () as d :
187
- (Path (d ) / "config.json" ).write_text (json .dumps (
188
- ({"auths" :{f"http://{ registry_host } " :{"auth" :b64encode (f"{ username } :{ password } " .encode ()).decode ()}}})
189
- ))
204
+ (Path (d ) / "config.json" ).write_text (
205
+ json .dumps (
206
+ {
207
+ "auths" : {
208
+ f"http://{ registry_host } " : {
209
+ "auth" : b64encode (
210
+ f"{ username } :{ password } " .encode ()
211
+ ).decode ()
212
+ }
213
+ }
214
+ }
215
+ )
216
+ )
190
217
os .environ ["DOCKER_CONFIG" ] = d
191
218
r2d .start ()
192
219
193
-
194
- proc = subprocess .run (["docker" , "manifest" , "inspect" , "--insecure" , image_name ])
220
+ proc = subprocess .run (
221
+ ["docker" , "manifest" , "inspect" , "--insecure" , image_name ]
222
+ )
195
223
assert proc .returncode == 0
196
224
finally :
197
225
os .environ .clear ()
198
- os .environ .update (old_environ )
226
+ os .environ .update (old_environ )
0 commit comments