-
Notifications
You must be signed in to change notification settings - Fork 929
/
buildpack_bits.go
308 lines (259 loc) · 7.17 KB
/
buildpack_bits.go
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package api
import (
"archive/zip"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
gonet "net"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
"code.cloudfoundry.org/cli/cf/appfiles"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
"code.cloudfoundry.org/gofileutils/fileutils"
)
//go:generate counterfeiter . BuildpackBitsRepository
type BuildpackBitsRepository interface {
UploadBuildpack(buildpack models.Buildpack, buildpackFile *os.File, zipFileName string) error
CreateBuildpackZipFile(buildpackPath string) (*os.File, string, error)
}
type CloudControllerBuildpackBitsRepository struct {
config coreconfig.Reader
gateway net.Gateway
zipper appfiles.Zipper
TrustedCerts []tls.Certificate
}
func NewCloudControllerBuildpackBitsRepository(config coreconfig.Reader, gateway net.Gateway, zipper appfiles.Zipper) (repo CloudControllerBuildpackBitsRepository) {
repo.config = config
repo.gateway = gateway
repo.zipper = zipper
return
}
func zipErrorHelper(err error) error {
return fmt.Errorf("%s: %s", T("Couldn't write zip file"), err.Error())
}
func (repo CloudControllerBuildpackBitsRepository) CreateBuildpackZipFile(buildpackPath string) (*os.File, string, error) {
zipFileToUpload, err := ioutil.TempFile("", "buildpack-upload")
if err != nil {
return nil, "", fmt.Errorf("%s: %s", T("Couldn't create temp file for upload"), err.Error())
}
var buildpackFileName string
if isWebURL(buildpackPath) {
buildpackFileName = path.Base(buildpackPath)
repo.downloadBuildpack(buildpackPath, func(downloadFile *os.File, downloadErr error) {
if downloadErr != nil {
err = downloadErr
return
}
downloadErr = normalizeBuildpackArchive(downloadFile, zipFileToUpload)
if downloadErr != nil {
err = downloadErr
return
}
})
if err != nil {
return nil, "", zipErrorHelper(err)
}
} else {
buildpackFileName = filepath.Base(buildpackPath)
dir, err := filepath.Abs(buildpackPath)
if err != nil {
return nil, "", zipErrorHelper(err)
}
buildpackFileName = filepath.Base(dir)
stats, err := os.Stat(dir)
if err != nil {
return nil, "", fmt.Errorf("%s: %s", T("Error opening buildpack file"), err.Error())
}
if stats.IsDir() {
buildpackFileName += ".zip" // FIXME: remove once #71167394 is fixed
err = repo.zipper.Zip(buildpackPath, zipFileToUpload)
if err != nil {
return nil, "", zipErrorHelper(err)
}
} else {
specifiedFile, err := os.Open(buildpackPath)
if err != nil {
return nil, "", fmt.Errorf("%s: %s", T("Couldn't open buildpack file"), err.Error())
}
err = normalizeBuildpackArchive(specifiedFile, zipFileToUpload)
if err != nil {
return nil, "", zipErrorHelper(err)
}
}
}
return zipFileToUpload, buildpackFileName, nil
}
func normalizeBuildpackArchive(inputFile *os.File, outputFile *os.File) error {
stats, toplevelErr := inputFile.Stat()
if toplevelErr != nil {
return toplevelErr
}
reader, toplevelErr := zip.NewReader(inputFile, stats.Size())
if toplevelErr != nil {
return toplevelErr
}
contents := reader.File
parentPath, hasBuildpack := findBuildpackPath(contents)
if !hasBuildpack {
return errors.New(T("Zip archive does not contain a buildpack"))
}
writer := zip.NewWriter(outputFile)
for _, file := range contents {
name := file.Name
if strings.HasPrefix(name, parentPath) {
relativeFilename := strings.TrimPrefix(name, parentPath+"/")
if relativeFilename == "" {
continue
}
fileInfo := file.FileInfo()
header, err := zip.FileInfoHeader(fileInfo)
if err != nil {
return err
}
header.Name = relativeFilename
w, err := writer.CreateHeader(header)
if err != nil {
return err
}
r, err := file.Open()
if err != nil {
return err
}
_, err = io.Copy(w, r)
if err != nil {
return err
}
err = r.Close()
if err != nil {
return err
}
}
}
toplevelErr = writer.Close()
if toplevelErr != nil {
return toplevelErr
}
_, toplevelErr = outputFile.Seek(0, 0)
if toplevelErr != nil {
return toplevelErr
}
return nil
}
func findBuildpackPath(zipFiles []*zip.File) (parentPath string, foundBuildpack bool) {
needle := "bin/compile"
for _, file := range zipFiles {
if strings.HasSuffix(file.Name, needle) {
foundBuildpack = true
parentPath = path.Join(file.Name, "..", "..")
if parentPath == "." {
parentPath = ""
}
return
}
}
return
}
func isWebURL(path string) bool {
return strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://")
}
func (repo CloudControllerBuildpackBitsRepository) downloadBuildpack(url string, cb func(*os.File, error)) {
fileutils.TempFile("buildpack-download", func(tempfile *os.File, err error) {
if err != nil {
cb(nil, err)
return
}
var certPool *x509.CertPool
if len(repo.TrustedCerts) > 0 {
certPool = x509.NewCertPool()
for _, tlsCert := range repo.TrustedCerts {
cert, _ := x509.ParseCertificate(tlsCert.Certificate[0])
certPool.AddCert(cert)
}
}
client := &http.Client{
Transport: &http.Transport{
Dial: (&gonet.Dialer{Timeout: 5 * time.Second}).Dial,
TLSClientConfig: &tls.Config{RootCAs: certPool},
Proxy: http.ProxyFromEnvironment,
},
}
response, err := client.Get(url)
if err != nil {
cb(nil, err)
return
}
defer response.Body.Close()
_, err = io.Copy(tempfile, response.Body)
if err != nil {
cb(nil, err)
return
}
_, err = tempfile.Seek(0, 0)
if err != nil {
cb(nil, err)
return
}
cb(tempfile, nil)
})
}
func (repo CloudControllerBuildpackBitsRepository) UploadBuildpack(buildpack models.Buildpack, buildpackFile *os.File, buildpackName string) error {
defer func() {
buildpackFile.Close()
os.Remove(buildpackFile.Name())
}()
return repo.performMultiPartUpload(
fmt.Sprintf("%s/v2/buildpacks/%s/bits", repo.config.APIEndpoint(), buildpack.GUID),
"buildpack",
buildpackName,
buildpackFile)
}
func (repo CloudControllerBuildpackBitsRepository) performMultiPartUpload(url string, fieldName string, fileName string, body io.Reader) error {
var capturedErr error
fileutils.TempFile("requests", func(requestFile *os.File, err error) {
if err != nil {
capturedErr = err
return
}
writer := multipart.NewWriter(requestFile)
part, err := writer.CreateFormFile(fieldName, fileName)
if err != nil {
_ = writer.Close()
capturedErr = err
return
}
_, err = io.Copy(part, body)
if err != nil {
capturedErr = fmt.Errorf("%s: %s", T("Error creating upload"), err.Error())
return
}
err = writer.Close()
if err != nil {
capturedErr = err
return
}
var request *net.Request
request, err = repo.gateway.NewRequestForFile("PUT", url, repo.config.AccessToken(), requestFile)
if err != nil {
capturedErr = err
return
}
contentType := fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary())
request.HTTPReq.Header.Set("Content-Type", contentType)
_, err = repo.gateway.PerformRequest(request)
if err != nil {
capturedErr = err
}
})
return capturedErr
}