-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-secrets.ps1
More file actions
437 lines (361 loc) · 14.4 KB
/
setup-secrets.ps1
File metadata and controls
437 lines (361 loc) · 14.4 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#############################################################################
# SharePoint to SQL Sync Tool - User Secrets Setup (PowerShell)
#
# This script helps you configure User Secrets for development.
# It validates inputs and sets all required secrets securely.
#
# Usage: .\setup-secrets.ps1
#############################################################################
$ErrorActionPreference = "Stop"
# Project directory
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ProjectDir = Join-Path $ScriptDir "..\ConsoleApp1Net8"
#############################################################################
# Helper Functions
#############################################################################
function Write-Header {
Write-Host ""
Write-Host "╔════════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ SharePoint to SQL Sync - User Secrets Setup (Dev) ║" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
}
function Write-Success {
param([string]$Message)
Write-Host "✓ $Message" -ForegroundColor Green
}
function Write-Warning {
param([string]$Message)
Write-Host "⚠ $Message" -ForegroundColor Yellow
}
function Write-Error {
param([string]$Message)
Write-Host "✗ $Message" -ForegroundColor Red
}
function Write-Info {
param([string]$Message)
Write-Host "ℹ $Message" -ForegroundColor Cyan
}
#############################################################################
# Validation Functions
#############################################################################
function Test-Email {
param([string]$Email)
$emailRegex = '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return $Email -match $emailRegex
}
function Test-SharePointUrl {
param([string]$Url)
$urlRegex = '^https://[a-zA-Z0-9.-]+\.sharepoint\.com/sites/[a-zA-Z0-9._-]+$'
return $Url -match $urlRegex
}
function Test-SqlConnectionString {
param([string]$ConnectionString)
# Check for required components
if ($ConnectionString -notmatch 'Server=.+') {
Write-Error "Connection string must contain 'Server=' parameter"
return $false
}
if ($ConnectionString -notmatch 'Database=.+') {
Write-Error "Connection string must contain 'Database=' parameter"
return $false
}
# Check for authentication method
if (($ConnectionString -notmatch 'Integrated Security=true') -and
($ConnectionString -notmatch 'User Id=.+')) {
Write-Error "Connection string must contain either 'Integrated Security=true' or 'User Id=' for authentication"
return $false
}
# Check for encryption (security best practice)
if ($ConnectionString -notmatch 'Encrypt=true') {
Write-Warning "Connection string should contain 'Encrypt=true' for security"
$response = Read-Host "Continue anyway? (y/N)"
if ($response -ne 'y' -and $response -ne 'Y') {
return $false
}
}
return $true
}
function Test-DotnetSdk {
try {
$dotnetVersion = & dotnet --version
Write-Success ".NET SDK version $dotnetVersion found"
return $true
}
catch {
Write-Error ".NET SDK is not installed or not in PATH"
Write-Info "Install .NET 8 SDK from: https://dotnet.microsoft.com/download"
return $false
}
}
function Test-Project {
$projectFile = Join-Path $ProjectDir "ConsoleApp1Net8.csproj"
if (-not (Test-Path $projectFile)) {
Write-Error "Project file not found at: $projectFile"
Write-Info "Make sure you run this script from the scripts directory"
return $false
}
Write-Success "Project found: $ProjectDir"
return $true
}
#############################################################################
# User Secrets Management
#############################################################################
function Initialize-UserSecrets {
Write-Info "Initializing User Secrets..."
Push-Location $ProjectDir
try {
$null = & dotnet user-secrets list 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Warning "User Secrets already initialized"
}
else {
& dotnet user-secrets init
Write-Success "User Secrets initialized"
}
}
finally {
Pop-Location
}
}
function Get-CurrentSecrets {
Write-Info "Current User Secrets:"
Push-Location $ProjectDir
try {
$secrets = & dotnet user-secrets list 2>&1
if ($LASTEXITCODE -eq 0) {
if ([string]::IsNullOrWhiteSpace($secrets) -or $secrets -like "No secrets configured*") {
Write-Warning "No secrets currently configured"
}
else {
$secrets -split "`n" | ForEach-Object {
# Mask passwords
if ($_ -match 'Password') {
$_ -replace '=.*', '= ********'
}
else {
$_
}
} | Write-Host
}
}
else {
Write-Error "Failed to list secrets: $secrets"
}
}
finally {
Pop-Location
}
}
function Set-UserSecret {
param(
[string]$Key,
[string]$Value
)
Push-Location $ProjectDir
try {
$null = & dotnet user-secrets set $Key $Value 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Success "Set: $Key"
return $true
}
else {
Write-Error "Failed to set: $Key"
return $false
}
}
finally {
Pop-Location
}
}
#############################################################################
# Interactive Configuration
#############################################################################
function Set-SharePointConfiguration {
Write-Host ""
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host "SharePoint Configuration" -ForegroundColor Cyan
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
# SharePoint Username
do {
Write-Host ""
$spUsername = Read-Host "SharePoint Username (email)"
if ([string]::IsNullOrWhiteSpace($spUsername)) {
Write-Error "Username cannot be empty"
continue
}
if (-not (Test-Email $spUsername)) {
Write-Error "Invalid email format. Please enter a valid email address."
continue
}
break
} while ($true)
# SharePoint Password
do {
Write-Host ""
$spPasswordSecure = Read-Host "SharePoint Password" -AsSecureString
$spPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto(
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($spPasswordSecure))
if ([string]::IsNullOrWhiteSpace($spPassword)) {
Write-Error "Password cannot be empty"
continue
}
if ($spPassword.Length -lt 8) {
Write-Warning "Password is less than 8 characters. This may not meet security requirements."
$response = Read-Host "Continue anyway? (y/N)"
if ($response -ne 'y' -and $response -ne 'Y') {
continue
}
}
break
} while ($true)
# SharePoint Site URL
do {
Write-Host ""
$spUrl = Read-Host "SharePoint Site URL (e.g., https://company.sharepoint.com/sites/yoursite)"
if ([string]::IsNullOrWhiteSpace($spUrl)) {
Write-Error "Site URL cannot be empty"
continue
}
if (-not (Test-SharePointUrl $spUrl)) {
Write-Warning "URL format doesn't match expected pattern (https://xxx.sharepoint.com/sites/xxx)"
$response = Read-Host "Continue anyway? (y/N)"
if ($response -ne 'y' -and $response -ne 'Y') {
continue
}
}
break
} while ($true)
# Set secrets
Set-UserSecret "SharePoint:Username" $spUsername
Set-UserSecret "SharePoint:Password" $spPassword
Set-UserSecret "SharePoint:SiteUrl" $spUrl
}
function Set-SqlConfiguration {
Write-Host ""
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host "SQL Server Configuration" -ForegroundColor Cyan
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host ""
Write-Info "Choose authentication method:"
Write-Host " 1) Windows Integrated Security (Recommended for domain-joined dev machine)"
Write-Host " 2) SQL Server Authentication (Username/Password)"
Write-Host ""
do {
$authChoice = Read-Host "Enter choice (1 or 2)"
if ($authChoice -eq '1' -or $authChoice -eq '2') {
break
}
Write-Error "Invalid choice. Please select 1 or 2."
} while ($true)
# Server name
Write-Host ""
do {
$sqlServer = Read-Host "SQL Server name (e.g., localhost, myserver.database.windows.net)"
if (-not [string]::IsNullOrWhiteSpace($sqlServer)) {
break
}
Write-Error "Server name cannot be empty"
} while ($true)
# Database name
do {
$sqlDatabase = Read-Host "Database name"
if (-not [string]::IsNullOrWhiteSpace($sqlDatabase)) {
break
}
Write-Error "Database name cannot be empty"
} while ($true)
# Build connection string based on auth method
if ($authChoice -eq '1') {
$sqlConnStr = "Server=$sqlServer;Database=$sqlDatabase;Integrated Security=true;Encrypt=true;TrustServerCertificate=false;"
}
elseif ($authChoice -eq '2') {
do {
$sqlUser = Read-Host "SQL Username"
if (-not [string]::IsNullOrWhiteSpace($sqlUser)) {
break
}
Write-Error "Username cannot be empty"
} while ($true)
do {
$sqlPasswordSecure = Read-Host "SQL Password" -AsSecureString
$sqlPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto(
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sqlPasswordSecure))
if (-not [string]::IsNullOrWhiteSpace($sqlPassword)) {
break
}
Write-Error "Password cannot be empty"
} while ($true)
$sqlConnStr = "Server=$sqlServer;Database=$sqlDatabase;User Id=$sqlUser;Password=$sqlPassword;Encrypt=true;TrustServerCertificate=false;"
}
# Validate connection string
Write-Host ""
$maskedConnStr = $sqlConnStr -replace 'Password=[^;]*', 'Password=********'
Write-Info "Connection string: $maskedConnStr"
if (Test-SqlConnectionString $sqlConnStr) {
Set-UserSecret "Sql:ConnectionString" $sqlConnStr
}
else {
Write-Error "Connection string validation failed"
$response = Read-Host "Retry SQL configuration? (Y/n)"
if ($response -ne 'n' -and $response -ne 'N') {
Set-SqlConfiguration
}
}
}
#############################################################################
# Main Flow
#############################################################################
function Main {
Write-Header
# Pre-flight checks
if (-not (Test-DotnetSdk)) {
exit 1
}
if (-not (Test-Project)) {
exit 1
}
Write-Host ""
Write-Info "This script will help you configure User Secrets for development."
Write-Warning "User Secrets are stored locally and NOT committed to source control."
Write-Warning "For production, use Environment Variables instead."
Write-Host ""
$response = Read-Host "Continue with configuration? (Y/n)"
if ($response -eq 'n' -or $response -eq 'N') {
Write-Info "Configuration cancelled."
exit 0
}
# Initialize User Secrets
Initialize-UserSecrets
Write-Host ""
Get-CurrentSecrets
Write-Host ""
$response = Read-Host "Do you want to configure SharePoint settings? (Y/n)"
if ($response -ne 'n' -and $response -ne 'N') {
Set-SharePointConfiguration
}
Write-Host ""
$response = Read-Host "Do you want to configure SQL Server settings? (Y/n)"
if ($response -ne 'n' -and $response -ne 'N') {
Set-SqlConfiguration
}
# Summary
Write-Host ""
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Green
Write-Host "Configuration Complete!" -ForegroundColor Green
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Green
Write-Host ""
Get-CurrentSecrets
Write-Host ""
Write-Info "Next steps:"
Write-Host " 1. Verify configuration: .\scripts\verify-config.sh"
Write-Host " 2. Run the application: cd ConsoleApp1Net8; dotnet run"
Write-Host ""
Write-Info "To update secrets later:"
Write-Host " - Run this script again, or"
Write-Host " - Use: dotnet user-secrets set `"<key>`" `"<value>`""
Write-Host ""
Write-Success "Setup complete!"
}
# Run main
Main