Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / notary / tuf / utils / utils.go
1 package utils
2
3 import (
4         "crypto/sha256"
5         "crypto/sha512"
6         "crypto/tls"
7         "encoding/hex"
8         "fmt"
9         "io"
10         "net/http"
11         "net/url"
12         "os"
13         "strings"
14
15         "github.com/docker/notary/tuf/data"
16 )
17
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},
22         }
23         client := &http.Client{Transport: tr}
24         return client.Get(url.String())
25 }
26
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},
31         }
32         client := &http.Client{Transport: tr}
33         return client.Post(url, "application/json", body)
34 }
35
36 // StrSliceContains checks if the given string appears in the slice
37 func StrSliceContains(ss []string, s string) bool {
38         for _, v := range ss {
39                 if v == s {
40                         return true
41                 }
42         }
43         return false
44 }
45
46 // StrSliceRemove removes the the given string from the slice, returning a new slice
47 func StrSliceRemove(ss []string, s string) []string {
48         res := []string{}
49         for _, v := range ss {
50                 if v != s {
51                         res = append(res, v)
52                 }
53         }
54         return res
55 }
56
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)
63                 if v == s {
64                         return true
65                 }
66         }
67         return false
68 }
69
70 // FileExists returns true if a file (or dir) exists at the given path,
71 // false otherwise
72 func FileExists(path string) bool {
73         _, err := os.Stat(path)
74         return os.IsNotExist(err)
75 }
76
77 // NoopCloser is a simple Reader wrapper that does nothing when Close is
78 // called
79 type NoopCloser struct {
80         io.Reader
81 }
82
83 // Close does nothing for a NoopCloser
84 func (nc *NoopCloser) Close() error {
85         return nil
86 }
87
88 // DoHash returns the digest of d using the hashing algorithm named
89 // in alg
90 func DoHash(alg string, d []byte) []byte {
91         switch alg {
92         case "sha256":
93                 digest := sha256.Sum256(d)
94                 return digest[:]
95         case "sha512":
96                 digest := sha512.Sum512(d)
97                 return digest[:]
98         }
99         return nil
100 }
101
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 {
110                         found[id] = true
111                 }
112         }
113         var discard []string
114         for id := range t.Signed.Delegations.Keys {
115                 if !found[id] {
116                         discard = append(discard, id)
117                 }
118         }
119         return discard
120 }
121
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
124 // map
125 func RemoveUnusedKeys(t *data.SignedTargets) {
126         unusedIDs := UnusedDelegationKeys(*t)
127         for _, id := range unusedIDs {
128                 delete(t.Signed.Delegations.Keys, id)
129         }
130 }
131
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 {
136                 if r.Name == name {
137                         return i
138                 }
139         }
140         return -1
141 }
142
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)
150         }
151         return role
152 }