فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
feat(a02): consent-gated local-only ZIP to SQL VERIFYONLY/restore and private encrypted schema lab
davoodmehraban89 committed
Oct 30, 2026 at 22:15 UTC
9fad05eefff9050664ab1f99abd591131886f53e
1 file changed
+155
scripts/a02-private-owner-local-lab.ps1
new
+155
@@ -0,0 +1,155 @@
1
+#requires -Version 5.1
2
+<#
3
+A02 private local lab: owner-only operator workflow, NOT end-user wizard.
4
+Test is synthetic only. No SQL install, remote connection, financial import,
5
+automatic privilege elevation, ACL modification, or automatic DROP DATABASE.
6
+#>
7
+[CmdletBinding()]
8
+param(
9
+ [ValidateSet('Test','StageAndInspect','RestoreAndInventory')]
10
+ [string]$Mode='Test',
11
+ [string]$SourceZip='',
12
+ [string]$ManifestOutputPath='',
13
+ [string]$StagingConsent='',
14
+ [string]$RestoreConsent=''
15
+)
16
+Set-StrictMode -Version Latest
17
+$ErrorActionPreference='Stop'
18
+$script:Phase='validate'
19
+$script:BackupPath=''
20
+function Require([bool]$ok,[string]$code){if(-not $ok){throw $code}}
21
+function Assert-ManifestDestination([string]$path){
22
+ Require (-not [string]::IsNullOrWhiteSpace($path) -and
23
+ $path -match '^[A-Za-z]:\\' -and
24
+ $path -notmatch '[/\x00-\x1f<>"|?*]' -and
25
+ -not $path.Substring(2).Contains(':')) 'A02_PRIVATE_MANIFEST_PATH_INVALID'
26
+ foreach($part in $path.Substring(3).Split('\')){
27
+ Require (-not [string]::IsNullOrWhiteSpace($part) -and
28
+ $part -ne '.' -and $part -ne '..' -and
29
+ -not $part.EndsWith(' ') -and -not $part.EndsWith('.')) 'A02_MANIFEST_PATH_AMBIGUOUS'
30
+ }
31
+ $dir=[IO.DirectoryInfo]::new([IO.Path]::GetDirectoryName($path))
32
+ Require $dir.Exists 'A02_MANIFEST_PARENT_MISSING'
33
+ while($null -ne $dir){
34
+ Require (($dir.Attributes -band [IO.FileAttributes]::ReparsePoint) -eq 0) 'A02_MANIFEST_PARENT_LINK'
35
+ $dir=$dir.Parent
36
+ }
37
+ Require (-not [IO.File]::Exists($path) -and -not [IO.Directory]::Exists($path)) 'A02_MANIFEST_WOULD_OVERWRITE'
38
+ $drive=[IO.DriveInfo]::new($path.Substring(0,1))
39
+ Require ($drive.IsReady -and $drive.DriveType -eq [IO.DriveType]::Fixed -and
40
+ $drive.AvailableFreeSpace -ge 100MB) 'A02_MANIFEST_DISK_INSUFFICIENT'
41
+}
42
+function Invoke-JsonStep([string]$scriptName,[string[]]$args,[string]$expected){
43
+ $file=Join-Path $PSScriptRoot $scriptName
44
+ Require ([IO.File]::Exists($file)) 'A02_REQUIRED_HELPER_MISSING'
45
+ # No raw error, SQL query or private source path is echoed by this wrapper.
46
+ $output=@(& powershell.exe -NoProfile -NonInteractive -File $file @args 2>$null)
47
+ Require ($LASTEXITCODE -eq 0 -and $output.Count -gt 0 -and $output.Count -le 5) 'A02_HELPER_FAILED'
48
+ $json=$output -join [Environment]::NewLine
49
+ Require ($json.Length -le 16384) 'A02_HELPER_OUTPUT_OVERSIZED'
50
+ $r=$json | ConvertFrom-Json -ErrorAction Stop
51
+ Require ($null -ne $r -and $r.status -ceq $expected -and
52
+ $r.canImport -eq $false) 'A02_HELPER_CONTRACT_FAILED'
53
+ return $r
54
+}
55
+
56
+if($Mode -eq 'Test'){
57
+ Require ([string]::IsNullOrWhiteSpace($SourceZip) -and
58
+ [string]::IsNullOrWhiteSpace($ManifestOutputPath) -and
59
+ [string]::IsNullOrWhiteSpace($StagingConsent) -and
60
+ [string]::IsNullOrWhiteSpace($RestoreConsent)) 'A02_SELFTEST_REQUIRES_SYNTHETIC_ONLY'
61
+ $tests=@(
62
+ @{File='a02-private-holoo-zip-staging.ps1';Status='synthetic-zip-staging-selftest-passed'},
63
+ @{File='a02-isolated-sql-restore.ps1';Status='selftest-passed'},
64
+ @{File='a02-isolated-schema-inventory.ps1';Status='schema-fingerprint-selftest-passed'},
65
+ @{File='a02-offline-sql-setup-preflight.ps1';Status='synthetic-offline-sql-setup-preflight-passed'}
66
+ )
67
+ foreach($test in $tests){
68
+ $r=Invoke-JsonStep $test.File @('-Mode','Test') $test.Status
69
+ Require ($r.canImport -eq $false) 'A02_SELFTEST_UNSAFE_IMPORT_CLAIM'
70
+ }
71
+ [pscustomobject]@{
72
+ status='private-lab-synthetic-orchestration-passed'
73
+ checks=$tests.Count;ownerBackupUsed=$false;sqlServerInstalled=$false
74
+ restoreExecuted=$false;financialMigrationPerformed=$false;canImport=$false
75
+ } | ConvertTo-Json -Compress
76
+ return
77
+}
78
+
79
+try{
80
+ Require ($StagingConsent -ceq 'I_AUTHORIZE_PRIVATE_HOLOO_STAGING') 'A02_STAGING_CONSENT_REQUIRED'
81
+ Require (-not [string]::IsNullOrWhiteSpace($SourceZip)) 'A02_OWNER_ZIP_PATH_REQUIRED'
82
+ if($Mode -eq 'RestoreAndInventory'){
83
+ Require ($RestoreConsent -ceq 'I_AUTHORIZE_A_NEW_ISOLATED_SQL_DATABASE') 'A02_RESTORE_CONSENT_REQUIRED'
84
+ $script:Phase='validate-manifest-destination'
85
+ Assert-ManifestDestination $ManifestOutputPath
86
+ }else{
87
+ Require ([string]::IsNullOrWhiteSpace($ManifestOutputPath) -and
88
+ [string]::IsNullOrWhiteSpace($RestoreConsent)) 'A02_INSPECT_FORBIDS_RESTORE_ARGS'
89
+ }
90
+
91
+ $script:Phase='zip-envelope'
92
+ $probe=Invoke-JsonStep 'a02-private-holoo-zip-staging.ps1' @('-Mode','Probe','-SourceZip',$SourceZip) 'zip-envelope-identified'
93
+ Require ($probe.databaseModified -eq $false -and $probe.crcVerified -eq $false) 'A02_ZIP_PROBE_INVALID'
94
+
95
+ $script:Phase='zip-stage-private'
96
+ $staged=Invoke-JsonStep 'a02-private-holoo-zip-staging.ps1' @(
97
+ '-Mode','Stage','-SourceZip',$SourceZip,'-ConsentPhrase',$StagingConsent
98
+ ) 'private-local-zip-staged-crc-and-tape-verified'
99
+ Require ($staged.crcVerified -eq $true -and $staged.sqlTapeHeaderVerified -eq $true -and
100
+ $staged.sqlBackupVerified -eq $false -and $staged.databaseModified -eq $false) 'A02_ZIP_STAGE_INCOMPLETE'
101
+ $script:BackupPath=[string]$staged.stagedBackupPath
102
+ Require (-not [string]::IsNullOrWhiteSpace($script:BackupPath)) 'A02_STAGED_BACKUP_PATH_MISSING'
103
+
104
+ $script:Phase='sql-header-filelist-verifyonly'
105
+ $check=Invoke-JsonStep 'a02-isolated-sql-restore.ps1' @(
106
+ '-Mode','Inspect','-BackupPath',$script:BackupPath
107
+ ) 'backup-verifyonly-passed'
108
+ Require ($check.restoreExecuted -eq $false -and
109
+ $check.financialReconciled -eq $false -and
110
+ $check.sourceSqlMajor -le $check.engineSqlMajor) 'A02_SQL_INSPECT_CONTRACT_INVALID'
111
+
112
+ if($Mode -eq 'StageAndInspect'){
113
+ [pscustomobject]@{
114
+ status='private-backup-staged-and-sql-verified'
115
+ stagedBackupPath=$script:BackupPath
116
+ sourceSqlMajor=$check.sourceSqlMajor;engineSqlMajor=$check.engineSqlMajor
117
+ engineEdition=$check.engineEdition;fileCount=$check.fileCount
118
+ sqlBackupVerified=$true;restoreExecuted=$false
119
+ financialMigrationPerformed=$false;canImport=$false
120
+ } | ConvertTo-Json -Compress
121
+ return
122
+ }
123
+ $script:Phase='sql-isolated-restore'
124
+ $restored=Invoke-JsonStep 'a02-isolated-sql-restore.ps1' @(
125
+ '-Mode','Restore','-BackupPath',$script:BackupPath,'-ConsentPhrase',$RestoreConsent
126
+ ) 'isolated-restore-completed-needs-mapping-and-reconciliation'
127
+ $database=[string]$restored.databaseName
128
+ Require ($database -cmatch '^FinoraHoloo_[0-9a-f]{32}$' -and
129
+ $restored.financialReconciled -eq $false -and
130
+ $restored.importPerformed -eq $false) 'A02_RESTORE_CONTRACT_INVALID'
131
+
132
+ $script:Phase='private-schema-export'
133
+ $schema=Invoke-JsonStep 'a02-isolated-schema-inventory.ps1' @(
134
+ '-Mode','PrivateManifest','-InstanceName','FINORAMIG',
135
+ '-DatabaseName',$database,'-OutputPath',$ManifestOutputPath
136
+ ) 'private-schema-manifest-encrypted'
137
+ Require ($schema.dpapiScope -ceq 'CurrentUser' -and
138
+ $schema.fileAclRestricted -eq $true -and
139
+ $schema.userRowsRead -eq $false -and
140
+ $schema.databaseModified -eq $false) 'A02_PRIVATE_SCHEMA_ENCRYPTION_INVALID'
141
+ [pscustomobject]@{
142
+ status='isolated-restore-and-encrypted-schema-created'
143
+ isolatedDatabaseName=$database;stagedBackupPath=$script:BackupPath
144
+ encryptedSchemaManifestPath=$ManifestOutputPath
145
+ tableCount=$schema.tableCount;columnCount=$schema.columnCount
146
+ sqlBackupVerified=$true;restoreExecuted=$true
147
+ financialReconciled=$false;financialMigrationPerformed=$false;canImport=$false
148
+ } | ConvertTo-Json -Compress
149
+}catch{
150
+ # A failed restore can leave a NEW incomplete DB or plaintext staged backup.
151
+ # Never silently delete data or a SQL DB; leave inspection to the owner.
152
+ Write-Error ('A02_PRIVATE_LAB_BLOCKED_AT_'+$script:Phase.ToUpperInvariant().Replace('-','_')+
153
+ '_NO_IMPORT_MANUAL_REVIEW_OF_PRIVATE_STAGING_AND_SQL_MAY_BE_REQUIRED')
154
+ exit 2
155
+}