-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-config.sh
More file actions
executable file
·416 lines (337 loc) · 14 KB
/
verify-config.sh
File metadata and controls
executable file
·416 lines (337 loc) · 14 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/bin/bash
#############################################################################
# SharePoint to SQL Sync Tool - Configuration Verification
#
# This script verifies that all required configuration is present and valid.
# It checks User Secrets (dev) and Environment Variables (prod) without
# exposing sensitive credentials.
#
# Usage: ./verify-config.sh [--env-vars]
# --env-vars: Check environment variables instead of user secrets
#############################################################################
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Flags
CHECK_ENV_VARS=false
# Project directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$SCRIPT_DIR/../ConsoleApp1Net8"
#############################################################################
# Helper Functions
#############################################################################
print_header() {
echo -e "${BLUE}"
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ SharePoint to SQL Sync - Config Verification ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠ $1${NC}"
}
print_error() {
echo -e "${RED}✗ $1${NC}"
}
print_info() {
echo -e "${BLUE}ℹ $1${NC}"
}
#############################################################################
# Configuration Checks
#############################################################################
check_required_config() {
local key="$1"
local value="$2"
local description="$3"
if [ -z "$value" ]; then
print_error "Missing: $description ($key)"
return 1
else
print_success "$description is set"
return 0
fi
}
validate_email() {
local email="$1"
if [[ "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
return 0
else
return 1
fi
}
validate_url() {
local url="$1"
if [[ "$url" =~ ^https?:// ]]; then
return 0
else
return 1
fi
}
validate_connection_string() {
local conn_str="$1"
local issues=0
# Check for Server
if [[ ! "$conn_str" =~ Server=.+ ]]; then
print_error " Missing 'Server=' in connection string"
issues=$((issues + 1))
fi
# Check for Database
if [[ ! "$conn_str" =~ Database=.+ ]]; then
print_error " Missing 'Database=' in connection string"
issues=$((issues + 1))
fi
# Check for authentication
if [[ ! "$conn_str" =~ "Integrated Security=true" ]] && \
[[ ! "$conn_str" =~ "User Id=".+ ]]; then
print_error " Missing authentication (need 'Integrated Security=true' or 'User Id=')"
issues=$((issues + 1))
fi
# Check for encryption
if [[ ! "$conn_str" =~ "Encrypt=true" ]]; then
print_warning " Missing 'Encrypt=true' (recommended for security)"
fi
# Check for TrustServerCertificate
if [[ "$conn_str" =~ "TrustServerCertificate=true" ]]; then
print_warning " 'TrustServerCertificate=true' is not recommended for production"
fi
return $issues
}
#############################################################################
# User Secrets Verification
#############################################################################
verify_user_secrets() {
print_info "Checking User Secrets configuration..."
echo ""
if [ ! -f "$PROJECT_DIR/ConsoleApp1Net8.csproj" ]; then
print_error "Project file not found: $PROJECT_DIR/ConsoleApp1Net8.csproj"
return 1
fi
cd "$PROJECT_DIR"
# Check if user secrets are initialized
if ! dotnet user-secrets list &> /dev/null; then
print_error "User Secrets not initialized"
print_info "Run: ./scripts/setup-secrets.sh"
cd - > /dev/null
return 1
fi
# Get all secrets
local secrets=$(dotnet user-secrets list 2>&1)
if [ -z "$secrets" ] || [[ "$secrets" == "No secrets configured"* ]]; then
print_error "No secrets configured"
print_info "Run: ./scripts/setup-secrets.sh"
cd - > /dev/null
return 1
fi
# Parse secrets into associative array
declare -A config
while IFS='=' read -r key value; do
# Trim whitespace
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs)
config["$key"]="$value"
done <<< "$secrets"
local errors=0
# Check SharePoint configuration
echo -e "${BLUE}SharePoint Configuration:${NC}"
check_required_config "SharePoint:Username" "${config[SharePoint:Username]}" "SharePoint Username" || errors=$((errors + 1))
if [ -n "${config[SharePoint:Username]}" ]; then
if validate_email "${config[SharePoint:Username]}"; then
print_success " Email format is valid"
else
print_warning " Email format may be invalid"
fi
fi
check_required_config "SharePoint:Password" "${config[SharePoint:Password]}" "SharePoint Password" || errors=$((errors + 1))
if [ -n "${config[SharePoint:Password]}" ]; then
local pwd_len=${#config[SharePoint:Password]}
if [ $pwd_len -lt 8 ]; then
print_warning " Password is less than 8 characters"
else
print_success " Password length is adequate ($pwd_len characters)"
fi
fi
check_required_config "SharePoint:SiteUrl" "${config[SharePoint:SiteUrl]}" "SharePoint Site URL" || errors=$((errors + 1))
if [ -n "${config[SharePoint:SiteUrl]}" ]; then
if validate_url "${config[SharePoint:SiteUrl]}"; then
print_success " URL format is valid"
else
print_error " URL must start with https://"
errors=$((errors + 1))
fi
fi
# Check SQL configuration
echo ""
echo -e "${BLUE}SQL Server Configuration:${NC}"
check_required_config "Sql:ConnectionString" "${config[Sql:ConnectionString]}" "SQL Connection String" || errors=$((errors + 1))
if [ -n "${config[Sql:ConnectionString]}" ]; then
validate_connection_string "${config[Sql:ConnectionString]}" || errors=$((errors + $?))
fi
cd - > /dev/null
return $errors
}
#############################################################################
# Environment Variables Verification
#############################################################################
verify_env_vars() {
print_info "Checking Environment Variables configuration..."
echo ""
local errors=0
# Check SharePoint configuration
echo -e "${BLUE}SharePoint Configuration:${NC}"
check_required_config "SPO2SQL_SharePoint__Username" "$SPO2SQL_SharePoint__Username" "SharePoint Username" || errors=$((errors + 1))
if [ -n "$SPO2SQL_SharePoint__Username" ]; then
if validate_email "$SPO2SQL_SharePoint__Username"; then
print_success " Email format is valid"
else
print_warning " Email format may be invalid"
fi
fi
check_required_config "SPO2SQL_SharePoint__Password" "$SPO2SQL_SharePoint__Password" "SharePoint Password" || errors=$((errors + 1))
if [ -n "$SPO2SQL_SharePoint__Password" ]; then
local pwd_len=${#SPO2SQL_SharePoint__Password}
if [ $pwd_len -lt 8 ]; then
print_warning " Password is less than 8 characters"
else
print_success " Password length is adequate ($pwd_len characters)"
fi
fi
check_required_config "SPO2SQL_SharePoint__SiteUrl" "$SPO2SQL_SharePoint__SiteUrl" "SharePoint Site URL" || errors=$((errors + 1))
if [ -n "$SPO2SQL_SharePoint__SiteUrl" ]; then
if validate_url "$SPO2SQL_SharePoint__SiteUrl"; then
print_success " URL format is valid"
else
print_error " URL must start with https://"
errors=$((errors + 1))
fi
fi
# Check SQL configuration
echo ""
echo -e "${BLUE}SQL Server Configuration:${NC}"
check_required_config "SPO2SQL_Sql__ConnectionString" "$SPO2SQL_Sql__ConnectionString" "SQL Connection String" || errors=$((errors + 1))
if [ -n "$SPO2SQL_Sql__ConnectionString" ]; then
validate_connection_string "$SPO2SQL_Sql__ConnectionString" || errors=$((errors + $?))
fi
return $errors
}
#############################################################################
# appsettings.json Verification
#############################################################################
verify_appsettings() {
print_info "Checking appsettings.json..."
echo ""
local appsettings="$PROJECT_DIR/appsettings.json"
if [ ! -f "$appsettings" ]; then
print_error "appsettings.json not found at: $appsettings"
return 1
fi
print_success "appsettings.json found"
# Check for secrets in appsettings.json (security check)
local has_secrets=0
if grep -qi '"Username"[[:space:]]*:[[:space:]]*"[^"]\+@' "$appsettings" 2>/dev/null; then
print_error "⚠ SECURITY: Username found in appsettings.json - should be in User Secrets!"
has_secrets=1
fi
if grep -qi '"Password"[[:space:]]*:[[:space:]]*"[^"]\+' "$appsettings" 2>/dev/null; then
local pwd_value=$(grep -oi '"Password"[[:space:]]*:[[:space:]]*"[^"]*"' "$appsettings" | cut -d'"' -f4)
if [ -n "$pwd_value" ]; then
print_error "⚠ SECURITY: Password found in appsettings.json - should be in User Secrets!"
has_secrets=1
fi
fi
if grep -qi '"ConnectionString"[[:space:]]*:[[:space:]]*"Server=' "$appsettings" 2>/dev/null; then
print_error "⚠ SECURITY: Connection string found in appsettings.json - should be in User Secrets!"
has_secrets=1
fi
if [ $has_secrets -eq 0 ]; then
print_success "No secrets found in appsettings.json (good!)"
fi
return $has_secrets
}
#############################################################################
# Main Flow
#############################################################################
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
--env-vars)
CHECK_ENV_VARS=true
shift
;;
--help|-h)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --env-vars Check environment variables instead of user secrets"
echo " --help, -h Show this help message"
echo ""
echo "Examples:"
echo " $0 # Verify user secrets (development)"
echo " $0 --env-vars # Verify environment variables (production)"
exit 0
;;
*)
print_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
}
main() {
parse_args "$@"
print_header
local total_errors=0
# Check appsettings.json
verify_appsettings || total_errors=$((total_errors + $?))
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# Check configuration source
if [ "$CHECK_ENV_VARS" = true ]; then
print_info "Mode: Production (Environment Variables)"
echo ""
verify_env_vars || total_errors=$((total_errors + $?))
else
print_info "Mode: Development (User Secrets)"
echo ""
verify_user_secrets || total_errors=$((total_errors + $?))
fi
# Summary
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
if [ $total_errors -eq 0 ]; then
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN}✓ Configuration is valid!${NC}"
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
print_info "You can now run the application:"
echo " cd ConsoleApp1Net8 && dotnet run"
return 0
else
echo -e "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${RED}✗ Configuration has $total_errors error(s)${NC}"
echo -e "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
print_info "To fix configuration:"
if [ "$CHECK_ENV_VARS" = true ]; then
echo " - Set missing environment variables with SPO2SQL_ prefix"
echo " - Use double underscores __ for nested sections"
echo " Example: export SPO2SQL_SharePoint__Username=\"user@company.com\""
else
echo " - Run: ./scripts/setup-secrets.sh"
echo " - Or manually: dotnet user-secrets set \"<key>\" \"<value>\""
fi
return 1
fi
}
# Run main with all arguments
main "$@"
exit $?