/
datasource_artifactory_file.go
323 lines (290 loc) · 9.65 KB
/
datasource_artifactory_file.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
package artifact
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jfrog/terraform-provider-artifactory/v10/pkg/artifactory/datasource"
"github.com/jfrog/terraform-provider-shared/util"
)
type FileInfo struct {
Repo string `json:"repo,omitempty"`
Path string `json:"path,omitempty"`
Created string `json:"created,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
LastModified string `json:"lastModified,omitempty"`
ModifiedBy string `json:"modifiedBy,omitempty"`
LastUpdated string `json:"lastUpdated,omitempty"`
DownloadUri string `json:"downloadUri,omitempty"`
MimeType string `json:"mimeType,omitempty"`
Size int `json:"size,string,omitempty"`
Checksums Checksums `json:"checksums,omitempty"`
OriginalChecksums Checksums `json:"originalChecksums,omitempty"`
Uri string `json:"uri,omitempty"`
}
type Checksums struct {
Md5 string `json:"md5,omitempty"`
Sha1 string `json:"sha1,omitempty"`
Sha256 string `json:"sha256,omitempty"`
}
func (fi FileInfo) Id() string {
if fi.DownloadUri != "" {
return fi.DownloadUri
} else {
return fi.Repo + fi.Path
}
}
func ArtifactoryFile() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceFileReader,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "Name of the repository where the file is stored.",
},
"path": {
Type: schema.TypeString,
Required: true,
Description: "The path to the file within the repository.",
},
"created": {
Type: schema.TypeString,
Computed: true,
Description: "The time & date when the file was created.",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
Description: "The user who created the file.",
},
"last_modified": {
Type: schema.TypeString,
Computed: true,
Description: "The time & date when the file was last modified.",
},
"modified_by": {
Type: schema.TypeString,
Computed: true,
Description: "The user who last modified the file.",
},
"last_updated": {
Type: schema.TypeString,
Computed: true,
Description: "The time & date when the file was last updated.",
},
"download_uri": {
Type: schema.TypeString,
Computed: true,
Description: "The URI that can be used to download the file.",
},
"mimetype": {
Type: schema.TypeString,
Computed: true,
Description: "The mimetype of the file.",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the file.",
},
"md5": {
Type: schema.TypeString,
Computed: true,
Description: "MD5 checksum of the file.",
},
"sha1": {
Type: schema.TypeString,
Computed: true,
Description: "SHA1 checksum of the file.",
},
"sha256": {
Type: schema.TypeString,
Computed: true,
Description: "SHA256 checksum of the file.",
},
"output_path": {
Type: schema.TypeString,
Required: true,
Description: "The local path the file should be downloaded to.",
},
"force_overwrite": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "If set to `true`, an existing file in the output_path will be overwritten.",
},
"path_is_aliased": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "If set to `true`, the provider will get the artifact path directly from Artifactory without attempting to resolve " +
"it or verify it and will delegate this to artifactory if the file exists. " +
"When using a smart remote repository, it is recommended to set this attribute to `true`. " +
"This is necessary to ensure that the provider fetches the artifact directly from Artifactory. " +
"If this attribute is not set or is set to `false`, there is a risk of fetching the `-cache` directory in Artifactory, " +
"potentially resulting in resource expiration and a 404 error.",
},
},
}
}
func createOutputFile(outputPath string) error {
outdir := filepath.Dir(outputPath)
err := os.MkdirAll(outdir, os.ModePerm)
if err != nil {
return err
}
outFile, err := os.Create(outputPath)
if err != nil {
return err
}
defer func(outFile *os.File) {
outFile.Close()
}(outFile)
return nil
}
func dataSourceFileReader(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
repository := d.Get("repository").(string)
path := d.Get("path").(string)
outputPath := d.Get("output_path").(string)
forceOverwrite := d.Get("force_overwrite").(bool)
pathIsAliased := d.Get("path_is_aliased").(bool)
var fileInfo FileInfo
var err error
tflog.Debug(ctx, "dataSourceFileReader", map[string]interface{}{
"repository": repository,
"path": path,
"outputPath": outputPath,
"forceOverwrite": forceOverwrite,
"pathIsAliased": pathIsAliased,
})
if !pathIsAliased {
tflog.Debug(ctx, "pathIsAliased == false")
fileInfo, err = downloadUsingFileInfo(ctx, outputPath, forceOverwrite, repository, path, m)
} else { // if we download the latest artifact (use path_is_aliased), we don't have all the data for the fileInfo struct, because no GET call was sent.
tflog.Debug(ctx, "pathIsAliased == true")
fileInfo, err = downloadWithoutChecks(ctx, outputPath, forceOverwrite, repository, path, m)
}
if err != nil {
return diag.FromErr(err)
}
return packFileInfo(fileInfo, d)
}
func downloadUsingFileInfo(ctx context.Context, outputPath string, forceOverwrite bool, repository string, path string, m interface{}) (FileInfo, error) {
fileInfo := FileInfo{}
tflog.Debug(ctx, "Fetching file info", map[string]interface{}{
"repository": repository,
"path": path,
})
client := m.(util.ProviderMetadata).Client
// switch to using Sprintf because Resty's SetPathParams() escape the path
// see https://github.com/go-resty/resty/blob/v2.7.0/middleware.go#L33
// should use url.JoinPath() eventually in go 1.20
requestUrl := fmt.Sprintf("%s/artifactory/api/storage/%s/%s", client.BaseURL, repository, path)
resp, err := client.R().
SetResult(&fileInfo).
Get(requestUrl)
if err != nil {
return fileInfo, err
}
if resp.IsError() {
return fileInfo, fmt.Errorf("%s", resp.String())
}
tflog.Debug(ctx, "File info fetched", map[string]interface{}{
"fileInfo": fileInfo,
})
checksumMatches := false
fileExists := datasource.FileExists(outputPath)
if fileExists {
checksumMatches, err = datasource.VerifySha256Checksum(outputPath, fileInfo.Checksums.Sha256)
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to verify checksum for %s", outputPath))
return fileInfo, err
}
}
tflog.Debug(ctx, "File info checked", map[string]interface{}{
"fileExists": fileExists,
"checksumMatches": checksumMatches,
})
/*--File Download logic--
1. File doesn't exist
2. In Data Source argument `force_overwrite` set to true, an existing file in the output_path will be overwritten. Ignore file exists or not
3. File exists but check sum doesn't match
*/
if !fileExists || forceOverwrite || (fileExists && !checksumMatches) {
tflog.Info(ctx, "Creating local output file")
err := createOutputFile(outputPath)
if err != nil {
return fileInfo, err
}
} else { //download not required
tflog.Info(ctx, "Skip downloading file")
return fileInfo, nil
}
tflog.Debug(ctx, "Downloading file...", map[string]interface{}{
"fileInfo.DownloadUri": fileInfo.DownloadUri,
"outputPath": outputPath,
})
_, err = m.(util.ProviderMetadata).Client.R().SetOutput(outputPath).Get(fileInfo.DownloadUri)
if err != nil {
return fileInfo, err
}
tflog.Debug(ctx, "Verify checksum with downloaded file")
checksumMatches, err = datasource.VerifySha256Checksum(outputPath, fileInfo.Checksums.Sha256)
if err != nil {
return fileInfo, err
}
if !checksumMatches {
return fileInfo, fmt.Errorf(
"Checksums for file %s and %s do not match, expected %s",
outputPath,
fileInfo.DownloadUri,
fileInfo.Checksums.Sha256,
)
}
return fileInfo, nil
}
func downloadWithoutChecks(ctx context.Context, outputPath string, forceOverwrite bool, repository string, path string, m interface{}) (FileInfo, error) {
fileInfo := FileInfo{
Repo: repository,
Path: path,
}
fileExists := datasource.FileExists(outputPath)
tflog.Debug(ctx, "File info", map[string]interface{}{
"fileInfo": fileInfo,
"fileExists": fileExists,
})
if !fileExists || forceOverwrite {
tflog.Info(ctx, "Creating local output file")
err := createOutputFile(outputPath)
if err != nil {
return fileInfo, err
}
} else { //download not required
tflog.Info(ctx, "Skip downloading file")
return fileInfo, nil
}
tflog.Debug(ctx, "Downloading file...", map[string]interface{}{
"repository": repository,
"path": path,
"outputPath": outputPath,
})
client := m.(util.ProviderMetadata).Client
// switch to using Sprintf because Resty's SetPathParams() escape the path
// see https://github.com/go-resty/resty/blob/v2.7.0/middleware.go#L33
// should use url.JoinPath() eventually in go 1.20
requestUrl := fmt.Sprintf("%s/artifactory/%s/%s", client.BaseURL, repository, path)
resp, err := client.R().
SetOutput(outputPath).
Get(requestUrl)
if err != nil {
return fileInfo, err
}
if resp.IsError() {
return fileInfo, fmt.Errorf("%s", resp.String())
}
return fileInfo, nil
}