Issues (121)

report/azureblob.go (4 issues)

1
/* Vuls - Vulnerability Scanner
2
Copyright (C) 2016  Future Corporation , Japan.
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
package report
19
20
import (
21
	"bytes"
22
	"encoding/json"
23
	"encoding/xml"
24
	"fmt"
25
	"time"
26
27
	storage "github.com/Azure/azure-sdk-for-go/storage"
28
	"golang.org/x/xerrors"
29
30
	c "github.com/future-architect/vuls/config"
31
	"github.com/future-architect/vuls/models"
32
)
33
34
// AzureBlobWriter writes results to AzureBlob
35
type AzureBlobWriter struct{}
36
37
// Write results to Azure Blob storage
38
func (w AzureBlobWriter) Write(rs ...models.ScanResult) (err error) {
39
	if len(rs) == 0 {
40
		return nil
41
	}
42
43
	cli, err := getBlobClient()
44
	if err != nil {
45
		return err
46
	}
47
48
	if c.Conf.FormatOneLineText {
49
		timestr := rs[0].ScannedAt.Format(time.RFC3339)
50
		k := fmt.Sprintf(timestr + "/summary.txt")
0 ignored issues
show
can't check non-constant format in call to Sprintf
Loading history...
51
		text := formatOneLineSummary(rs...)
52
		b := []byte(text)
53
		if err := createBlockBlob(cli, k, b); err != nil {
54
			return err
55
		}
56
	}
57
58
	for _, r := range rs {
59
		key := r.ReportKeyName()
60
		if c.Conf.FormatJSON {
61
			k := key + ".json"
62
			var b []byte
63
			if b, err = json.Marshal(r); err != nil {
64
				return xerrors.Errorf("Failed to Marshal to JSON: %w", err)
0 ignored issues
show
unrecognized printf verb 'w'
Loading history...
65
			}
66
			if err := createBlockBlob(cli, k, b); err != nil {
67
				return err
68
			}
69
		}
70
71
		if c.Conf.FormatList {
72
			k := key + "_short.txt"
73
			b := []byte(formatList(r))
74
			if err := createBlockBlob(cli, k, b); err != nil {
75
				return err
76
			}
77
		}
78
79
		if c.Conf.FormatFullText {
80
			k := key + "_full.txt"
81
			b := []byte(formatFullPlainText(r))
82
			if err := createBlockBlob(cli, k, b); err != nil {
83
				return err
84
			}
85
		}
86
87
		if c.Conf.FormatXML {
88
			k := key + ".xml"
89
			var b []byte
90
			if b, err = xml.Marshal(r); err != nil {
91
				return xerrors.Errorf("Failed to Marshal to XML: %w", err)
0 ignored issues
show
unrecognized printf verb 'w'
Loading history...
92
			}
93
			allBytes := bytes.Join([][]byte{[]byte(xml.Header + vulsOpenTag), b, []byte(vulsCloseTag)}, []byte{})
94
			if err := createBlockBlob(cli, k, allBytes); err != nil {
95
				return err
96
			}
97
		}
98
	}
99
	return
100
}
101
102
// CheckIfAzureContainerExists check the existence of Azure storage container
103
func CheckIfAzureContainerExists() error {
104
	cli, err := getBlobClient()
105
	if err != nil {
106
		return err
107
	}
108
	r, err := cli.ListContainers(storage.ListContainersParameters{})
109
	if err != nil {
110
		return err
111
	}
112
113
	found := false
114
	for _, con := range r.Containers {
115
		if con.Name == c.Conf.Azure.ContainerName {
116
			found = true
117
			break
118
		}
119
	}
120
	if !found {
121
		return xerrors.Errorf("Container not found. Container: %s", c.Conf.Azure.ContainerName)
122
	}
123
	return nil
124
}
125
126
func getBlobClient() (storage.BlobStorageClient, error) {
127
	api, err := storage.NewBasicClient(c.Conf.Azure.AccountName, c.Conf.Azure.AccountKey)
128
	if err != nil {
129
		return storage.BlobStorageClient{}, err
130
	}
131
	return api.GetBlobService(), nil
132
}
133
134
func createBlockBlob(cli storage.BlobStorageClient, k string, b []byte) error {
135
	var err error
136
	if c.Conf.GZIP {
137
		if b, err = gz(b); err != nil {
138
			return err
139
		}
140
		k += ".gz"
141
	}
142
143
	ref := cli.GetContainerReference(c.Conf.Azure.ContainerName)
144
	blob := ref.GetBlobReference(k)
145
	if err := blob.CreateBlockBlobFromReader(bytes.NewReader(b), nil); err != nil {
146
		return xerrors.Errorf("Failed to upload data to %s/%s, err: %w",
0 ignored issues
show
unrecognized printf verb 'w'
Loading history...
147
			c.Conf.Azure.ContainerName, k, err)
148
	}
149
	return nil
150
}
151