-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchittycheck-logger.sh
More file actions
252 lines (213 loc) Β· 8.45 KB
/
chittycheck-logger.sh
File metadata and controls
252 lines (213 loc) Β· 8.45 KB
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
#!/bin/bash
# ChittyCheck Central Logging System
# Reports compliance violations to central registry for systematic tracking
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
RESET='\033[0m'
# Central logging configuration
CHITTYCHECK_LOG_DIR="$HOME/.chittyos/compliance-logs"
CHITTYCHECK_CENTRAL_LOG="$CHITTYCHECK_LOG_DIR/central.jsonl"
CHITTYCHECK_REGISTRY_ENDPOINT="${REGISTRY_SERVICE:-https://registry.chitty.cc}/api/v1/compliance"
# Ensure log directory exists
mkdir -p "$CHITTYCHECK_LOG_DIR"
# Function to log compliance results locally and centrally
log_compliance_result() {
local project_name="$1"
local project_path="$2"
local violations="$3"
local warnings="$4"
local passed="$5"
local critical_violations="$6" # JSON array as string
local rogue_patterns_detail="$7" # Detailed pattern analysis
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
local session_id="${CLAUDE_SESSION_ID:-$(uuidgen)}"
local compliance_score=0
local total_checks=$((violations + warnings + passed))
if [ $total_checks -gt 0 ]; then
compliance_score=$(( (passed * 100) / total_checks ))
fi
# Create compliance record
local compliance_record=$(cat <<EOF
{
"timestamp": "$timestamp",
"session_id": "$session_id",
"project": {
"name": "$project_name",
"path": "$project_path"
},
"compliance": {
"score": $compliance_score,
"violations": $violations,
"warnings": $warnings,
"passed": $passed,
"total_checks": $total_checks,
"grade": "$(get_compliance_grade $compliance_score)"
},
"critical_violations": $critical_violations,
"rogue_patterns": $rogue_patterns_detail,
"environment": {
"framework_version": "${CHITTYOS_FRAMEWORK_VERSION:-1.0.1}",
"has_token": $([ -n "$CHITTY_ID_TOKEN" ] && [ "$CHITTY_ID_TOKEN" != "YOUR_TOKEN_HERE_REPLACE_ME" ] && echo "true" || echo "false"),
"git_branch": "$(git branch --show-current 2>/dev/null || echo 'none')"
},
"system": {
"hostname": "$(hostname)",
"user": "$(whoami)",
"os": "$(uname -s)",
"chittyos_account": "${CHITTYOS_ACCOUNT_ID:-unknown}"
}
}
EOF
)
# Log locally
echo "$compliance_record" >> "$CHITTYCHECK_CENTRAL_LOG"
# Report to central registry (async)
report_to_registry "$compliance_record" &
# Generate daily summary if needed
check_daily_summary
echo -e "${CYAN}π Logged compliance result: ${compliance_score}% (${violations}v/${warnings}w)${RESET}" >&2
}
# Function to get compliance grade
get_compliance_grade() {
local score=$1
if [ $score -ge 95 ]; then echo "A+"
elif [ $score -ge 90 ]; then echo "A"
elif [ $score -ge 85 ]; then echo "B+"
elif [ $score -ge 80 ]; then echo "B"
elif [ $score -ge 75 ]; then echo "C+"
elif [ $score -ge 70 ]; then echo "C"
elif [ $score -ge 65 ]; then echo "D"
else echo "F"
fi
}
# Function to report to central registry
report_to_registry() {
local compliance_record="$1"
if [ -n "$CHITTY_ID_TOKEN" ] && [ "$CHITTY_ID_TOKEN" != "YOUR_TOKEN_HERE_REPLACE_ME" ]; then
local response=$(curl -s -X POST "$CHITTYCHECK_REGISTRY_ENDPOINT/report" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $CHITTY_ID_TOKEN" \
-H "X-ChittyOS-Account: $CHITTYOS_ACCOUNT_ID" \
-d "$compliance_record" 2>/dev/null)
if [ $? -eq 0 ]; then
echo -e "${GREEN}β
Reported to central registry${RESET}" >&2
else
echo -e "${YELLOW}β οΈ Could not report to registry (offline/unreachable)${RESET}" >&2
fi
fi
}
# Function to analyze rogue patterns in detail
analyze_rogue_patterns() {
local patterns_json="[]"
# Define rogue patterns with descriptions
declare -A rogue_patterns_map
rogue_patterns_map["crypto\.randomBytes.*toString"]="Crypto.randomBytes with toString (insecure)"
rogue_patterns_map["crypto\.randomUUID"]="Crypto.randomUUID (non-deterministic)"
rogue_patterns_map["Date\.now\(\).*id"]="Date.now() for ID generation (time-based)"
rogue_patterns_map["Math\.random.*toString"]="Math.random with toString (predictable)"
rogue_patterns_map["nanoid\(\)"]="NanoID library (external dependency)"
rogue_patterns_map["\+\+.*[iI][dD]"]="Incremental ID pattern (non-unique)"
rogue_patterns_map["generateId\|generateID\|genId\|makeId"]="Local ID generation functions"
rogue_patterns_map["uuid\.v[0-9]"]="UUID library (non-ChittyID)"
local pattern_details="["
local first=true
for pattern in "${!rogue_patterns_map[@]}"; do
local count=$(grep -r "$pattern" --include="*.js" --include="*.ts" --include="*.py" . 2>/dev/null | grep -v node_modules | wc -l | tr -d ' ')
if [ "$count" -gt 0 ]; then
[ "$first" = false ] && pattern_details="${pattern_details},"
pattern_details="${pattern_details}{\"pattern\":\"$pattern\",\"description\":\"${rogue_patterns_map[$pattern]}\",\"count\":$count,\"files\":$(get_pattern_files "$pattern")}"
first=false
fi
done
pattern_details="${pattern_details}]"
echo "$pattern_details"
}
# Function to get files containing a pattern
get_pattern_files() {
local pattern="$1"
local files=$(grep -r -l "$pattern" --include="*.js" --include="*.ts" --include="*.py" . 2>/dev/null | grep -v node_modules | head -10)
local files_json="["
local first=true
while IFS= read -r file; do
[ -z "$file" ] && continue
[ "$first" = false ] && files_json="${files_json},"
files_json="${files_json}\"$file\""
first=false
done <<< "$files"
files_json="${files_json}]"
echo "$files_json"
}
# Function to check if daily summary is needed
check_daily_summary() {
local today=$(date +"%Y-%m-%d")
local summary_file="$CHITTYCHECK_LOG_DIR/daily-${today}.json"
if [ ! -f "$summary_file" ]; then
generate_daily_summary "$today" > "$summary_file"
fi
}
# Function to generate daily summary
generate_daily_summary() {
local date="$1"
local today_logs=$(grep "\"$date" "$CHITTYCHECK_CENTRAL_LOG" 2>/dev/null)
if [ -z "$today_logs" ]; then
echo '{"date":"'$date'","summary":"No compliance checks today"}'
return
fi
local total_checks=$(echo "$today_logs" | wc -l)
local failed_checks=$(echo "$today_logs" | grep '"violations":[1-9]' | wc -l)
local avg_score=$(echo "$today_logs" | jq -s 'map(.compliance.score) | add / length' 2>/dev/null || echo "0")
cat <<EOF
{
"date": "$date",
"summary": {
"total_projects_checked": $total_checks,
"failed_projects": $failed_checks,
"average_compliance_score": $avg_score,
"generated_at": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
},
"top_violations": $(echo "$today_logs" | jq -s '[.[].critical_violations[]] | group_by(.) | map({violation: .[0], count: length}) | sort_by(-.count) | .[0:5]' 2>/dev/null || echo '[]')
}
EOF
}
# Function to show compliance trends
show_compliance_trends() {
echo -e "${CYAN}${BOLD}π COMPLIANCE TRENDS${RESET}"
echo -e "ββββββββββββββββββββββββββββββββββββ"
if [ ! -f "$CHITTYCHECK_CENTRAL_LOG" ]; then
echo "No compliance data available"
return
fi
local total_records=$(wc -l < "$CHITTYCHECK_CENTRAL_LOG")
local recent_records=$(tail -10 "$CHITTYCHECK_CENTRAL_LOG" | jq -s 'map(.compliance.score) | add / length' 2>/dev/null || echo "0")
local trend_direction="π"
echo "Total compliance checks: $total_records"
echo "Recent average score: ${recent_records}%"
echo "Log location: $CHITTYCHECK_CENTRAL_LOG"
echo ""
echo "Recent violations by type:"
tail -20 "$CHITTYCHECK_CENTRAL_LOG" | jq -r '.critical_violations[]?' 2>/dev/null | sort | uniq -c | sort -nr | head -5
echo ""
echo "Daily summaries available:"
ls -1 "$CHITTYCHECK_LOG_DIR"/daily-*.json 2>/dev/null | wc -l | xargs echo
}
# Export functions for use in chittycheck
export -f log_compliance_result
export -f analyze_rogue_patterns
export -f show_compliance_trends
# If called directly, show trends
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
case "$1" in
"trends"|"")
show_compliance_trends
;;
"summary")
generate_daily_summary "$(date +%Y-%m-%d)"
;;
*)
echo "Usage: $0 [trends|summary]"
;;
esac
fi