-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_security_fix.sh
More file actions
282 lines (232 loc) Β· 7.44 KB
/
deploy_security_fix.sh
File metadata and controls
282 lines (232 loc) Β· 7.44 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
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
#!/bin/bash
# ===========================================
# SaaS Tenant Isolation Security Fix - Deployment Script
# Version: 1.0
# Date: May 5, 2026
# ===========================================
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
ENVIRONMENT=${1:-staging}
PROJECT_ROOT="/d/Projects/IMS-v2.0-19-01-2026"
BACKUP_DIR="$PROJECT_ROOT/backups/$(date +%Y%m%d_%H%M%S)"
LOG_FILE="$PROJECT_ROOT/deployment_$(date +%Y%m%d_%H%M%S).log"
# Logging function
log() {
echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" | tee -a "$LOG_FILE"
}
error() {
echo -e "${RED}[ERROR] $1${NC}" | tee -a "$LOG_FILE"
exit 1
}
success() {
echo -e "${GREEN}[SUCCESS] $1${NC}" | tee -a "$LOG_FILE"
}
warning() {
echo -e "${YELLOW}[WARNING] $1${NC}" | tee -a "$LOG_FILE"
}
# Pre-deployment checks
pre_deployment_checks() {
log "π Running pre-deployment checks..."
# Check if we're in the right directory
if [[ ! -d "$PROJECT_ROOT/server" ]]; then
error "Server directory not found. Are you in the correct project root?"
fi
# Check if required files exist
local required_files=(
"server/index.js"
"server/middleware/security/tenantIsolationValidation.js"
"server/middleware/auth.js"
"server/config/SECURITY_AUDIT.js"
)
for file in "${required_files[@]}"; do
if [[ ! -f "$PROJECT_ROOT/$file" ]]; then
error "Required file missing: $file"
fi
done
# Check Node.js version
local node_version=$(node --version | sed 's/v//')
if [[ "$(printf '%s\n' "$node_version" "16.0.0" | sort -V | head -n1)" != "16.0.0" ]]; then
warning "Node.js version $node_version detected. Recommended: 16+"
fi
# Check if server can start (syntax check)
log "Checking server syntax..."
if ! node -c "$PROJECT_ROOT/server/index.js" 2>/dev/null; then
error "Server has syntax errors. Fix before deployment."
fi
success "Pre-deployment checks passed"
}
# Create backup
create_backup() {
log "πΎ Creating backup..."
mkdir -p "$BACKUP_DIR"
# Backup critical files
local files_to_backup=(
"server/index.js"
"server/middleware/auth.js"
"server/middleware/security/tenantIsolationValidation.js"
"server/config/SECURITY_AUDIT.js"
"server/middleware/SaaS/globalTenantMiddleware.js"
)
for file in "${files_to_backup[@]}"; do
if [[ -f "$PROJECT_ROOT/$file" ]]; then
cp "$PROJECT_ROOT/$file" "$BACKUP_DIR/"
log "Backed up: $file"
fi
done
success "Backup created at: $BACKUP_DIR"
}
# Deploy to staging
deploy_to_staging() {
log "π Deploying to $ENVIRONMENT environment..."
cd "$PROJECT_ROOT"
# Install dependencies
log "Installing dependencies..."
if [[ -f "server/package.json" ]]; then
cd server
npm ci --production=false
cd ..
fi
# Run security tests
log "Running security tests..."
if [[ -f "server/package.json" ]] && grep -q "test" server/package.json; then
cd server
npm test -- --grep="security" || warning "Security tests failed or not found"
cd ..
fi
# Environment-specific configuration
case $ENVIRONMENT in
staging)
export NODE_ENV=staging
export PORT=3001
;;
production)
export NODE_ENV=production
export PORT=3000
;;
*)
warning "Unknown environment: $ENVIRONMENT. Using default settings."
;;
esac
success "Deployment to $ENVIRONMENT completed"
}
# Post-deployment verification
post_deployment_verification() {
log "β
Running post-deployment verification..."
cd "$PROJECT_ROOT/server"
# Test server startup
log "Testing server startup..."
timeout 10s node index.js > /dev/null 2>&1 &
local server_pid=$!
sleep 3
if kill -0 $server_pid 2>/dev/null; then
success "Server started successfully"
kill $server_pid
else
error "Server failed to start"
fi
# Verify middleware loading
log "Verifying middleware integration..."
if grep -q "tenantIsolationValidation" index.js; then
success "Tenant isolation validation middleware integrated"
else
error "Tenant isolation validation middleware not found in server"
fi
success "Post-deployment verification completed"
}
# Security monitoring setup
setup_monitoring() {
log "π Setting up security monitoring..."
# Create monitoring script
cat > "$PROJECT_ROOT/monitor_security.sh" << 'EOF'
#!/bin/bash
# Security Monitoring Script for Tenant Isolation
LOG_FILE="/var/log/ims_security.log"
ALERT_EMAIL="security@imsmymunc.com"
echo "π Starting security monitoring..."
# Monitor for tenant isolation breaches
if grep -i "TENANT ISOLATION BREACH" "$LOG_FILE" | tail -5; then
echo "π¨ TENANT ISOLATION BREACH DETECTED!"
# Send alert email (implement based on your email system)
# mail -s "SECURITY ALERT: Tenant Isolation Breach" "$ALERT_EMAIL"
fi
# Monitor for authentication failures
AUTH_FAILURES=$(grep -c "Authentication failed\|Invalid token\|Token expired" "$LOG_FILE")
if [ "$AUTH_FAILURES" -gt 10 ]; then
echo "β οΈ High authentication failure rate detected: $AUTH_FAILURES"
fi
# Monitor for rate limiting
RATE_LIMITS=$(grep -c "Rate limit exceeded" "$LOG_FILE")
if [ "$RATE_LIMITS" -gt 5 ]; then
echo "β οΈ High rate limiting detected: $RATE_LIMITS"
fi
echo "β
Security monitoring completed"
EOF
chmod +x "$PROJECT_ROOT/monitor_security.sh"
success "Security monitoring script created: monitor_security.sh"
}
# Rollback function
rollback() {
warning "π Starting rollback..."
if [[ ! -d "$BACKUP_DIR" ]]; then
error "No backup found for rollback"
fi
# Restore files
local files_to_restore=(
"server/index.js"
"server/middleware/auth.js"
"server/middleware/security/tenantIsolationValidation.js"
"server/config/SECURITY_AUDIT.js"
"server/middleware/SaaS/globalTenantMiddleware.js"
)
for file in "${files_to_restore[@]}"; do
if [[ -f "$BACKUP_DIR/$(basename "$file")" ]]; then
cp "$BACKUP_DIR/$(basename "$file")" "$PROJECT_ROOT/$file"
log "Restored: $file"
fi
done
success "Rollback completed"
}
# Main deployment flow
main() {
log "π Starting SaaS Security Fix Deployment"
log "Environment: $ENVIRONMENT"
log "Project Root: $PROJECT_ROOT"
log "Log File: $LOG_FILE"
case ${2:-deploy} in
deploy)
pre_deployment_checks
create_backup
deploy_to_staging
post_deployment_verification
setup_monitoring
;;
rollback)
rollback
;;
test)
pre_deployment_checks
post_deployment_verification
;;
*)
error "Usage: $0 [environment] [deploy|rollback|test]"
;;
esac
success "π Deployment process completed successfully!"
log "π Next steps:"
log " 1. Monitor logs for tenant isolation breaches"
log " 2. Run security tests: ./monitor_security.sh"
log " 3. Check application health"
log " 4. Notify team about deployment"
}
# Run main function with error handling
if [[ $# -eq 0 ]]; then
main staging
else
main "$@"
fi