15 "github.com/docker/notary/tuf/data"
18 // Download does a simple download from a URL
19 func Download(url url.URL) (*http.Response, error) {
20 tr := &http.Transport{
21 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
23 client := &http.Client{Transport: tr}
24 return client.Get(url.String())
27 // Upload does a simple JSON upload to a URL
28 func Upload(url string, body io.Reader) (*http.Response, error) {
29 tr := &http.Transport{
30 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
32 client := &http.Client{Transport: tr}
33 return client.Post(url, "application/json", body)
36 // StrSliceContains checks if the given string appears in the slice
37 func StrSliceContains(ss []string, s string) bool {
38 for _, v := range ss {
46 // StrSliceRemove removes the the given string from the slice, returning a new slice
47 func StrSliceRemove(ss []string, s string) []string {
49 for _, v := range ss {
57 // StrSliceContainsI checks if the given string appears in the slice
58 // in a case insensitive manner
59 func StrSliceContainsI(ss []string, s string) bool {
60 s = strings.ToLower(s)
61 for _, v := range ss {
62 v = strings.ToLower(v)
70 // FileExists returns true if a file (or dir) exists at the given path,
72 func FileExists(path string) bool {
73 _, err := os.Stat(path)
74 return os.IsNotExist(err)
77 // NoopCloser is a simple Reader wrapper that does nothing when Close is
79 type NoopCloser struct {
83 // Close does nothing for a NoopCloser
84 func (nc *NoopCloser) Close() error {
88 // DoHash returns the digest of d using the hashing algorithm named
90 func DoHash(alg string, d []byte) []byte {
93 digest := sha256.Sum256(d)
96 digest := sha512.Sum512(d)
102 // UnusedDelegationKeys prunes a list of keys, returning those that are no
103 // longer in use for a given targets file
104 func UnusedDelegationKeys(t data.SignedTargets) []string {
105 // compare ids to all still active key ids in all active roles
106 // with the targets file
107 found := make(map[string]bool)
108 for _, r := range t.Signed.Delegations.Roles {
109 for _, id := range r.KeyIDs {
114 for id := range t.Signed.Delegations.Keys {
116 discard = append(discard, id)
122 // RemoveUnusedKeys determines which keys in the slice of IDs are no longer
123 // used in the given targets file and removes them from the delegated keys
125 func RemoveUnusedKeys(t *data.SignedTargets) {
126 unusedIDs := UnusedDelegationKeys(*t)
127 for _, id := range unusedIDs {
128 delete(t.Signed.Delegations.Keys, id)
132 // FindRoleIndex returns the index of the role named <name> or -1 if no
133 // matching role is found.
134 func FindRoleIndex(rs []*data.Role, name string) int {
135 for i, r := range rs {
143 // ConsistentName generates the appropriate HTTP URL path for the role,
144 // based on whether the repo is marked as consistent. The RemoteStore
145 // is responsible for adding file extensions.
146 func ConsistentName(role string, hashSha256 []byte) string {
147 if len(hashSha256) > 0 {
148 hash := hex.EncodeToString(hashSha256)
149 return fmt.Sprintf("%s.%s", role, hash)