-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
310 lines (277 loc) · 12.7 KB
/
action.yml
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
302
303
304
305
306
307
308
309
310
---
name: Powerpipe Check
author: Turbot
description: >
Run Powerpipe mod benchmarks and controls. Create annotations for Infrastructure as Code (IaC) controls.
branding:
color: green
icon: shield
inputs:
mod-url:
description: "Mod URL to clone. Examples: https://github.com/turbot/steampipe-mod-aws-compliance"
required: true
mod-branch:
description: "Mod branch or tag to clone. Defaults to 'main'. Examples: main, release/v0.7, v0.5, 'v1.0-beta.0"
required: false
default: main
controls:
description: Space separated list of controls to run. Can also pass in as a multi-line string.
required: false
benchmarks:
description: Space separated list of benchmarks to run. Can also pass in as a multi-line string.
required: false
database:
description: Database connection string to use for running controls and benchmarks.
required: false
pipes-snapshot-visibility:
description: Set this to 'anyone_with_link' or 'workspace' to create a snapshot in Turbot Pipes with the specified visibility.
required: false
pipes-token:
description: The Turbot Pipes token used to save snapshots. Can also be set via the PIPES_TOKEN environment variable (https://powerpipe.io/docs/reference/env-vars/pipes_token).
required: false
additional-args:
description: Space separated args to add to the 'powerpipe control/benchmark run' command.
required: false
artifact-exports:
description: "Comma separated export types to save as artifacts. Example: 'csv,json'. For all valid formats please see https://powerpipe.io/docs/reference/cli/control#output-formats."
required: false
github-token:
description: GitHub token used to download the Powerpipe mod, upload artifacts, and create annotations.
required: false
default: ${{ github.token }}
runs:
using: composite
steps:
- name: Create temporary directories
shell: bash
id: make-temp-dir
run: |
# Create temporary directories for Steampipe and mod installation
tmp=$(mktemp -d)
tmp_mod_dir=$tmp/mod_dir
mkdir -p $tmp_mod_dir
echo "tmp-mod-dir=$tmp_mod_dir" >> $GITHUB_OUTPUT
- name: Clone the mod and install dependencies
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.github-token }}
run: |
# Clone and setup mod
if [ -z "${{inputs.mod-branch}}" ]; then
git clone --depth 1 "${{ inputs.mod-url }}" "${{ steps.make-temp-dir.outputs.tmp-mod-dir }}"
else
git clone --depth 1 -b "${{ inputs.mod-branch }}" --single-branch "${{ inputs.mod-url }}" "${{ steps.make-temp-dir.outputs.tmp-mod-dir }}"
fi
cd ${{ steps.make-temp-dir.outputs.tmp-mod-dir }}
powerpipe mod install
cd -
- name: Get Powerpipe control names
if: inputs.controls != ''
id: get-controls
shell: bash
run: |
# Convert multiline 'controls' input to single line arguments which we can send to 'powerpipe control run'
run_list=
while read line; do
run_list="$run_list $line"
done <<EOF
${{ inputs.controls }}
EOF
echo "run_list=$run_list" >> $GITHUB_OUTPUT
- name: Get Powerpipe benchmark names
if: inputs.benchmarks != ''
id: get-benchmarks
shell: bash
run: |
# Convert multiline 'benchmarks' input to single line arguments which we can send to 'powerpipe benchmark run'
run_list=
while read line; do
run_list="$run_list $line"
done <<EOF
${{ inputs.benchmarks }}
EOF
echo "run_list=$run_list" >> $GITHUB_OUTPUT
- name: Get database connection string
if: inputs.database != ''
id: get-database
shell: bash
run: |
echo "database=${{ inputs.database }}" >> $GITHUB_OUTPUT
- name: Parse exports and artifact paths
id: export-paths
shell: bash
run: |
# Parse exports & artifact paths
inputs=${{ inputs.artifact-exports }}
# Default exports: markdown is used for Summary page and json for creating annotations (only saved as artifacts if requested)
exports="--export=json --export=md"
paths=""
date=$(date '+%Y%m%d')
if [ "$inputs" != "" ];then
for i in ${inputs//,/ }
do
if [ "$i" != "json" ] && [ "$i" != "md" ];then
exports="$exports --export=$i"
fi
if [[ "$paths" == "" ]];then
paths="*$date*.${i}"
else
paths=$(echo -e "${paths}\n*$date*.${i}")
fi
done
fi
echo "control_exports=$exports" >> $GITHUB_OUTPUT
echo "benchmark_exports=$exports" >> $GITHUB_OUTPUT
echo "summary_file_pattern=*$date*.md" >> $GITHUB_OUTPUT
echo "artifact_paths<<EOF" >> $GITHUB_OUTPUT
echo "$paths" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Run Powerpipe control
if: steps.get-controls.outputs.run_list != ''
id: run-controls
shell: bash
env:
STEAMPIPE_CHECK_DISPLAY_WIDTH: "120"
STEAMPIPE_DISPLAY_WIDTH: "120"
run: |
set +e
# Set outputs for artifact naming
echo "uuid=$(echo $RANDOM | md5sum | head -c 20)" >> $GITHUB_OUTPUT
echo "mod=$(echo ${{ inputs.mod-url }} | rev | cut -d '/' -f 1 | rev)" >> $GITHUB_OUTPUT
# Set workflow_id / run_url for later use
workflow_id=$(echo $GITHUB_WORKFLOW | sed -e "s/ /_/g")
run_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
# Clear snapshot flags from additional args (should be via input)
additional_args=$(echo "${{ inputs.additional-args }}" | sed -e "s/--share//g" | sed -e "s/--snapshot//g")
# Allow override for output if set in additional-args
control_output="--output=brief"
if [[ "${{ inputs.additional-args }}" == *"--output"* ]]; then
control_output=""
fi
# Set export types
control_exports="${{ steps.export-paths.outputs.control_exports }}"
# Snapshot setup
snapshot_type=""
snapshot_tags="--snapshot-tag github_repo=$GITHUB_REPOSITORY --snapshot-tag github_run_url=$run_url --snapshot-tag github_workflow=$workflow_id"
pipes_inputs=""
if [[ -n "${{ inputs.pipes-token }}" ]];then
pipes_inputs="--pipes-host pipes.turbot.com --pipes-token ${{ inputs.pipes-token }}"
fi
if [ "${{ inputs.pipes-snapshot-visibility }}" == "anyone_with_link" ];then
snapshot_type="--share $snapshot_tags"
elif [ "${{ inputs.pipes-snapshot-visibility }}" == "workspace" ];then
snapshot_type="--snapshot $snapshot_tags"
elif [[ -n "${{ inputs.pipes-snapshot-visibility }}" ]];then
echo "Error: invalid value for input 'snapshot-visibility' provided: ${{ inputs.pipes-snapshot-visibility }} - valid values are 'anyone_with_link' and 'workspace'"
exit 3
fi
# Run control command
if [[ -n "${{ steps.get-database.outputs.database }}" ]]; then
powerpipe control run ${{ steps.get-controls.outputs.run_list }} $control_output $control_exports --database=${{ steps.get-database.outputs.database }} --mod-location=${{ steps.make-temp-dir.outputs.tmp-mod-dir }} $snapshot_type $pipes_inputs $additional_args | tee /tmp/output.log
else
powerpipe control run ${{ steps.get-controls.outputs.run_list }} $control_output $control_exports --mod-location=${{ steps.make-temp-dir.outputs.tmp-mod-dir }} $snapshot_type $pipes_inputs $additional_args | tee /tmp/output.log
fi
exit_code=$?
# Write summary
snapshot_url=$(cat /tmp/output.log | grep "Snapshot uploaded" | sed -e "s/^Snapshot uploaded to //")
if [ -n "$snapshot_url" ];then
echo "> Turbot Pipes ${{ inputs.pipes-snapshot-visibility }} snapshot(s)" >> $GITHUB_STEP_SUMMARY
echo "> $snapshot_url" >> $GITHUB_STEP_SUMMARY
fi
for f in $(echo "${{ steps.export-paths.outputs.summary_file_pattern }}")
do
echo "$(cat $f)" >> $GITHUB_STEP_SUMMARY
done
# Handle exit code: 0 ok, 1 alarms, 2 errors, these should exit ok - other codes should exit out with error code
if [[ $exit_code -le 2 ]];then
exit 0
else
exit $exit_code
fi
- name: Run Powerpipe benchmark
if: steps.get-benchmarks.outputs.run_list != ''
id: run-benchmarks
shell: bash
env:
STEAMPIPE_CHECK_DISPLAY_WIDTH: "120"
STEAMPIPE_DISPLAY_WIDTH: "120"
run: |
set +e
# Set outputs for artifact naming
echo "uuid=$(echo $RANDOM | md5sum | head -c 20)" >> $GITHUB_OUTPUT
echo "mod=$(echo ${{ inputs.mod-url }} | rev | cut -d '/' -f 1 | rev)" >> $GITHUB_OUTPUT
# Set workflow_id / run_url for later use
workflow_id=$(echo $GITHUB_WORKFLOW | sed -e "s/ /_/g")
run_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
# Clear snapshot flags from additional args (should be via input)
additional_args=$(echo "${{ inputs.additional-args }}" | sed -e "s/--share//g" | sed -e "s/--snapshot//g")
# Allow override for output if set in additional-args
benchmark_output="--output=brief"
if [[ "${{ inputs.additional-args }}" == *"--output"* ]]; then
benchmark_output=""
fi
# Set export types
benchmark_exports="${{ steps.export-paths.outputs.benchmark_exports }}"
# Snapshot setup
snapshot_type=""
snapshot_tags="--snapshot-tag github_repo=$GITHUB_REPOSITORY --snapshot-tag github_run_url=$run_url --snapshot-tag github_workflow=$workflow_id"
pipes_inputs=""
if [[ -n "${{ inputs.pipes-token }}" ]];then
pipes_inputs="--pipes-host pipes.turbot.com --pipes-token ${{ inputs.pipes-token }}"
fi
if [ "${{ inputs.pipes-snapshot-visibility }}" == "anyone_with_link" ];then
snapshot_type="--share $snapshot_tags"
elif [ "${{ inputs.pipes-snapshot-visibility }}" == "workspace" ];then
snapshot_type="--snapshot $snapshot_tags"
elif [[ -n "${{ inputs.pipes-snapshot-visibility }}" ]];then
echo "Error: invalid value for input 'snapshot-visibility' provided: ${{ inputs.pipes-snapshot-visibility }} - valid values are 'anyone_with_link' and 'workspace'"
exit 3
fi
# Run benchmark command
if [[ -n "${{ steps.get-database.outputs.database }}" ]]; then
powerpipe benchmark run ${{ steps.get-benchmarks.outputs.run_list }} $benchmark_output $benchmark_exports --database=${{ steps.get-database.outputs.database }} --mod-location=${{ steps.make-temp-dir.outputs.tmp-mod-dir }} $snapshot_type $pipes_inputs $additional_args | tee /tmp/output.log
else
powerpipe benchmark run ${{ steps.get-benchmarks.outputs.run_list }} $benchmark_output $benchmark_exports --mod-location=${{ steps.make-temp-dir.outputs.tmp-mod-dir }} $snapshot_type $pipes_inputs $additional_args | tee /tmp/output.log
fi
exit_code=$?
# Write summary
snapshot_url=$(cat /tmp/output.log | grep "Snapshot uploaded" | sed -e "s/^Snapshot uploaded to //")
if [ -n "$snapshot_url" ];then
echo "> Turbot Pipes ${{ inputs.pipes-snapshot-visibility }} snapshot(s)" >> $GITHUB_STEP_SUMMARY
echo "> $snapshot_url" >> $GITHUB_STEP_SUMMARY
fi
for f in $(echo "${{ steps.export-paths.outputs.summary_file_pattern }}")
do
echo "$(cat $f)" >> $GITHUB_STEP_SUMMARY
done
# Handle exit code: 0 ok, 1 alarms, 2 errors, these should exit ok - other codes should exit out with error code
if [[ $exit_code -le 2 ]];then
exit 0
else
exit $exit_code
fi
- name: Upload artifacts
id: upload-artifacts
if: steps.export-paths.outputs.artifact_paths != ''
continue-on-error: true
uses: actions/upload-artifact@v3
env:
GITHUB_TOKEN: ${{ inputs.github-token }}
with:
name: ${{ steps.run-controls.outputs.mod }}-${{ steps.run-controls.outputs.uuid }}
path: |
${{ steps.export-paths.outputs.artifact_paths }}
- name: Setup Node
uses: actions/setup-node@v4
- name: Create annotations(controls)
shell: bash
run: |
node ${{ github.action_path }}/dist/index.js ${{ steps.get-controls.outputs.run_list }}
env:
GITHUB_TOKEN: ${{ inputs.github-token }}
- name: Create annotations(benchmarks)
shell: bash
run: |
node ${{ github.action_path }}/dist/index.js ${{ steps.get-benchmarks.outputs.run_list }}
env:
GITHUB_TOKEN: ${{ inputs.github-token }}