跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

initialize-windows-test-accounts.ps1

120 行 · 7.3 KB历史下载
1#requires -Version 5.1
2<#
3.SYNOPSIS
4Create isolated QA accounts after an authorized guest administrator completes Windows OOBE.
5.DESCRIPTION
6Supply a guest administrator credential with Get-Credential. Both new accounts
7get independent random passwords. On Windows, Export-Clixml protects the saved
8credentials with DPAPI for this host user. Use the returned AdminCredentialFile
9with Import-Clixml and New-PSSession -VMId for later PowerShell Direct sessions.
10The standard user is intended for the interactive Claude Desktop session.
11#>
12[CmdletBinding()]
13param(
14 [Parameter(Mandatory)][guid]$VMId,
15 [Parameter(Mandatory)][string]$InstanceDirectory,
16 [Parameter(Mandatory)][pscredential]$GuestAdministrator
17)
18$ErrorActionPreference = 'Stop'
19if ([Environment]::OSVersion.Platform -ne 'Win32NT') { throw 'Run on the Hyper-V host in Windows PowerShell.' }
20$root = Get-Item -LiteralPath $InstanceDirectory
21$qa = Join-Path $root.FullName 'evaluation-setup'
22$qaDir = Get-Item -LiteralPath $qa
23if (($root.Attributes -band [IO.FileAttributes]::ReparsePoint) -or ($qaDir.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
24 throw 'The private QA directories cannot be redirected.'
25}
26$receipt = Get-Content -LiteralPath (Join-Path $qa 'receipt.json') -Raw | ConvertFrom-Json
27$vm = Get-VM -Id $VMId
28$disks = @(Get-VMHardDiskDrive -VM $vm)
29if ([guid]$receipt.VMId -ne $VMId -or $receipt.InstanceDirectory -ne $root.FullName -or $vm.State -ne 'Running' -or
30 $disks.Count -ne 1 -or $disks[0].Path -ne $receipt.Disk -or @(Get-VMNetworkAdapter -VM $vm).Count) {
31 throw 'Select the running offline evaluation VM that owns this setup receipt.'
32}
33$session = New-PSSession -VMId $VMId -Credential $GuestAdministrator
34try {
35 $guestComputer = Invoke-Command -Session $session -ArgumentList $VMId -ScriptBlock {
36 param($expected)
37 $ErrorActionPreference = 'Stop'
38 $identity = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters' -Name VirtualMachineId
39 $setup = Get-ItemProperty 'HKLM:\SYSTEM\Setup'
40 $imageState = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\State' -Name ImageState
41 $os = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name EditionID
42 $account = [Security.Principal.WindowsPrincipal]::new([Security.Principal.WindowsIdentity]::GetCurrent())
43 if ([guid]$identity.VirtualMachineId -ne $expected -or $os.EditionID -ne 'EnterpriseEval' -or
44 $setup.SystemSetupInProgress -ne 0 -or $setup.OOBEInProgress -eq 1 -or $imageState.ImageState -ne 'IMAGE_STATE_COMPLETE' -or
45 !$account.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
46 throw 'PowerShell Direct must reach the matching Enterprise Evaluation guest after OOBE with guest administrator rights.'
47 }
48 $env:COMPUTERNAME
49 }
50
51 $credentials = @()
52 $secrets = Join-Path $qa 'credentials'
53 $owner = [Security.Principal.WindowsIdentity]::GetCurrent().User
54 $permitted = @($owner.Value, 'S-1-5-18', 'S-1-5-32-544')
55 if (!(Test-Path -LiteralPath $secrets)) {
56 $acl = [Security.AccessControl.DirectorySecurity]::new()
57 $acl.SetAccessRuleProtection($true, $false)
58 $acl.SetOwner($owner)
59 foreach ($value in $permitted) {
60 $sid = [Security.Principal.SecurityIdentifier]::new($value)
61 $acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($sid, 'FullControl', 'ContainerInherit, ObjectInherit', 'None', 'Allow'))
62 }
63 if ($PSVersionTable.PSEdition -eq 'Desktop') {
64 $null = [IO.Directory]::CreateDirectory($secrets, $acl)
65 } else { [IO.FileSystemAclExtensions]::Create([IO.DirectoryInfo]::new($secrets), $acl) }
66 }
67 $secretsDir = Get-Item -LiteralPath $secrets
68 $acl = Get-Acl -LiteralPath $secrets
69 $unexpected = @($acl.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier]) |
70 Where-Object { $_.AccessControlType -eq 'Allow' -and $_.IdentityReference.Value -notin $permitted })
71 if (($secretsDir.Attributes -band [IO.FileAttributes]::ReparsePoint) -or !$acl.AreAccessRulesProtected -or $unexpected.Count) {
72 throw 'The test credential directory must have a private, protected Windows ACL.'
73 }
74 foreach ($name in @('CageQaAdmin', 'CageQaUser')) {
75 $path = Join-Path $secrets "$name.credential.xml"
76 $qualified = "$guestComputer\$name"
77 if (Test-Path -LiteralPath $path) {
78 $credential = Import-Clixml -LiteralPath $path
79 if ($credential -isnot [pscredential] -or $credential.UserName -ne $qualified) { throw 'Unexpected saved QA credential identity.' }
80 } else {
81 $random = [byte[]]::new(32)
82 $generator = [Security.Cryptography.RandomNumberGenerator]::Create()
83 try { $generator.GetBytes($random) } finally { $generator.Dispose() }
84 $password = ConvertTo-SecureString ('Qa9!' + [Convert]::ToBase64String($random)) -AsPlainText -Force
85 $credential = [pscredential]::new($qualified, $password)
86 $credential | Export-Clixml -LiteralPath $path -NoClobber
87 }
88 $credentials += $credential
89 }
90 $provision = Invoke-Command -Session $session -ArgumentList $VMId,$credentials[0],$credentials[1] -ScriptBlock {
91 param($expected, [pscredential]$admin, [pscredential]$standard)
92 $ErrorActionPreference = 'Stop'
93 $tag = "Cage isolated test VM $expected"
94 $admins = Get-LocalGroup -SID ([Security.Principal.SecurityIdentifier]::new('S-1-5-32-544'))
95 $users = Get-LocalGroup -SID ([Security.Principal.SecurityIdentifier]::new('S-1-5-32-545'))
96 foreach ($credential in @($admin,$standard)) {
97 $name = $credential.UserName.Split('\')[-1]
98 $u = Get-LocalUser -Name $name -ErrorAction SilentlyContinue
99 if ($u -and $u.Description -ne $tag) { throw "An unrelated guest account already uses the name $name." }
100 if (!$u) { $u = New-LocalUser -Name $name -Password $credential.Password -Description $tag }
101 $group = if ($name -eq 'CageQaAdmin') { $admins } else { $users }
102 if (!(Get-LocalGroupMember -Group $group | Where-Object { $_.SID -eq $u.SID })) {
103 Add-LocalGroupMember -Group $group -Member $u
104 }
105 if ($name -eq 'CageQaUser' -and (Get-LocalGroupMember -Group $admins | Where-Object { $_.SID -eq $u.SID })) {
106 throw 'The test desktop user is unexpectedly an administrator.'
107 }
108 }
109 [pscustomobject]@{ Computer = $env:COMPUTERNAME; Admin = 'CageQaAdmin'; DesktopUser = 'CageQaUser' }
110 }
111 # Authenticate a fresh VMBus session using the new administrator.
112 $verified = Invoke-Command -VMId $VMId -Credential $credentials[0] -ScriptBlock { [Security.Principal.WindowsIdentity]::GetCurrent().Name }
113 if ($verified -ne $credentials[0].UserName) { throw 'The independent PowerShell Direct login did not match the QA administrator.' }
114 [pscustomobject]@{
115 VMId = $VMId; Computer = $provision.Computer; Admin = $provision.Admin; StandardDesktopUser = $provision.DesktopUser
116 PowerShellDirectVerified = $true
117 AdminCredentialFile = Join-Path $secrets 'CageQaAdmin.credential.xml'
118 DesktopCredentialFile = Join-Path $secrets 'CageQaUser.credential.xml'
119 }
120} finally { Remove-PSSession -Session $session }