mirror of
https://github.com/theupdateframework/python-tuf
synced 2026-05-24 10:08:28 +00:00
Added the generate_repository_data.py script and its produce
The generate_repository_data script works in the exact same way as the generate script and it generates a fresh batch of pre-signed metadata to test the load_project function.
This commit is contained in:
parent
915f07a88a
commit
07e8ef0be7
13 changed files with 168 additions and 0 deletions
160
tests/repository_data/generate_project_data.py
Executable file
160
tests/repository_data/generate_project_data.py
Executable file
|
|
@ -0,0 +1,160 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
<Program Name>
|
||||
generate_project_data.py
|
||||
|
||||
<Author>
|
||||
Santiago Torres <torresariass@gmail.com>
|
||||
|
||||
|
||||
<Copyright>
|
||||
See LICENSE for licensing information.
|
||||
|
||||
<Purpose>
|
||||
Generate a pre-fabricated set of metadata files to use for the unit testing.
|
||||
"""
|
||||
|
||||
import shutil
|
||||
import datetime
|
||||
import optparse
|
||||
import os
|
||||
|
||||
from tuf.developer_tool import *
|
||||
import tuf.util
|
||||
|
||||
|
||||
parser = optparse.OptionParser()
|
||||
|
||||
parser.add_option("-d","--dry-run", action='store_true', dest="dry_run",
|
||||
help="Do not write the files, just run", default=False)
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
|
||||
project_key_file = 'keystore/root_key'
|
||||
targets_key_file = 'keystore/targets_key'
|
||||
delegation_key_file = 'keystore/delegation_key'
|
||||
|
||||
# the files we use for signing in the unit tests should exist, if they are not
|
||||
# populated, run generate.py
|
||||
assert os.path.exists(project_key_file)
|
||||
assert os.path.exists(targets_key_file)
|
||||
assert os.path.exists(delegation_key_file)
|
||||
|
||||
# Import the public keys. These keys are needed so that metadata roles are
|
||||
# assigned verification keys, which clients use to verify the signatures created
|
||||
# by the corresponding private keys.
|
||||
project_public = import_rsa_publickey_from_file(project_key_file+'.pub')
|
||||
targets_public = import_rsa_publickey_from_file(targets_key_file+'.pub')
|
||||
delegation_public = import_rsa_publickey_from_file(delegation_key_file+'.pub')
|
||||
|
||||
# Import the private keys. These private keys are needed to generate the
|
||||
# signatures included in metadata.
|
||||
project_private = import_rsa_privatekey_from_file(project_key_file, 'password')
|
||||
targets_private = import_rsa_privatekey_from_file(targets_key_file, 'password')
|
||||
delegation_private = import_rsa_privatekey_from_file(delegation_key_file, 'password')
|
||||
|
||||
os.mkdir("project")
|
||||
os.mkdir("project/targets")
|
||||
|
||||
# Create the target files (downloaded by clients) whose file size and digest
|
||||
# are specified in the 'targets.json' file.
|
||||
target1_filepath = 'project/targets/file1.txt'
|
||||
tuf.util.ensure_parent_dir(target1_filepath)
|
||||
target2_filepath = 'project/targets/file2.txt'
|
||||
tuf.util.ensure_parent_dir(target2_filepath)
|
||||
target3_filepath = 'project/targets/file3.txt'
|
||||
tuf.util.ensure_parent_dir(target2_filepath)
|
||||
|
||||
if not options.dry_run:
|
||||
with open(target1_filepath, 'wt') as file_object:
|
||||
file_object.write('This is an example target file.')
|
||||
|
||||
with open(target2_filepath, 'wt') as file_object:
|
||||
file_object.write('This is an another example target file.')
|
||||
|
||||
with open(target3_filepath, 'wt') as file_object:
|
||||
file_object.write('This is role1\'s target file.')
|
||||
|
||||
|
||||
project = create_new_project("test-flat", 'project/test-flat', 'prefix',
|
||||
'project/targets')
|
||||
|
||||
# Add target files to the top-level 'targets.json' role. These target files
|
||||
# should already exist.
|
||||
project.add_target(target1_filepath)
|
||||
project.add_target(target2_filepath)
|
||||
|
||||
# add keys to the project
|
||||
project.add_verification_key(project_public)
|
||||
project.load_signing_key(project_private)
|
||||
|
||||
project.delegate('role1', [delegation_public], [target3_filepath])
|
||||
project('role1').load_signing_key(delegation_private)
|
||||
|
||||
# Set the top-level expiration times far into the future so that
|
||||
# they do not expire anytime soon, or else the tests fail. Unit tests may
|
||||
# modify the expiration datetimes (of the copied files), if they wish.
|
||||
project.expiration = datetime.datetime(2030, 1, 1, 0, 0)
|
||||
project('role1').expiration = datetime.datetime(2030, 1, 1, 0, 0)
|
||||
|
||||
# Compress the 'targets.json' role so that the unit tests have a pre-generated
|
||||
# example of compressed metadata.
|
||||
project.compressions = ['gz']
|
||||
|
||||
# Create the actual metadata files, which are saved to 'metadata.staged'.
|
||||
if not options.dry_run:
|
||||
project.write()
|
||||
|
||||
tuf.roledb.clear_roledb()
|
||||
tuf.keydb.clear_keydb()
|
||||
|
||||
project = create_new_project("test-repo-like", "project/test-repo", 'prefix')
|
||||
|
||||
# Create the target files (downloaded by clients) whose file size and digest
|
||||
# are specified in the 'targets.json' file.
|
||||
target1_filepath = 'project/test-repo/targets/file1.txt'
|
||||
tuf.util.ensure_parent_dir(target1_filepath)
|
||||
target2_filepath = 'project/test-repo/targets/file2.txt'
|
||||
tuf.util.ensure_parent_dir(target2_filepath)
|
||||
target3_filepath = 'project/test-repo/targets/file3.txt'
|
||||
tuf.util.ensure_parent_dir(target2_filepath)
|
||||
|
||||
if not options.dry_run:
|
||||
with open(target1_filepath, 'wt') as file_object:
|
||||
file_object.write('This is an example target file.')
|
||||
|
||||
with open(target2_filepath, 'wt') as file_object:
|
||||
file_object.write('This is an another example target file.')
|
||||
|
||||
with open(target3_filepath, 'wt') as file_object:
|
||||
file_object.write('This is role1\'s target file.')
|
||||
|
||||
|
||||
# Add target files to the top-level 'targets.json' role. These target files
|
||||
# should already exist.
|
||||
project.add_target(target1_filepath)
|
||||
project.add_target(target2_filepath)
|
||||
|
||||
# add keys to the project
|
||||
project.add_verification_key(project_public)
|
||||
project.load_signing_key(project_private)
|
||||
|
||||
project.delegate('role1', [delegation_public], [target3_filepath])
|
||||
project('role1').load_signing_key(delegation_private)
|
||||
|
||||
# Set the top-level expiration times far into the future so that
|
||||
# they do not expire anytime soon, or else the tests fail. Unit tests may
|
||||
# modify the expiration datetimes (of the copied files), if they wish.
|
||||
project.expiration = datetime.datetime(2030, 1, 1, 0, 0)
|
||||
project('role1').expiration = datetime.datetime(2030, 1, 1, 0, 0)
|
||||
|
||||
# Compress the 'targets.json' role so that the unit tests have a pre-generated
|
||||
# example of compressed metadata.
|
||||
project.compressions = ['gz']
|
||||
|
||||
# Create the actual metadata files, which are saved to 'metadata.staged'.
|
||||
if not options.dry_run:
|
||||
project.write()
|
||||
|
||||
|
||||
1
tests/repository_data/project/targets/file1.txt
Normal file
1
tests/repository_data/project/targets/file1.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
This is an example target file.
|
||||
1
tests/repository_data/project/targets/file2.txt
Normal file
1
tests/repository_data/project/targets/file2.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
This is an another example target file.
|
||||
1
tests/repository_data/project/targets/file3.txt
Normal file
1
tests/repository_data/project/targets/file3.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
This is role1's target file.
|
||||
1
tests/repository_data/project/test-flat/project.cfg
Normal file
1
tests/repository_data/project/test-flat/project.cfg
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"project_name": "test-flat", "targets_location": "/home/santiago/Documents/v2014/TUF/tuf/tests/repository_data/project/targets", "prefix": "prefix", "metadata_location": "/home/santiago/Documents/v2014/TUF/tuf/tests/repository_data/project/test-flat", "threshold": 1, "public_keys": {"6986b667c736a3b37471e030cf4ce7aa6c7e0d530325e64c2660276b77be3754": {"keytype": "rsa", "keyval": {"public": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7J15ZaeDQPrhQsRj29wB\nPhibH+Do59xsT2396L+uCg793gZlar5wZN2eHSh725cNQWyTAa9LwG+lXaKMukQ+\n8176CKR2J5sv3DezrGVu3x8V1qhyJyy79FlNZRVYTVqNaYzvJzxsVnFPpg7f8B7C\nffiqWJr9XkpqwRlCpxooXm4hplZ7uek5Ku21CzQ4OWg7hbuc+ZjCGzpXfm8NuosU\n7TipnKGpEt0Agiph5g6TB2/scoeFar1CKMONIl80maxzAQk+xkWgiJ00+Z2qFCsx\nESfis/YkILS6RMFyZz7oa1WwMtUjYmrsRuz+jlFcbNuxZpIkaISiG9a2YdGcJ1Aj\n3QIDAQAB\n-----END PUBLIC KEY-----"}}}, "layout_type": "flat"}
|
||||
BIN
tests/repository_data/project/test-flat/test-flat.json
Normal file
BIN
tests/repository_data/project/test-flat/test-flat.json
Normal file
Binary file not shown.
BIN
tests/repository_data/project/test-flat/test-flat/role1.json
Normal file
BIN
tests/repository_data/project/test-flat/test-flat/role1.json
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
1
tests/repository_data/project/test-repo/project.cfg
Normal file
1
tests/repository_data/project/test-repo/project.cfg
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"project_name": "test-repo-like", "targets_location": "/home/santiago/Documents/v2014/TUF/tuf/tests/repository_data/project/test-repo/targets", "prefix": "prefix", "metadata_location": "/home/santiago/Documents/v2014/TUF/tuf/tests/repository_data/project/test-repo/metadata", "threshold": 1, "public_keys": {"6986b667c736a3b37471e030cf4ce7aa6c7e0d530325e64c2660276b77be3754": {"keytype": "rsa", "keyval": {"public": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7J15ZaeDQPrhQsRj29wB\nPhibH+Do59xsT2396L+uCg793gZlar5wZN2eHSh725cNQWyTAa9LwG+lXaKMukQ+\n8176CKR2J5sv3DezrGVu3x8V1qhyJyy79FlNZRVYTVqNaYzvJzxsVnFPpg7f8B7C\nffiqWJr9XkpqwRlCpxooXm4hplZ7uek5Ku21CzQ4OWg7hbuc+ZjCGzpXfm8NuosU\n7TipnKGpEt0Agiph5g6TB2/scoeFar1CKMONIl80maxzAQk+xkWgiJ00+Z2qFCsx\nESfis/YkILS6RMFyZz7oa1WwMtUjYmrsRuz+jlFcbNuxZpIkaISiG9a2YdGcJ1Aj\n3QIDAQAB\n-----END PUBLIC KEY-----"}}}, "layout_type": "repo-like"}
|
||||
|
|
@ -0,0 +1 @@
|
|||
This is an example target file.
|
||||
|
|
@ -0,0 +1 @@
|
|||
This is an another example target file.
|
||||
|
|
@ -0,0 +1 @@
|
|||
This is role1's target file.
|
||||
Loading…
Reference in a new issue