-
Notifications
You must be signed in to change notification settings - Fork 571
/
Copy pathsupport-inspect.go
301 lines (265 loc) · 9.03 KB
/
support-inspect.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
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// 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 cmd
import (
"context"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"hash/crc32"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/fatih/color"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/madmin-go/v3"
"github.com/minio/madmin-go/v3/estream"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/v3/console"
)
const (
defaultPublicKey = "MIIBCgKCAQEAs/128UFS9A8YSJY1XqYKt06dLVQQCGDee69T+0Tip/1jGAB4z0/3QMpH0MiS8Wjs4BRWV51qvkfAHzwwdU7y6jxU05ctb/H/WzRj3FYdhhHKdzear9TLJftlTs+xwj2XaADjbLXCV1jGLS889A7f7z5DgABlVZMQd9BjVAR8ED3xRJ2/ZCNuQVJ+A8r7TYPGMY3wWvhhPgPk3Lx4WDZxDiDNlFs4GQSaESSsiVTb9vyGe/94CsCTM6Cw9QG6ifHKCa/rFszPYdKCabAfHcS3eTr0GM+TThSsxO7KfuscbmLJkfQev1srfL2Ii2RbnysqIJVWKEwdW05ID8ryPkuTuwIDAQAB"
)
var supportInspectFlags = append(subnetCommonFlags,
cli.BoolFlag{
Name: "legacy",
Usage: "use the older inspect format",
},
)
var supportInspectCmd = cli.Command{
Name: "inspect",
Usage: "upload raw object contents for analysis",
Action: mainSupportInspect,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: supportInspectFlags,
HideHelpCommand: true,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [FLAGS] TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Upload 'xl.meta' of a specific object from all the drives
{{.Prompt}} {{.HelpName}} myminio/bucket/test*/xl.meta
2. Upload recursively all objects at a prefix. NOTE: This can be an expensive operation use it with caution.
{{.Prompt}} {{.HelpName}} myminio/bucket/test/**
3. Download 'xl.meta' of a specific object from all the drives locally, and upload to SUBNET manually
{{.Prompt}} {{.HelpName}} myminio/bucket/test*/xl.meta --airgap
`,
}
type inspectMessage struct {
Status string `json:"status"`
AliasedURL string `json:"aliasedURL,omitempty"`
File string `json:"file,omitempty"`
Key string `json:"key,omitempty"`
}
// Colorized message for console printing.
func (t inspectMessage) String() string {
var msg string
if globalAirgapped || t.File != "" {
if t.Key == "" {
msg = fmt.Sprintf("File data successfully downloaded as %s", console.Colorize("File", t.File))
} else {
msg = fmt.Sprintf("Encrypted file data successfully downloaded as %s\n", console.Colorize("File", t.File))
msg += fmt.Sprintf("Decryption key: %s\n\n", console.Colorize("Key", t.Key))
msg += "The decryption key will ONLY be shown here. It cannot be recovered.\n"
msg += "The encrypted file can safely be shared without the decryption key.\n"
msg += "Even with the decryption key, data stored with encryption cannot be accessed."
}
} else {
msg = fmt.Sprintf("Object inspection data for '%s' uploaded to SUBNET successfully", t.AliasedURL)
}
return console.Colorize(supportSuccessMsgTag, msg)
}
func (t inspectMessage) JSON() string {
t.Status = "success"
jsonMessageBytes, e := json.MarshalIndent(t, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonMessageBytes)
}
func checkSupportInspectSyntax(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
showCommandHelpAndExit(ctx, 1) // last argument is exit code
}
}
// mainSupportInspect - the entry function of inspect command
func mainSupportInspect(ctx *cli.Context) error {
// Check for command syntax
checkSupportInspectSyntax(ctx)
setSuccessMessageColor()
// Get the alias parameter from cli
args := ctx.Args()
aliasedURL := args.Get(0)
alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true)
if len(apiKey) == 0 {
// api key not passed as flag. Check that the cluster is registered.
apiKey = validateClusterRegistered(alias, true)
}
console.SetColor("File", color.New(color.FgWhite, color.Bold))
console.SetColor("Key", color.New(color.FgHiRed, color.Bold))
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
if err != nil {
fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.")
return nil
}
// Compute bucket and object from the aliased URL
aliasedURL = filepath.ToSlash(aliasedURL)
splits := splitStr(aliasedURL, "/", 3)
bucket, prefix := splits[1], splits[2]
shellName, _ := getShellName()
if runtime.GOOS != "windows" && shellName != "bash" && strings.Contains(prefix, "*") {
console.Infoln("Your shell is auto determined as '" + shellName + "', wildcard patterns are only supported with 'bash' SHELL.")
}
var publicKey []byte
if !ctx.Bool("legacy") {
var e error
publicKey, e = os.ReadFile(filepath.Join(mustGetMcConfigDir(), "support_public.pem"))
if e != nil && !os.IsNotExist(e) {
fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to inspect file.")
} else if len(publicKey) > 0 {
if !globalJSON && !globalQuiet {
console.Infoln("Using public key from ", filepath.Join(mustGetMcConfigDir(), "support_public.pem"))
}
}
// Fall back to MinIO public key.
if len(publicKey) == 0 {
// Public key for MinIO confidential information.
publicKey, _ = base64.StdEncoding.DecodeString(defaultPublicKey)
}
}
key, r, e := client.Inspect(context.Background(), madmin.InspectOptions{
Volume: bucket,
File: prefix,
PublicKey: publicKey,
})
fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to inspect file.")
// Download the inspect data in a temporary file first
tmpFile, e := os.CreateTemp("", "mc-inspect-")
fatalIf(probe.NewError(e), "Unable to download file data.")
copied := false
// Validate stream, report errors on stream.
if len(key) == 0 {
pr, pw := io.Pipe()
copied = true
var aErr error
var wg sync.WaitGroup
wg.Add(1)
// Validate stream...
go func() {
defer wg.Done()
er, err := estream.NewReader(pr)
if err != nil {
// Ignore if header is non-parsable...
io.Copy(io.Discard, pr)
return
}
// We don't care about decrypting at this point.
er.SkipEncrypted(true)
for {
var st *estream.Stream
st, aErr = er.NextStream()
if errors.Is(aErr, io.EOF) {
aErr = nil
pr.CloseWithError(nil)
return
}
if aErr != nil {
pr.CloseWithError(aErr)
return
}
aErr = st.Skip()
if aErr != nil {
fmt.Println("Skip:", aErr)
pr.CloseWithError(aErr)
return
}
}
}()
_, e = io.Copy(io.MultiWriter(tmpFile, pw), r)
pw.CloseWithError(e)
wg.Wait()
if e == nil && aErr != nil {
e = aErr
}
}
if !copied {
_, e = io.Copy(tmpFile, r)
}
fatalIf(probe.NewError(e), "Unable to download file data.")
r.Close()
tmpFile.Close()
wantFileName := "inspect-" + conservativeFileName(strings.Join(splits, "_")) + ".enc"
if globalAirgapped {
saveInspectDataFile(wantFileName, key, tmpFile)
return nil
}
uploadURL := SubnetUploadURL("inspect")
reqURL, headers := prepareSubnetUploadURL(uploadURL, alias, apiKey)
tmpFileName := tmpFile.Name()
_, e = (&SubnetFileUploader{
alias: alias,
FilePath: tmpFileName,
filename: wantFileName,
ReqURL: reqURL,
Headers: headers,
DeleteAfterUpload: true,
}).UploadFileToSubnet()
if e != nil {
console.Errorln("Unable to upload inspect data to SUBNET portal: " + e.Error())
saveInspectDataFile(wantFileName, key, tmpFile)
return nil
}
printMsg(inspectMessage{AliasedURL: aliasedURL})
return nil
}
func saveInspectDataFile(dstFileName string, key []byte, tmpFile *os.File) {
var keyHex string
// Choose a name and move the inspect data to its final destination
if key != nil {
// Create an id that is also crc.
var id [4]byte
binary.LittleEndian.PutUint32(id[:], crc32.ChecksumIEEE(key[:]))
// We use 4 bytes of the 32 bytes to identify they file.
dstFileName = fmt.Sprintf("inspect-data.%s.enc", hex.EncodeToString(id[:]))
keyHex = hex.EncodeToString(id[:]) + hex.EncodeToString(key[:])
}
fi, e := os.Stat(dstFileName)
if e == nil && !fi.IsDir() {
e = moveFile(dstFileName, dstFileName+"."+time.Now().Format(dateTimeFormatFilename))
fatalIf(probe.NewError(e), "Unable to create a backup of "+dstFileName)
} else {
if !os.IsNotExist(e) {
fatal(probe.NewError(e), "Unable to download file data")
}
}
fatalIf(probe.NewError(moveFile(tmpFile.Name(), dstFileName)), "Unable to rename downloaded data, file exists at %s", tmpFile.Name())
printMsg(inspectMessage{
File: dstFileName,
Key: keyHex,
})
}