47 lines
2.0 KiB
PowerShell
47 lines
2.0 KiB
PowerShell
[CmdletBinding()]
|
|
param()
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$connectionString = $env:INOGEN_RUNTIME_CONNECTION
|
|
$allowedServer = $env:INOGEN_TARGET_ALLOWED_SERVER
|
|
$allowedDatabase = $env:INOGEN_TARGET_ALLOWED_DATABASE
|
|
if ([string]::IsNullOrWhiteSpace($connectionString) -or
|
|
[string]::IsNullOrWhiteSpace($allowedServer) -or
|
|
[string]::IsNullOrWhiteSpace($allowedDatabase)) {
|
|
throw 'Set INOGEN_RUNTIME_CONNECTION and the explicit target server/database allowlist.'
|
|
}
|
|
$builder = New-Object System.Data.SqlClient.SqlConnectionStringBuilder($connectionString)
|
|
if ($builder.DataSource -ne $allowedServer -or $builder.InitialCatalog -ne $allowedDatabase -or
|
|
$builder.InitialCatalog -match 'APADANA-IATM|NOPC') {
|
|
throw 'Runtime connection does not match the approved target.'
|
|
}
|
|
|
|
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
|
|
try {
|
|
$connection.Open()
|
|
$command = $connection.CreateCommand()
|
|
$command.CommandText = @'
|
|
SELECT
|
|
ISNULL(IS_SRVROLEMEMBER('sysadmin'), 0),
|
|
ISNULL(IS_MEMBER('db_owner'), 0),
|
|
ISNULL(HAS_PERMS_BY_NAME('inogen', 'SCHEMA', 'ALTER'), 0),
|
|
ISNULL(HAS_PERMS_BY_NAME('inogen', 'SCHEMA', 'SELECT'), 0),
|
|
ISNULL(HAS_PERMS_BY_NAME('inogen', 'SCHEMA', 'INSERT'), 0),
|
|
ISNULL(HAS_PERMS_BY_NAME('inogen', 'SCHEMA', 'UPDATE'), 0),
|
|
ISNULL(HAS_PERMS_BY_NAME('inogen', 'SCHEMA', 'DELETE'), 0)
|
|
'@
|
|
$reader = $command.ExecuteReader()
|
|
try {
|
|
if (-not $reader.Read()) { throw 'SQL permission probe returned no data.' }
|
|
if ($reader.GetInt32(0) -ne 0 -or $reader.GetInt32(1) -ne 0 -or $reader.GetInt32(2) -ne 0) {
|
|
throw 'Runtime SQL principal has an excessive privilege.'
|
|
}
|
|
foreach ($index in 3..6) {
|
|
if ($reader.GetInt32($index) -ne 1) { throw 'Runtime SQL principal lacks a required data permission.' }
|
|
}
|
|
}
|
|
finally { $reader.Dispose() }
|
|
Write-Host 'Runtime SQL principal has the expected data permissions without sysadmin, db_owner or schema ALTER.'
|
|
}
|
|
finally { $connection.Dispose() }
|