Skip to content

Commit 5e9c759

Browse files
committed
CI: don't generate files in the source directory
That was just a lazy kludge, the real way we want to do this is by generating the config files more dynamically so they can use non-relative paths.
1 parent ae007d8 commit 5e9c759

24 files changed

+164
-187
lines changed

.ruff.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include = ["contrib/openarc-keygen"]
1+
include = ["contrib/openarc-keygen", "*.py"]
22
line-length = 160
33

44
[lint]

test/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
EXTRA_DIST = files/*.conf files/peerlist* files/unsafe.key pytest.ini *.py
1+
EXTRA_DIST = files/*.conf files/peerlist* pytest.ini *.py
22

33
check:
44
pytest -vv

test/conftest.py

+90-53
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,114 @@
11
#!/usr/bin/env python3
22

3-
import os
3+
import json
4+
import pathlib
45
import subprocess
56
import time
67

78
import pytest
89

910

10-
@pytest.fixture()
11-
def private_key(scope='session'):
12-
basepath = os.path.dirname(os.path.realpath(__file__))
13-
keypath = os.path.join(basepath, 'files', 'private.key')
14-
binargs = [
15-
'openssl',
16-
'genrsa',
17-
'-out',
18-
keypath,
19-
'2048',
20-
]
21-
subprocess.run(binargs)
22-
23-
pubpath = os.path.join(basepath, 'files', 'public.key')
24-
binargs = [
25-
'openssl',
26-
'rsa',
27-
'-in',
28-
keypath,
29-
'-pubout',
30-
]
31-
res = subprocess.run(binargs, capture_output=True, text=True)
32-
with open(pubpath, 'w') as f:
33-
key = ''.join(res.stdout.splitlines()[1:-1])
34-
f.write(
35-
'sel._domainkey.dkimpy.example.com v=DKIM1; k=rsa; '
36-
'p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqf/MoqRqzK3/bcCyLSx5'
37-
'CDvyPotNDBjLLFHdMmcWDiSZ8saslFyNR6FkFxuNtw843m7MkwOSJ9TRd9p+OoRLDv'
38-
'H0jDR1Dqq22QOJKiG5XQ91aZwin9jpWKkuoRoRZRhWrzUOJWAybHarsEQm9iCPh2zn'
39-
'dbSPSzPQL1OsjURIuw5G9+/nr5rhJ72Qi6v86zofWUKdXhLf+oVmho79D0xGMFFm0f'
40-
'b98xIeZlgJTnmrj/zuxIKHeVmGKI1j6L3xttdcDiUVRGxoubkFzg9TIBGhdeFkpa0C'
41-
'ZuhB/1/U3f1oG3Upx5o/jXTQk/dwVaaeEXnRmTsfGYn4GQ9ziity1ijLsQIDAQAB\n'
42-
)
43-
f.write(f'elpmaxe._domainkey.example.com v=DKIM1; k=rsa; h=sha256; p={key}\n')
44-
f.write(f'xn--2j5b._domainkey.xn--vv4b606a.example.com v=DKIM1; k=rsa; h=sha256; p={key}\n')
11+
@pytest.fixture(scope='session')
12+
def private_key(tmp_path_factory, tool_path):
13+
basepath = tmp_path_factory.mktemp('keys')
4514

15+
for s, d in [
16+
['elpmaxe', 'example.com'],
17+
['xn--2j5b', 'xn--vv4b606a.example.com'],
18+
['unsafe', 'example.com'],
19+
]:
20+
binargs = [
21+
tool_path('contrib/openarc-keygen'),
22+
'-D',
23+
str(basepath),
24+
'-d',
25+
d,
26+
'-s',
27+
s,
28+
'--hash-algorithms',
29+
'sha256',
30+
'-f',
31+
'testkey',
32+
]
33+
subprocess.run(binargs, check=True)
34+
35+
basepath.joinpath('unsafe._domainkey.example.com.key').chmod(0o644)
36+
37+
testkeys = (
38+
'sel._domainkey.dkimpy.example.com v=DKIM1; k=rsa; '
39+
'p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqf/MoqRqzK3/bcCyLSx5'
40+
'CDvyPotNDBjLLFHdMmcWDiSZ8saslFyNR6FkFxuNtw843m7MkwOSJ9TRd9p+OoRLDv'
41+
'H0jDR1Dqq22QOJKiG5XQ91aZwin9jpWKkuoRoRZRhWrzUOJWAybHarsEQm9iCPh2zn'
42+
'dbSPSzPQL1OsjURIuw5G9+/nr5rhJ72Qi6v86zofWUKdXhLf+oVmho79D0xGMFFm0f'
43+
'b98xIeZlgJTnmrj/zuxIKHeVmGKI1j6L3xttdcDiUVRGxoubkFzg9TIBGhdeFkpa0C'
44+
'ZuhB/1/U3f1oG3Upx5o/jXTQk/dwVaaeEXnRmTsfGYn4GQ9ziity1ijLsQIDAQAB\n'
45+
)
46+
47+
for fname in [
48+
'elpmaxe._domainkey.example.com.txt',
49+
'xn--2j5b._domainkey.xn--vv4b606a.example.com.txt',
50+
]:
51+
with open(basepath.joinpath(fname), 'r') as f:
52+
testkeys += f.read()
4653

47-
@pytest.fixture()
48-
def tool_path(scope='session'):
54+
keyfile = basepath.joinpath('public.key')
55+
with open(keyfile, 'w') as f:
56+
f.write(testkeys)
57+
58+
return {
59+
'basepath': basepath,
60+
'public_keys': str(keyfile),
61+
}
62+
63+
64+
@pytest.fixture(scope='session')
65+
def tool_path():
4966
def _tool_path(tool):
50-
binpath = os.path.dirname(os.path.realpath(__file__))
51-
binpath = os.path.join(binpath, '..', tool)
52-
return os.path.realpath(binpath)
67+
return pathlib.Path(__file__).parent.parent.joinpath(tool).absolute()
5368

5469
return _tool_path
5570

5671

5772
@pytest.fixture()
5873
def milter_config(request, tmp_path, private_key):
59-
base_path = os.path.join(request.fspath.dirname, 'files')
74+
base_path = request.path.parent.joinpath('files')
75+
6076
config = {
61-
'cwd': base_path,
62-
'file': os.path.join(base_path, 'milter.conf'),
63-
'sock': tmp_path.joinpath('milter.sock'),
77+
'Domain': 'example.com',
78+
'AuthservID': 'example.com',
79+
'TestKeys': private_key['public_keys'],
80+
'Selector': 'elpmaxe',
81+
'KeyFile': 'elpmaxe._domainkey.example.com.key',
82+
'Mode': 'sv',
83+
'FixedTimestamp': '1234567890',
84+
'RequireSafeKeys': 'false', # tmp is world writeable
6485
}
86+
6587
for candidate in [
66-
request.fspath.basename, # test file
88+
request.path.name, # test file
6789
request.function.__name__, # test function
6890
]:
69-
fname = os.path.join(base_path, '.'.join([candidate, 'conf']))
70-
if os.path.isfile(fname):
71-
config['file'] = fname
72-
return config
91+
fname = base_path.joinpath(f'{candidate}.conf')
92+
if fname.exists():
93+
config.update(json.loads(fname.read_text()))
94+
95+
if config['KeyFile']:
96+
config['KeyFile'] = private_key['basepath'].joinpath(config['KeyFile'])
7397

74-
return config
98+
for static_file in ['PeerList', 'InternalHosts']:
99+
if config.get(static_file):
100+
config[static_file] = base_path.joinpath(config[static_file])
101+
102+
fname = tmp_path.joinpath('milter.conf')
103+
with open(fname, 'w') as f:
104+
for k, v in config.items():
105+
if v is not None:
106+
f.write(f'{k} {v}\n')
107+
108+
return {
109+
'file': fname,
110+
'sock': tmp_path.joinpath('milter.sock'),
111+
}
75112

76113

77114
@pytest.fixture()
@@ -89,8 +126,8 @@ def milter_cmdline(tmp_path, tool_path, milter_config):
89126

90127
@pytest.fixture()
91128
def milter(milter_cmdline, milter_config):
92-
milter_proc = subprocess.Popen(milter_cmdline, cwd=milter_config['cwd'])
93-
while not milter_proc.poll() and not os.path.exists(milter_config['sock']):
129+
milter_proc = subprocess.Popen(milter_cmdline)
130+
while not milter_proc.poll() and not milter_config['sock'].exists():
94131
time.sleep(0.1)
95132

96133
yield milter_proc

test/files/milter.conf

-7
This file was deleted.

test/files/test_config_fail.conf

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
Domain example.com
2-
Selector elpmaxe
1+
{
2+
"Selector": null
3+
}
+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile unsafe.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode sv
1+
{
2+
"RequireSafeKeys": "true",
3+
"KeyFile": "unsafe._domainkey.example.com.key"
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile unsafe.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode sv
7-
RequireSafeKeys false
1+
{
2+
"RequireSafeKeys": "false",
3+
"KeyFile": "unsafe._domainkey.example.com.key"
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode sv
7-
PermitAuthenticationOverrides false
1+
{
2+
"PermitAuthenticationOverrides": "false"
3+
}

test/files/test_milter_authresip.conf

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
Domain example.com
2-
AuthservID example.com
3-
AuthResIP false
4-
KeyFile private.key
5-
TestKeys public.key
6-
Selector elpmaxe
7-
Mode sv
1+
{
2+
"AuthResIP": "false"
3+
}
+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode s
7-
Canonicalization simple/simple
1+
{
2+
"Mode": "s",
3+
"Canonicalization": "simple/simple"
4+
}
+3-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
FinalReceiver yes
2-
Domain example.com
3-
AuthservID example.com
4-
KeyFile private.key
5-
TestKeys public.key
6-
Selector elpmaxe
7-
Mode sv
8-
FixedTimestamp 1234567890
1+
{
2+
"FinalReceiver": "yes"
3+
}

test/files/test_milter_idna.conf

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Domain 시험.example.com
2-
AuthservID 시험.example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector 예
6-
Mode s
7-
FixedTimestamp 1234567890
1+
{
2+
"Domain": "시험.example.com",
3+
"AuthservID": "시험.example.com",
4+
"Selector": "예",
5+
"KeyFile": "xn--2j5b._domainkey.xn--vv4b606a.example.com.key",
6+
"Mode": "s"
7+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode s
7-
FixedTimestamp 1234567890
8-
MinimumKeySizeRSA 2048
1+
{
2+
"Mode": "s",
3+
"MinimumKeySizeRSA": "2048"
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode s
7-
FixedTimestamp 1234567890
8-
MinimumKeySizeRSA 2049
1+
{
2+
"Mode": "s",
3+
"MinimumKeySizeRSA": "2049"
4+
}
+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
1+
{
2+
"Mode": null
3+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
InternalHosts peerlist_nolocalhost
1+
{
2+
"Mode": null,
3+
"InternalHosts": "peerlist_nolocalhost"
4+
}

test/files/test_milter_mode_s.conf

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode s
1+
{
2+
"Mode": "s"
3+
}

test/files/test_milter_mode_v.conf

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode v
1+
{
2+
"Mode": "v"
3+
}

test/files/test_milter_peerlist.conf

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode sv
7-
PeerList peerlist
1+
{
2+
"PeerList": "peerlist"
3+
}

test/files/test_milter_resign.conf

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode sv
7-
MaximumHeaders 0
8-
PermitAuthenticationOverrides false
1+
{
2+
"MaximumHeaders": "0",
3+
"PermitAuthenticationOverrides": "false"
4+
}

test/files/test_milter_resign_s.conf

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
Domain example.com
2-
AuthservID example.com
3-
KeyFile private.key
4-
TestKeys public.key
5-
Selector elpmaxe
6-
Mode s
7-
MaximumHeaders 0
8-
PermitAuthenticationOverrides false
1+
{
2+
"MaximumHeaders": "0",
3+
"PermitAuthenticationOverrides": "false",
4+
"Mode": "s"
5+
}

0 commit comments

Comments
 (0)