Skip to content

Commit 5393f22

Browse files
j-fuentesjetstack-bot
authored andcommitted
Add Azure Blob Storage option for the output (#52)
* Add Azure Blob Storage option for the output Signed-off-by: Jose Fuentes <[email protected]> * Add doc Signed-off-by: Jose Fuentes <[email protected]>
1 parent 0f3f9d6 commit 5393f22

File tree

8 files changed

+204
-18
lines changed

8 files changed

+204
-18
lines changed

cmd/check.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/jetstack/preflight/pkg/datagatherer/gke"
1717
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
1818
"github.com/jetstack/preflight/pkg/output"
19+
"github.com/jetstack/preflight/pkg/output/azblob"
1920
"github.com/jetstack/preflight/pkg/output/gcs"
2021
"github.com/jetstack/preflight/pkg/packagesources/local"
2122
"github.com/jetstack/preflight/pkg/packaging"
@@ -243,14 +244,43 @@ func check() {
243244
}
244245
op, err = output.NewCLIOutput(outputFormat)
245246
} else if outputType == "local" {
246-
outputFormat := outputDefinition["format"].(string)
247-
outputPath := expandHome(outputDefinition["path"].(string))
248-
op, err = output.NewLocalOutput(outputFormat, outputPath)
247+
outputFormat, ok := outputDefinition["format"].(string)
248+
if !ok {
249+
log.Fatal("Missing 'format' property in local output configuration.")
250+
}
251+
outputPath, ok := outputDefinition["path"].(string)
252+
if !ok {
253+
log.Fatal("Missing 'path' property in local output configuration.")
254+
}
255+
op, err = output.NewLocalOutput(outputFormat, expandHome(outputPath))
249256
} else if outputType == "gcs" {
250-
outputFormat := outputDefinition["format"].(string)
251-
outputBucketName := outputDefinition["bucket-name"].(string)
252-
outputCredentialsPath := outputDefinition["credentials-path"].(string)
257+
outputFormat, ok := outputDefinition["format"].(string)
258+
if !ok {
259+
log.Fatal("Missing 'format' property in gcs output configuration.")
260+
}
261+
outputBucketName, ok := outputDefinition["bucket-name"].(string)
262+
if !ok {
263+
log.Fatal("Missing 'bucket-name' property in gcs output configuration.")
264+
}
265+
outputCredentialsPath, ok := outputDefinition["credentials-path"].(string)
266+
if !ok {
267+
log.Fatal("Missing 'credentials-path' property in gcs output configuration.")
268+
}
253269
op, err = gcs.NewOutput(ctx, outputFormat, outputBucketName, outputCredentialsPath)
270+
} else if outputType == "azblob" {
271+
outputFormat, ok := outputDefinition["format"].(string)
272+
if !ok {
273+
log.Fatal("Missing 'format' property in azblob output configuration.")
274+
}
275+
outputContainer, ok := outputDefinition["container"].(string)
276+
if !ok {
277+
log.Fatal("Missing 'container' property in azblob output configuration.")
278+
}
279+
accountName, accountKey := os.Getenv("AZURE_STORAGE_ACCOUNT"), os.Getenv("AZURE_STORAGE_ACCESS_KEY")
280+
if len(accountName) == 0 || len(accountKey) == 0 {
281+
log.Fatal("Either the AZURE_STORAGE_ACCOUNT or AZURE_STORAGE_ACCESS_KEY environment variable is not set.")
282+
}
283+
op, err = azblob.NewOutput(ctx, outputFormat, outputContainer, accountName, accountKey)
254284
} else {
255285
log.Fatalf("Output type not recognised: %s", outputType)
256286
}

