diff --git a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psd1 b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psd1 index 271ff64..735cc3e 100644 --- a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psd1 +++ b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psd1 @@ -34,7 +34,7 @@ RequiredModules = @( ) # Functions to export from this module -FunctionsToExport = @('Connect-SsoAdminServer', 'Disconnect-SsoAdminServer', 'New-PersonUser', 'Get-PersonUser', 'Set-PersonUser', 'Remove-PersonUser', 'Get-Group', 'Get-PasswordPolicy', 'Set-PasswordPolicy') +FunctionsToExport = @('Connect-SsoAdminServer', 'Disconnect-SsoAdminServer', 'New-PersonUser', 'Get-PersonUser', 'Set-PersonUser', 'Remove-PersonUser', 'Get-Group', 'Get-PasswordPolicy', 'Set-PasswordPolicy', 'Get-LockoutPolicy', 'Set-LockoutPolicy') # Cmdlets to export from this module CmdletsToExport = @() diff --git a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 index 9f06eee..932a9ad 100644 --- a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 +++ b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 @@ -940,4 +940,156 @@ function Set-PasswordPolicy { } } } +#endregion + +#region LockoutPolicy cmdlets +function Get-LockoutPolicy { +<# + .NOTES + =========================================================================== + Created on: 9/30/2020 + Created by: Dimitar Milov + Twitter: @dimitar_milov + Github: https://github.com/dmilov + =========================================================================== + .DESCRIPTION + This function gets lockout policy. + + .PARAMETER Server + Specifies the vSphere Sso Admin Server on which you want to run the cmdlet. + If not specified the servers available in $global:DefaultSsoAdminServers variable will be used. + + .EXAMPLE + Get-LockoutPolicy + + Gets lockout policy for the server connections available in $global:defaultSsoAdminServers +#> +[CmdletBinding()] + param( + [Parameter( + Mandatory=$false, + ValueFromPipeline=$false, + ValueFromPipelineByPropertyName=$false, + HelpMessage='Connected SsoAdminServer object')] + [ValidateNotNull()] + [VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer] + $Server) + + Process { + $serversToProcess = $global:DefaultSsoAdminServers + if ($Server -ne $null) { + $serversToProcess = $Server + } + foreach ($connection in $serversToProcess) { + if (-not $connection.IsConnected) { + Write-Error "Server $connection is disconnected" + continue + } + + $connection.Client.GetLockoutPolicy(); + } + } +} + +function Set-LockoutPolicy { +<# + .NOTES + =========================================================================== + Created on: 9/30/2020 + Created by: Dimitar Milov + Twitter: @dimitar_milov + Github: https://github.com/dmilov + =========================================================================== + .DESCRIPTION + This function updates lockout policy settings. + + .PARAMETER LockoutPolicy + Specifies the LockoutPolicy instance which will be used as original policy. If some properties are not specified they will be updated with the properties from this object. + + .PARAMETER Description + + .PARAMETER AutoUnlockIntervalSec + + .PARAMETER FailedAttemptIntervalSec + + .PARAMETER MaxFailedAttempts + + .EXAMPLE + Get-LockoutPolicy | Set-LockoutPolicy -AutoUnlockIntervalSec 15 -MaxFailedAttempts 4 + + Updates lockout policy auto unlock interval seconds and maximum failed attempts +#> +[CmdletBinding()] + param( + [Parameter( + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$false, + HelpMessage='LockoutPolicy instance you want to update')] + [VMware.vSphere.SsoAdminClient.DataTypes.LockoutPolicy] + $LockoutPolicy, + + [Parameter( + Mandatory=$false, + ValueFromPipeline=$false, + ValueFromPipelineByPropertyName=$false, + HelpMessage='LockoutPolicy description')] + [string] + $Description, + + [Parameter( + Mandatory=$false, + ValueFromPipeline=$false, + ValueFromPipelineByPropertyName=$false)] + [Nullable[System.Int64]] + $AutoUnlockIntervalSec, + + [Parameter( + Mandatory=$false, + ValueFromPipeline=$false, + ValueFromPipelineByPropertyName=$false)] + [Nullable[System.Int64]] + $FailedAttemptIntervalSec, + + [Parameter( + Mandatory=$false, + ValueFromPipeline=$false, + ValueFromPipelineByPropertyName=$false)] + [Nullable[System.Int32]] + $MaxFailedAttempts) + + Process { + + foreach ($lp in $LockoutPolicy) { + + $ssoAdminClient = $lp.GetClient() + if ((-not $ssoAdminClient)) { + Write-Error "Object '$lp' is from disconnected server" + continue + } + + if ([string]::IsNullOrEmpty($Description)) { + $Description = $lp.Description + } + + if ($AutoUnlockIntervalSec -eq $null) { + $AutoUnlockIntervalSec = $lp.AutoUnlockIntervalSec + } + + if ($FailedAttemptIntervalSec -eq $null) { + $FailedAttemptIntervalSec = $lp.FailedAttemptIntervalSec + } + + if ($MaxFailedAttempts -eq $null) { + $MaxFailedAttempts = $lp.MaxFailedAttempts + } + + $ssoAdminClient.SetLockoutPolicy( + $Description, + $AutoUnlockIntervalSec, + $FailedAttemptIntervalSec, + $MaxFailedAttempts); + } + } +} #endregion \ No newline at end of file diff --git a/Modules/VMware.vSphere.SsoAdmin/net45/VMware.vSphere.SsoAdminClient.dll b/Modules/VMware.vSphere.SsoAdmin/net45/VMware.vSphere.SsoAdminClient.dll index 67323cd..1f73a75 100644 Binary files a/Modules/VMware.vSphere.SsoAdmin/net45/VMware.vSphere.SsoAdminClient.dll and b/Modules/VMware.vSphere.SsoAdmin/net45/VMware.vSphere.SsoAdminClient.dll differ diff --git a/Modules/VMware.vSphere.SsoAdmin/netcoreapp2.0/VMware.vSphere.SsoAdminClient.dll b/Modules/VMware.vSphere.SsoAdmin/netcoreapp2.0/VMware.vSphere.SsoAdminClient.dll index 2227ff4..50c208d 100644 Binary files a/Modules/VMware.vSphere.SsoAdmin/netcoreapp2.0/VMware.vSphere.SsoAdminClient.dll and b/Modules/VMware.vSphere.SsoAdmin/netcoreapp2.0/VMware.vSphere.SsoAdminClient.dll differ diff --git a/Modules/VMware.vSphere.SsoAdmin/src/VMware.vSphere.SsoAdmin.Client/VMware.vSphere.SsoAdminClient.Tests/IntegrationTests.cs b/Modules/VMware.vSphere.SsoAdmin/src/VMware.vSphere.SsoAdmin.Client/VMware.vSphere.SsoAdminClient.Tests/IntegrationTests.cs index db923c2..1e9f639 100644 --- a/Modules/VMware.vSphere.SsoAdmin/src/VMware.vSphere.SsoAdmin.Client/VMware.vSphere.SsoAdminClient.Tests/IntegrationTests.cs +++ b/Modules/VMware.vSphere.SsoAdmin/src/VMware.vSphere.SsoAdmin.Client/VMware.vSphere.SsoAdminClient.Tests/IntegrationTests.cs @@ -10,7 +10,7 @@ namespace VMware.vSphere.SsoAdminClient.Tests { private string _vc = ""; private string _user = ""; - private string _rawPassword = ""; + private string _rawPassword = " + _ssoAdminBindingClient.GetLockoutPolicyAsync( + new ManagedObjectReference { + type = "SsoAdminLockoutPolicyService", + Value = "lockoutPolicyService" + })).Result; + + if (ssoAdminLockoutPolicy != null) { + result = new LockoutPolicy(this) { + Description = ssoAdminLockoutPolicy.description, + AutoUnlockIntervalSec = ssoAdminLockoutPolicy.autoUnlockIntervalSec, + FailedAttemptIntervalSec = ssoAdminLockoutPolicy.failedAttemptIntervalSec, + MaxFailedAttempts = ssoAdminLockoutPolicy.maxFailedAttempts + }; + } + + return result; + } + + public LockoutPolicy SetLockoutPolicy( + string description, + long? autoUnlockIntervalSec, + long? failedAttemptIntervalSec, + int? maxFailedAttempts) { + + if (description != null || + autoUnlockIntervalSec != null || + failedAttemptIntervalSec != null || + maxFailedAttempts != null) { + + var ssoAdminLockoutPolicy = new SsoAdminLockoutPolicy(); + + ssoAdminLockoutPolicy.description = description; + + if (autoUnlockIntervalSec != null) { + ssoAdminLockoutPolicy.autoUnlockIntervalSec = autoUnlockIntervalSec.Value; + } + + if (failedAttemptIntervalSec != null) { + ssoAdminLockoutPolicy.failedAttemptIntervalSec = failedAttemptIntervalSec.Value; + } + + if (maxFailedAttempts != null) { + ssoAdminLockoutPolicy.maxFailedAttempts = maxFailedAttempts.Value; + } + + // Create Authorization Invocation Context + var authorizedInvocationContext = + CreateAuthorizedInvocationContext(); + + // Invoke SSO Admin GetLockoutPolicyAsync operation + authorizedInvocationContext. + InvokeOperation(() => + _ssoAdminBindingClient.UpdateLockoutPolicyAsync( + new ManagedObjectReference { + type = "SsoAdminLockoutPolicyService", + Value = "lockoutPolicyService" + }, + ssoAdminLockoutPolicy)).Wait(); + + } + + return GetLockoutPolicy(); + } #endregion } } diff --git a/Modules/VMware.vSphere.SsoAdmin/src/test/LockoutPolicy.Tests.ps1 b/Modules/VMware.vSphere.SsoAdmin/src/test/LockoutPolicy.Tests.ps1 new file mode 100644 index 0000000..c889602 --- /dev/null +++ b/Modules/VMware.vSphere.SsoAdmin/src/test/LockoutPolicy.Tests.ps1 @@ -0,0 +1,73 @@ +#************************************************************************** +# Copyright (c) VMware, Inc. All rights reserved. +#************************************************************************** + +param( + [Parameter(Mandatory = $true)] + [string] + $VcAddress, + + [Parameter(Mandatory = $true)] + [string] + $User, + + [Parameter(Mandatory = $true)] + [string] + $Password +) + +# Import Vmware.vSphere.SsoAdmin Module +$modulePath = Join-Path (Split-Path $PSScriptRoot | Split-Path) "VMware.vSphere.SsoAdmin.psd1" +Import-Module $modulePath + +Describe "LockoutPolicy Tests" { + BeforeEach { + Connect-SsoAdminServer ` + -Server $VcAddress ` + -User $User ` + -Password $Password ` + -SkipCertificateCheck + } + + AfterEach { + $connectionsToCleanup = $global:DefaultSsoAdminServers.ToArray() + foreach ($connection in $connectionsToCleanup) { + Disconnect-SsoAdminServer -Server $connection + } + } + + Context "Get-LockoutPolicy" { + It 'Gets lockout policy' { + # Act + $actual = Get-LockoutPolicy + + # Assert + $actual | Should Not Be $null + } + } + + Context "Set-LockoutPolicy" { + It 'Updates lockout policy AutoUnlockIntervalSec and MaxFailedAttempts' { + # Arrange + $lockoutPolicyToUpdate = Get-LockoutPolicy + $expectedAutoUnlockIntervalSec = 33 + $expectedMaxFailedAttempts = 7 + + # Act + $actual = Set-LockoutPolicy ` + -LockoutPolicy $lockoutPolicyToUpdate ` + -AutoUnlockIntervalSec $expectedAutoUnlockIntervalSec ` + -MaxFailedAttempts $expectedMaxFailedAttempts + + # Assert + $actual | Should Not Be $null + $actual.AutoUnlockIntervalSec | Should Be $expectedAutoUnlockIntervalSec + $actual.MaxFailedAttempts | Should Be $expectedMaxFailedAttempts + $actual.FailedAttemptIntervalSec | Should Be $lockoutPolicyToUpdate.FailedAttemptIntervalSec + $actual.Description | Should Be $lockoutPolicyToUpdate.Description + + # Cleanup + $lockoutPolicyToUpdate | Set-LockoutPolicy + } + } +} \ No newline at end of file