Enhance configuration data handling with new reference resolution functions, dummy secret support, and update module version to 1.1.0
This commit is contained in:
39
Private/Get-ConfigurationDataObjectPropertyValue.ps1
Normal file
39
Private/Get-ConfigurationDataObjectPropertyValue.ps1
Normal file
@@ -0,0 +1,39 @@
|
||||
function Get-ConfigurationDataObjectPropertyValue {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[AllowNull()]
|
||||
$Value,
|
||||
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]
|
||||
$Property,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]
|
||||
$Path = ""
|
||||
)
|
||||
|
||||
if([string]::IsNullOrWhiteSpace($Property)){
|
||||
throw "Configuration data reference property must not be empty."
|
||||
}
|
||||
|
||||
if($null -eq $Value){
|
||||
throw "Configuration data reference property [$Property] was requested from a null value at path [$Path]."
|
||||
}
|
||||
|
||||
if($Value -is [System.Collections.IDictionary]){
|
||||
if(-not $Value.Contains($Property)){
|
||||
throw "Configuration data reference property [$Property] was not found at path [$Path]."
|
||||
}
|
||||
|
||||
return $Value[$Property]
|
||||
}
|
||||
|
||||
$ObjectProperty = $Value.PSObject.Properties[$Property]
|
||||
if($null -eq $ObjectProperty){
|
||||
throw "Configuration data reference property [$Property] was not found at path [$Path]."
|
||||
}
|
||||
|
||||
return $ObjectProperty.Value
|
||||
}
|
||||
61
Private/Get-ConfigurationDataPathValue.ps1
Normal file
61
Private/Get-ConfigurationDataPathValue.ps1
Normal file
@@ -0,0 +1,61 @@
|
||||
function Get-ConfigurationDataPathValue {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[AllowNull()]
|
||||
$Value,
|
||||
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]
|
||||
$Path
|
||||
)
|
||||
|
||||
if([string]::IsNullOrWhiteSpace($Path)){
|
||||
throw "Configuration data reference path must not be empty."
|
||||
}
|
||||
|
||||
$Current = $Value
|
||||
$Segments = @($Path -split '\.')
|
||||
|
||||
foreach($Segment in $Segments){
|
||||
if([string]::IsNullOrWhiteSpace($Segment)){
|
||||
throw "Configuration data reference path [$Path] contains an empty segment."
|
||||
}
|
||||
|
||||
if($null -eq $Current){
|
||||
throw "Configuration data reference path [$Path] was not found. Segment [$Segment] resolved from a null value."
|
||||
}
|
||||
|
||||
if($Current -is [System.Collections.IDictionary]){
|
||||
if(-not $Current.Contains($Segment)){
|
||||
throw "Configuration data reference path [$Path] was not found. Segment [$Segment] is missing."
|
||||
}
|
||||
|
||||
$Current = $Current[$Segment]
|
||||
continue
|
||||
}
|
||||
|
||||
if($Current -is [System.Array] -and $Current -isnot [string]){
|
||||
$Index = 0
|
||||
if(-not [int]::TryParse($Segment, [ref]$Index)){
|
||||
throw "Configuration data reference path [$Path] expected a numeric array index at segment [$Segment]."
|
||||
}
|
||||
|
||||
if($Index -lt 0 -or $Index -ge @($Current).Count){
|
||||
throw "Configuration data reference path [$Path] array index [$Index] is out of range."
|
||||
}
|
||||
|
||||
$Current = @($Current)[$Index]
|
||||
continue
|
||||
}
|
||||
|
||||
$Property = $Current.PSObject.Properties[$Segment]
|
||||
if($null -eq $Property){
|
||||
throw "Configuration data reference path [$Path] was not found. Property [$Segment] is missing."
|
||||
}
|
||||
|
||||
$Current = $Property.Value
|
||||
}
|
||||
|
||||
return $Current
|
||||
}
|
||||
@@ -24,6 +24,17 @@ function Invoke-ConfigurationDataExpressionFunction {
|
||||
Assert-ConfigurationDataExpressionArgumentCount -Name $Name -Arguments $Arguments -Count 1
|
||||
return Resolve-ConfigurationDataVariable -Name ([string]$Arguments[0]) -Context $Context
|
||||
}
|
||||
"reference" {
|
||||
if($Arguments.Count -eq 1){
|
||||
return Resolve-ConfigurationDataReference -Path ([string]$Arguments[0]) -Context $Context
|
||||
}
|
||||
|
||||
if($Arguments.Count -eq 2){
|
||||
return Resolve-ConfigurationDataReference -Path ([string]$Arguments[0]) -Property ([string]$Arguments[1]) -Context $Context
|
||||
}
|
||||
|
||||
throw "Function [$Name] expects 1 or 2 arguments, but received [$($Arguments.Count)]."
|
||||
}
|
||||
"concat" {
|
||||
return (@($Arguments) | ForEach-Object { [string]$_ }) -join ""
|
||||
}
|
||||
|
||||
68
Private/New-ConfigurationDataDummySecretValue.ps1
Normal file
68
Private/New-ConfigurationDataDummySecretValue.ps1
Normal file
@@ -0,0 +1,68 @@
|
||||
function New-ConfigurationDataDummySecretValue {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[ValidateSet("credential", "securestring", "string")]
|
||||
[string]
|
||||
$ExpectedType,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[System.Collections.IDictionary]
|
||||
$Reference,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]
|
||||
$ParameterName = ""
|
||||
)
|
||||
|
||||
$Name = ""
|
||||
if($null -ne $Reference -and (Test-ConfigurationDataMapContainsKey -Map $Reference -Key "Name")){
|
||||
$Name = [string](Get-ConfigurationDataMapValue -Map $Reference -Key "Name" -DefaultValue "")
|
||||
}
|
||||
|
||||
$UserName = ""
|
||||
if($null -ne $Reference -and (Test-ConfigurationDataMapContainsKey -Map $Reference -Key "UserName")){
|
||||
$UserName = [string](Get-ConfigurationDataMapValue -Map $Reference -Key "UserName" -DefaultValue "")
|
||||
}
|
||||
|
||||
if([string]::IsNullOrWhiteSpace($UserName)){
|
||||
$UserNameSource = $ParameterName
|
||||
if([string]::IsNullOrWhiteSpace($UserNameSource)){
|
||||
$UserNameSource = $Name
|
||||
}
|
||||
|
||||
if([string]::IsNullOrWhiteSpace($UserNameSource)){
|
||||
$UserNameSource = "Credential"
|
||||
}
|
||||
|
||||
$UserNameLeaf = @($UserNameSource -split '[\\/]+')[-1]
|
||||
if([string]::IsNullOrWhiteSpace($UserNameLeaf)){
|
||||
$UserNameLeaf = "Credential"
|
||||
}
|
||||
|
||||
$UserName = "DUMMY\$UserNameLeaf"
|
||||
}
|
||||
|
||||
switch($ExpectedType.ToLowerInvariant()){
|
||||
"credential" {
|
||||
return [pscredential]::new(
|
||||
$UserName,
|
||||
(ConvertTo-SecureString -String "DummyPassword!" -AsPlainText -Force)
|
||||
)
|
||||
}
|
||||
"securestring" {
|
||||
return ConvertTo-SecureString -String "DummySecret!" -AsPlainText -Force
|
||||
}
|
||||
"string" {
|
||||
if([string]::IsNullOrWhiteSpace($Name)){
|
||||
$Name = $ParameterName
|
||||
}
|
||||
|
||||
if([string]::IsNullOrWhiteSpace($Name)){
|
||||
return "DummySecret"
|
||||
}
|
||||
|
||||
return "DummySecret:$Name"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,13 @@ function New-ConfigurationDataResolutionContext {
|
||||
)
|
||||
|
||||
$Context = [PSCustomObject]@{
|
||||
ConfigurationData = $ConfigurationData
|
||||
Parameters = @{}
|
||||
Variables = @{}
|
||||
VariableDefinitions = @{}
|
||||
ResolvingVariables = @{}
|
||||
ReferenceCache = @{}
|
||||
ResolvingReferences = @{}
|
||||
}
|
||||
|
||||
if($ConfigurationData.ContainsKey("Parameters") -and $null -ne $ConfigurationData.Parameters){
|
||||
|
||||
@@ -7,7 +7,11 @@ function Resolve-ConfigurationDataParameterSecrets {
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[hashtable]
|
||||
$ProviderSettings = @{}
|
||||
$ProviderSettings = @{},
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[switch]
|
||||
$UseDummySecrets
|
||||
)
|
||||
|
||||
$ResolvedConfigurationData = $ConfigurationData.Clone()
|
||||
@@ -32,7 +36,11 @@ function Resolve-ConfigurationDataParameterSecrets {
|
||||
$ExpectedType = $TypeName.ToLowerInvariant()
|
||||
foreach($ValueKey in @("Value", "DefaultValue")){
|
||||
if((Test-ConfigurationDataMapContainsKey -Map $Definition -Key $ValueKey) -and (Test-ConfigurationDataSecretReference -Value $Definition[$ValueKey])){
|
||||
$Definition[$ValueKey] = Resolve-ConfigurationDataSecretReference -Reference $Definition[$ValueKey] -ExpectedType $ExpectedType -ProviderSettings $ProviderSettings
|
||||
if($UseDummySecrets){
|
||||
$Definition[$ValueKey] = New-ConfigurationDataDummySecretValue -ExpectedType $ExpectedType -Reference $Definition[$ValueKey] -ParameterName $Parameter.Name
|
||||
}else{
|
||||
$Definition[$ValueKey] = Resolve-ConfigurationDataSecretReference -Reference $Definition[$ValueKey] -ExpectedType $ExpectedType -ProviderSettings $ProviderSettings
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
46
Private/Resolve-ConfigurationDataReference.ps1
Normal file
46
Private/Resolve-ConfigurationDataReference.ps1
Normal file
@@ -0,0 +1,46 @@
|
||||
function Resolve-ConfigurationDataReference {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]
|
||||
$Path,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[AllowEmptyString()]
|
||||
[string]
|
||||
$Property = "",
|
||||
|
||||
[Parameter(Mandatory=$true)]
|
||||
$Context
|
||||
)
|
||||
|
||||
$ReferenceKey = $Path
|
||||
if(-not [string]::IsNullOrWhiteSpace($Property)){
|
||||
$ReferenceKey = "$Path::$Property"
|
||||
}
|
||||
|
||||
if($Context.ReferenceCache.ContainsKey($ReferenceKey)){
|
||||
return $Context.ReferenceCache[$ReferenceKey]
|
||||
}
|
||||
|
||||
if($Context.ResolvingReferences.ContainsKey($ReferenceKey)){
|
||||
$Stack = @($Context.ResolvingReferences.Keys) + $ReferenceKey
|
||||
throw "Circular configuration data reference detected: $($Stack -join ' -> ')."
|
||||
}
|
||||
|
||||
$Context.ResolvingReferences[$ReferenceKey] = $true
|
||||
try {
|
||||
$RawValue = Get-ConfigurationDataPathValue -Value $Context.ConfigurationData -Path $Path
|
||||
$ResolvedValue = Resolve-ConfigurationDataValue -Value $RawValue -Context $Context
|
||||
|
||||
if(-not [string]::IsNullOrWhiteSpace($Property)){
|
||||
$ResolvedValue = Get-ConfigurationDataObjectPropertyValue -Value $ResolvedValue -Property $Property -Path $Path
|
||||
}
|
||||
|
||||
$Context.ReferenceCache[$ReferenceKey] = $ResolvedValue
|
||||
return $ResolvedValue
|
||||
}
|
||||
finally {
|
||||
$Context.ResolvingReferences.Remove($ReferenceKey)
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,9 @@ function Resolve-ConfigurationDataValue {
|
||||
if($Value -is [System.Collections.Specialized.OrderedDictionary]){
|
||||
$Resolved = [ordered]@{}
|
||||
foreach($Entry in $Value.GetEnumerator()){
|
||||
if($Entry.Name -eq "Sealed"){
|
||||
continue
|
||||
}
|
||||
$Resolved[$Entry.Name] = Resolve-ConfigurationDataValue -Value $Entry.Value -Context $Context
|
||||
}
|
||||
return $Resolved
|
||||
@@ -23,6 +26,9 @@ function Resolve-ConfigurationDataValue {
|
||||
if($Value -is [System.Collections.Hashtable]){
|
||||
$Resolved = @{}
|
||||
foreach($Entry in $Value.GetEnumerator()){
|
||||
if($Entry.Name -eq "Sealed"){
|
||||
continue
|
||||
}
|
||||
$Resolved[$Entry.Name] = Resolve-ConfigurationDataValue -Value $Entry.Value -Context $Context
|
||||
}
|
||||
return $Resolved
|
||||
|
||||
Reference in New Issue
Block a user