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

fix: fix targets removal so it doesn't interfere with delegations #414

Merged
merged 2 commits into from
Sep 28, 2022
Merged
Changes from 1 commit
Commits
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
16 changes: 7 additions & 9 deletions cmd/tuf/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func InitCmd(ctx context.Context, directory, previous string,
}

// Add targets (copy them into the repository and add them to the targets.json)
// Add the new targets in the config.
expectedTargets := make(map[string]bool)
for tt, custom := range targetsConfig {
from, err := os.Open(filepath.Join(targetsDir, tt))
Expand All @@ -228,8 +229,6 @@ func InitCmd(ctx context.Context, directory, previous string,
}
expectedTargets[tt] = true
}

// Remove old targets that were not included in config.
targetsToRemove := []string{}
allTargets, err := repo.Targets()
if err != nil {
Expand All @@ -240,13 +239,6 @@ func InitCmd(ctx context.Context, directory, previous string,
targetsToRemove = append(targetsToRemove, path)
}
}
// Only call RemoveTargetsWithExpires if there are targets to remove.
// Calling the function with an empty slice will remove all targets.
if len(targetsToRemove) > 0 {
if err := repo.RemoveTargetsWithExpires(targetsToRemove, GetExpiration("targets")); err != nil {
return fmt.Errorf("error removing old targets: %w", err)
}
}

// Reset and delegations: they will be updated in DelegationCmd.
if err := repo.ResetTargetsDelegationsWithExpires("targets", GetExpiration("targets")); err != nil {
Expand All @@ -266,6 +258,12 @@ func InitCmd(ctx context.Context, directory, previous string,
if err != nil {
return err
}
// Remove any targets not present: only removes from targets to avoid
// removing from delegations.
asraa marked this conversation as resolved.
Show resolved Hide resolved
for _, tt := range targetsToRemove {
delete(t.Targets, tt)
}

if err := setMetaWithSigKeyIDs(store, "targets.json", t, maps.Keys(targetKeys)); err != nil {
return err
}
Expand Down