audit: allow explicit local provider endpoints
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
Set-StrictMode -Version Latest
|
||||
|
||||
$script:AllowedCognitiveWorkerEnvironment = @{
|
||||
'CORE_RUNTIME_URL' = 'http_url'
|
||||
'LOCAL_CPU_PROVIDER_URL' = 'http_url'
|
||||
'LOCAL_GPU_PROVIDER_URL' = 'http_url'
|
||||
'LOCAL_OLLAMA_PROVIDER_URL' = 'http_url'
|
||||
'COGNITIVE_OWNER_PRINCIPAL_ID' = 'nonempty'
|
||||
'COGNITIVE_ALLOW_CANARY' = 'boolean'
|
||||
'LISTEN_ADDR' = 'listen_address'
|
||||
'OPENAI_API_KEY_FILE' = 'secret_file'
|
||||
'PATH' = 'nonempty'
|
||||
}
|
||||
|
||||
function Test-CognitiveWorkerEnvironmentPolicy {
|
||||
[CmdletBinding()]
|
||||
param([Parameter(Mandatory)][string[]]$Environment)
|
||||
|
||||
foreach ($entry in $Environment) {
|
||||
$parts = $entry -split '=', 2
|
||||
if ($parts.Count -ne 2 -or [string]::IsNullOrWhiteSpace($parts[0])) {
|
||||
throw "Cognitive worker environment entry is malformed: $entry"
|
||||
}
|
||||
|
||||
$name = $parts[0]
|
||||
$value = $parts[1]
|
||||
if (-not $script:AllowedCognitiveWorkerEnvironment.ContainsKey($name)) {
|
||||
if ($name -match '(?i)(API_KEY|TOKEN|PASSWORD|SECRET|CREDENTIAL|ACCESS_KEY|PRIVATE_KEY)') {
|
||||
throw "Cognitive worker credential-like environment is not an approved CredentialRef file: $name"
|
||||
}
|
||||
throw "Cognitive worker environment is not explicitly approved: $name"
|
||||
}
|
||||
|
||||
switch ($script:AllowedCognitiveWorkerEnvironment[$name]) {
|
||||
'http_url' {
|
||||
$uri = $null
|
||||
if (-not [uri]::TryCreate($value, [System.UriKind]::Absolute, [ref]$uri) -or $uri.Scheme -notin @('http', 'https') -or [string]::IsNullOrWhiteSpace($uri.Host)) {
|
||||
throw "Cognitive worker endpoint is invalid: $name"
|
||||
}
|
||||
}
|
||||
'boolean' {
|
||||
if ($value -notin @('true', 'false')) { throw "Cognitive worker boolean is invalid: $name" }
|
||||
}
|
||||
'listen_address' {
|
||||
if ($value -notmatch '^:[0-9]{1,5}$') { throw "Cognitive worker listen address is invalid: $name" }
|
||||
}
|
||||
'secret_file' {
|
||||
if ($value -notmatch '^/run/secrets/[A-Za-z0-9][A-Za-z0-9._-]*$') {
|
||||
throw "Cognitive worker CredentialRef file is invalid: $name"
|
||||
}
|
||||
}
|
||||
'nonempty' {
|
||||
if ([string]::IsNullOrWhiteSpace($value)) { throw "Cognitive worker environment is empty: $name" }
|
||||
}
|
||||
default { throw "Cognitive worker environment policy is invalid for: $name" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version Latest
|
||||
|
||||
. "$PSScriptRoot/cognitive_worker_env_policy.ps1"
|
||||
|
||||
function Assert-PolicyPass([string]$Name, [string[]]$Environment) {
|
||||
try { Test-CognitiveWorkerEnvironmentPolicy -Environment $Environment }
|
||||
catch { throw "$Name expected PASS: $($_.Exception.Message)" }
|
||||
}
|
||||
|
||||
function Assert-PolicyFail([string]$Name, [string[]]$Environment) {
|
||||
try { Test-CognitiveWorkerEnvironmentPolicy -Environment $Environment }
|
||||
catch { return }
|
||||
throw "$Name expected FAIL"
|
||||
}
|
||||
|
||||
Assert-PolicyPass 'CPU provider endpoint' @('LOCAL_CPU_PROVIDER_URL=http://docker.cin.su:18082/v1')
|
||||
Assert-PolicyPass 'Ollama provider endpoint' @('LOCAL_OLLAMA_PROVIDER_URL=http://192.168.220.91:11434')
|
||||
Assert-PolicyFail 'plaintext OpenAI API key' @('OPENAI_API_KEY=secret')
|
||||
Assert-PolicyFail 'plaintext provider token' @('PROVIDER_TOKEN=secret')
|
||||
Assert-PolicyPass 'approved OpenAI CredentialRef file' @('OPENAI_API_KEY_FILE=/run/secrets/openai_api_key')
|
||||
Assert-PolicyFail 'unknown credential-like variable' @('UNRECOGNIZED_API_KEY_FILE=/run/secrets/unknown')
|
||||
|
||||
Write-Output 'Cognitive worker environment policy regression tests: PASS'
|
||||
@@ -10,6 +10,8 @@ param(
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version Latest
|
||||
|
||||
. "$PSScriptRoot/cognitive_worker_env_policy.ps1"
|
||||
|
||||
function Get-ContainerEnvironment([string]$Container) {
|
||||
$result = @{}
|
||||
$inspect = ((& docker.exe --context $DockerContext inspect $Container) -join "`n") | ConvertFrom-Json
|
||||
@@ -72,8 +74,7 @@ $injected = Invoke-StatusRequest "$BaseURL/api/advisory" $authorization $injecte
|
||||
if ($injected.StatusCode -ne 400) { throw "Authority-field injection status=$($injected.StatusCode)" }
|
||||
|
||||
$cognitiveInspect = ((& docker.exe --context $DockerContext inspect $CognitiveContainer) -join "`n") | ConvertFrom-Json
|
||||
$forbiddenEnvironment = @($cognitiveInspect[0].Config.Env | Where-Object { ($_ -split '=', 2)[0] -match '(?i)(POSTGRES|DATABASE|COMMIT|EXECUTOR|CREATOR|PASSWORD|TOKEN|PROVIDER|API_KEY)' })
|
||||
if ($forbiddenEnvironment.Count -ne 0) { throw 'Cognitive worker has forbidden privileged/provider environment' }
|
||||
Test-CognitiveWorkerEnvironmentPolicy -Environment @($cognitiveInspect[0].Config.Env)
|
||||
$forbiddenMounts = @($cognitiveInspect[0].Mounts | Where-Object { $_.Destination -match '(?i)(secret|credential|token|postgres|docker)' })
|
||||
if ($forbiddenMounts.Count -ne 0) { throw 'Cognitive worker has a forbidden credential or infrastructure mount' }
|
||||
$recentLogs = ((& docker.exe --context $DockerContext logs --since 5m $CognitiveContainer 2>&1) -join "`n")
|
||||
|
||||
Reference in New Issue
Block a user