analyze_configs.ps1 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Configuration Security Analysis Script
  2. $services = @(
  3. 'shop-recycle-account',
  4. 'shop-recycle-agent-pc-web',
  5. 'shop-recycle-async-web',
  6. 'shop-recycle-customer-wechat-web',
  7. 'shop-recycle-data-statistics',
  8. 'shop-recycle-dealdata-service',
  9. 'shop-recycle-dispatcher',
  10. 'shop-recycle-erp-pc-web',
  11. 'shop-recycle-gateway',
  12. 'shop-recycle-gateway-out',
  13. 'shop-recycle-gateway-out-upgrade',
  14. 'shop-recycle-import-web',
  15. 'shop-recycle-login-center',
  16. 'shop-recycle-marketer-pc-web',
  17. 'shop-recycle-merchant',
  18. 'shop-recycle-merchant-pc-web',
  19. 'shop-recycle-merchant-wechat-web',
  20. 'shop-recycle-msg',
  21. 'shop-recycle-order-center',
  22. 'shop-recycle-order-search',
  23. 'shop-recycle-oss-web',
  24. 'shop-recycle-out-web',
  25. 'shop-recycle-payment',
  26. 'shop-recycle-payment-web',
  27. 'shop-recycle-pis',
  28. 'shop-recycle-platform',
  29. 'shop-recycle-platform-pc-web',
  30. 'shop-recycle-sche',
  31. 'shop-recycle-store',
  32. 'shop-recycle-store-pc-web',
  33. 'shop-recycle-store-wechat-web',
  34. 'shop-recycle-wechat',
  35. 'shop-recycle-wechat-web',
  36. 'shop-recycle-ws-web'
  37. )
  38. $basePath = "d:\coding-area\devops\helm\conf"
  39. # Define sensitive information patterns
  40. $sensitivePatterns = @{
  41. 'database_password' = @(
  42. '^\s*password\s*:\s*(?!null|"")',
  43. '^\s*jdbc-password\s*:\s*',
  44. '^\s*datasource.*password\s*:\s*'
  45. )
  46. 'database_username' = @(
  47. '^\s*user(name)?\s*:\s*(?!null|"")',
  48. '^\s*jdbc-user(name)?\s*:\s*'
  49. )
  50. 'database_host' = @(
  51. '^\s*(host|server|url)\s*:\s*(?!null|"")',
  52. '^\s*jdbc-url\s*:\s*'
  53. )
  54. 'redis_password' = @(
  55. '^\s*redis.*password\s*:\s*(?!null|"")',
  56. '^\s*spring\.redis\.password\s*:\s*'
  57. )
  58. 'rabbitmq_password' = @(
  59. '^\s*rabbitmq.*password\s*:\s*(?!null|"")',
  60. '^\s*spring\.rabbitmq\.password\s*:\s*'
  61. )
  62. 'wechat_app_id' = @(
  63. '^\s*(app[_-]?id|appid|wechat[_-]?id)\s*:\s*(?!null|"")',
  64. 'wechat.*app[_-]?id\s*:\s*'
  65. )
  66. 'wechat_app_secret' = @(
  67. '^\s*(app[_-]?secret|secret|wechat[_-]?secret)\s*:\s*(?!null|"")',
  68. 'wechat.*secret\s*:\s*'
  69. )
  70. 'mongodb_password' = @(
  71. '^\s*mongodb.*password\s*:\s*(?!null|"")',
  72. '^\s*spring\.data\.mongodb\.password\s*:\s*'
  73. )
  74. 'mongodb_username' = @(
  75. '^\s*mongodb.*user(name)?\s*:\s*(?!null|"")',
  76. '^\s*spring\.data\.mongodb\.user(name)?\s*:\s*'
  77. )
  78. 'nacos_username' = @(
  79. '^\s*nacos.*user(name)?\s*:\s*(?!null|"")',
  80. 'nacos\.authentication\.user(name)?\s*:\s*'
  81. )
  82. 'nacos_password' = @(
  83. '^\s*nacos.*password\s*:\s*(?!null|"")',
  84. 'nacos\.authentication\.password\s*:\s*'
  85. )
  86. 'seata_password' = @(
  87. '^\s*seata.*password\s*:\s*(?!null|"")',
  88. 'seata.*password\s*:\s*'
  89. )
  90. 'api_key' = @(
  91. '^\s*(api[_-]?key|key)\s*:\s*(?!null|"")',
  92. 'secret[_-]?key\s*:\s*'
  93. )
  94. 'token' = @(
  95. '^\s*token\s*:\s*(?!null|"")',
  96. '^\s*access[_-]?token\s*:\s*'
  97. )
  98. }
  99. $analysisResults = @()
  100. $sensitiveInfoSummary = @{}
  101. foreach ($service in $services) {
  102. $ymlPath = "$basePath\$service\conf\application.yml"
  103. if (Test-Path $ymlPath) {
  104. $content = Get-Content $ymlPath -Raw
  105. $lines = $content -split "`n"
  106. $detectedSensitiveInfo = @{}
  107. $sensitiveFields = @()
  108. foreach ($line in $lines) {
  109. # Skip empty lines and comments
  110. if ($line.Trim() -eq '' -or $line.TrimStart().StartsWith('#')) {
  111. continue
  112. }
  113. # Check each sensitive information pattern
  114. foreach ($category in $sensitivePatterns.Keys) {
  115. $patterns = $sensitivePatterns[$category]
  116. foreach ($pattern in $patterns) {
  117. if ($line -match $pattern) {
  118. # Ensure this line is not a comment
  119. if (-not $line.TrimStart().StartsWith('#')) {
  120. if (-not $detectedSensitiveInfo.ContainsKey($category)) {
  121. $detectedSensitiveInfo[$category] = @()
  122. }
  123. $fieldName = ($line -split ':')[0].Trim()
  124. $detectedSensitiveInfo[$category] += $fieldName
  125. $sensitiveFields += $fieldName
  126. }
  127. }
  128. }
  129. }
  130. }
  131. $needsSecret = $detectedSensitiveInfo.Count -gt 0
  132. $result = [PSCustomObject]@{
  133. Service = $service
  134. NeedsSecret = $needsSecret
  135. SensitiveTypes = @($detectedSensitiveInfo.Keys)
  136. SensitiveFields = ($sensitiveFields | Select-Object -Unique)
  137. FieldCount = ($sensitiveFields | Select-Object -Unique).Count
  138. }
  139. $analysisResults += $result
  140. # Collect statistics for sensitive information types
  141. foreach ($type in $detectedSensitiveInfo.Keys) {
  142. if (-not $sensitiveInfoSummary.ContainsKey($type)) {
  143. $sensitiveInfoSummary[$type] = 0
  144. }
  145. $sensitiveInfoSummary[$type]++
  146. }
  147. }
  148. }
  149. # Output results
  150. Write-Host "========== SERVICE CONFIGURATION ANALYSIS REPORT ==========" -ForegroundColor Cyan
  151. Write-Host ""
  152. # Output each service
  153. foreach ($result in $analysisResults) {
  154. Write-Host "Service Name: $($result.Service)" -ForegroundColor Yellow
  155. $needsSecretStr = if($result.NeedsSecret) { "YES" } else { "NO" }
  156. $color = if($result.NeedsSecret) { 'Red' } else { 'Green' }
  157. Write-Host "Requires Secret: $needsSecretStr" -ForegroundColor $color
  158. if ($result.NeedsSecret) {
  159. Write-Host "Sensitive Info Types: $($result.SensitiveTypes -join ', ')"
  160. Write-Host "Sensitive Fields: $($result.SensitiveFields -join ', ')"
  161. Write-Host "Field Count: $($result.FieldCount)"
  162. }
  163. Write-Host ""
  164. }
  165. # Summary statistics
  166. Write-Host "========== SUMMARY REPORT ==========" -ForegroundColor Cyan
  167. $needsSecretCount = ($analysisResults | Where-Object { $_.NeedsSecret }).Count
  168. Write-Host "Total Services: $($services.Count)"
  169. Write-Host "Services Requiring Secret: $needsSecretCount"
  170. Write-Host "Services without Sensitive Info: $($services.Count - $needsSecretCount)"
  171. Write-Host ""
  172. Write-Host "========== SENSITIVE INFO TYPE STATISTICS ==========" -ForegroundColor Cyan
  173. foreach ($type in ($sensitiveInfoSummary.Keys | Sort-Object)) {
  174. Write-Host "$type : $($sensitiveInfoSummary[$type]) services"
  175. }
  176. Write-Host ""
  177. Write-Host "========== DETAILED ANALYSIS RESULTS (JSON FORMAT) ==========" -ForegroundColor Cyan
  178. $jsonOutput = $analysisResults | ConvertTo-Json
  179. Write-Host $jsonOutput
  180. # Save to file
  181. $outputPath = "d:\coding-area\devops\helm\config-analysis-report.json"
  182. $analysisResults | ConvertTo-Json | Out-File $outputPath -Encoding UTF8
  183. Write-Host ""
  184. Write-Host "Detailed report saved to: $outputPath" -ForegroundColor Green