-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathentrypoint.sh
executable file
·177 lines (153 loc) · 8.49 KB
/
entrypoint.sh
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
#!/bin/bash
inputpath="$INPUT_PATH"
cliArguments="$INPUT_CLIARGUMENTS"
isHelmChart="$INPUT_ISHELMCHART"
helmArgs="$INPUT_HELMARGUMENTS"
isKustomization="$INPUT_ISKUSTOMIZATION"
kustomizeArgs="$INPUT_KUSTOMIZEARGUMENTS"
printf "datree version: "
datree version
printf "\n"
# enable recursive globbing (to support **/*.yaml for instance)
shopt -s globstar
if [ -z "$DATREE_TOKEN" ]; then
printf "No account token configured, see https://github.com/datreeio/action-datree for instructions\n"
exit 1
fi
EXIT_STATUS=0
EXIT_STATUS_REPORT=0
function create_report() {
EXIT_STATUS_REPORT="$?"
# Github step summary
RESULT_JSON_PATH="$HOME/.datree/lastPolicyCheck.json"
if [ ! -f "$RESULT_JSON_PATH" ]; then
echo ""
echo "No report lastPolicyCheck.json found!"
return
fi
PASSED_YAML=$(jq .evaluationSummary.passedYamlValidationCount "$RESULT_JSON_PATH")
PASSED_K8S=$(jq .evaluationSummary.k8sValidation "$RESULT_JSON_PATH" | awk -F[\"/] '{print $2}')
PASSED_POLICY=$(jq .evaluationSummary.passedPolicyValidationCount "$RESULT_JSON_PATH")
POLICY_NAME=$(jq .policySummary.policyName "$RESULT_JSON_PATH" | awk -F[\"\"] '{print $2}')
TOTAL_RULES=$(jq .policySummary.totalRulesInPolicy "$RESULT_JSON_PATH")
CONFIGS_COUNT=$(jq .evaluationSummary.configsCount "$RESULT_JSON_PATH")
FILES_COUNT=$(jq .evaluationSummary.filesCount "$RESULT_JSON_PATH")
PASSED=$(jq .policySummary.totalPassedCount "$RESULT_JSON_PATH")
FAILED_RULES=$(jq .policySummary.totalRulesFailed "$RESULT_JSON_PATH")
SKIPPED=$(jq .policySummary.totalSkippedRules "$RESULT_JSON_PATH")
FAILED_SCHEMA=$(jq ".k8sValidationResults | length" "$RESULT_JSON_PATH")
FAILED_YAML=$(jq ".yamlValidationResults | length" "$RESULT_JSON_PATH")
echo "<img src=\"https://raw.githubusercontent.com/datreeio/datree/main/images/datree_logo_color.svg\" width=\"350\"/> " >>"$GITHUB_STEP_SUMMARY"
echo "" >>"$GITHUB_STEP_SUMMARY"
echo "☸️ Want guardrails on your cluster as well? Try out our [admission webhook!](https://github.com/datreeio/admission-webhook-datree#datree-admission-webhook) ☸️ " >>"$GITHUB_STEP_SUMMARY"
echo "## Datree policy check results" >>"$GITHUB_STEP_SUMMARY"
echo "**Source path:** ${1}" >>"$GITHUB_STEP_SUMMARY"
if [[ -n "$POLICY_NAME" ]]; then
echo "**Policy name:** "$POLICY_NAME"" >>"$GITHUB_STEP_SUMMARY"
fi
echo "" >>"$GITHUB_STEP_SUMMARY"
echo "**Passed YAML validation:** ${PASSED_YAML}/${FILES_COUNT}" >>"$GITHUB_STEP_SUMMARY"
echo "**Passed Kubernetes schema validation:** ${PASSED_K8S}/${FILES_COUNT}" >>"$GITHUB_STEP_SUMMARY"
echo "**Passed policy check:** ${PASSED_POLICY}/${FILES_COUNT}" >>"$GITHUB_STEP_SUMMARY"
if [[ -n "$POLICY_NAME" ]]; then
echo "| ✹✹ | ✹✹ |" >>"$GITHUB_STEP_SUMMARY"
echo "|---|---|" >>"$GITHUB_STEP_SUMMARY"
echo "| **Enabled rules in policy** | <div align=\"center\">**${TOTAL_RULES}**</div> |" >>"$GITHUB_STEP_SUMMARY"
echo "| **Configs tested against policy** | <div align=\"center\">**${CONFIGS_COUNT}**</div> |" >>"$GITHUB_STEP_SUMMARY"
echo "| **Total rules evaluated** | <div align=\"center\">**$((TOTAL_RULES * FILES_COUNT))**</div> |" >>"$GITHUB_STEP_SUMMARY"
echo "| **Total rules skipped** | <div align=\"center\">**${SKIPPED}**</div> |" >>"$GITHUB_STEP_SUMMARY"
echo "| **Total rules failed** ⛔ | <div align=\"center\">**${FAILED_RULES}**</div> |" >>"$GITHUB_STEP_SUMMARY"
echo "| **Total rules passed** ✅ | <div align=\"center\">**${PASSED}**</div> |" >>"$GITHUB_STEP_SUMMARY"
echo "| **See all rules in policy** | <div align=\"center\">**[https://app.datree.io](https://app.datree.io)**</div> |" >>"$GITHUB_STEP_SUMMARY"
echo "" >>"$GITHUB_STEP_SUMMARY"
echo "" >>"$GITHUB_STEP_SUMMARY"
fi
if [[ $FAILED_YAML -eq 0 && $FAILED_SCHEMA -eq 0 && $FAILED_RULES -eq 0 ]]; then
echo "### 🥳 All validations passed successfully! 🥳" >>"$GITHUB_STEP_SUMMARY"
return
fi
if [[ $FAILED_SCHEMA -gt 0 ]]; then
echo "### Schema validation errors:" >> "$GITHUB_STEP_SUMMARY"
for ((i = 0; i < "$FAILED_SCHEMA"; i++)); do
FILENAME=$(jq ".k8sValidationResults[$i].path" "$RESULT_JSON_PATH")
echo "**>> Filename: $FILENAME**" >> "$GITHUB_STEP_SUMMARY"
for ((j = 0; j < $(jq ".k8sValidationResults[$i].errors | length" "$RESULT_JSON_PATH"); j++)); do
ERROR=$(jq ".k8sValidationResults[$i].errors[$j].ErrorMessage" "$RESULT_JSON_PATH")
echo "❌ k8s schema validation error: $ERROR" >> "$GITHUB_STEP_SUMMARY"
done
done
echo "" >>"$GITHUB_STEP_SUMMARY"
echo "" >>"$GITHUB_STEP_SUMMARY"
fi
if [[ $FAILED_RULES -gt 0 ]]; then
echo "### Failed rules:" >>"$GITHUB_STEP_SUMMARY"
echo "" >>"$GITHUB_STEP_SUMMARY"
fi
NUM_OF_TESTED_FILES=$(jq ".policyValidationResults | length" "$RESULT_JSON_PATH")
INDEX=0
while [[ $INDEX -lt $NUM_OF_TESTED_FILES ]]; do
FILENAME=$(jq ".policyValidationResults[$INDEX] | .fileName" "$RESULT_JSON_PATH")
echo "**>> Filename: ${FILENAME}**" >>"$GITHUB_STEP_SUMMARY"
echo "" >>"$GITHUB_STEP_SUMMARY"
FAILED_IN_FILE=$(jq ".policyValidationResults[$INDEX] | .ruleResults | length" "$RESULT_JSON_PATH")
for ((i = 0; i < "$FAILED_IN_FILE"; i++)); do
IS_SKIPPED=$(jq ".policyValidationResults[0] | .ruleResults[$i] | .occurrencesDetails[0] | .isSkipped" "$RESULT_JSON_PATH")
if [[ $IS_SKIPPED == "true" ]]; then
continue
fi
VIOLATED_RULE_ID=$(jq ".policyValidationResults[$INDEX] | .ruleResults[$i] | .identifier" "$RESULT_JSON_PATH")
VIOLATED_RULE_NAME=$(jq ".policyValidationResults[$INDEX] | .ruleResults[$i] | .name" "$RESULT_JSON_PATH")
VIOLATED_RULE_OCCURRENCES=$(jq ".policyValidationResults[$INDEX] | .ruleResults[$i] | .occurrencesDetails | length" "$RESULT_JSON_PATH")
VIOLATED_RULE_FAIL_MESSAGE=$(jq ".policyValidationResults[$INDEX] | .ruleResults[$i] | .messageOnFailure" "$RESULT_JSON_PATH")
echo "❌ **$VIOLATED_RULE_NAME [$VIOLATED_RULE_OCCURRENCES occurrence/s]**" >>"$GITHUB_STEP_SUMMARY"
for ((j = 0; j < "$VIOLATED_RULE_OCCURRENCES"; j++)); do
VIOLATED_RULE_METADATA_NAME=$(jq ".policyValidationResults[0] | .ruleResults[$i] | .occurrencesDetails[$j] | .metadataName" "$RESULT_JSON_PATH")
VIOLATED_RULE_KIND=$(jq ".policyValidationResults[0] | .ruleResults[$i] | .occurrencesDetails[$j] | .kind" "$RESULT_JSON_PATH")
echo " metadata.name: $VIOLATED_RULE_METADATA_NAME (kind: $VIOLATED_RULE_KIND)" >>"$GITHUB_STEP_SUMMARY"
VIOLATED_RULE_SCHEMA_PATH=$(jq ".policyValidationResults[0] | .ruleResults[$i] | .occurrencesDetails[$j] | .failureLocations[0] | .schemaPath" "$RESULT_JSON_PATH")
VIOLATED_RULE_LINE=$(jq ".policyValidationResults[0] | .ruleResults[$i] | .occurrencesDetails[$j] | .failureLocations[0] | .failedErrorLine" "$RESULT_JSON_PATH")
VIOLATED_RULE_COLUMN=$(jq ".policyValidationResults[0] | .ruleResults[$i] | .occurrencesDetails[$j] | .failureLocations[0] | .failedErrorColumn" "$RESULT_JSON_PATH")
echo " \> key: $VIOLATED_RULE_SCHEMA_PATH (line: $VIOLATED_RULE_LINE:$VIOLATED_RULE_COLUMN)" >> $GITHUB_STEP_SUMMARY
done
echo "💡 $VIOLATED_RULE_FAIL_MESSAGE " >>"$GITHUB_STEP_SUMMARY"
echo "" >>"$GITHUB_STEP_SUMMARY"
echo "---" >>"$GITHUB_STEP_SUMMARY"
done
echo "" >>"$GITHUB_STEP_SUMMARY"
((INDEX = INDEX + 1))
done
}
if [ "$isHelmChart" = "true" ]; then
while read -r helmchart; do
dir="$(dirname "$helmchart")"
chart_type=`cat $helmchart|grep "type:"|cut -d' ' -f 2`
if [ "$chart_type" != "library" ]; then
echo "*** Proceeding to test Helm chart: $helmchart ***"
set +e
helm datree test "$dir" $cliArguments -- $helmArgs
create_report "$dir"
set -e
if [ "$EXIT_STATUS_REPORT" -gt "$EXIT_STATUS" ]; then
EXIT_STATUS="$EXIT_STATUS_REPORT"
fi
echo ""
else
echo "skipping check on $helmchart as it is of type library"
fi
done < <(find "$inputpath" -type f -name 'Chart.y*ml')
elif [ "$isKustomization" = "true" ]; then
datree kustomize test $inputpath $cliArguments -- $kustomizeArgs
create_report "$inputpath"
else
if [[ "$cliArguments" == *"sarif"* ]]; then
echo "A file named 'datree.sarif' will be created containing your test results"
datree test $inputpath $cliArguments > datree.sarif
else
datree test $inputpath $cliArguments
fi
create_report "$inputpath"
fi
if [ "$EXIT_STATUS_REPORT" -gt "$EXIT_STATUS" ]; then
EXIT_STATUS="$EXIT_STATUS_REPORT"
fi
exit $EXIT_STATUS