6
6
7
7
from plumbum import FG , local
8
8
from plumbum .cmd import cat , docker
9
+ from plumbum .commands .processes import ProcessExecutionError
9
10
10
11
# Make sure all paths are relative to tests dir
11
12
local .cwd .chdir (os .path .dirname (__file__ ))
@@ -16,7 +17,8 @@ class PostgresAutoconfCase(unittest.TestCase):
16
17
"""Test behavior for this docker image"""
17
18
def setUp (self ):
18
19
with local .cwd (local .cwd / ".." ):
19
- local ["./hooks/build" ]()
20
+ print ("Building image" )
21
+ local ["./hooks/build" ] & FG
20
22
docker ("network" , "create" , "lan" )
21
23
docker ("network" , "create" , "wan" )
22
24
self .version = os .environ ["DOCKER_TAG" ]
@@ -32,19 +34,19 @@ def tearDown(self):
32
34
try :
33
35
print ("Postgres container logs:" )
34
36
docker ["container" , "logs" , self .postgres_container ] & FG
35
- docker ( "container" , "stop" , self .postgres_container )
36
- docker ( "container" , "rm" , self .postgres_container )
37
+ docker [ "container" , "stop" , self .postgres_container ] & FG
38
+ docker [ "container" , "rm" , self .postgres_container ] & FG
37
39
except AttributeError :
38
40
pass # No postgres daemon
39
41
docker ("network" , "rm" , "lan" , "wan" )
40
42
return super ().tearDown ()
41
43
42
- def generate_certs (self ):
44
+ def _generate_certs (self ):
43
45
"""Generate certificates for testing the image."""
44
46
certgen ("example.com" , "test_user" )
45
47
46
- def check_cert_config (self ):
47
- """Check that the cert config is OK ."""
48
+ def _check_local_connection (self ):
49
+ """Check that local connection works fine ."""
48
50
# The 1st test could fail while postgres boots
49
51
for attempt in range (10 ):
50
52
try :
@@ -65,31 +67,40 @@ def check_cert_config(self):
65
67
raise
66
68
else :
67
69
continue
68
- run = docker [
70
+
71
+ def _check_password_auth (self , host = None ):
72
+ """Test connection with password auth work fine."""
73
+ if not host :
74
+ # Connect via LAN by default
75
+ host = self .postgres_container [:12 ]
76
+ self .assertEqual ("1\n " , docker (
69
77
"container" , "run" ,
70
- ]
71
- # Test LAN connection with password auth works fine
72
- self .assertEqual ("1\n " , run (
73
78
"--network" , "lan" ,
74
79
"-e" , "PGDATABASE=test_db" ,
75
80
"-e" , "PGPASSWORD=test_password" ,
76
81
"-e" , "PGSSLMODE=disable" ,
77
82
"-e" , "PGUSER=test_user" ,
78
83
self .image , "psql" ,
79
- "--host" , self . postgres_container [: 12 ] ,
84
+ "--host" , host ,
80
85
"--command" , "SELECT 1" ,
81
86
"--no-align" ,
82
87
"--tuples-only" ,
83
88
))
84
- # Attach a new network to mock a WAN connection
89
+
90
+ def _connect_wan_network (self , alias = "example.com" ):
91
+ """Bind a new network, to imitate WAN connections."""
85
92
docker (
86
93
"network" , "connect" ,
87
- "--alias" , "example.com" ,
94
+ "--alias" , alias ,
88
95
"wan" ,
89
96
self .postgres_container ,
90
97
)
91
- # Test WAN connection with cert auth works fine
92
- self .assertEqual ("1\n " , run (
98
+
99
+ def _check_cert_auth (self ):
100
+ """Test connection with cert auth work fine."""
101
+ # Test connection with cert auth works fine
102
+ self .assertEqual ("1\n " , docker (
103
+ "container" , "run" ,
93
104
"--network" , "wan" ,
94
105
"-e" , "PGDATABASE=test_db" ,
95
106
"-e" , "PGSSLCERT=/certs/client.cert.pem" ,
@@ -109,7 +120,7 @@ def test_server_certs_var(self):
109
120
"""Test server enables cert authentication through env vars."""
110
121
with local .tempdir () as tdir :
111
122
with local .cwd (tdir ):
112
- self .generate_certs ()
123
+ self ._generate_certs ()
113
124
certs_var = {name : cat (name ) for name in self .cert_files }
114
125
self .postgres_container = docker (
115
126
"container" , "run" ,
@@ -120,13 +131,16 @@ def test_server_certs_var(self):
120
131
"-e" , "POSTGRES_USER=test_user" ,
121
132
self .image ,
122
133
).strip ()
123
- self .check_cert_config ()
134
+ self ._check_local_connection ()
135
+ self ._check_password_auth ()
136
+ self ._connect_wan_network ()
137
+ self ._check_cert_auth ()
124
138
125
139
def test_server_certs_mount (self ):
126
140
"""Test server enables cert authentication through file mounts."""
127
141
with local .tempdir () as tdir :
128
142
with local .cwd (tdir ):
129
- self .generate_certs ()
143
+ self ._generate_certs ()
130
144
cert_vols = [
131
145
"-v{0}/{1}:/etc/postgres/{1}" .format (local .cwd , cert )
132
146
for cert in [
@@ -144,7 +158,65 @@ def test_server_certs_mount(self):
144
158
* cert_vols ,
145
159
self .image ,
146
160
).strip ()
147
- self .check_cert_config ()
161
+ self ._check_local_connection ()
162
+ self ._check_password_auth ()
163
+ self ._connect_wan_network ()
164
+ self ._check_cert_auth ()
165
+
166
+ def test_no_certs_lan (self ):
167
+ """Normal configuration without certs works fine."""
168
+ self .postgres_container = docker (
169
+ "container" , "run" , "-d" ,
170
+ "--network" , "lan" ,
171
+ "-e" , "POSTGRES_DB=test_db" ,
172
+ "-e" , "POSTGRES_PASSWORD=test_password" ,
173
+ "-e" , "POSTGRES_USER=test_user" ,
174
+ self .image ,
175
+ ).strip ()
176
+ self ._check_local_connection ()
177
+ self ._check_password_auth ()
178
+ self ._connect_wan_network ()
179
+ with self .assertRaises (ProcessExecutionError ):
180
+ self ._check_password_auth ("example.com" )
181
+
182
+ def test_no_certs_wan (self ):
183
+ """Unencrypted WAN access works (although this is dangerous)."""
184
+ self .postgres_container = docker (
185
+ "container" , "run" , "-d" ,
186
+ "--network" , "lan" ,
187
+ "-e" , "POSTGRES_DB=test_db" ,
188
+ "-e" , "POSTGRES_PASSWORD=test_password" ,
189
+ "-e" , "POSTGRES_USER=test_user" ,
190
+ "-e" , "WAN_AUTH_METHOD=md5" ,
191
+ "-e" , "WAN_CONNECTION=host" ,
192
+ self .image ,
193
+ ).strip ()
194
+ self ._check_local_connection ()
195
+ self ._check_password_auth ()
196
+ self ._connect_wan_network ()
197
+ with self .assertRaises (ProcessExecutionError ):
198
+ self ._check_password_auth ("example.com" )
199
+
200
+ def test_certs_falsy_lan (self ):
201
+ """Configuration with falsy values for certs works fine."""
202
+ self .postgres_container = docker (
203
+ "container" , "run" , "-d" ,
204
+ "--network" , "lan" ,
205
+ "-e" , "POSTGRES_DB=test_db" ,
206
+ "-e" , "POSTGRES_PASSWORD=test_password" ,
207
+ "-e" , "POSTGRES_USER=test_user" ,
208
+ "-e" , "CERTS={}" .format (json .dumps ({
209
+ "client.ca.cert.pem" : False ,
210
+ "server.cert.pem" : False ,
211
+ "server.key.pem" : False ,
212
+ })),
213
+ self .image ,
214
+ ).strip ()
215
+ self ._check_local_connection ()
216
+ self ._check_password_auth ()
217
+ self ._connect_wan_network ()
218
+ with self .assertRaises (ProcessExecutionError ):
219
+ self ._check_password_auth ("example.com" )
148
220
149
221
150
222
if __name__ == "__main__" :
0 commit comments