Skip to content

Ensure Sensitive handling of PSCredentials; module version munging; DSC metadata in metadata.json #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/functions/Update-PuppetModuleMetadata.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ function Update-PuppetModuleMetadata {
$PuppetMetadata.name = $PuppetMetadata.name -replace '(^\S+)-(\S+)', "$PuppetModuleAuthor-`$2"
$PuppetMetadata.author = $PuppetModuleAuthor
}
$PuppetMetadata.version = $PowerShellMetadata.ModuleVersion
$PuppetMetadata.version = Get-PuppetModuleVersion -Version $PowerShellMetadata.ModuleVersion
$PuppetMetadata.summary = $PowerShellMetadata.Description -Replace "(`r`n|`n)", '`n'
$PuppetMetadata.source = $PowerShellMetadata.PrivateData.PSData.ProjectUri
# If we can find the issues page, link to it, otherwise default to project page.
Switch -Regex ($PowerShellMetadata.PrivateData.PSData.ProjectUri) {
'(github\.com|gitlab\.com|bitbucket\.com)' {
$IssueUri = $PowerShellMetadata.PrivateData.PSData.ProjectUri + '/issues'
Try {
Invoke-WebRequest -Uri $IssueUri -UseBasicParsing -ErrorAction Stop
$null = Invoke-WebRequest -Uri $IssueUri -UseBasicParsing -ErrorAction Stop
$PuppetMetadata | Add-Member -MemberType NoteProperty -Name issues_url -Value $IssueUri
} Catch {
$PuppetMetadata | Add-Member -MemberType NoteProperty -Name issues_url -Value $PowerShellMetadata.PrivateData.PSData.ProjectUri
Expand Down Expand Up @@ -85,6 +85,13 @@ function Update-PuppetModuleMetadata {
)
# Clarify Puppet lower bound
$PuppetMetadata.requirements[0].version_requirement = '>= 6.0.0 < 7.0.0'
# Add new metadata sections
$PuppetMetadata | Add-Member -MemberType NoteProperty -Name dsc_module_metadata -Value @{
name = Get-Module -ListAvailable -Name $PowerShellModuleManifestPath | Select-Object -ExpandProperty Name
version = $PowerShellMetadata.ModuleVersion
author = $PowerShellMetadata.Author
guid = $PowerShellMetadata.Guid
}
$PuppetMetadataJson = ConvertTo-UnescapedJson -InputObject $PuppetMetadata -Depth 10
If ($PSCmdlet.ShouldProcess($PuppetModuleMetadataFilePath, "Overwrite Puppet Module metadata with:`n`n$PuppetMetadataJson")) {
Out-Utf8File -Path $PuppetModuleMetadataFilePath -InputObject $PuppetMetadataJson
Expand Down
2 changes: 1 addition & 1 deletion src/internal/functions/Get-PuppetDataType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Function Get-PuppetDataType {
'int64' { 'Integer[-9223372036854775808, 9223372036854775807]' }
{ $_ -in $OtherIntegers } { 'Integer' }
{ $_ -in $Floats } { 'Float' }
'PSCredential' { 'Struct[{ user => String[1], password => String[1] }]' }
'PSCredential' { 'Struct[{ user => String[1], password => Sensitive[String[1]] }]' }
# Can we mandate that an attribute be a sensitive string? Does this even make sense?
'SecureString' { 'Sensitive' }
# TODO: Should this just be a string? Do we need/want to validate this?
Expand Down
37 changes: 37 additions & 0 deletions src/internal/functions/Get-PuppetModuleVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Function Get-PuppetModuleVersion {
<#
.SYNOPSIS
Get a valid Puppet module version from a PowerShell version object
.DESCRIPTION
Get a valid Puppet module version from a PowerShell version object, writing a string which
adds prerelease text for the revision version (if any), and a fifth digit representing the
build version, starting at 0.
.PARAMETER Version
The PowerShell version to base the Puppet version on.
.PARAMETER BuildNumber
The build number for the generated module.
.EXAMPLE
Get-PuppetModuleVersion -Version 1.2.3

This will return '1.2.3-0-0' as the valid Puppet module version mapping to the specified PowerShell.
.EXAMPLE
Get-PuppetModuleVersion -Version 1.2.3.4

This will return '1.2.3-4-0' as the valid Puppet module version mapping to the specified PowerShell.
.EXAMPLE
Get-PuppetModuleVersion -Version 1.2.3 -BuildNumber 3

This will return '1.2.3-0-3' as the valid Puppet module version mapping to the specified PowerShell.
#>
[cmdletbinding()]
[OutputType([String])]
Param (
[version]$Version,
[int]$BuildNumber = 0
)
If ($Version.Revision -gt 0) {
"$($Version.Major).$($Version.Minor).$($Version.Build)-$($Version.Revision)-$BuildNumber"
} Else {
"$($Version.Major).$($Version.Minor).$($Version.Build)-0-$BuildNumber"
}
}
2 changes: 1 addition & 1 deletion src/tests/functions/Get-PuppetDataType.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Describe 'Get-PuppetDataType' {
Get-PuppetDataType -DscResourceProperty (New-DscParameter -PropertyType '[Bool]') | Should -BeExactly """Optional[Boolean]"""
Get-PuppetDataType -DscResourceProperty (New-DscParameter -PropertyType '[Byte]') | Should -BeExactly """Optional[Integer[0, 255]]"""
Get-PuppetDataType -DscResourceProperty (New-DscParameter -PropertyType '[int]') | Should -BeExactly """Optional[Integer[-2147483648, 2147483647]]"""
Get-PuppetDataType -DscResourceProperty (New-DscParameter -PropertyType '[PSCredential]') | Should -BeExactly """Optional[Struct[{ user => String[1], password => String[1] }]]"""
Get-PuppetDataType -DscResourceProperty (New-DscParameter -PropertyType '[PSCredential]') | Should -BeExactly """Optional[Struct[{ user => String[1], password => Sensitive[String[1]] }]]"""
Get-PuppetDataType -DscResourceProperty (New-DscParameter -PropertyType '[SecureString]') | Should -BeExactly """Optional[Sensitive]"""
Get-PuppetDataType -DscResourceProperty (New-DscParameter -PropertyType '[DateTime]') | Should -BeExactly """Optional[Timestamp]"""
Get-PuppetDataType -DscResourceProperty (New-DscParameter -PropertyType '[HashTable]') | Should -BeExactly """Optional[Hash]"""
Expand Down
12 changes: 12 additions & 0 deletions src/tests/functions/Get-PuppetModuleVersion.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Describe 'Get-PuppetModuleVersion' {
InModuleScope puppet.dsc {
Context 'Basic functionality' {
It 'Returns a valid Puppet module version' {
Get-PuppetModuleVersion -Version '1.2.3' | Should -BeExactly '1.2.3-0-0'
Get-PuppetModuleVersion -Version '1.2.3.0' | Should -BeExactly '1.2.3-0-0'
Get-PuppetModuleVersion -Version '1.2.3.1' | Should -BeExactly '1.2.3-1-0'
Get-PuppetModuleVersion -Version '1.2.3.1' -Build 1 | Should -BeExactly '1.2.3-1-1'
}
}
}
}
8 changes: 7 additions & 1 deletion src/tests/functions/Update-PuppetModuleMetadata.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Describe 'Update-PuppetModuleMetadata' {
Assert-MockCalled Out-Utf8File -Times 1
}
It 'Updates the version' {
$Result.version | Should -Be '2.2.3'
$Result.version | Should -Be '2.2.3-0-0'
}
It 'Updates the summary' {
$Result.summary | Should -Be 'PowerShell module with commands for discovering, installing, updating and publishing the PowerShell artifacts like Modules, DSC Resources, Role Capabilities and Scripts.'
Expand Down Expand Up @@ -83,6 +83,12 @@ Describe 'Update-PuppetModuleMetadata' {
It 'Updates the Puppet lower bound' {
$Result.requirements[0].version_requirement | Should -Be '>= 6.0.0 < 7.0.0'
}
It 'Adds metadata about the Puppetized PowerShell module' {
$Result.dsc_module_metadata.name | Should -BeExactly 'PowerShellGet'
$Result.dsc_module_metadata.version | Should -BeExactly '2.2.3'
$Result.dsc_module_metadata.author | Should -BeExactly 'Microsoft Corporation'
$Result.dsc_module_metadata.guid | Should -BeExactly '1d73a601-4a6c-43c5-ba3f-619b18bbb404'
}
}
Context 'Edge Cases' {
Context 'Issues Url' {
Expand Down