-
Notifications
You must be signed in to change notification settings - Fork 148
/
step_unpack.go
228 lines (193 loc) · 6.61 KB
/
step_unpack.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package upgrade
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/pkg/core/logger"
)
// unpack unpacks archive correctly, skips root (symlink, config...) unpacks data/*
func (u *Upgrader) unpack(version, archivePath string) (string, error) {
// unpack must occur in directory that holds the installation directory
// or the extraction will be double nested
var hash string
var err error
if runtime.GOOS == windows {
hash, err = unzip(u.log, archivePath)
} else {
hash, err = untar(u.log, version, archivePath)
}
if err != nil {
u.log.Errorw("Failed to unpack upgrade artifact", "error.message", err, "version", version, "file.path", archivePath, "hash", hash)
return "", err
}
u.log.Infow("Unpacked upgrade artifact", "version", version, "file.path", archivePath, "hash", hash)
return hash, nil
}
func unzip(log *logger.Logger, archivePath string) (string, error) {
var hash, rootDir string
r, err := zip.OpenReader(archivePath)
if err != nil {
return "", err
}
defer r.Close()
fileNamePrefix := strings.TrimSuffix(filepath.Base(archivePath), ".zip") + "/" // omitting `elastic-agent-{version}-{os}-{arch}/` in filename
unpackFile := func(f *zip.File) (err error) {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if cerr := rc.Close(); cerr != nil {
err = multierror.Append(err, cerr)
}
}()
//get hash
fileName := strings.TrimPrefix(f.Name, fileNamePrefix)
if fileName == agentCommitFile {
hashBytes, err := io.ReadAll(rc)
if err != nil || len(hashBytes) < hashLen {
return err
}
hash = string(hashBytes[:hashLen])
return nil
}
// skip everything outside data/
if !strings.HasPrefix(fileName, "data/") {
return nil
}
path := filepath.Join(paths.Data(), strings.TrimPrefix(fileName, "data/"))
if f.FileInfo().IsDir() {
log.Debugw("Unpacking directory", "archive", "zip", "file.path", path)
_ = os.MkdirAll(path, f.Mode())
} else {
log.Debugw("Unpacking file", "archive", "zip", "file.path", path)
_ = os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if cerr := f.Close(); cerr != nil {
err = multierror.Append(err, cerr)
}
}()
//nolint:gosec // legacy
if _, err = io.Copy(f, rc); err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
if rootDir == "" && filepath.Base(f.Name) == filepath.Dir(f.Name) {
// skip top level files
continue
}
if currentDir := filepath.Dir(f.Name); rootDir == "" || len(currentDir) < len(rootDir) {
rootDir = currentDir
}
if err := unpackFile(f); err != nil {
return "", err
}
}
return hash, nil
}
func untar(log *logger.Logger, version string, archivePath string) (string, error) {
r, err := os.Open(archivePath)
if err != nil {
return "", errors.New(fmt.Sprintf("artifact for 'elastic-agent' version '%s' could not be found at '%s'", version, archivePath), errors.TypeFilesystem, errors.M(errors.MetaKeyPath, archivePath))
}
defer r.Close()
zr, err := gzip.NewReader(r)
if err != nil {
return "", errors.New("requires gzip-compressed body", err, errors.TypeFilesystem)
}
tr := tar.NewReader(zr)
var rootDir string
var hash string
fileNamePrefix := strings.TrimSuffix(filepath.Base(archivePath), ".tar.gz") + "/" // omitting `elastic-agent-{version}-{os}-{arch}/` in filename
// go through all the content of a tar archive
// if elastic-agent.active.commit file is found, get commit of the version unpacked
// otherwise copy everything inside data directory (everything related to new version),
// pieces outside of data we already have and should not be overwritten as they are usually configs
for {
f, err := tr.Next()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return "", err
}
if !validFileName(f.Name) {
return "", errors.New("tar contained invalid filename: %q", f.Name, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, f.Name))
}
//get hash
fileName := strings.TrimPrefix(f.Name, fileNamePrefix)
if fileName == agentCommitFile {
hashBytes, err := io.ReadAll(tr)
if err != nil || len(hashBytes) < hashLen {
return "", err
}
hash = string(hashBytes[:hashLen])
continue
}
// skip everything outside data/
if !strings.HasPrefix(fileName, "data/") {
continue
}
rel := filepath.FromSlash(strings.TrimPrefix(fileName, "data/"))
abs := filepath.Join(paths.Data(), rel)
// find the root dir
if currentDir := filepath.Dir(abs); rootDir == "" || len(filepath.Dir(rootDir)) > len(currentDir) {
rootDir = currentDir
}
fi := f.FileInfo()
mode := fi.Mode()
switch {
case mode.IsRegular():
log.Debugw("Unpacking file", "archive", "tar", "file.path", abs)
// just to be sure, it should already be created by Dir type
if err := os.MkdirAll(filepath.Dir(abs), 0755); err != nil {
return "", errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs))
}
wf, err := os.OpenFile(abs, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode.Perm())
if err != nil {
return "", errors.New(err, "TarInstaller: creating file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs))
}
//nolint:gosec // legacy
_, err = io.Copy(wf, tr)
if closeErr := wf.Close(); closeErr != nil && err == nil {
err = closeErr
}
if err != nil {
return "", fmt.Errorf("TarInstaller: error writing to %s: %w", abs, err)
}
case mode.IsDir():
log.Debugw("Unpacking directory", "archive", "tar", "file.path", abs)
if err := os.MkdirAll(abs, 0755); err != nil {
return "", errors.New(err, "TarInstaller: creating directory for file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs))
}
default:
return "", errors.New(fmt.Sprintf("tar file entry %s contained unsupported file type %v", fileName, mode), errors.TypeFilesystem, errors.M(errors.MetaKeyPath, fileName))
}
}
return hash, nil
}
func validFileName(p string) bool {
if p == "" || strings.Contains(p, `\`) || strings.HasPrefix(p, "/") || strings.Contains(p, "../") {
return false
}
return true
}