Set-StrictMode -Version Latest $script:AllowedCognitiveWorkerEnvironment = @{ 'CORE_RUNTIME_URL' = 'http_url' 'LOCAL_CPU_PROVIDER_URL' = 'http_url' 'LOCAL_CPU_PROVIDER_API_KEY_FILE' = 'secret_file' 'LOCAL_CPU_PROVIDER_CREDENTIAL_REF' = 'nonempty' 'LOCAL_MEDIUM_PROVIDER_URL' = 'http_url' 'LOCAL_MEDIUM_PROVIDER_API_KEY_FILE' = 'secret_file' 'LOCAL_MEDIUM_PROVIDER_CREDENTIAL_REF' = 'nonempty' '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" } } } }