Skip to content

Commit 75e8162

Browse files
SUP-4329: Refactor to use WIZ_CLIENT_ID and WIZ_CLIENT_SECRET (#29)
* feat: produce additional output file for non-human output formats * feat: update parameters and format * docs: update README to reflect parameter changes * feat: reflect paramter changes in hook * test: reflect parameter changes in tests * fix: add set -e to ensure hook doesn't silently fail * chore: remove version, pin images and remove Read Only * feat/fix: add shared lib for handling reading env vars correctly for strings or arrays parameters * Revert "fix: add set -e to ensure hook doesn't silently fail" This reverts commit f0bc975. * fix/test: correct Env Var used to read and add test * fix: make shared.bash executable for shellcheck * fix: update pipeline for shellcheck shared.bash * chore: re-add read-only for volume mounts as read-write not required * feat: add check for duplicate file formats * set a default output that will be used for the Build Annotations so that can be refactored separately * added notes for future changes required for loop to support multiple output files with the same file format per Wiz CLI docs Signed-off-by: Tom Watt <tom@buildkite.com> * tests: add tests for duplicate file output formats Signed-off-by: Tom Watt <tom@buildkite.com> * docs/chore: file-output-format is optional with no default Signed-off-by: Tom Watt <tom@buildkite.com> * refactor: move setupWiz to separate file to run unit-tests * feat: pass env vars to container instead of using flags to remove for job logs * refactor: remove api-secret-env parameter in favour of using WIZ_CLIENT_SECRET Signed-off-by: Tom Watt <tom@buildkite.com> * feat: add check for Wiz Client Credentials Signed-off-by: Tom Watt <tom@buildkite.com> * tests: reflect usage of WIZ_CLIENT_* Credentials * refactor: add teardown to cleanup dirs/files for authentication tests Signed-off-by: Tom Watt <tom@buildkite.com> * docs: Update README to reflect usage of WIZ_CLIENT_* credentials --------- Signed-off-by: Tom Watt <tom@buildkite.com> Co-authored-by: Shimon Ulewicz <sulewicz@groq.com>
1 parent 75786cb commit 75e8162

File tree

5 files changed

+98
-102
lines changed

5 files changed

+98
-102
lines changed

README.md

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ Scans your infrastructure-as-code Cloudformation stacks or docker images for sec
66

77
This plugin is forked from [blstrco/wiz-buildkite-plugin](https://github.com/blstrco/wiz-buildkite-plugin).
88

9+
## Requirements
10+
11+
In order to use this plugin, you will need to have the following installed on your buildkite agent:
12+
13+
- Docker
14+
15+
And the following environment variables exported in the job (e.g. via an Agent hook or Plugin):
16+
17+
- WIZ_CLIENT_ID (Wiz service account's client ID)
18+
- WIZ_CLIENT_SECRET (Wiz service account's secret)
19+
20+
Check out [Buildkite's documentation](https://buildkite.com/docs/pipelines/security/secrets/managing) for more information on how to manage secrets in Buildkite.
21+
922
## Examples
1023

1124
### Docker Scanning
@@ -15,8 +28,6 @@ Add the following to your `pipeline.yml`, the plugin will pull the image, scan i
1528
```yml
1629
steps:
1730
- command: ls
18-
env:
19-
- WIZ_API_ID: "<your-id-goes-here>"
2031
plugins:
2132
- wiz#v1.4.0:
2233
scan-type: 'docker'
@@ -39,8 +50,6 @@ To avoid adding build time overhead, you can add IaC scanning to your `cdk diff`
3950
```yml
4051
steps:
4152
- command: ls
42-
env:
43-
- WIZ_API_ID: "<your-id-goes-here>"
4453
plugins:
4554
- docker-compose#v4.16.0:
4655
# to get the output of CDK diff, mount the volume in cdk diff stage
@@ -59,8 +68,6 @@ Add the following to your `pipeline.yml`, the plugin will scan a specific CloudF
5968
steps:
6069
- label: "Scan CloudFormation template file"
6170
command: ls
62-
env:
63-
- WIZ_API_ID: "<your-id-goes-here>"
6471
plugins:
6572
- wiz#v1.4.0:
6673
scan-type: 'iac'
@@ -79,8 +86,6 @@ Add the following to your `pipeline.yml`, the plugin will scan a specific Terraf
7986
steps:
8087
- label: "Scan Terraform File"
8188
command: ls *.tf
82-
env:
83-
- WIZ_API_ID: "<your-id-goes-here>"
8489
plugins:
8590
- wiz#v1.4.0:
8691
scan-type: 'iac'
@@ -96,8 +101,6 @@ To change the directory, add the following to your `pipeline.yml`, the plugin wi
96101
steps:
97102
- label: "Scan Terraform Files in Directory"
98103
command: ls my-terraform-dir/*.tf
99-
env:
100-
- WIZ_API_ID: "<your-id-goes-here>"
101104
plugins:
102105
- wiz#v1.4.0:
103106
scan-type: 'iac'
@@ -113,8 +116,6 @@ Add the following to your `pipeline.yml`, the plugin will scan a Terraform Plan.
113116
steps:
114117
- label: "Scan Terraform Plan"
115118
command: terraform plan -out plan.tfplan && terraform show -json plan.tfplan | jq -er . > plan.tfplanjson
116-
env:
117-
- WIZ_API_ID: "<your-id-goes-here>"
118119
plugins:
119120
- wiz#v1.4.0:
120121
scan-type: 'iac'
@@ -130,8 +131,6 @@ Add the following to your `pipeline.yml`, the plugin will scan a directory.
130131
steps:
131132
- label: "Scan Directory"
132133
command: ls .
133-
env:
134-
- WIZ_API_ID: "<your-id-goes-here>"
135134
plugins:
136135
- wiz#v1.4.0:
137136
scan-type: 'dir'
@@ -145,8 +144,6 @@ To change the directory, add the following to your `pipeline.yml`, the plugin wi
145144
steps:
146145
- label: "Scan Files in different Directory"
147146
command: ls my-dir
148-
env:
149-
- WIZ_API_ID: "<your-id-goes-here>"
150147
plugins:
151148
- wiz#v1.4.0:
152149
scan-type: 'dir'
@@ -155,10 +152,6 @@ steps:
155152

156153
## Configuration
157154

158-
### `api-secret-env` (Optional, string)
159-
160-
The environment variable that the Wiz API Secret is stored in. Defaults to using `WIZ_API_SECRET`. Refer to the [documentation](https://buildkite.com/docs/pipelines/secrets#using-a-secrets-storage-service) for more information about managing secrets on your Buildkite agents.
161-
162155
### `scan-type` (Required, string) : `dir | docker | iac'
163156

164157
The type of resource to be scanned.

hooks/post-command

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
77
# shellcheck source=lib/shared.bash
88
. "$DIR/../lib/shared.bash"
99

10+
# shellcheck source=lib/plugin.bash
11+
. "$DIR/../lib/plugin.bash"
12+
1013
WIZ_DIR="$HOME/.wiz"
1114
SCAN_TYPE="${BUILDKITE_PLUGIN_WIZ_SCAN_TYPE:-}"
1215
FILE_PATH="${BUILDKITE_PLUGIN_WIZ_PATH:-}"
@@ -25,13 +28,6 @@ if [[ "${SCAN_TYPE}" == "docker" && -z "${BUILDKITE_PLUGIN_WIZ_IMAGE_ADDRESS:-}"
2528
exit 1
2629
fi
2730

28-
api_secret_var="${BUILDKITE_PLUGIN_WIZ_API_SECRET_ENV:-WIZ_API_SECRET}"
29-
30-
if [[ -z "${!api_secret_var:-}" ]]; then
31-
echo "+++ 🚨 No Wiz API Secret password found in \$${api_secret_var}"
32-
exit 1
33-
fi
34-
3531
##
3632
# Wiz CLI Parameters
3733
##
@@ -122,24 +118,6 @@ esac
122118
wiz_cli_container_repository="wiziocli.azurecr.io/wizcli"
123119
wiz_cli_container="${wiz_cli_container_repository}:${container_image_tag}"
124120

125-
#TODO move this to agent-startup so all agents have wiz setup to save time, possibly directly as cli
126-
setupWiz() {
127-
echo "Setting up and authenticating wiz"
128-
mkdir -p "$WIZ_DIR"
129-
docker run \
130-
--rm -it \
131-
--mount type=bind,src="${WIZ_DIR}",dst=/cli \
132-
"${wiz_cli_container}" \
133-
auth --id="${WIZ_API_ID}" --secret="${!api_secret_var}"
134-
# check that wiz-auth work expected, and a file in WIZ_DIR is created
135-
if [ -z "$(ls -A "${WIZ_DIR}")" ]; then
136-
echo "Wiz authentication failed, please confirm that credentials are set for WIZ_API_ID and WIZ_API_SECRET"
137-
exit 1
138-
else
139-
echo "Authenticated successfully"
140-
fi
141-
}
142-
143121
#$1 type, $2 name, $3 pass/fail, $4 result file
144122
buildAnnotation() {
145123
annotation_file=${RANDOM:0:2}-annotation.md
@@ -247,15 +225,15 @@ dirScan() {
247225

248226
case "${SCAN_TYPE}" in
249227
iac)
250-
setupWiz
228+
setupWiz "$wiz_cli_container" "$WIZ_DIR"
251229
iacScan
252230
;;
253231
docker)
254-
setupWiz
232+
setupWiz "$wiz_cli_container" "$WIZ_DIR"
255233
dockerImageScan
256234
;;
257235
dir)
258-
setupWiz
236+
setupWiz "$wiz_cli_container" "$WIZ_DIR"
259237
dirScan
260238
;;
261239
esac

lib/plugin.bash

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
function validateWizClientCredentials() {
4+
local missing_vars=()
5+
6+
[ -z "${WIZ_CLIENT_ID}" ] && missing_vars+=("WIZ_CLIENT_ID")
7+
[ -z "${WIZ_CLIENT_SECRET}" ] && missing_vars+=("WIZ_CLIENT_SECRET")
8+
9+
if [ ${#missing_vars[@]} -gt 0 ]; then
10+
echo "+++ 🚨 The following required environment variables are not set: ${missing_vars[*]}"
11+
exit 1
12+
fi
13+
}
14+
15+
# Use WIZ_CLIENT_ID and WIZ_CLIENT_SECRET environment variables to authenticate to Wiz and get auth file
16+
# $1 - Wiz CLI Container Image
17+
# $2 - Directory to store auth file
18+
function setupWiz() {
19+
local wiz_container_image="${1}"
20+
local wiz_dir="${2}"
21+
22+
echo "Setting up and authenticating wiz"
23+
validateWizClientCredentials
24+
mkdir -p "$wiz_dir"
25+
26+
docker run \
27+
--rm -it \
28+
--mount type=bind,src="${wiz_dir}",dst=/cli \
29+
-e WIZ_CLIENT_ID \
30+
-e WIZ_CLIENT_SECRET \
31+
"${wiz_container_image}" \
32+
auth
33+
34+
# check that wiz-auth work expected, and a file in WIZ_DIR is created
35+
if [ -z "$(ls -A "${wiz_dir}")" ]; then
36+
echo "Wiz authentication failed, please confirm the credentials are set for WIZ_CLIENT_ID and WIZ_CLIENT_SECRET"
37+
exit 1
38+
else
39+
echo "Authenticated successfully"
40+
fi
41+
}

plugin.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ requirements:
66
- docker
77
configuration:
88
properties:
9-
api-secret-env:
10-
type: string
119
image-address:
1210
type: string
1311
scan-format:

0 commit comments

Comments
 (0)