Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transition to the newest version of TUF #561

Merged
merged 117 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
117 commits
Select commit Hold shift + click to select a range
60b8fa9
Update tuf, securesystemslib and cryptography deps
lukpueh Aug 23, 2024
f62907e
Add alternative TUF metadata repo implementation
lukpueh Aug 23, 2024
ad2b58f
Change create and add_keys API to take signers
lukpueh Aug 26, 2024
b46645b
Make sure targets is signed on add key
lukpueh Aug 26, 2024
33750eb
Assert keytype rsa in taf.tuf.keys helper
lukpueh Aug 27, 2024
42fbfac
Add signer implementation for Yubikeys
lukpueh Aug 28, 2024
25371d7
Comment out legacy imports (WIP)
lukpueh Aug 28, 2024
ec294a8
Merge branch 'feature/tuf-repositoty' into tuf-upgrade
renatav Oct 29, 2024
70927b7
Merge pull request #512 from lukpueh/tuf-upgrade
renatav Oct 29, 2024
9bf3fb9
feat: moved get threshold to the new repository class, implement dele…
renatav Oct 29, 2024
c7be3cb
refact: move get expiration date, get all targets and check expiratio…
renatav Oct 30, 2024
7601b9b
refact: move get role paths and all target files to the new repositor…
renatav Oct 30, 2024
b8d247e
refact: work on making the create repository method more flexible
renatav Oct 31, 2024
ade9af1
refact: extend creation of repositories using the new TUF, added supp…
renatav Oct 31, 2024
f1c1b6c
test: add create repository with delegations test
renatav Nov 1, 2024
ea05769
test: use repository created using create in tests
renatav Nov 1, 2024
c9857d0
test, refact: reorganize tests, implement keyid-roles mapping using t…
renatav Nov 1, 2024
b7eb34a
test: re-enabled add target test
renatav Nov 2, 2024
ccb94b8
refact: moved modify targets to the new repository class
renatav Nov 4, 2024
43af588
test: create test repos with target files and custom data
renatav Nov 5, 2024
feb7b53
refac, test: move get_all_target_files_state to the new repository cl…
renatav Nov 5, 2024
3202900
refact: move generate_roles_description to the new repository class
renatav Nov 5, 2024
af59138
test, refact: minor tests refactoring, reimplement is_valid_metadata_key
renatav Nov 6, 2024
51d63c9
refact: test: reimplement add metadata keys, enable keys tests
renatav Nov 6, 2024
e57b8e3
feat, test: implement revoke key
renatav Nov 7, 2024
abeaa2c
test, fix: minor add and revoke key improvements
renatav Nov 7, 2024
f3bc1f7
refact, test: initial work on reworking signing, add set expiration d…
renatav Nov 8, 2024
2681d4a
refact: remeve outdated imports
renatav Nov 8, 2024
0d67dd5
refact: check and set expiration date reimplemented
renatav Nov 8, 2024
ba7d3eb
refact: update key generation
renatav Nov 8, 2024
ea93127
refact: reimplement repository_at_revision
renatav Nov 9, 2024
4122100
refact: update updater and the creation of a new repository
renatav Nov 9, 2024
e21edfd
fix: bare repositories fix
renatav Nov 9, 2024
d9e5cc0
refact: reimplement addition of verification keys when creating a new…
renatav Nov 11, 2024
67fbc2b
fix: minor create repo fix
renatav Nov 11, 2024
c1cd853
refact: remove do_snapshot and timestamp from add/revoke keys
renatav Nov 11, 2024
7fe4d2f
refact: work on initializing repository and signers in api
renatav Nov 14, 2024
e77210b
fix, feat: fix add keys, add revoke key command
renatav Nov 14, 2024
947f1e4
test: add add delegated paths test
renatav Nov 14, 2024
5c56ede
refact: rework create new role
renatav Nov 15, 2024
c92c39b
refact: support adding multiple new roles
renatav Nov 15, 2024
f63a058
refact, fix: fix create delagations when no previouis delgations, rew…
renatav Nov 15, 2024
66a2742
refact: refactored remove paths
renatav Nov 16, 2024
2fe6d72
fix: minor fixes, update update-expiration-dates
renatav Nov 16, 2024
5ddb0c3
refact, test: remove unused code, remove paths test added
renatav Nov 16, 2024
dfbcde6
refact: rework targets update
renatav Nov 16, 2024
c541df8
test: add update targets roles test
renatav Nov 18, 2024
45c8c02
refact: reimplement add target repo
renatav Nov 19, 2024
4917b71
refact: rework remove target repo
renatav Nov 19, 2024
2f817d3
refact: update add/remove dependencies
renatav Nov 19, 2024
de7f5f8
test, refact: refact repository init tests, remove outdated tests
renatav Nov 19, 2024
96c7df1
test: work on refactoring test_create_repository tests
renatav Nov 20, 2024
7a787f4
test, refact: reorganize conftest
renatav Nov 20, 2024
1b7146b
test: rework test dependencies, metadata and roles api tests
renatav Nov 21, 2024
c5de6e1
chore: remove ramaining test repos
renatav Nov 21, 2024
739c014
test: refact test targets
renatav Nov 21, 2024
4fd29db
test, refact: update repositoriesdb tests, some refactoring, remove u…
renatav Nov 21, 2024
fbc0a8a
fix: fix snapshot info length/hash issue
renatav Nov 22, 2024
68fb96e
feat: initial implementation of git storage backed, which can load me…
renatav Nov 22, 2024
84a52f2
fix: bypass storage singleton
renatav Nov 22, 2024
8224179
fix: update root version number when updating snaphost. Work on updat…
renatav Nov 25, 2024
5afeaf5
test: update test_update_invalid
renatav Nov 26, 2024
6573a6a
fix: fix update expiration dates. Snapshot info was not getting updated
renatav Nov 27, 2024
959ab49
test: update remaining updater tests, minor cleanup
renatav Nov 27, 2024
16abe0c
chore: cleanup, formatting, remove unused code and imports
renatav Nov 27, 2024
4f02ce1
chore: bump yubikey-manager version
renatav Nov 27, 2024
62a8cac
chore: fixing mypy issues
renatav Nov 27, 2024
cb5b45a
chore: import and mypy issues
renatav Nov 27, 2024
2070426
chore: mypy issues
renatav Nov 27, 2024
106836c
chore: remove unused import
renatav Nov 27, 2024
896eb73
chore: comment out yubukey tests
renatav Nov 27, 2024
f262d81
test: remove generation of tests
renatav Nov 27, 2024
324e642
chore: formatting
renatav Nov 27, 2024
ea6dbf2
test: fix a number of failing tests
renatav Nov 28, 2024
32b2930
chore: remove unused import
renatav Nov 28, 2024
b37a631
test: rework keys tests
renatav Nov 28, 2024
d3d7263
test: update number of root keys in updater keys description
renatav Nov 29, 2024
3000095
fix: fix failing tests
renatav Nov 29, 2024
65c128b
refact: update yubikey cli functions
renatav Nov 29, 2024
4805128
test: refact yubikey tests
renatav Nov 30, 2024
789b591
refact: refactor creation of repositories using yubikeys
renatav Nov 30, 2024
ff146df
chore: formatting and mypy fixes
renatav Nov 30, 2024
e402196
test: add pytest-mock to test requirements
renatav Nov 30, 2024
de1bd82
Merge branch 'master' into feature/tuf-repositoty
renatav Nov 30, 2024
da5344d
chore: update changelog
renatav Nov 30, 2024
2f16959
chore: merge master
renatav Nov 30, 2024
f22da80
test: tun tests without having ykman installed
renatav Dec 4, 2024
7db3588
chore: ignore unused import
renatav Dec 4, 2024
6410f52
docs: GitStorageBackend docstring
renatav Dec 4, 2024
4ab8c48
feat: add a command for rotating keys
renatav Dec 9, 2024
dc431c5
test: add test revoke signing key test
renatav Dec 9, 2024
5ea21e7
docs: update docs related to repository classes
renatav Dec 10, 2024
bef7f8e
chore: merge master and resolve conflicts
renatav Dec 13, 2024
4490e9b
docs: add/update a number of metadata repository docstrings
renatav Dec 19, 2024
81e0799
docs: add docstrings to metadata repository
renatav Dec 19, 2024
78a91b5
docs: update docstrings, comments and docs
renatav Dec 20, 2024
cc45f38
chore: mypy and formatting fixes
renatav Dec 21, 2024
22dd4b8
refact: update validation.py imports
renatav Dec 21, 2024
8b20832
feat: introduce cli tests with click `CliRunner`
n-dusan Dec 21, 2024
005b293
test: fix failing cli tests
renatav Dec 24, 2024
6f79d2b
chore: merge master and resolve conflicts
renatav Dec 24, 2024
f496fe9
chore: formatting
renatav Dec 24, 2024
019d963
fix, tests: minor update metadata fix and additional tests
renatav Dec 24, 2024
503653b
fix: do not update snapshot and timestamp twice
renatav Dec 25, 2024
8ce54e0
fix: fix update timestamp without snapshot
renatav Dec 25, 2024
95263f0
feat, fix: add update timestamp and snapshot api function, minor fixes
renatav Dec 25, 2024
408f9bd
feat: introduce get_role_paths back to auth_repo.py
n-dusan Dec 26, 2024
061a860
feat: add create/remove target files as a separate repository function
renatav Dec 26, 2024
8f1b2e9
refact: update fake yubikey
renatav Dec 26, 2024
af0af2e
chore: mypy return type fix
renatav Dec 26, 2024
19ebb23
fix: set storage commit to None in repository at revision context man…
renatav Dec 26, 2024
61e6788
feat: add a function for syncing snapshot with the provided roles
renatav Dec 29, 2024
60e2c70
fix: set update_expiration_date in update snapshot and timestamp
n-dusan Dec 30, 2024
87394b1
chore: fix typo
n-dusan Jan 3, 2025
1a38bb3
fix: convert CRLF to LF before creating target object
n-dusan Jan 3, 2025
42771cc
feat: add an option to update certain metadata files when signing tar…
renatav Jan 4, 2025
5520204
Merge branch 'master' into feature/tuf-repositoty
renatav Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions taf/api/api_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from taf.api.utils._conf import find_keystore
from taf.auth_repo import AuthenticationRepository
from taf.constants import DEFAULT_RSA_SIGNATURE_SCHEME
from taf.exceptions import CommandValidationError, InvalidRepositoryError, TAFError
from taf.exceptions import CommandValidationError, InvalidRepositoryError, SigningError, TAFError
from taf.git import GitRepository
from taf.keys import load_signers
from taf.log import taf_logger
Expand All @@ -26,6 +26,7 @@ def manage_repo_and_signers(
push=True,
commit_key=None,
commit_msg=None,
no_commit_warning=True,
):
try:
if roles:
Expand All @@ -43,28 +44,29 @@ def manage_repo_and_signers(
roles_to_load.add("snapshot")
roles_to_load.add("timestamp")

for role in roles_to_load:
keystore_signers, yubikeys = load_signers(
auth_repo,
role,
loaded_yubikeys=loaded_yubikeys,
keystore=keystore_path,
scheme=scheme,
prompt_for_keys=prompt_for_keys,
)
auth_repo.load_signers({role: keystore_signers})
for role in roles_to_load:
keystore_signers, yubikeys = load_signers(
auth_repo,
role,
loaded_yubikeys=loaded_yubikeys,
keystore=keystore_path,
scheme=scheme,
prompt_for_keys=prompt_for_keys,
)
auth_repo.load_signers({role: keystore_signers})
yield
if auth_repo.something_to_commit() and commit:
if not commit_msg and commit_key:
commit_msg = git_commit_message(commit_key)
auth_repo.commit_and_push(commit_msg=commit_msg, push=push)
else:
elif not no_commit_warning:
taf_logger.log("NOTICE", "\nPlease commit manually\n")

except CommandValidationError:
pass
except Exception as e:
taf_logger.error(f"An error occurred: {e}")
if auth_repo.is_git_repository:
auth_repo.clean_and_reset()
# restore metadata, leave targets as they might have been modified by the user
# TODO flag for also resetting targets?
# also update the CLI error handling
auth_repo.restore(["metadata"])
raise TAFError from e
66 changes: 40 additions & 26 deletions taf/api/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from collections import defaultdict
from pathlib import Path
from logdecorator import log_on_end, log_on_error, log_on_start
from taf.api.api_workflow import manage_repo_and_signers
from taf.api.utils._metadata import (
update_snapshot_and_timestamp,
update_target_metadata,
Expand Down Expand Up @@ -326,8 +327,8 @@ def register_target_files(
roles_key_infos: Optional[str] = None,
commit: Optional[bool] = True,
scheme: Optional[str] = DEFAULT_RSA_SIGNATURE_SCHEME,
taf_repo: Optional[TUFRepository] = None,
write: Optional[bool] = False,
auth_repo: Optional[AuthenticationRepository] = None,
update_snapshot_and_timestamp: Optional[bool] = True,
prompt_for_keys: Optional[bool] = False,
push: Optional[bool] = True,
no_commit_warning: Optional[bool] = True,
Expand All @@ -340,7 +341,7 @@ def register_target_files(
path: Authentication repository's path.
keystore: Location of the keystore files.
roles_key_infos: A dictionary whose keys are role names, while values contain information about the keys.
scheme (optional): Signing scheme. Set to rsa-pkcs1v15-sha256 by default.
scheme (optional): Signing scheme. Set to rsa-pkcs1v15-sha256 by default.
taf_repo (optional): If taf repository is already initialized, it can be passed and used.
write (optional): Write metadata updates to disk if set to True
commit (optional): Indicates if the changes should be committed and pushed automatically.
Expand All @@ -352,34 +353,47 @@ def register_target_files(
Returns:
True if there were targets that were updated, False otherwise
"""

# find files that should be added/modified/removed

if auth_repo is None:
auth_repo = AuthenticationRepository(path=path)

added_targets_data, removed_targets_data = auth_repo.get_all_target_files_state()
if not added_targets_data and not removed_targets_data:
taf_logger.log("NOTICE", "No updated targets")
return False

all_updated_targets = list(added_targets_data.keys()) if added_targets_data else []
if removed_targets_data:
all_updated_targets.extend(list(removed_targets_data.keys()))

roles_and_targets = defaultdict(list)
for path in all_updated_targets:
roles_and_targets[auth_repo.get_role_from_target_paths([path])].append(path)

_, keystore, _ = _initialize_roles_and_keystore(
roles_key_infos, keystore, enter_info=False
)
if taf_repo is None:
path = Path(path).resolve()
taf_repo = TUFRepository(str(path))

# find files that should be added/modified/removed
added_targets_data, removed_targets_data = taf_repo.get_all_target_files_state()
updated = update_target_metadata(
taf_repo,
added_targets_data,
removed_targets_data,
with manage_repo_and_signers(
auth_repo,
set(roles_and_targets.keys()),
keystore,
scheme=scheme,
write=write,
prompt_for_keys=prompt_for_keys,
)

if updated and write:
if commit:
auth_repo = AuthenticationRepository(path=taf_repo.path)
commit_msg = git_commit_message("update-targets")
auth_repo.commit_and_push(commit_msg=commit_msg, push=push)
elif not no_commit_warning:
taf_logger.log("NOTICE", "\nPlease commit manually\n")

return updated
scheme,
prompt_for_keys,
load_snapshot_and_timestamp=update_snapshot_and_timestamp,
load_parents=False,
load_roles=True,
commit=commit,
push=push,
commit_key="update-targets",
no_commit_warning=no_commit_warning,
):
for role, targets in roles_and_targets.items():
auth_repo.update_target_role(role, targets)
if update_snapshot_and_timestamp:
auth_repo.update_snapshot_and_timestamp()


@log_on_start(DEBUG, "Removing target repository {target_name:s}", logger=taf_logger)
Expand Down
5 changes: 5 additions & 0 deletions taf/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,11 @@ def reset_to_commit(
if hard:
self._git(f"reset {flag} HEAD")

def restore(
self, subdirectories: str
) -> None:
self._git(f"restore {' '.join(subdirectories)}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I believe this assumes that user has a more recent version of git installed. git restore was introduced since git v2.23, IIUC. We currently don't seem to have a way to let the user know that the installed git is incompatible with currently installed taf.

E.g. user has outdated git installed (v2.21) and tries to run a taf API command. This seems like it should probably be a separate issue? What we could possibly do is something along the lines of have taf cli emit warning if outdated git is installed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind implementing this using something different or asking the user's to update their version of git (v2.23 was released over 5 years ago, so I'd assume that everyone on our team at least has a newer version). Would it be okay to update this in a separate PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes


def update_branch_refs(self, branch: str, commit: str) -> None:
# Update the local branch reference to the specific commit
self._git(f"update-ref refs/heads/{branch} {commit}")
Expand Down
3 changes: 2 additions & 1 deletion taf/tools/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def wrapper(*args, **kwargs):
path = kwargs["path"]
repo = GitRepository(path=path)
if repo.is_git_repository:
repo.clean_and_reset()
repo.restore(["metadata"])
# repo.clean_and_reset()
if remove_dir_on_error:
shutil.rmtree(path, onerror=on_rm_error)

Expand Down
2 changes: 1 addition & 1 deletion taf/tools/targets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def sign(path, keystore, keys_description, scheme, prompt_for_keys, no_commit):
keystore=keystore,
roles_key_infos=keys_description,
scheme=scheme,
write=True,
update_snapshot_and_timestamp=True,
prompt_for_keys=prompt_for_keys,
commit=not no_commit
)
Expand Down
47 changes: 42 additions & 5 deletions taf/tuf/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ def add_target_files_to_role(self, added_data: Dict[str, Dict]) -> None:
"""
self.modify_targets(added_data=added_data)

self.do_snapshot()
self.do_timestamp()


def add_path_to_delegated_role(self, role: str, paths: List[str]) -> bool:
"""
Expand Down Expand Up @@ -530,6 +529,18 @@ def create_delegated_role(self, roles_data: List[TargetsRole], signers: Dict[str
added_roles.append(role_data.name)
return added_roles, existing_roles

def _create_target_object(self, filesystem_path: str, target_path: str, custom: Optional[Dict]):
target_file = TargetFile.from_file(
target_file_path=target_path,
local_path=filesystem_path,
hash_algorithms=["sha256", "sha512"],
)
if custom:
unrecognized_fields = {
"custom": custom
}
target_file.unrecognized_fields=unrecognized_fields
return target_file

def delete_unregistered_target_files(self, targets_role="targets"):
"""
Expand Down Expand Up @@ -680,10 +691,12 @@ def get_all_target_files_state(self):
_, hashes = get_file_details(str(target_file))
# register only new or changed files
if hashes.get(HASH_FUNCTION) != self.get_target_file_hashes(file_name):
custom = self.get_target_file_custom_data(file_name)
added_target_files[file_name] = {
"target": target_file.read_text(),
"custom": self.get_target_file_custom_data(file_name),
}
if custom:
added_target_files[file_name]["custom"] = custom

# removed files
for file_name in signed_target_files - fs_target_files:
Expand Down Expand Up @@ -841,7 +854,10 @@ def get_target_file_custom_data(self, target_path: str) -> Optional[Dict]:
"""
try:
role = self.get_role_from_target_paths([target_path])
return self.get_targets_of_role(role)[target_path].custom
target_obj = self.get_targets_of_role(role).get(target_path)
if target_obj:
return target_obj.custom
return None
except KeyError:
raise TAFError(f"Target {target_path} does not exist")

Expand All @@ -851,7 +867,10 @@ def get_target_file_hashes(self, target_path, hash_func=HASH_FUNCTION):
"""
try:
role = self.get_role_from_target_paths([target_path])
hashes = self.get_targets_of_role(role)[target_path].hashes
targets_of_role = self.get_targets_of_role(role)
if target_path not in targets_of_role:
return None, None
hashes = targets_of_role[target_path].hashes
if hash_func not in hashes:
raise TAFError(f"Invalid hashing algorithm {hash_func}")
return hashes[hash_func]
Expand Down Expand Up @@ -1330,6 +1349,24 @@ def sort_roles_targets_for_filenames(self):
roles_targets.setdefault(role, []).append(target_file)
return roles_targets

def update_target_role(self, role: str, target_paths: Dict):
n-dusan marked this conversation as resolved.
Show resolved Hide resolved
if not self.check_if_role_exists(role):
raise TAFError(f"Role {role} does not exist")
self.verify_signers_loaded([role])
removed_paths = []
target_files = []
for target_path in target_paths:
full_path = self.path / TARGETS_DIRECTORY_NAME / target_path
# file removed, removed from te role
if not full_path.is_file():
removed_paths.append(target_path)
else:
custom_data = self.get_target_file_custom_data(target_path)
target_file = self._create_target_object(full_path, target_path, custom_data)
target_files.append(target_file)

self._modify_tarets_role(target_files, removed_paths, role)

def update_role(self, role_name: str, signers: List[CryptoSigner]):
self._load_signers(role_name, signers)
with self.eidt(role_name) as role:
Expand Down
Loading