Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / notary / client / witness.go
1 package client
2
3 import (
4         "path/filepath"
5
6         "github.com/docker/notary/client/changelist"
7         "github.com/docker/notary/tuf"
8         "github.com/docker/notary/tuf/data"
9 )
10
11 // Witness creates change objects to witness (i.e. re-sign) the given
12 // roles on the next publish. One change is created per role
13 func (r *NotaryRepository) Witness(roles ...string) ([]string, error) {
14         cl, err := changelist.NewFileChangelist(filepath.Join(r.tufRepoPath, "changelist"))
15         if err != nil {
16                 return nil, err
17         }
18         defer cl.Close()
19
20         successful := make([]string, 0, len(roles))
21         for _, role := range roles {
22                 // scope is role
23                 c := changelist.NewTUFChange(
24                         changelist.ActionUpdate,
25                         role,
26                         changelist.TypeWitness,
27                         "",
28                         nil,
29                 )
30                 err = cl.Add(c)
31                 if err != nil {
32                         break
33                 }
34                 successful = append(successful, role)
35         }
36         return successful, err
37 }
38
39 func witnessTargets(repo *tuf.Repo, invalid *tuf.Repo, role string) error {
40         if r, ok := repo.Targets[role]; ok {
41                 // role is already valid, mark for re-signing/updating
42                 r.Dirty = true
43                 return nil
44         }
45
46         if roleObj, err := repo.GetDelegationRole(role); err == nil && invalid != nil {
47                 // A role with a threshold > len(keys) is technically invalid, but we let it build in the builder because
48                 // we want to be able to download the role (which may still have targets on it), add more keys, and then
49                 // witness the role, thus bringing it back to valid.  However, if no keys have been added before witnessing,
50                 // then it is still an invalid role, and can't be witnessed because nothing can bring it back to valid.
51                 if roleObj.Threshold > len(roleObj.Keys) {
52                         return data.ErrInvalidRole{
53                                 Role:   role,
54                                 Reason: "role does not specify enough valid signing keys to meet its required threshold",
55                         }
56                 }
57                 if r, ok := invalid.Targets[role]; ok {
58                         // role is recognized but invalid, move to valid data and mark for re-signing
59                         repo.Targets[role] = r
60                         r.Dirty = true
61                         return nil
62                 }
63         }
64         // role isn't recognized, even as invalid
65         return data.ErrInvalidRole{
66                 Role:   role,
67                 Reason: "this role is not known",
68         }
69 }