Skip to content

Commit

Permalink
fix: do not update snapshot and timestamp twice
Browse files Browse the repository at this point in the history
  • Loading branch information
renatav committed Dec 25, 2024
1 parent 019d963 commit f313992
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
31 changes: 16 additions & 15 deletions taf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def update_metadata_expiration_date(
scheme: Optional[str] = DEFAULT_RSA_SIGNATURE_SCHEME,
start_date: Optional[datetime] = None,
commit: Optional[bool] = True,
commit_msg: Optional[str] = None,
prompt_for_keys: Optional[bool] = False,
push: Optional[bool] = True,
update_snapshot_and_timestamp: Optional[bool] = True,
Expand All @@ -113,6 +114,7 @@ def update_metadata_expiration_date(
start_date (optional): Date to which expiration interval is added.
Set to today if not specified.
commit (optional): Indicates if the changes should be committed and pushed automatically.
commit_msg (optional): Custom commit messages.
prompt_for_keys (optional): Whether to ask the user to enter their key if it is not located inside the keystore directory.
push (optional): Flag specifying whether to push to remote
Expand All @@ -128,15 +130,14 @@ def update_metadata_expiration_date(
if start_date is None:
start_date = datetime.now()

commit_msg = git_commit_message("update-expiration-dates", roles=",".join(roles))
commit_msg = commit_msg or git_commit_message(
"update-expiration-dates", roles=",".join(roles)
)

# update the order, snapshot has to be updated before timestamp
# and all other roles have to be updated before snapshot
# all other roles can be updated in any order

if len(roles) == 1 and Timestamp.type in roles:
update_snapshot_and_timestamp = False

update_snapshot_expiration_date = Snapshot.type in roles
update_timestamp_expiration_date = Timestamp.type in roles

Expand All @@ -151,21 +152,21 @@ def update_metadata_expiration_date(
commit_msg=commit_msg,
push=push,
):
if update_snapshot_and_timestamp:
if update_snapshot_expiration_date:
auth_repo.add_to_open_metadata(Snapshot.type)
if update_timestamp_expiration_date:
auth_repo.add_to_open_metadata(Timestamp.type)
if update_snapshot_expiration_date:
auth_repo.add_to_open_metadata([Snapshot.type])
if update_timestamp_expiration_date:
auth_repo.add_to_open_metadata([Timestamp.type])

for role in roles:
auth_repo.set_metadata_expiration_date(
role, start_date=start_date, interval=interval
)

auth_repo.clear_open_metadata()
auth_repo.remove_from_open_metadata([Snapshot.type])
# it is important to update snapshot first
if update_snapshot_expiration_date or update_snapshot_and_timestamp :
auth_repo.do_snapshot(force=True)

if update_snapshot_and_timestamp:
if len(roles) == 1 and Snapshot.type in roles:
auth_repo.do_timestamp()
else:
auth_repo.update_snapshot_and_timestamp()
auth_repo.remove_from_open_metadata([Timestamp.type])
if update_timestamp_expiration_date or update_snapshot_and_timestamp:
auth_repo.do_timestamp(force=True)
11 changes: 10 additions & 1 deletion taf/tuf/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ def add_new_roles_to_snapshot(self, roles: List[str]) -> None:
def add_to_open_metadata(self, roles: List[str]) -> None:
"""
In order to execute several methods before updating the metadata on disk,
these methods need to be added to the _metadata_to_keep_open list.
some metadata might need to be kept open, which is done by adding them to
_metadata_to_keep_open list.
This method adds all roles from the provided list to _metadata_to_keep_open.
"""
self._metadata_to_keep_open.update(roles)
Expand Down Expand Up @@ -1407,6 +1408,14 @@ def remove_delegated_paths(self, roles_paths: Dict[str, List[str]]):
updated = True
return updated

def remove_from_open_metadata(self, roles: List[str]) -> None:
"""
Removes the listed roles from metadata_to_keep_open list
"""
for role in roles:
if role in self._metadata_to_keep_open:
self._metadata_to_keep_open.remove(role)

def roles_targets_for_filenames(self, target_filenames):
"""Sort target files by roles
Args:
Expand Down

0 comments on commit f313992

Please sign in to comment.