Azure / nxtools

Azure Automanage Machine Configuration Linux DSC resources
Other
13 stars 11 forks source link

nxUser: Using nxUser fails if any users in /etc/passwd have uppercase letters in their username #33

Closed coderjoe closed 1 year ago

coderjoe commented 1 year ago

Details of the scenario you tried and the problem that is occurring

Once I install the snmpd package on Ubuntu 22.04 it creates a user named Debian-snmp (note the uppercase D). Following this change every use of nxUser fails even if it does not reference that user.

ForEach-Object: Cannot convert value "Debian-snmp:x:116:124::/var/lib/snmp:/bin/false" to type
"nxLocalUser". Error: "Unrecognised passwd entry:
'Debian-snmp:x:116:124::/var/lib/snmp:/bin/false'."

Verbose logs showing the problem

VERBOSE: Populating RepositorySourceLocation property for module nxtools.
VERBOSE: Loading module from path '/root/.local/share/powershell/Modules/nxtools/1.2.0/nxtools.psm1'.
VERBOSE: The Get-DscResource input 'Name' parameter value is 'nxUser'.
VERBOSE: Skipping resource 'Reason' as it does not match the requested name.
VERBOSE: Skipping resource 'nxFile' as it does not match the requested name.
VERBOSE: Skipping resource 'nxGroup' as it does not match the requested name.
VERBOSE: Creating resource 'nxUser'.
VERBOSE: Skipping resource 'nxPackage' as it does not match the requested name.
VERBOSE: Skipping resource 'nxFileLine' as it does not match the requested name.
VERBOSE: Skipping resource 'nxFileContentReplace' as it does not match the requested name.
VERBOSE: Skipping resource 'nxService' as it does not match the requested name.
VERBOSE: Skipping resource 'nxScript' as it does not match the requested name.
VERBOSE: Skipping resource 'GC_LinuxGroup' as it does not match the requested name.
VERBOSE: Skipping resource 'GC_msid110' as it does not match the requested name.
VERBOSE: Skipping resource 'GC_msid121' as it does not match the requested name.
VERBOSE: Skipping resource 'GC_msid232' as it does not match the requested name.
VERBOSE: Skipping resource 'GC_InstalledApplicationLinux' as it does not match the requested name.
VERBOSE: Skipping resource 'GC_NotInstalledApplicationLinux' as it does not match the requested name.
VERBOSE: Skipping resource 'GC_LinuxLogAnalyticsAgent' as it does not match the requested name.
ForEach-Object: Cannot convert value "Debian-snmp:x:116:124::/var/lib/snmp:/bin/false" to type
"nxLocalUser". Error: "Unrecognised passwd entry:
'Debian-snmp:x:116:124::/var/lib/snmp:/bin/false'."

Suggested solution to the issue

The regular expression used to parse passwords appears to be wrong. It dies if there are any upper-case letters in the username.

Change source/Classes/nxLocalUser.ps1#4 through 12 to:

    static [regex] $PasswordLineParser = @(
        '^(?<username>[a-zA-Z_]([a-zA-Z0-9_-]{0,31}|[a-zA-Z0-9_-]{0,30}\$))'
        '(?<password>[^:]+)'
        '(?<userid>[\d]+)'
        '(?<groupid>[\d]+)'
        '(?<userinfo>[^:]*)'
        '(?<homedir>[^:]*)'
        '(?<shellcmd>[^:]*)'
    ) -join ':'

Example modified regexp: https://regexr.com/7k1fr

The DSC configuration that is used to reproduce the issue (as detailed as possible)

$commonParams = @{
        Name = 'nxUser'
        Property = @{ UserName = 'localadmin'; PasswordChangeRequired = $false; Ensure = 'Present' }
        ModuleName = 'nxTools'
        Verbose = $true
}
Invoke-DscResource @commonParams -Method Set

The operating system the target node is running

/tmp/dsc$ uname -a
Linux hostname 5.19.0-41-generic #42~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 18 17:40:00 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

/tmp/dsc$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:        22.04
Codename:       jammy

Version and build of PowerShell the target node is running

PS /tmp/dsc> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Linux 5.19.0-41-generic #42~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 18 17:40:00 UTC 2
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Version of the DSC module that was used

PS /tmp/dsc> Get-Module -Name 'nxtools' -ListAvailable | ft Name,Version,Path

Name    Version Path
----    ------- ----
nxtools 1.2.0   /root/.local/share/powershell/Modules/nxtools/1.2.0/nxtools.psd1