/
artifactory.go
369 lines (310 loc) · 10.2 KB
/
artifactory.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package component
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/Masterminds/semver/v3"
"github.com/pivotal-cf/kiln/pkg/cargo"
)
type ArtifactoryReleaseSource struct {
cargo.ReleaseSourceConfig
Client *http.Client
logger *log.Logger
ID string
}
type ArtifactoryFileMetadata struct {
Checksums struct {
Sha1 string `json:"sha1"`
Sha256 string `json:"sha256"`
MD5 string `json:"md5"`
} `json:"checksums"`
}
type ArtifactoryFolderInfo struct {
Children []struct {
URI string `json:"uri"`
Folder bool `json:"folder"`
} `json:"children"`
}
type ArtifactoryFileInfo struct {
Checksums struct {
SHA1 string `json:"sha1"`
} `json:"checksums"`
}
// NewArtifactoryReleaseSource will provision a new ArtifactoryReleaseSource Project
// from the Kilnfile (ReleaseSourceConfig). If type is incorrect it will PANIC
func NewArtifactoryReleaseSource(c cargo.ReleaseSourceConfig) *ArtifactoryReleaseSource {
if c.Type != "" && c.Type != ReleaseSourceTypeArtifactory {
panic(panicMessageWrongReleaseSourceType)
}
// ctx := context.TODO()
return &ArtifactoryReleaseSource{
Client: http.DefaultClient,
ReleaseSourceConfig: c,
ID: c.ID,
logger: log.New(os.Stderr, "[Artifactory release source] ", log.Default().Flags()),
}
}
func (ars *ArtifactoryReleaseSource) DownloadRelease(releaseDir string, remoteRelease cargo.BOSHReleaseTarballLock) (Local, error) {
u, err := url.Parse(ars.ArtifactoryHost)
if err != nil {
return Local{}, fmt.Errorf("error parsing artifactory host: %w", err)
}
downloadURL := ars.ArtifactoryHost
if path.Base(u.Path) != "artifactory" {
downloadURL += "/artifactory"
}
downloadURL += "/" + ars.Repo + "/" + remoteRelease.RemotePath
ars.logger.Printf(logLineDownload, remoteRelease.Name, ReleaseSourceTypeArtifactory, ars.ID)
resp, err := ars.Client.Get(downloadURL)
if err != nil {
return Local{}, wrapVPNError(err)
}
if resp.StatusCode != http.StatusOK {
return Local{}, fmt.Errorf("failed to download %s release from artifactory with error code %d", remoteRelease.Name, resp.StatusCode)
}
filePath := filepath.Join(releaseDir, filepath.Base(remoteRelease.RemotePath))
out, err := os.Create(filePath)
if err != nil {
return Local{}, err
}
defer func() { _ = out.Close() }()
_, err = io.Copy(out, resp.Body)
_ = resp.Body.Close()
if err != nil {
return Local{}, err
}
_, err = out.Seek(0, 0)
if err != nil {
return Local{}, fmt.Errorf("error reseting file cursor: %w", err) // untested
}
hash := sha1.New()
_, err = io.Copy(hash, out)
if err != nil {
return Local{}, fmt.Errorf("error hashing file contents: %w", err) // untested
}
remoteRelease.SHA1 = hex.EncodeToString(hash.Sum(nil))
return Local{Lock: remoteRelease, LocalPath: filePath}, nil
}
func (ars *ArtifactoryReleaseSource) getFileSHA1(release cargo.BOSHReleaseTarballLock) (string, error) {
fullURL := ars.ArtifactoryHost + "/api/storage/" + ars.Repo + "/" + release.RemotePath
ars.logger.Printf("Getting %s file info from artifactory", release.Name)
resp, err := ars.Client.Get(fullURL)
if err != nil {
return "", wrapVPNError(err)
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to get %s release info from artifactory with error code %d", release.Name, resp.StatusCode)
}
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var artifactoryFileInfo ArtifactoryFileInfo
if err := json.Unmarshal(responseBody, &artifactoryFileInfo); err != nil {
return "", fmt.Errorf("json is malformed: %s", err)
}
return artifactoryFileInfo.Checksums.SHA1, nil
}
func (ars *ArtifactoryReleaseSource) Configuration() cargo.ReleaseSourceConfig {
return ars.ReleaseSourceConfig
}
// GetMatchedRelease uses the Name and Version and if supported StemcellOS and StemcellVersion
// fields on Requirement to download a specific release.
func (ars *ArtifactoryReleaseSource) GetMatchedRelease(spec cargo.BOSHReleaseTarballSpecification) (cargo.BOSHReleaseTarballLock, error) {
remotePath, err := ars.RemotePath(spec)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, err
}
fullUrl := fmt.Sprintf("%s/%s/%s/%s", ars.ArtifactoryHost, "api/storage", ars.Repo, remotePath)
request, err := http.NewRequest(http.MethodGet, fullUrl, nil)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, err
}
response, err := ars.Client.Do(request)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, wrapVPNError(err)
}
defer func() {
_ = response.Body.Close()
}()
switch response.StatusCode {
case http.StatusOK:
case http.StatusNotFound:
return cargo.BOSHReleaseTarballLock{}, ErrNotFound
default:
return cargo.BOSHReleaseTarballLock{}, fmt.Errorf("unexpected http status: %s", http.StatusText(response.StatusCode))
}
return cargo.BOSHReleaseTarballLock{
Name: spec.Name,
Version: spec.Version,
RemotePath: remotePath,
RemoteSource: ars.ID,
}, nil
}
// FindReleaseVersion may use any of the fields on Requirement to return the best matching
// release.
func (ars *ArtifactoryReleaseSource) FindReleaseVersion(spec cargo.BOSHReleaseTarballSpecification, _ bool) (cargo.BOSHReleaseTarballLock, error) {
remotePath, err := ars.RemotePath(spec)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, err
}
fullUrl := fmt.Sprintf("%s/%s/%s/%s", ars.ArtifactoryHost, "api/storage", ars.Repo, path.Dir(remotePath))
request, err := http.NewRequest(http.MethodGet, fullUrl, nil)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, err
}
response, err := ars.Client.Do(request)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, wrapVPNError(err)
}
defer func() {
_ = response.Body.Close()
}()
switch response.StatusCode {
case http.StatusOK:
case http.StatusNotFound:
return cargo.BOSHReleaseTarballLock{}, ErrNotFound
default:
return cargo.BOSHReleaseTarballLock{}, fmt.Errorf("unexpected http status: %s", http.StatusText(response.StatusCode))
}
var artifactoryFolderInfo ArtifactoryFolderInfo
var _ *semver.Constraints
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, err
}
if err := json.Unmarshal(responseBody, &artifactoryFolderInfo); err != nil {
return cargo.BOSHReleaseTarballLock{}, fmt.Errorf("json from %s is malformed: %s", request.URL.Host, err)
}
semverPattern, err := regexp.Compile(`([-v])\d+(.\d+)*`)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, err
}
foundRelease := cargo.BOSHReleaseTarballLock{}
constraint, err := spec.VersionConstraints()
if err != nil {
return cargo.BOSHReleaseTarballLock{}, err
}
for _, releases := range artifactoryFolderInfo.Children {
if releases.Folder {
continue
}
versions := semverPattern.FindAllString(filepath.Base(releases.URI), -1)
version := versions[0]
stemcellVersion := versions[len(versions)-1]
version = strings.Replace(version, "-", "", -1)
version = strings.Replace(version, "v", "", -1)
stemcellVersion = strings.Replace(stemcellVersion, "-", "", -1)
if len(versions) > 1 && stemcellVersion != spec.StemcellVersion {
continue
}
if version != "" {
newVersion, _ := semver.NewVersion(version)
if !constraint.Check(newVersion) {
continue
}
remotePathToUpdate := path.Dir(remotePath) + releases.URI
if (foundRelease == cargo.BOSHReleaseTarballLock{}) {
foundRelease = cargo.BOSHReleaseTarballLock{
Name: spec.Name,
Version: version,
RemotePath: remotePathToUpdate,
RemoteSource: ars.ReleaseSourceConfig.ID,
}
} else {
foundVersion, _ := semver.NewVersion(foundRelease.Version)
if newVersion.GreaterThan(foundVersion) {
foundRelease = cargo.BOSHReleaseTarballLock{
Name: spec.Name,
Version: version,
RemotePath: remotePathToUpdate,
RemoteSource: ars.ReleaseSourceConfig.ID,
}
}
}
}
}
if (foundRelease == cargo.BOSHReleaseTarballLock{}) {
return cargo.BOSHReleaseTarballLock{}, ErrNotFound
}
foundRelease.SHA1, err = ars.getFileSHA1(foundRelease)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, err
}
return foundRelease, nil
}
func (ars *ArtifactoryReleaseSource) UploadRelease(spec cargo.BOSHReleaseTarballSpecification, file io.Reader) (cargo.BOSHReleaseTarballLock, error) {
remotePath, err := ars.RemotePath(spec)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, err
}
ars.logger.Printf("uploading release %q to %s at %q...\n", spec.Name, ars.ID, remotePath)
fullUrl := ars.ArtifactoryHost + "/artifactory/" + ars.Repo + "/" + remotePath
request, err := http.NewRequest(http.MethodPut, fullUrl, file)
if err != nil {
fmt.Println(err)
return cargo.BOSHReleaseTarballLock{}, err
}
request.SetBasicAuth(ars.Username, ars.Password)
// TODO: check Sha1/2
// request.Header.Set("X-Checksum-Sha1", spec.??? )
response, err := ars.Client.Do(request)
if err != nil {
return cargo.BOSHReleaseTarballLock{}, wrapVPNError(err)
}
switch response.StatusCode {
case http.StatusCreated:
default:
return cargo.BOSHReleaseTarballLock{}, fmt.Errorf(response.Status)
}
return cargo.BOSHReleaseTarballLock{
Name: spec.Name,
Version: spec.Version,
RemotePath: remotePath,
RemoteSource: ars.ReleaseSourceConfig.ID,
}, nil
}
func (ars *ArtifactoryReleaseSource) RemotePath(spec cargo.BOSHReleaseTarballSpecification) (string, error) {
pathBuf := new(bytes.Buffer)
err := ars.pathTemplate().Execute(pathBuf, spec)
if err != nil {
return "", fmt.Errorf("unable to evaluate path_template: %w", err)
}
return pathBuf.String(), nil
}
func (ars *ArtifactoryReleaseSource) pathTemplate() *template.Template {
return template.Must(
template.New("remote-path").
Funcs(template.FuncMap{"trimSuffix": strings.TrimSuffix}).
Parse(ars.ReleaseSourceConfig.PathTemplate))
}
type vpnError struct {
wrapped error
}
func (fe *vpnError) Unwrap() error {
return fe.wrapped
}
func (fe *vpnError) Error() string {
return fmt.Sprintf("failed to dial (hint: Are you connected to the corporate vpn?): %s", fe.wrapped)
}
func wrapVPNError(err error) error {
x := new(net.DNSError)
if errors.As(err, &x) {
return &vpnError{wrapped: err}
}
return err
}