docs/configuration.md

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ outputs:
9595
- type: cli
9696
```
9797

98-
Possible types of output include:
99-
- `local` for a local file.
100-
- `gcs` for a Google Cloud Storage bucket.
101-
- `cli` for command line output.
98+
There are different ways to output the results.
99+
The `type` property indicates support of platform where results are going to be written.
102100

103101
Most types also require a `format` to be specified.
104102
Possible formats are:
@@ -107,13 +105,61 @@ Possible formats are:
107105
- `html` for a HTML formatted report.
108106
- `intermediate` to output the raw JSON fetched by the *data gatherers*.
109107

110-
With the `cli` type output the format is optional
111-
and defaults to the `cli` format, for a coloured CLI formatted report.
112-
113108
The reports in `markdown`, `html` and `cli` format make use of the
114109
*policy manifest* to produce a human readable report describing
115110
which checks passed and which failed.
116111
The `json` format is raw output from OPA evaluation.
117112

118113
If no `outputs` are specified Preflight will output a report
119114
of the results to the CLI.
115+
116+
### cli
117+
118+
With the `cli` type output the format is optional
119+
and defaults to the `cli` format, for a coloured CLI formatted report.
120+
121+
### local
122+
123+
The `local` type output writes the results to a local directory.
124+
The directory must be specified with the `path` property.
125+
It will be created in case it does not exist.
126+
127+
```
128+
- type: local
129+
format: json
130+
path: ./output
131+
```
132+
133+
### gcs
134+
135+
The `gcs` type output uploads the results to a Google Cloud Storage bucket.
136+
137+
The property `bucket-name` indicates the ID of the GCS bucket where the results are going to be uploaded.
138+
Preflight assumes the bucket already exists.
139+
140+
`credentials-path` is the path to the credentials that can be used to write to that bucket.
141+
It is recommended to create a JSON key for a dedicated service account.
142+
143+
```
144+
- type: gcs
145+
format: json
146+
bucket-name: myresultsbucket
147+
credentials-path: ./credentials.json
148+
```
149+
150+
### azblob
151+
152+
The `azblob` output uploads the results to an Azure Blob Storage container.
153+
154+
The `container` property indicated the ID of the container where to upload the results.
155+
Preflight assumes the container already exists.
156+
157+
```
158+
- type: azblob
159+
format: json
160+
container: myresultscontainer
161+
```
162+
163+
Authentication is done by setting the environment variables `AZURE_STORAGE_ACCOUNT` and `AZURE_STORAGE_ACCESS_KEY`. You can create keys for a storage account from the Azure portal:
164+
165+
<img align="center" width="460" height="300" src="./images/azblob_keys.png">

docs/images/azblob_keys.png

49.7 KB
Loading

examples/aks.preflight.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ enabled-packages:
1616

1717
# This configures how the results will be reported.
1818
outputs:
19-
# writes a human readable doc that can be opened with a browser.
20-
- type: local
21-
path: ./output
19+
# uploads a json file to Azure Blob Storage.
20+
# `AZURE_STORAGE_ACCOUNT` and `AZURE_STORAGE_ACCESS_KEY` env vars need to be set.
21+
- type: azblob/
22+
container: mycontainer
2223
format: json
2324
# writes a file with the data gathered.
2425
- type: local

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ go 1.13
55
require (
66
cloud.google.com/go/storage v1.4.0
77
github.com/Azure/aks-engine v0.43.1
8+
github.com/Azure/azure-pipeline-go v0.2.2 // indirect
9+
github.com/Azure/azure-storage-blob-go v0.8.0
810
github.com/aws/aws-sdk-go v1.25.30
911
github.com/blang/semver v3.5.1+incompatible
1012
github.com/gomarkdown/markdown v0.0.0-20191104174740-4d42851d4d5a
@@ -14,13 +16,16 @@ require (
1416
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
1517
github.com/juju/testing v0.0.0-20191001232224-ce9dec17d28b // indirect
1618
github.com/mattn/go-colorable v0.1.4 // indirect
19+
github.com/mattn/go-ieproxy v0.0.0-20191113090002-7c0f6868bffe // indirect
1720
github.com/open-policy-agent/opa v0.16.0
1821
github.com/pkg/errors v0.8.1
1922
github.com/spf13/cobra v0.0.5
2023
github.com/spf13/pflag v1.0.5
2124
github.com/spf13/viper v1.5.0
2225
github.com/yudai/gojsondiff v1.0.0
26+
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
2327
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6
28+
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 // indirect
2429
google.golang.org/api v0.15.0
2530
gopkg.in/yaml.v2 v2.2.7
2631
k8s.io/api v0.17.0

go.sum

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ cloud.google.com/go/storage v1.4.0/go.mod h1:ZusYJWlOshgSBGbt6K3GnB3MT3H1xs2id9+
1414
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
1515
github.com/Azure/aks-engine v0.43.1 h1:NQkKWjP5DyDcbSTIaNm7ghA+saTd02KSPDfk/Bid+F0=
1616
github.com/Azure/aks-engine v0.43.1/go.mod h1:/zhzyPzbyvlU4j0P9aB7iPQRpr/fU52s+mQbrgZp950=
17+
github.com/Azure/azure-pipeline-go v0.2.1 h1:OLBdZJ3yvOn2MezlWvbrBMTEUQC72zAftRZOMdj5HYo=
18+
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
19+
github.com/Azure/azure-pipeline-go v0.2.2 h1:6oiIS9yaG6XCCzhgAgKFfIWyo4LLCiDhZot6ltoThhY=
20+
github.com/Azure/azure-pipeline-go v0.2.2/go.mod h1:4rQ/NZncSvGqNkkOsNpOU1tgoNuIlp9AfUH5G1tvCHc=
1721
github.com/Azure/azure-sdk-for-go v34.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
22+
github.com/Azure/azure-storage-blob-go v0.8.0 h1:53qhf0Oxa0nOjgbDeeYPUeyiNmafAFEY95rZLK0Tj6o=
23+
github.com/Azure/azure-storage-blob-go v0.8.0/go.mod h1:lPI3aLPpuLTeUwh1sViKXFxwl2B6teiRqI0deQUvsw0=
1824
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
1925
github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
2026
github.com/Azure/go-autorest/autorest v0.9.2 h1:6AWuh3uWrsZJcNoCHrCF/+g4aKPCU39kaMO6/qrnK/4=
@@ -183,12 +189,14 @@ github.com/gomarkdown/markdown v0.0.0-20191104174740-4d42851d4d5a/go.mod h1:aii0
183189
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
184190
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
185191
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
192+
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
186193
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
187194
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
188195
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
189196
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
190197
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
191198
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
199+
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
192200
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
193201
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
194202
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
@@ -294,8 +302,10 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
294302
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
295303
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
296304
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
305+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
297306
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
298307
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
308+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
299309
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
300310
github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
301311
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
@@ -308,6 +318,11 @@ github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN
308318
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
309319
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
310320
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
321+
github.com/mattn/go-ieproxy v0.0.0-20190610004146-91bb50d98149 h1:HfxbT6/JcvIljmERptWhwa8XzP7H3T+Z2N26gTsaDaA=
322+
github.com/mattn/go-ieproxy v0.0.0-20190610004146-91bb50d98149/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc=
323+
github.com/mattn/go-ieproxy v0.0.0-20190702010315-6dee0af9227d/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc=
324+
github.com/mattn/go-ieproxy v0.0.0-20191113090002-7c0f6868bffe h1:YioO2TiJyAHWHyCRQCP8jk5IzTqmsbGc5qQPIhHo6xs=
325+
github.com/mattn/go-ieproxy v0.0.0-20191113090002-7c0f6868bffe/go.mod h1:pYabZ6IHcRpFh7vIaLfK7rdcWgFEb3SFJ6/gNWuh88E=
311326
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
312327
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
313328
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
@@ -382,6 +397,7 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
382397
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
383398
github.com/pkg/term v0.0.0-20180730021639-bffc007b7fd5/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=
384399
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
400+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
385401
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
386402
github.com/prometheus/client_golang v0.0.0-20171201122222-661e31bf844d/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
387403
github.com/prometheus/client_golang v0.0.0-20181025174421-f30f42803563/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
@@ -446,6 +462,7 @@ github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
446462
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
447463
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
448464
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
465+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
449466
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
450467
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
451468
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
@@ -529,6 +546,9 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
529546
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
530547
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 h1:rjwSpXsdiK0dV8/Naq3kAw9ymfAeJIyd0upUIElB+lI=
531548
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
549+
golang.org/x/net v0.0.0-20191112182307-2180aed22343/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
550+
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA=
551+
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
532552
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
533553
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
534554
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -563,6 +583,9 @@ golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7w
563583
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
564584
golang.org/x/sys v0.0.0-20191104094858-e8c54fb511f6 h1:ZJUmhYTp8GbGC0ViZRc2U+MIYQ8xx9MscsdXnclfIhw=
565585
golang.org/x/sys v0.0.0-20191104094858-e8c54fb511f6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
586+
golang.org/x/sys v0.0.0-20191112214154-59a1497f0cea/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
587+
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=
588+
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
566589
golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
567590
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
568591
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -634,6 +657,7 @@ google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
634657
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
635658
gopkg.in/asn1-ber.v1 v1.0.0-20170511165959-379148ca0225/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
636659
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
660+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
637661
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
638662
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
639663
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=

pkg/output/azblob/azblob.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package azblob
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
"time"
8+
9+
"github.com/jetstack/preflight/api"
10+
"github.com/jetstack/preflight/pkg/exporter"
11+
"github.com/jetstack/preflight/pkg/packaging"
12+
"github.com/jetstack/preflight/pkg/results"
13+
14+
"github.com/Azure/azure-storage-blob-go/azblob"
15+
)
16+
17+
// Output writes to an Azure Blob Storage bucket
18+
type Output struct {
19+
credential azblob.Credential
20+
container url.URL
21+
exporter exporter.Exporter
22+
}
23+
24+
// NewOutput creates a new Output
25+
func NewOutput(ctx context.Context, format, containerName, accountName, accountKey string) (*Output, error) {
26+
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
container, err := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/%s", accountName, containerName))
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
var e exporter.Exporter
37+
switch format {
38+
case exporter.FormatJSON:
39+
e = exporter.NewJSONExporter()
40+
case exporter.FormatRaw:
41+
e = exporter.NewRawExporter()
42+
case exporter.FormatMD:
43+
e = exporter.NewMarkdownExporter()
44+
case exporter.FormatHTML:
45+
e = exporter.NewHTMLExporter()
46+
case exporter.FormatIntermediate:
47+
e = exporter.NewIntermediateExporter()
48+
default:
49+
return nil, fmt.Errorf("format %q not supported", format)
50+
}
51+
52+
return &Output{
53+
credential: credential,
54+
container: *container,
55+
exporter: e,
56+
}, nil
57+
}
58+
59+
// Write exports data in the specified format and writes it to the speficied bucket
60+
func (o *Output) Write(ctx context.Context, policyManifest *packaging.PolicyManifest, intermediateJSON []byte, rc *results.ResultCollection, cluster string, timestamp time.Time) error {
61+
buffer, err := o.exporter.Export(ctx, policyManifest, intermediateJSON, rc)
62+
if err != nil {
63+
return err
64+
}
65+
66+
pipeline := azblob.NewPipeline(o.credential, azblob.PipelineOptions{})
67+
containerURL := azblob.NewContainerURL(o.container, pipeline)
68+
blobURL := containerURL.NewBlockBlobURL(fmt.Sprintf("%s/%s/%s%s", cluster, timestamp.Format(api.TimeFormat), policyManifest.ID, o.exporter.FileExtension()))
69+
70+
_, err = azblob.UploadStreamToBlockBlob(ctx, buffer, blobURL, azblob.UploadStreamToBlockBlobOptions{
71+
// values chosen arbitrarily
72+
BufferSize: 2 * 1024 * 1024,
73+
MaxBuffers: 3,
74+
})
75+
if err != nil {
76+
return err
77+
}
78+
79+
return nil
80+
}

pkg/output/gcs/gcs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
"google.golang.org/api/option"
1515
)
1616

17-
// GCSOutput writes to a Google Cloud Storage bucket
17+
// Output writes to a Google Cloud Storage bucket
1818
type Output struct {
1919
bucket *storage.BucketHandle
2020
exporter exporter.Exporter
2121
}
2222

23-
// NewOutput creates a new GCSOutput
23+
// NewOutput creates a new Output
2424
func NewOutput(ctx context.Context, format, bucketName, credentialsPath string) (*Output, error) {
2525
c, err := storage.NewClient(ctx, option.WithCredentialsFile(credentialsPath))
2626
if err != nil {

0 commit comments

Comments
 (0)