-
Notifications
You must be signed in to change notification settings - Fork 0
/
preorder-dfs-microbenchmark.py
executable file
·315 lines (250 loc) · 8.89 KB
/
preorder-dfs-microbenchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
'''
A microbenchmark for measuring the worst-case preorder DFS time for processing
delegations.
'''
# 1st-party
import binascii
import datetime
import errno
import glob
import hashlib
import json
import os
import random
import re
# 2nd-party
from nouns import PACKAGES_DIRECTORY, PYPI_DIRECTORY, SIMPLE_DIRECTORY
CLAIMED_PROJECTS = None
RARELY_UPDATED_PROJECTS = None
NEW_PROJECTS = None
UNCLAIMED_PROJECTS = None
OUTPUT_DIR = '/var/experiments-output/preorder-dfs-microbenchmark/'
ROLE_TO_BACKTRACK = {}
ROLE_TO_KEYIDS = {}
ROLE_TO_PATHS = {}
KEYID_TO_KEYVAL = {}
# Expires this many days from this UTC timestamp.
# Use the Javascript ISO 8601 format.
def make_expiration_timestamp(timestamp, days):
expires = datetime.datetime.utcfromtimestamp(timestamp)+\
datetime.timedelta(days=days)
return expires.isoformat()+'Z'
# Deterministic generation of "signature" given the same filenames, timestamp,
# and version number.
# EITHER the filenames, timestamp, OR the version number MUST change in order
# for the entire signature to change.
def make_pseudo_signature(filenames, timestamp, version):
change = '{}{}{}'.format(''.join(sorted(filenames)), timestamp, version)
# Concatenate two 64-byte hashes to get one 128-byte "signature".
first_half = get_sha256(change.encode('utf-8'))
second_half = get_sha256(first_half.encode('utf-8'))
return first_half+second_half
def make_targets_metadata(keyids=(), roles=(), targets={}, timestamp=0,
version=0):
role_keyids = []
for role in roles:
role_keyids.extend(ROLE_TO_KEYIDS[role])
return {
'signatures': [
{
'keyid': keyid,
'method': 'ed25519',
'sig': make_pseudo_signature(targets, timestamp, version)
} for keyid in keyids
],
'signed': {
'_type': 'Targets',
'delegations': {
'keys': {
keyid: {
'keytype': 'ed25519',
'keyval': {
'public': KEYID_TO_KEYVAL[keyid]
}
} for keyid in role_keyids
},
'roles': [
{
'backtrack': ROLE_TO_BACKTRACK[role],
'keyids': ROLE_TO_KEYIDS[role],
'name': role,
'paths': sorted(ROLE_TO_PATHS[role]),
'threshold': 1
} for role in roles
]
},
# Expire a year from now, following PEP 458.
'expires': make_expiration_timestamp(timestamp, 365),
'targets': targets,
'version': version
}
}
def mkdir(directory):
try:
os.makedirs(directory)
except OSError as os_error:
if os_error.errno != errno.EEXIST:
raise
def get_random_keyid():
return get_random_hexstring(64)
def get_random_ed25519_keyval():
return get_random_hexstring(64)
def get_random_hexstring(length_in_hex):
assert length_in_hex % 2 == 0
return binascii.b2a_hex(os.urandom(int(length_in_hex/2))).decode('utf-8')
def get_random_sha256():
return get_random_hexstring(64)
def get_sha256(data):
return hashlib.sha256(data).hexdigest()
def get_target_metadata(sha256, length):
assert length >= 0
return {
'hashes': {
'sha256': sha256
},
'length': length
}
def get_projects():
global CLAIMED_PROJECTS
global RARELY_UPDATED_PROJECTS
global NEW_PROJECTS
global UNCLAIMED_PROJECTS
NUMBER_OF_DELEGATEES = 1
projects = os.listdir(SIMPLE_DIRECTORY)
random.shuffle(projects)
assert len(projects) % NUMBER_OF_DELEGATEES == 0
i = len(projects) // NUMBER_OF_DELEGATEES
CLAIMED_PROJECTS = projects[:i]
RARELY_UPDATED_PROJECTS = projects[i:i*2]
NEW_PROJECTS = projects[i*2:i*3]
UNCLAIMED_PROJECTS = projects[i*3:]
assert len(CLAIMED_PROJECTS) + \
len(RARELY_UPDATED_PROJECTS) + \
len(NEW_PROJECTS) + \
len(UNCLAIMED_PROJECTS) == len(projects)
def write_json_to_disk(metadata_path, metadata_dict):
assert metadata_path.endswith('.json')
assert isinstance(metadata_dict, dict)
metadata_path = os.path.join(OUTPUT_DIR, metadata_path)
dirname = os.path.dirname(metadata_path)
mkdir(dirname)
with open(metadata_path, 'wt') as metadata_file:
json.dump(metadata_dict, metadata_file, indent=1, sort_keys=True)
print('W {}'.format(metadata_path))
def associate(role, paths, backtrack=True):
global KEYID_TO_KEYVAL
global ROLE_TO_KEYIDS
global ROLE_TO_PATHS
keyid = get_random_keyid()
keyval = get_random_ed25519_keyval()
assert isinstance(backtrack, bool)
ROLE_TO_BACKTRACK[role] = backtrack
assert role not in ROLE_TO_KEYIDS
ROLE_TO_KEYIDS[role] = [keyid]
assert keyid not in KEYID_TO_KEYVAL
KEYID_TO_KEYVAL[keyid] = keyval
assert role not in ROLE_TO_PATHS
assert isinstance(paths, list)
ROLE_TO_PATHS[role] = paths
def make_keys():
associate('projects', ['.*'])
associate('projects/claimed-projects', ['.*'])
for project in CLAIMED_PROJECTS:
associate('projects/claimed-projects/{}'.format(project),
['packages/.*/.*/{}/.*'.format(project)], backtrack=False)
associate('projects/rarely-updated-projects',
['packages/.*/.*/{}/.*'.format(project) \
for project in RARELY_UPDATED_PROJECTS], backtrack=False)
associate('projects/new-projects', ['.*'])
for project in NEW_PROJECTS:
associate('projects/new-projects/{}'.format(project),
['packages/.*/.*/{}/.*'.format(project)], backtrack=False)
associate('projects/unclaimed-projects', ['.*'])
def get_targets(projects):
targets = {}
for project in projects:
packages_directory = os.path.join(PACKAGES_DIRECTORY,
'*/*/{}/*'.format(project))
# NOTE: With PyPI renaming/canonical-ization of project names,
# simple names may not directly correspond to package names. What this
# means is that there may seem to be no packages for renamed projects.
for package in sorted(glob.glob(packages_directory)):
sha256 = get_random_sha256()
length = os.path.getsize(package)
targets[package] = get_target_metadata(sha256, length)
return targets
def write():
keyids = ROLE_TO_KEYIDS['projects']
roles = ('projects/claimed-projects', 'projects/rarely-updated-projects',
'projects/new-projects', 'projects/unclaimed-projects')
metadata_dict = make_targets_metadata(keyids, roles)
write_json_to_disk('projects.json', metadata_dict)
keyids = ROLE_TO_KEYIDS['projects/claimed-projects']
roles = ['projects/claimed-projects/{}'.format(project) \
for project in CLAIMED_PROJECTS]
metadata_dict = make_targets_metadata(keyids, roles)
write_json_to_disk('projects/claimed-projects.json', metadata_dict)
keyids = ROLE_TO_KEYIDS['projects/rarely-updated-projects']
roles = ()
targets = get_targets(RARELY_UPDATED_PROJECTS)
metadata_dict = make_targets_metadata(keyids, roles, targets)
write_json_to_disk('projects/rarely-updated-projects.json', metadata_dict)
keyids = ROLE_TO_KEYIDS['projects/new-projects']
roles = ['projects/new-projects/{}'.format(project) \
for project in NEW_PROJECTS]
metadata_dict = make_targets_metadata(keyids, roles)
write_json_to_disk('projects/new-projects.json', metadata_dict)
keyids = ROLE_TO_KEYIDS['projects/unclaimed-projects']
roles = ()
targets = get_targets(UNCLAIMED_PROJECTS)
metadata_dict = make_targets_metadata(keyids, roles, targets)
write_json_to_disk('projects/unclaimed-projects.json', metadata_dict)
def preorder_dfs(role, package):
print(role)
metadata_path = os.path.join(OUTPUT_DIR, '{}.json'.format(role))
with open(metadata_path) as metadata_file:
metadata = json.load(metadata_file)
signatures = metadata['signatures']
signed = metadata['signed']
delegations = signed['delegations']
keys = delegations['keys']
roles = delegations['roles']
targets = signed['targets']
# Preorder: do I know about it?
target = targets.get(os.path.join(PYPI_DIRECTORY, package))
if target:
return target
# Do delegatees have it?
else:
# Ask delegatees in order of priority/appearance.
for role in roles:
backtrack = role['backtrack']
name = role['name']
paths = role['paths']
# Have I delegated it to this delegatee?
for path in paths:
if re.match(path, package):
target = preorder_dfs(name, package)
# Does this delegatee know about it?
if target:
return target
# If not, should I ask the rest of the delegatees?
elif not backtrack:
return None
# Nobody else left to ask.
else:
return None
if __name__ == '__main__':
# Write the delegations.
#get_projects()
#make_keys()
#write()
# claimed-projects
preorder_dfs("projects", "packages/source/c/chem/chem-2.0.tar.gz")
# rarely-updated-projects
#preorder_dfs("projects", "packages/source/z/zyzz/zyzz-1.0.1.tar.gz")
# new-projects
#preorder_dfs("projects", "packages/source/a/agree/agree.tar.gz")
# unclaimed-projects
preorder_dfs("projects", "packages/source/z/zzz/zzz-0.0.2.tar.gz")