Files
zyt/identity-admin/scripts/enable-kefu-metadata-local.ps1
T
2026-11-11 13:53:14 +08:00

28 lines
1.9 KiB
PowerShell

$ErrorActionPreference='Stop'
$projectRoot=[IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..'))
$runtime=[IO.Path]::GetFullPath((Join-Path $projectRoot 'server/runtime'))
$keyPath=[IO.Path]::GetFullPath((Join-Path $runtime 'kefu-metadata.key'))
if(-not $keyPath.StartsWith($runtime+[IO.Path]::DirectorySeparatorChar,[StringComparison]::OrdinalIgnoreCase)){throw '只读同步密钥路径越界'}
New-Item -ItemType Directory -Path $runtime -Force | Out-Null
if(-not(Test-Path -LiteralPath $keyPath)){
$key=[Convert]::ToHexString([Security.Cryptography.RandomNumberGenerator]::GetBytes(32)).ToLowerInvariant()
$stream=[IO.File]::Open($keyPath,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::None)
try{$bytes=[Text.Encoding]::UTF8.GetBytes($key);$stream.Write($bytes,0,$bytes.Length)}finally{$stream.Dispose()}
$key=$null;$bytes=$null
}
$value=[IO.File]::ReadAllText($keyPath)
if($value -cnotmatch '^[a-f0-9]{64}$'){throw '现有只读同步密钥格式无效,未覆盖'}
$value=$null
# Only this Windows user and SYSTEM may read the machine credential.
$acl=[Security.AccessControl.FileSecurity]::new()
$acl.SetAccessRuleProtection($true,$false)
$identity=[Security.Principal.WindowsIdentity]::GetCurrent().User
foreach($sid in @($identity,[Security.Principal.SecurityIdentifier]::new('S-1-5-18'))){
$acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($sid,'FullControl','Allow'))
}
Set-Acl -LiteralPath $keyPath -AclObject $acl
$statePath=Join-Path $projectRoot '.test-runtime/kefu-metadata-local.json'
New-Item -ItemType Directory -Path ([IO.Path]::GetDirectoryName($statePath)) -Force | Out-Null
@{metadata_api_url='http://127.0.0.1:8766/api/v2';metadata_key_file=$keyPath} | ConvertTo-Json | Set-Content -LiteralPath $statePath -Encoding utf8
Write-Output '本机子系统只读同步已配置,密钥未输出;重载集团API与客服API后生效。'