Skip to content

Commit 0530a16

Browse files
authored
Merge pull request #1308 from mimachniak/win_psDscAdapter-PSCredentials-fix-ScriptDSC
win_psDscAdapter and psDscAdapter PScredentials fix for passing username and password
2 parents 9cc1346 + 4e9ace3 commit 0530a16

12 files changed

Lines changed: 598 additions & 33 deletions

.github/workflows/rust.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ on:
1010
- "*.md"
1111
- ".vscode/*.json"
1212
- ".github/ISSUE_TEMPLATE/**"
13-
1413
env:
1514
CARGO_TERM_COLOR: always
1615

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
$VerbosePreference = 'SilentlyContinue'
2+
$InformationPreference = 'SilentlyContinue'
3+
$ProgressPreference = 'Continue'
4+
$ErrorActionPreference = 'SilentlyContinue'
5+
6+
function Get-TargetResource {
7+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
8+
[OutputType([Hashtable])]
9+
param (
10+
[Parameter(Mandatory)]
11+
[string] $Name,
12+
13+
[Parameter(Mandatory = $true)]
14+
[System.Management.Automation.PSCredential]
15+
$Credential
16+
)
17+
Write-Verbose "[GET] Get Function running"
18+
return @{
19+
Name = $Name
20+
Credential = $Credential
21+
}
22+
23+
}
24+
25+
function Test-TargetResource {
26+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
27+
[OutputType([System.Boolean])]
28+
param (
29+
[Parameter(Mandatory)]
30+
[string] $Name,
31+
32+
[Parameter(Mandatory = $true)]
33+
[System.Management.Automation.PSCredential]
34+
$Credential
35+
36+
)
37+
Write-Verbose "[TEST]Checking credentials"
38+
Write-Verbose "[TEST]Checking credentials UserName: $($Credential.UserName)"
39+
Write-Verbose "[TEST]Checking credentials Password: <redacted>"
40+
41+
if ($null -eq $Credential) {
42+
$inDesiredState = $false
43+
return $false
44+
}
45+
46+
if ($Credential.UserName -ne 'MyUser') {
47+
$inDesiredState = $false
48+
} else {
49+
$inDesiredState = $true
50+
}
51+
52+
53+
return $inDesiredState
54+
55+
}
56+
57+
function Set-TargetResource {
58+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
59+
[CmdletBinding()]
60+
param (
61+
[Parameter(Mandatory)]
62+
[string] $Name,
63+
64+
[Parameter(Mandatory = $true)]
65+
[System.Management.Automation.PSCredential]
66+
$Credential
67+
68+
)
69+
70+
if ($null -eq $Credential) {
71+
$inDesiredState = $false
72+
return $false
73+
}
74+
75+
if ($Credential.UserName -ne 'MyUser') {
76+
$inDesiredState = $false
77+
} else {
78+
$inDesiredState = $true
79+
}
80+
81+
Write-Verbose "[SET]Credential cannot be remediated by DSC."
82+
return $inDesiredState
83+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[ClassVersion("1.0.0.0"), FriendlyName("CredentialValidation")]
2+
class CredentialValidation : OMI_BaseResource
3+
{
4+
[Key] string Name;
5+
[Required, Description("Test Credentials for Script Base"), EmbeddedInstance("MSFT_Credential")] String Credential;
6+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
@{
2+
# Script module or binary module file associated with this manifest.
3+
RootModule = 'TestScriptBaseDSC.psm1'
4+
5+
# Version number of this module.
6+
moduleVersion = '0.0.1'
7+
8+
# ID used to uniquely identify this module
9+
GUID = 'c3775be8-84a1-43f5-a99c-1b9f2d6bc178'
10+
11+
# Author of this module
12+
Author = ''
13+
14+
# Company or vendor of this module
15+
CompanyName = ''
16+
17+
# Copyright statement for this module
18+
Copyright = ''
19+
20+
# Description of the functionality provided by this module
21+
Description = ''
22+
23+
# Minimum version of the Windows PowerShell engine required by this module
24+
PowerShellVersion = '5.0'
25+
26+
# Cmdlets to export from this module
27+
CmdletsToExport = @()
28+
29+
# Variables to export from this module
30+
VariablesToExport = @()
31+
32+
# Aliases to export from this module
33+
AliasesToExport = @()
34+
35+
# Dsc Resources to export from this module
36+
DscResourcesToExport = @('CredentialValidation')
37+
38+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
39+
PrivateData = @{
40+
41+
PSData = @{
42+
43+
# Tags applied to this module. These help with module discovery in online galleries.
44+
Tags = @('DesiredStateConfiguration', 'DSC', 'DSCResourceKit', 'DSCResource')
45+
46+
# A URL to the license for this module.
47+
LicenseUri = ''
48+
49+
# A URL to the main website for this project.
50+
ProjectUri = ''
51+
52+
# A URL to an icon representing this module.
53+
IconUri = ''
54+
55+
# ReleaseNotes of this module
56+
ReleaseNotes = ''
57+
58+
# Set to a prerelease string value if the release should be a prerelease.
59+
Prerelease = ''
60+
} # End of PSData hashtable
61+
} # End of PrivateData hashtable
62+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Root module for CredentialValidationDsc
2+
# No code required
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
5+
parameters:
6+
showSecrets:
7+
type: bool
8+
defaultValue: true
9+
cred:
10+
type: secureObject
11+
metadata:
12+
Microsoft.DSC:
13+
requiredSecurityContext: elevated # this is the default and just used as an example indicating this config works for admins and non-admins
14+
resources:
15+
- name: Working with classic DSC resources
16+
type: Microsoft.DSC/PowerShell
17+
properties:
18+
resources:
19+
- name: Class-resource Info
20+
type: TestClassResource/TestClassResource
21+
properties:
22+
Name: TestClassResource1
23+
Prop1: ValueForProp1
24+
Credential: "[parameters('cred')]"
25+
- name: SecureObject
26+
type: Microsoft.DSC.Debug/Echo
27+
properties:
28+
output: "[parameters('cred')]"
29+
showSecrets: "[parameters('showSecrets')]"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
cred:
3+
username: admin
4+
password: {To be Override}

adapters/powershell/Tests/powershellgroup.config.tests.ps1

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,28 @@ Describe 'PowerShell adapter resource tests' {
300300
It 'Config works with credential object' {
301301
$yaml = @"
302302
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
303+
parameters:
304+
Credential:
305+
type: secureObject
306+
defaultValue:
307+
username: User
308+
password: Password
303309
resources:
304-
- name: Class-resource Info
305-
type: TestClassResource/TestClassResource
310+
- name: Working with classic DSC resources
311+
type: Microsoft.DSC/PowerShell
306312
properties:
307-
Name: 'TestClassResource'
308-
Credential:
309-
UserName: 'User'
310-
Password: 'Password'
313+
resources:
314+
- name: Class-resource Info
315+
type: TestClassResource/TestClassResource
316+
properties:
317+
Name: TestClassResource1
318+
Prop1: ValueForProp1
319+
Credential: "[parameters('Credential')]"
311320
"@
312321
$out = dsc config get -i $yaml | ConvertFrom-Json
313322
$LASTEXITCODE | Should -Be 0
314-
$out.results.result.actualstate.Credential.UserName | Should -Be 'User'
315-
$out.results.result.actualState.result.Credential.Password.Length | Should -Not -BeNullOrEmpty
323+
$out.results.result.actualstate.result.properties.Credential.UserName | Should -Be 'User'
324+
$out.results.result.actualState.result.properties.Credential.Password.Length | Should -Not -BeNullOrEmpty
316325
}
317326

318327
It 'Config does not work when credential properties are missing required fields' {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
5+
parameters:
6+
cred:
7+
type: secureObject
8+
defaultValue:
9+
username: MyUser
10+
password: Password
11+
resources:
12+
- name: Working with classic DSC resources
13+
type: Microsoft.Windows/WindowsPowerShell
14+
properties:
15+
resources:
16+
- name: Script-resource Info
17+
type: TestScriptBaseDSC/CredentialValidation
18+
properties:
19+
Name: TestScriptResource1
20+
Credential: "[parameters('cred')]"

0 commit comments

Comments
 (0)