Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
237 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,4 +123,4 @@ last_modified_at: 2021-03-03T00:00:00-00:00 | |
|
||
# RELATED LINKS: | ||
Get-TssUser | ||
Get-TssUserStub | ||
New-TssUser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
function New-User { | ||
<# | ||
.SYNOPSIS | ||
Create a new Secret Server User | ||
.DESCRIPTION | ||
Create a new Secret Server User | ||
.LINK | ||
https://thycotic-ps.github.io/thycotic.secretserver/commands/New-TssUser | ||
.LINK | ||
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/<folder>/New-User.ps1 | ||
.NOTES | ||
Requires TssSession object returned by New-TssSession | ||
#> | ||
[CmdletBinding(SupportsShouldProcess)] | ||
[OutputType('TssUser')] | ||
param ( | ||
# TssSession object created by New-TssSession for auth | ||
[Parameter(Mandatory,ValueFromPipeline,Position = 0)] | ||
[TssSession] | ||
$TssSession, | ||
|
||
# Username | ||
[Parameter(Mandatory,ValueFromPipeline)] | ||
[string] | ||
$Username, | ||
|
||
# Display Name | ||
[Parameter(Mandatory,ValueFromPipeline)] | ||
[string] | ||
$DisplayName, | ||
|
||
# Password (for local only) | ||
[Parameter(Mandatory)] | ||
[securestring] | ||
$Password, | ||
|
||
# Enable the account on creation | ||
[switch] | ||
$Active, | ||
|
||
# Create as an application account | ||
[switch] | ||
$IsApplicationAccount, | ||
|
||
# Email address | ||
[string] | ||
$EmailAddress, | ||
|
||
# Active Directory Domain ID | ||
[ValidateRange(-1,[int]::MaxValue)] | ||
[int] | ||
$DomainId, | ||
|
||
# Active Directory GUID | ||
[ValidateLength(36,50)] | ||
[string] | ||
$AdGuid, | ||
|
||
# 2FA type | ||
[ValidateSet('DUO', 'FIDO', 'RADIUS', 'OATH')] | ||
[string] | ||
$TwoFactorType, | ||
|
||
[string] | ||
$RadiusUsername | ||
) | ||
begin { | ||
$tssNewParams = $PSBoundParameters | ||
$invokeParams = . $GetInvokeTssParams $TssSession | ||
} | ||
process { | ||
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)" | ||
if ($tssNewParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) { | ||
. $CheckVersion $TssSession '10.9.000000' $PSCmdlet.MyInvocation | ||
$restResponse = $null | ||
$uri = $TssSession.ApiUrl, 'users' -join '/' | ||
$invokeParams.Uri = $uri | ||
$invokeParams.Method = 'POST' | ||
|
||
$newBody = [ordered]@{} | ||
switch ($tssNewParams.Keys) { | ||
'Username' { $newBody.Add('userName',$Username) } | ||
'DisplayName' { $newBody.Add('displayName',$DisplayName) } | ||
'Password' { | ||
$passwd = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)) | ||
$newBody.Add('password',$passwd) | ||
} | ||
'Active' { $newBody.Add('enabled',[boolean]$Active) } | ||
'IsApplicationAccount' { $newBody.Add('isApplicationAccount',[boolean]$IsApplicationAccount) } | ||
'EmailAddress' { $newBody.Add('emailAddress',$EmailAddress) } | ||
'DomainId' { $newBody.Add('domainId',$DomainId) } | ||
'AdGuid' { $newBody.Add('adGuid',$AdGuid) } | ||
'TwoFactorType' { | ||
if ($TwoFactorType -eq 'RADIUS' -and -not $tssNewParams.ContainsKey('RadiusUsername')) { | ||
Write-Warning 'Radius Username is required to create a user with RADIUS 2FA' | ||
return | ||
} | ||
$newBody.Add('radiusTwoFactor',$true) | ||
$newBody.Add('radiusUserName',$RadiusUsername) | ||
} | ||
} | ||
|
||
$invokeParams.Body = ($newBody | ConvertTo-Json) | ||
|
||
Write-Verbose "Performing the operation $($invokeParams.Method) $uri with:`n $newBody" | ||
if (-not $PSCmdlet.ShouldProcess("", "$($invokeParams.Method) $uri with $($invokeParams.Body)")) { return } | ||
try { | ||
$restResponse = . $InvokeApi @invokeParams | ||
} catch { | ||
Write-Warning "Issue creating report [User]" | ||
$err = $_ | ||
. $ErrorHandling $err | ||
} | ||
|
||
if ($restResponse) { | ||
[TssUser]$restResponse | ||
} | ||
} else { | ||
Write-Warning "No valid session found" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
BeforeDiscovery { | ||
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf | ||
. ([IO.Path]::Combine([string]$PSScriptRoot, '..', 'constants.ps1')) | ||
} | ||
Describe "$commandName verify parameters" { | ||
BeforeDiscovery { | ||
[object[]]$knownParameters = 'TssSession', 'Username', 'DisplayName', 'Password', 'Active', 'IsApplicationAccount', 'EmailAddress', 'DomainId', 'AdGuid', 'TwoFactorType' , 'RadiusUsername' | ||
[object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')).Parameters.Keys | ||
[object[]]$commandDetails = [System.Management.Automation.CommandInfo]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function') | ||
$unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru | ||
} | ||
Context "Verify parameters" -Foreach @{currentParams = $currentParams } { | ||
It "$commandName should contain <_> parameter" -TestCases $knownParameters { | ||
$_ -in $currentParams | Should -Be $true | ||
} | ||
It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters { | ||
$_ | Should -BeNullOrEmpty | ||
} | ||
} | ||
Context "Command specific details" { | ||
It "$commandName should set OutputType to TssUser" -TestCases $commandDetails { | ||
$_.OutputType.Name | Should -Be 'TssUser' | ||
} | ||
} | ||
} | ||
Describe "$commandName functions" { | ||
Context "Checking" { | ||
BeforeAll { | ||
$session = [pscustomobject]@{ | ||
ApiVersion = 'api/v1' | ||
Take = 2147483647 | ||
SecretServer = 'http://alpha/' | ||
ApiUrl = 'http://alpha/api/v1' | ||
AccessToken = 'AgJf5YLChrisPine312UcBrM1s1KB2BGZ5Ufc4qLZ' | ||
RefreshToken = '9oacYeah0YqgBNg0L7VinDiesel6-Z9ITE51Humus' | ||
TokenType = 'bearer' | ||
ExpiresIn = 1199 | ||
} | ||
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match '/version' } -MockWith { | ||
return @{ | ||
model = [pscustomobject]@{ | ||
Version = '10.9.000033' | ||
} | ||
} | ||
} | ||
|
||
$userId = 42 | ||
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match '/users' } -MockWith { | ||
return [pscustomobject]@{ | ||
adAccountExpires = "1970-01-01T00:00:00.000Z" | ||
adGuid = $null | ||
created = "1970-01-01T00:00:00.000Z" | ||
dateOptionId = 0 | ||
displayName = "New User" | ||
domainId = 0 | ||
duoTwoFactor = $false | ||
emailAddress = "user@new.local" | ||
enabled = $false | ||
fido2TwoFactor = $false | ||
id = $userId | ||
isApplicationAccount = $false | ||
isEmailCopiedFromAD = $false | ||
isEmailVerified = $false | ||
isLockedOut = $false | ||
lastLogin = "1970-01-01T00:00:00.000Z" | ||
lastSessionActivity = "1970-01-01T00:00:00.000Z" | ||
lockOutReason = "string" | ||
lockOutReasonDescription = "string" | ||
loginFailures = 0 | ||
mustVerifyEmail = $false | ||
oathTwoFactor = $false | ||
oathVerified = $false | ||
passwordLastChanged = "1970-01-01T00:00:00.000Z" | ||
radiusTwoFactor = $false | ||
radiusUserName = "string" | ||
resetSessionStarted = "1970-01-01T00:00:00.000Z" | ||
timeOptionId = 0 | ||
twoFactor = $false | ||
unixAuthenticationMethod = 'Password' | ||
userLcid = 0 | ||
userName = "user" | ||
verifyEmailSentDate = "1970-01-01T00:00:00.000Z" | ||
} | ||
} | ||
$object = New-User -TssSession $session -Username 'user' -DisplayName 'New User' -Password (ConvertTo-SecureString 'pass' -AsPlainText -Force) -Email 'user@new.local' | ||
Assert-VerifiableMock | ||
} | ||
It "Should not be empty" { | ||
$object | Should -Not -BeNullOrEmpty | ||
} | ||
It "Should have property <_>" -TestCases 'Id', 'DisplayName', 'EmailAddress' { | ||
$object[0].PSObject.Properties.Name | Should -Contain $_ | ||
} | ||
It "Should have property DisplayName equal 'New User'" { | ||
$object.DisplayName | Should -Be 'New User' | ||
} | ||
It "Should have called Invoke-RestMethod 2 times" { | ||
Assert-MockCalled -CommandName Invoke-RestMethod -Times 2 -Scope Describe | ||
} | ||
} | ||
} |