-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.go
74 lines (62 loc) · 1.99 KB
/
upload.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
// Copyright 2018 Saferwall. All rights reserved.
// Use of this source code is governed by Apache v2 license
// license that can be found in the LICENSE file.
package cmd
import (
"bytes"
"context"
"log"
"path/filepath"
s "github.com/saferwall/saferwall-cli/internal/storage"
"github.com/saferwall/saferwall-cli/internal/util"
"github.com/spf13/cobra"
)
func init() {
uploadCmd.Flags().StringVarP(&filePath, "path", "p", "",
"Destination directory for the file to upload.")
}
var uploadCmd = &cobra.Command{
Use: "upload",
Short: "Upload samples directly to object storage.",
Long: `Uploading samples directly to object storage and without triggering a scan`,
Run: func(cmd *cobra.Command, args []string) {
// Authenticate to object storage service.
opts := s.Options{}
switch cfg.Storage.DeploymentKind {
case "aws":
opts.Region = cfg.Storage.S3.Region
opts.AccessKey = cfg.Storage.S3.AccessKey
opts.SecretKey = cfg.Storage.S3.SecretKey
case "minio":
opts.Region = cfg.Storage.MinIO.Region
opts.AccessKey = cfg.Storage.MinIO.AccessKey
opts.SecretKey = cfg.Storage.MinIO.SecretKey
opts.MinIOEndpoint = cfg.Storage.MinIO.Endpoint
case "local":
opts.LocalRootDir = cfg.Storage.Local.RootDir
}
opts.Bucket = cfg.Storage.SamplesBucket
sto, err := s.New(cfg.Storage.DeploymentKind, opts)
if err != nil {
log.Fatalf("failed to create a storage client: %v", err)
return
}
filePaths, err := util.WalkAllFilesInDir(filePath)
if err != nil {
log.Fatalf("failed to walk directory %s: %v", filePaths, err)
}
for _, filePath := range filePaths {
fileContent, err := util.ReadAll(filePath)
if err != nil {
log.Fatalf("failed to read file %s,: %v", filePath, err)
}
sha256 := filepath.Base(filePath)
r := bytes.NewReader(fileContent)
log.Printf("uploading %s", sha256)
err = sto.Upload(context.TODO(), cfg.Storage.SamplesBucket, sha256, r)
if err != nil {
log.Fatalf("failed to upload file %s: %v", filePath, err)
}
}
},
}