-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfingerprint.go
More file actions
211 lines (172 loc) · 5.05 KB
/
fingerprint.go
File metadata and controls
211 lines (172 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"crypto/sha1"
"fmt"
"hash"
"io"
"os"
"path/filepath"
)
// fingerprintMode represents the fingerprinting mode for Dockerfile sources.
type fingerprintMode string
const (
// modeCommit uses git commit hashes for fingerprinting
modeCommit fingerprintMode = "commit"
// modeSHA1 uses file content hashing for fingerprinting
modeSHA1 fingerprintMode = "sha1"
// modeAuto tries git commit hash first, falls back to content hashing
modeAuto fingerprintMode = "auto"
)
// fingerprintModeOptions returns the string representation of the fingerprint
// mode options.
func fingerprintModeOptions() string {
return fmt.Sprintf("\"%s\", \"%s\", or \"%s\"",
modeCommit, modeSHA1, modeAuto)
}
// fingerprint represents a fingerprint of a Dockerfile source.
type fingerprint struct {
mode fingerprintMode
hash string
}
// String returns the string representation of the fingerprint.
func (fp fingerprint) String() string {
return fmt.Sprintf("%s:%s", fp.mode, fp.hash)
}
// fingerprintFromSHA1 builds a fingerprint from the hexadecimal
// representation of the hashsum.
func fingerprintFromSHA1(h hash.Hash) fingerprint {
return fingerprint{modeSHA1, fmt.Sprintf("%x", h.Sum(nil))}
}
// hashFiles hashes the files in the given pathname using SHA1 and returns
// the hashsum as a hexadecimal string. Files are processed in lexical order
// as guaranteed by filepath.Walk.
func hashFiles(pathname string) (fingerprint, error) {
h := sha1.New()
err := filepath.Walk(pathname, func(p string,
info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
// Ignore hidden directories
if p != "." && filepath.Base(p)[0] == '.' {
return filepath.SkipDir
}
return nil
}
// Skip all symlinks to avoid following them
if info.Mode()&os.ModeSymlink != 0 {
return nil
}
f, err := os.Open(p)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(h, f); err != nil {
return err
}
return nil
})
if err != nil {
return fingerprint{}, err
}
return fingerprintFromSHA1(h), nil
}
// parseAndHashDockerfile parses the Dockerfile, extracts the sources from it,
// and returns the the sources and the hashsum of the Dockerfile using SHA1.
func parseAndHashDockerfile(dockerfile string) ([]string, fingerprint, error) {
f, err := os.Open(dockerfile)
if err != nil {
return nil, fingerprint{}, err
}
defer f.Close()
sources, err := collectSourcesFromDockerfile(f)
if err != nil {
return nil, fingerprint{}, err
}
if _, err = f.Seek(0, io.SeekStart); err != nil {
return nil, fingerprint{}, err
}
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
return nil, fingerprint{}, err
}
return sources, fingerprintFromSHA1(h), nil
}
// fingerprintFunc defines the type of functions that compute Dockerfile source
// fingerprints.
type fingerprintFunc func(pathname string) (fingerprint, error)
// computeImageFingerprint computes the fingerprint of the Dockerfile, all
// sources from it, the build arguments, and the platform (if specified) using
// SHA1 and returns the fingerprint as a hexadecimal string.
func computeImageFingerprint(workingDir, dockerfile string, buildArgs []string,
computeFingerprint fingerprintFunc, platform string,
quiet bool) (fingerprint, error) {
workingDir = filepath.Clean(workingDir)
if dockerfile == "" {
dockerfile = filepath.Join(workingDir, "Dockerfile")
}
sources, dockerfileFingerprint, err := parseAndHashDockerfile(
dockerfile)
if err != nil {
return fingerprint{}, err
}
h := sha1.New()
addSourceFingerprint := func(source string, fp fingerprint) {
if !quiet {
fmt.Println(source, "fingerprint", fp)
}
h.Write([]byte(source + "@" + fp.String() + "\n"))
}
addSourceFingerprint("Dockerfile", dockerfileFingerprint)
computeAndAddSourceFingerprint := func(source, pathname string) error {
fp, err := computeFingerprint(pathname)
if err != nil {
return err
}
addSourceFingerprint(source, fp)
return nil
}
for _, source := range sources {
source = filepath.Clean(source)
pathname := filepath.Join(workingDir, source)
if _, err := os.Stat(pathname); err != nil {
if !os.IsNotExist(err) {
return fingerprint{}, err
}
// Try interpreting the path as a glob pattern.
matches, _ := filepath.Glob(pathname)
// If nothing matched, return the original Stat() error.
if len(matches) == 0 {
return fingerprint{}, err
}
for _, pathname = range matches {
// Ignore the impossible Rel() error.
source, _ = filepath.Rel(workingDir, pathname)
if err = computeAndAddSourceFingerprint(
source, pathname); err != nil {
return fingerprint{}, err
}
}
} else if err = computeAndAddSourceFingerprint(
source, pathname); err != nil {
return fingerprint{}, err
}
}
for _, buildArg := range buildArgs {
if !quiet {
fmt.Println("Arg:", buildArg)
}
h.Write([]byte(buildArg))
h.Write([]byte("\n"))
}
if platform != "" {
if !quiet {
fmt.Println("Platform:", platform)
}
h.Write([]byte(platform))
h.Write([]byte("\n"))
}
return fingerprintFromSHA1(h), nil
}