| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Validates and processes a service descriptor hashtable. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Validates the structure and required keys of a format 1.0 service descriptor hashtable. |
| | | 6 | | Also checks that referenced entry point files exist within the package root and do not escape it. |
| | | 7 | | .PARAMETER Descriptor |
| | | 8 | | The service descriptor as a hashtable, typically parsed from Service.psd1. |
| | | 9 | | .PARAMETER DescriptorPath |
| | | 10 | | The file path of the descriptor, used for error messages. |
| | | 11 | | .PARAMETER PackageRoot |
| | | 12 | | The root directory of the package, used to resolve and validate script paths. |
| | | 13 | | .EXAMPLE |
| | | 14 | | $descriptor = @{ |
| | | 15 | | Name = 'MyService' |
| | | 16 | | FormatVersion = '1.0' |
| | | 17 | | EntryPoint = 'server.ps1' |
| | | 18 | | Description = 'A sample service.' |
| | | 19 | | Version = '1.0.0' |
| | | 20 | | } |
| | | 21 | | Test-KrServiceDescriptorData -Descriptor $descriptor -DescriptorPath '.\Service.psd1' -PackageRoot '.\' |
| | | 22 | | #> |
| | | 23 | | function Test-KrServiceDescriptorData { |
| | | 24 | | param( |
| | | 25 | | [hashtable]$Descriptor, |
| | | 26 | | [string]$DescriptorPath, |
| | | 27 | | [string]$PackageRoot |
| | | 28 | | ) |
| | | 29 | | |
| | 1 | 30 | | if (-not $Descriptor.ContainsKey('Name') -or [string]::IsNullOrWhiteSpace([string]$Descriptor['Name'])) { |
| | 0 | 31 | | throw "Descriptor '$DescriptorPath' is missing required key 'Name'." |
| | | 32 | | } |
| | | 33 | | |
| | 1 | 34 | | $packageRootFullPath = [System.IO.Path]::GetFullPath($PackageRoot) |
| | 1 | 35 | | $packageRootNormalized = [System.IO.Path]::TrimEndingDirectorySeparator($packageRootFullPath) |
| | | 36 | | |
| | 1 | 37 | | $isWithinPackageRoot = { |
| | | 38 | | param([string]$PathToValidate) |
| | | 39 | | |
| | 1 | 40 | | $normalizedPath = [System.IO.Path]::TrimEndingDirectorySeparator($PathToValidate) |
| | 1 | 41 | | $relativePath = [System.IO.Path]::GetRelativePath($packageRootNormalized, $normalizedPath) |
| | | 42 | | |
| | 1 | 43 | | if ([string]::Equals($relativePath, '.', [System.StringComparison]::Ordinal)) { |
| | 0 | 44 | | return $true |
| | | 45 | | } |
| | | 46 | | |
| | 1 | 47 | | if ([System.IO.Path]::IsPathRooted($relativePath)) { |
| | 0 | 48 | | return $false |
| | | 49 | | } |
| | | 50 | | |
| | 1 | 51 | | return -not ( |
| | 1 | 52 | | [string]::Equals($relativePath, '..', [System.StringComparison]::Ordinal) -or |
| | 1 | 53 | | $relativePath.StartsWith("..$([System.IO.Path]::DirectorySeparatorChar)", [System.StringComparison]::Ordinal |
| | 1 | 54 | | $relativePath.StartsWith("..$([System.IO.Path]::AltDirectorySeparatorChar)", [System.StringComparison]::Ordi |
| | | 55 | | ) |
| | | 56 | | } |
| | | 57 | | |
| | 1 | 58 | | $normalizedPreservePaths = @() |
| | 1 | 59 | | if ($Descriptor.ContainsKey('PreservePaths') -and $null -ne $Descriptor['PreservePaths']) { |
| | 1 | 60 | | $rawPreservePaths = @() |
| | 1 | 61 | | if ($Descriptor['PreservePaths'] -is [string]) { |
| | 0 | 62 | | $rawPreservePaths = @([string]$Descriptor['PreservePaths']) |
| | 1 | 63 | | } elseif ($Descriptor['PreservePaths'] -is [System.Collections.IEnumerable]) { |
| | 2 | 64 | | $rawPreservePaths = @($Descriptor['PreservePaths']) |
| | | 65 | | } else { |
| | 0 | 66 | | throw "Descriptor '$DescriptorPath' key 'PreservePaths' must be a string array." |
| | | 67 | | } |
| | | 68 | | |
| | 1 | 69 | | foreach ($preservePathValue in $rawPreservePaths) { |
| | 1 | 70 | | $preservePath = [string]$preservePathValue |
| | 1 | 71 | | if ([string]::IsNullOrWhiteSpace($preservePath)) { |
| | 0 | 72 | | throw "Descriptor '$DescriptorPath' key 'PreservePaths' cannot contain empty values." |
| | | 73 | | } |
| | | 74 | | |
| | 1 | 75 | | if ([System.IO.Path]::IsPathRooted($preservePath)) { |
| | 0 | 76 | | throw "Descriptor '$DescriptorPath' PreservePaths entry '$preservePath' must be a relative path." |
| | | 77 | | } |
| | | 78 | | |
| | 1 | 79 | | $combinedPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($packageRootFullPath, $preservePath) |
| | 2 | 80 | | if (-not (& $isWithinPackageRoot $combinedPath)) { |
| | 1 | 81 | | throw "Descriptor '$DescriptorPath' PreservePaths entry '$preservePath' escapes the package root." |
| | | 82 | | } |
| | | 83 | | |
| | 1 | 84 | | $normalizedPreservePaths += $preservePath |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | 1 | 88 | | if (-not $Descriptor.ContainsKey('FormatVersion') -or [string]::IsNullOrWhiteSpace([string]$Descriptor['FormatVersio |
| | 0 | 89 | | throw "Descriptor '$DescriptorPath' is missing required key 'FormatVersion'." |
| | | 90 | | } |
| | | 91 | | |
| | 1 | 92 | | $formatVersion = [string]$Descriptor['FormatVersion'] |
| | 1 | 93 | | if (-not [string]::Equals($formatVersion.Trim(), '1.0', [System.StringComparison]::Ordinal)) { |
| | 0 | 94 | | throw "Descriptor '$DescriptorPath' has unsupported FormatVersion '$formatVersion'. Expected '1.0'." |
| | | 95 | | } |
| | | 96 | | |
| | 2 | 97 | | foreach ($requiredKey in @('Description', 'Version', 'EntryPoint')) { |
| | 1 | 98 | | if (-not $Descriptor.ContainsKey($requiredKey) -or [string]::IsNullOrWhiteSpace([string]$Descriptor[$requiredKey |
| | 0 | 99 | | throw "Descriptor '$DescriptorPath' is missing required key '$requiredKey'." |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | 1 | 103 | | $parsedVersion = $null |
| | 1 | 104 | | if (-not [version]::TryParse([string]$Descriptor['Version'], [ref]$parsedVersion)) { |
| | 0 | 105 | | throw "Descriptor '$DescriptorPath' has invalid Version value '$($Descriptor['Version'])'." |
| | | 106 | | } |
| | | 107 | | |
| | 1 | 108 | | $entryPoint = [string]$Descriptor['EntryPoint'] |
| | 1 | 109 | | if ([System.IO.Path]::IsPathRooted($entryPoint)) { |
| | 0 | 110 | | throw "Descriptor '$DescriptorPath' EntryPoint must be a relative path." |
| | | 111 | | } |
| | | 112 | | |
| | 1 | 113 | | $entryPointFullPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($packageRootFullPath, $entryPoint)) |
| | 2 | 114 | | if (-not (& $isWithinPackageRoot $entryPointFullPath)) { |
| | 1 | 115 | | throw "Descriptor '$DescriptorPath' EntryPoint escapes the package root." |
| | | 116 | | } |
| | | 117 | | |
| | 2 | 118 | | if (-not (Test-Path -LiteralPath $entryPointFullPath -PathType Leaf)) { |
| | 0 | 119 | | throw "EntryPoint file '$entryPoint' was not found under '$PackageRoot'." |
| | | 120 | | } |
| | | 121 | | |
| | 1 | 122 | | [pscustomobject]@{ |
| | 1 | 123 | | Name = [string]$Descriptor['Name'] |
| | 1 | 124 | | FormatVersion = '1.0' |
| | 1 | 125 | | EntryPoint = $entryPoint |
| | 1 | 126 | | Description = [string]$Descriptor['Description'] |
| | 1 | 127 | | Version = $parsedVersion.ToString() |
| | 3 | 128 | | ServiceLogPath = if ($Descriptor.ContainsKey('ServiceLogPath')) { [string]$Descriptor['ServiceLogPath'] } else { |
| | 1 | 129 | | PreservePaths = $normalizedPreservePaths |
| | | 130 | | } |
| | | 131 | | } |