forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.go
269 lines (240 loc) · 6.85 KB
/
archive.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package lib
import (
"archive/tar"
"bytes"
"encoding/json"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
)
var volumeRE = regexp.MustCompile(`^([a-zA-Z]):(.*)`)
var sharedRE = regexp.MustCompile(`^//([^/]+)`) // matches a shared folder in Windows after backslack replacement. i.e //VMBOXSVR/k6/script.js
var homeDirRE = regexp.MustCompile(`^(/[a-zA-Z])?/(Users|home|Documents and Settings)/(?:[^/]+)`)
// Normalizes (to use a / path separator) and anonymizes a file path, by scrubbing usernames from home directories.
func NormalizeAndAnonymizePath(path string) string {
path = filepath.Clean(path)
p := volumeRE.ReplaceAllString(path, `/$1$2`)
p = strings.Replace(p, "\\", "/", -1)
p = sharedRE.ReplaceAllString(p, `/nobody`)
return homeDirRE.ReplaceAllString(p, `$1/$2/nobody`)
}
// An Archive is a rollup of all resources and options needed to reproduce a test identically elsewhere.
type Archive struct {
// The runner to use, eg. "js".
Type string `json:"type"`
// Options to use.
Options Options `json:"options"`
// Filename and contents of the main file being executed.
Filename string `json:"filename"`
Data []byte `json:"-"`
// Working directory for resolving relative paths.
Pwd string `json:"pwd"`
// Archived filesystem.
Scripts map[string][]byte `json:"-"` // included scripts
Files map[string][]byte `json:"-"` // non-script resources
// Environment variables
Env map[string]string `json:"env"`
}
// Reads an archive created by Archive.Write from a reader.
func ReadArchive(in io.Reader) (*Archive, error) {
r := tar.NewReader(in)
arc := &Archive{
Scripts: make(map[string][]byte),
Files: make(map[string][]byte),
}
for {
hdr, err := r.Next()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
if hdr.Typeflag != tar.TypeReg && hdr.Typeflag != tar.TypeRegA {
continue
}
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
switch hdr.Name {
case "metadata.json":
if err := json.Unmarshal(data, &arc); err != nil {
return nil, err
}
// Path separator normalization for older archives (<=0.20.0)
arc.Filename = NormalizeAndAnonymizePath(arc.Filename)
arc.Pwd = NormalizeAndAnonymizePath(arc.Pwd)
continue
case "data":
arc.Data = data
}
// Path separator normalization for older archives (<=0.20.0)
normPath := NormalizeAndAnonymizePath(hdr.Name)
idx := strings.IndexRune(normPath, '/')
if idx == -1 {
continue
}
pfx := normPath[:idx]
name := normPath[idx+1:]
if name != "" && name[0] == '_' {
name = name[1:]
}
var dst map[string][]byte
switch pfx {
case "files":
dst = arc.Files
case "scripts":
dst = arc.Scripts
default:
continue
}
dst[name] = data
}
return arc, nil
}
// Write serialises the archive to a writer.
//
// The format should be treated as opaque; currently it is simply a TAR rollup, but this may
// change. If it does change, ReadArchive must be able to handle all previous formats as well as
// the current one.
func (arc *Archive) Write(out io.Writer) error {
w := tar.NewWriter(out)
t := time.Now()
metaArc := *arc
metaArc.Filename = NormalizeAndAnonymizePath(metaArc.Filename)
metaArc.Pwd = NormalizeAndAnonymizePath(metaArc.Pwd)
metadata, err := metaArc.json()
if err != nil {
return err
}
_ = w.WriteHeader(&tar.Header{
Name: "metadata.json",
Mode: 0644,
Size: int64(len(metadata)),
ModTime: t,
Typeflag: tar.TypeReg,
})
if _, err := w.Write(metadata); err != nil {
return err
}
_ = w.WriteHeader(&tar.Header{
Name: "data",
Mode: 0644,
Size: int64(len(arc.Data)),
ModTime: t,
Typeflag: tar.TypeReg,
})
if _, err := w.Write(arc.Data); err != nil {
return err
}
arcfs := []struct {
name string
files map[string][]byte
}{
{"scripts", arc.Scripts},
{"files", arc.Files},
}
for _, entry := range arcfs {
_ = w.WriteHeader(&tar.Header{
Name: entry.name,
Mode: 0755,
ModTime: t,
Typeflag: tar.TypeDir,
})
// A couple of things going on here:
// - You can't just create file entries, you need to create directory entries too.
// Figure out which directories are in use here.
// - We want archives to be comparable by hash, which means the entries need to be written
// in the same order every time. Go maps are shuffled, so we need to sort lists of keys.
// - We don't want to leak private information (eg. usernames) in archives, so make sure to
// anonymize paths before stuffing them in a shareable archive.
foundDirs := make(map[string]bool)
paths := make([]string, 0, len(entry.files))
files := make(map[string][]byte, len(entry.files))
for filePath, data := range entry.files {
filePath = NormalizeAndAnonymizePath(filePath)
files[filePath] = data
paths = append(paths, filePath)
dir := path.Dir(filePath)
for {
foundDirs[dir] = true
idx := strings.LastIndexByte(dir, os.PathSeparator)
if idx == -1 {
break
}
dir = dir[:idx]
}
}
dirs := make([]string, 0, len(foundDirs))
for dirpath := range foundDirs {
dirs = append(dirs, dirpath)
}
sort.Strings(paths)
sort.Strings(dirs)
for _, dirpath := range dirs {
if dirpath == "" || dirpath[0] == '/' {
dirpath = "_" + dirpath
}
_ = w.WriteHeader(&tar.Header{
Name: path.Clean(entry.name + "/" + dirpath),
Mode: 0755,
ModTime: t,
Typeflag: tar.TypeDir,
})
}
for _, filePath := range paths {
data := files[filePath]
if filePath[0] == '/' {
filePath = "_" + filePath
}
_ = w.WriteHeader(&tar.Header{
Name: path.Clean(entry.name + "/" + filePath),
Mode: 0644,
Size: int64(len(data)),
ModTime: t,
Typeflag: tar.TypeReg,
})
if _, err := w.Write(data); err != nil {
return err
}
}
}
return w.Close()
}
func (arc *Archive) json() ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
// this prevents <, >, and & from being escaped in JSON strings
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err := encoder.Encode(arc); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}