|
|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set -e |
|
|
|
|
|
|
|
|
RED='\033[0;31m' |
|
|
GREEN='\033[0;32m' |
|
|
YELLOW='\033[1;33m' |
|
|
NC='\033[0m' |
|
|
|
|
|
echo -e "${GREEN}========================================${NC}" |
|
|
echo -e "${GREEN}Security Hardening Verification${NC}" |
|
|
echo -e "${GREEN}========================================${NC}" |
|
|
echo "" |
|
|
|
|
|
ISSUES_FOUND=0 |
|
|
|
|
|
|
|
|
echo -e "${YELLOW}1. Checking for hardcoded secrets...${NC}" |
|
|
|
|
|
SECRETS_FOUND=$(grep -r "password\|api_key\|secret\|token" backend/src/ \ |
|
|
--exclude-dir=__pycache__ \ |
|
|
--include="*.py" \ |
|
|
| grep -v "os.getenv\|environ\|Secret\|Validation\|#" \ |
|
|
|| true) |
|
|
|
|
|
if [ -n "$SECRETS_FOUND" ]; then |
|
|
echo -e "${RED}β Found hardcoded secrets:${NC}" |
|
|
echo "$SECRETS_FOUND" |
|
|
ISSUES_FOUND=$((ISSUES_FOUND + 1)) |
|
|
else |
|
|
echo -e "${GREEN}β No hardcoded secrets found${NC}" |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${YELLOW}2. Checking Kubernetes Secrets usage...${NC}" |
|
|
|
|
|
if kubectl get secrets -n phase-5 > /dev/null 2>&1; then |
|
|
SECRETS_COUNT=$(kubectl get secrets -n phase-5 --no-headers | wc -l) |
|
|
|
|
|
if [ "$SECRETS_COUNT" -gt 0 ]; then |
|
|
echo -e "${GREEN}β Found ${SECRETS_COUNT} Kubernetes secrets${NC}" |
|
|
|
|
|
|
|
|
IMPORTANT_SECRETS=("db-credentials" "ollama-config" "sendgrid-config") |
|
|
for secret in "${IMPORTANT_SECRETS[@]}"; do |
|
|
if kubectl get secret "$secret" -n phase-5 > /dev/null 2>&1; then |
|
|
echo -e "${GREEN} β Secret '$secret' exists${NC}" |
|
|
else |
|
|
echo -e "${YELLOW} β Secret '$secret' not found${NC}" |
|
|
fi |
|
|
done |
|
|
else |
|
|
echo -e "${RED}β No Kubernetes secrets found${NC}" |
|
|
ISSUES_FOUND=$((ISSUES_FOUND + 1)) |
|
|
fi |
|
|
else |
|
|
echo -e "${YELLOW}β Kubernetes cluster not accessible${NC}" |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${YELLOW}3. Checking TLS certificates...${NC}" |
|
|
|
|
|
if kubectl get certificates -n phase-5 > /dev/null 2>&1; then |
|
|
CERTS=$(kubectl get certificates -n phase-5 --no-headers | wc -l) |
|
|
|
|
|
if [ "$CERTS" -gt 0 ]; then |
|
|
echo -e "${GREEN}β Found ${CERTS} TLS certificates${NC}" |
|
|
|
|
|
|
|
|
kubectl get certificates -n phase-5 | while read name ready secret age; do |
|
|
if [ "$ready" == "True" ]; then |
|
|
echo -e "${GREEN} β Certificate '$name' is ready${NC}" |
|
|
else |
|
|
echo -e "${RED} β Certificate '$name' is not ready${NC}" |
|
|
ISSUES_FOUND=$((ISSUES_FOUND + 1)) |
|
|
fi |
|
|
done |
|
|
else |
|
|
echo -e "${RED}β No TLS certificates found${NC}" |
|
|
ISSUES_FOUND=$((ISSUES_FOUND + 1)) |
|
|
fi |
|
|
else |
|
|
echo -e "${YELLOW}β Kubernetes cluster not accessible${NC}" |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${YELLOW}4. Checking NetworkPolicies...${NC}" |
|
|
|
|
|
if kubectl get networkpolicies -n phase-5 > /dev/null 2>&1; then |
|
|
NETPOL_COUNT=$(kubectl get networkpolicies -n phase-5 --no-headers | wc -l) |
|
|
|
|
|
if [ "$NETPOL_COUNT" -gt 0 ]; then |
|
|
echo -e "${GREEN}β Found ${NETPOL_COUNT} NetworkPolicies${NC}" |
|
|
else |
|
|
echo -e "${YELLOW}β No NetworkPolicies found (recommended for security)${NC}" |
|
|
fi |
|
|
else |
|
|
echo -e "${YELLOW}β Kubernetes cluster not accessible${NC}" |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${YELLOW}5. Checking input validation...${NC}" |
|
|
|
|
|
VALIDATION_FILES=$(find backend/src/api -name "*.py" -exec grep -l "pydantic\|BaseModel\|Field\|validator" {} \;) |
|
|
|
|
|
if [ -n "$VALIDATION_FILES" ]; then |
|
|
echo -e "${GREEN}β Input validation found in:${NC}" |
|
|
echo "$VALIDATION_FILES" | while read file; do |
|
|
echo " - $file" |
|
|
done |
|
|
else |
|
|
echo -e "${RED}β No input validation found${NC}" |
|
|
ISSUES_FOUND=$((ISSUES_FOUND + 1)) |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${YELLOW}6. Checking SQL injection protection...${NC}" |
|
|
|
|
|
if grep -r "execute\|executemany" backend/src/ --include="*.py" | grep -v "session.execute\|text(" > /dev/null 2>&1; then |
|
|
echo -e "${YELLOW}β Found raw SQL execution - ensure parameters are used${NC}" |
|
|
else |
|
|
echo -e "${GREEN}β Using SQLAlchemy ORM (SQL injection protected)${NC}" |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${YELLOW}7. Checking CORS configuration...${NC}" |
|
|
|
|
|
if grep -r "CORSMiddleware\|allow_origins" backend/src/main.py > /dev/null 2>&1; then |
|
|
echo -e "${GREEN}β CORS is configured${NC}" |
|
|
|
|
|
|
|
|
if grep -r "allow_origins.*\*" backend/src/main.py > /dev/null 2>&1; then |
|
|
echo -e "${YELLOW}β CORS allows all origins (*) - consider restricting${NC}" |
|
|
fi |
|
|
else |
|
|
echo -e "${RED}β CORS not configured${NC}" |
|
|
ISSUES_FOUND=$((ISSUES_FOUND + 1)) |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${YELLOW}8. Checking rate limiting...${NC}" |
|
|
|
|
|
if grep -r "rate_limit\|RateLimiter\|slowapi" backend/src/ > /dev/null 2>&1; then |
|
|
echo -e "${GREEN}β Rate limiting is configured${NC}" |
|
|
else |
|
|
echo -e "${YELLOW}β No rate limiting found (consider implementing)${NC}" |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${YELLOW}9. Checking security headers...${NC}" |
|
|
|
|
|
if grep -r "X-Content-Type\|X-Frame\|CSP\|Strict-Transport" backend/src/ > /dev/null 2>&1; then |
|
|
echo -e "${GREEN}β Security headers are configured${NC}" |
|
|
else |
|
|
echo -e "${YELLOW}β Security headers not found (consider adding)${NC}" |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${YELLOW}10. Checking for dependency vulnerabilities...${NC}" |
|
|
|
|
|
if command -v safety &> /dev/null; then |
|
|
cd backend |
|
|
if safety check --json > /tmp/safety-report.json 2>&1; then |
|
|
echo -e "${GREEN}β No known vulnerabilities found${NC}" |
|
|
else |
|
|
echo -e "${RED}β Vulnerabilities found:${NC}" |
|
|
cat /tmp/safety-report.json |
|
|
ISSUES_FOUND=$((ISSUES_FOUND + 1)) |
|
|
fi |
|
|
cd .. |
|
|
else |
|
|
echo -e "${YELLOW}β 'safety' not installed - run: pip install safety${NC}" |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo -e "${GREEN}========================================${NC}" |
|
|
echo -e "${GREEN}Security Scan Summary${NC}" |
|
|
echo -e "${GREEN}========================================${NC}" |
|
|
|
|
|
if [ $ISSUES_FOUND -eq 0 ]; then |
|
|
echo -e "${GREEN}β All security checks passed!${NC}" |
|
|
exit 0 |
|
|
else |
|
|
echo -e "${RED}β Found ${ISSUES_FOUND} issues that need attention${NC}" |
|
|
exit 1 |
|
|
fi |
|
|
|