// downloadTargets downloads all targets and delegated targets for the repository. // It uses a pre-order tree traversal as it's necessary to download parents first // to obtain the keys to validate children. func (c *Client) downloadTargets(role string) error { logrus.Debug("Downloading Targets...") stack := utils.NewStack() stack.Push(role) for !stack.Empty() { role, err := stack.PopString() if err != nil { return err } if c.local.Snapshot == nil { return ErrMissingMeta{role: role} } snap := c.local.Snapshot.Signed root := c.local.Root.Signed r := c.keysDB.GetRole(role) if r == nil { return fmt.Errorf("Invalid role: %s", role) } keyIDs := r.KeyIDs s, err := c.getTargetsFile(role, keyIDs, snap.Meta, root.ConsistentSnapshot, r.Threshold) if err != nil { if _, ok := err.(ErrMissingMeta); ok && role != data.CanonicalTargetsRole { // if the role meta hasn't been published, // that's ok, continue continue } logrus.Error("Error getting targets file:", err) return err } t, err := data.TargetsFromSigned(s) if err != nil { return err } err = c.local.SetTargets(role, t) if err != nil { return err } // push delegated roles contained in the targets file onto the stack for _, r := range t.Signed.Delegations.Roles { stack.Push(r.Name) } } return nil }
// downloadTargets downloads all targets and delegated targets for the repository. // It uses a pre-order tree traversal as it's necessary to download parents first // to obtain the keys to validate children. func (c *Client) downloadTargets(role string) error { logrus.Debug("Downloading Targets...") stack := utils.NewStack() stack.Push(role) for !stack.Empty() { role, err := stack.PopString() if err != nil { return err } if c.local.Snapshot == nil { return tuf.ErrNotLoaded{Role: data.CanonicalSnapshotRole} } snap := c.local.Snapshot.Signed root := c.local.Root.Signed s, err := c.getTargetsFile(role, snap.Meta, root.ConsistentSnapshot) if err != nil { if _, ok := err.(data.ErrMissingMeta); ok && role != data.CanonicalTargetsRole { // if the role meta hasn't been published, // that's ok, continue continue } logrus.Error("Error getting targets file:", err) return err } t, err := data.TargetsFromSigned(s, role) if err != nil { return err } err = c.local.SetTargets(role, t) if err != nil { return err } // push delegated roles contained in the targets file onto the stack for _, r := range t.Signed.Delegations.Roles { if path.Dir(r.Name) == role { // only load children that are direct 1st generation descendants // of the role we've just downloaded stack.Push(r.Name) } } } return nil }