Skip to content

Commit

Permalink
PowerCommander: non-interactive environment
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-keeper committed Nov 12, 2024
1 parent 28234e5 commit 4047242
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
32 changes: 24 additions & 8 deletions PowerCommander/AuthCommands.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ $expires = @(
[KeeperSecurity.Authentication.TwoFactorDuration]::Every30Days,
[KeeperSecurity.Authentication.TwoFactorDuration]::Forever)

function Test-InteractiveSession {
return $Host.Name -eq 'ConsoleHost' -and $Host.UI.SupportsVirtualTerminal
}

function twoFactorChannelToText ([KeeperSecurity.Authentication.TwoFactorChannel] $channel) {
if ($channel -eq [KeeperSecurity.Authentication.TwoFactorChannel]::Authenticator) {
return 'authenticator'
Expand Down Expand Up @@ -356,6 +360,7 @@ function Connect-Keeper {
} else {
$storage = New-Object KeeperSecurity.Configuration.JsonConfigurationStorage
}

if (-not $Server) {
$Server = $storage.LastServer
if ($Server) {
Expand Down Expand Up @@ -389,11 +394,14 @@ function Connect-Keeper {
if ($Username) {
Write-Output "$(($namePrompt + ': ').PadLeft(21, ' ')) $Username"
}
else {
elseif (Test-InteractiveSession) {
while (-not $Username) {
$Username = Read-Host -Prompt $namePrompt.PadLeft(20, ' ')
}
} else {
Write-Error "Non-interactive session detected" -ErrorAction Stop
}

if ($SsoProvider.IsPresent) {
$authFlow.LoginSso($Username).GetAwaiter().GetResult() | Out-Null
}
Expand All @@ -419,16 +427,24 @@ function Connect-Keeper {
$prompt = getStepPrompt $authFlow

if ($authFlow.Step -is [KeeperSecurity.Authentication.Sync.PasswordStep]) {
$securedPassword = Read-Host -Prompt $prompt -AsSecureString
if ($securedPassword.Length -gt 0) {
$action = [Net.NetworkCredential]::new('', $securedPassword).Password
}
else {
$action = ''
if (Test-InteractiveSession) {
$securedPassword = Read-Host -Prompt $prompt -AsSecureString
if ($securedPassword.Length -gt 0) {
$action = [Net.NetworkCredential]::new('', $securedPassword).Password
}
else {
$action = ''
}
} else {
Write-Error "Non-interactive session detected" -ErrorAction Stop
}
}
else {
$action = Read-Host -Prompt $prompt
if (Test-InteractiveSession) {
$action = Read-Host -Prompt $prompt
} else {
Write-Error "Non-interactive session detected" -ErrorAction Stop
}
}

if ($action) {
Expand Down
11 changes: 8 additions & 3 deletions PowerCommander/Enterprise.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,17 @@ function Move-KeeperEnterpriseUser {
return
}
if (-not $Force.IsPresent) {
Write-Output "This action cannot be undone.`n"
$answer = Read-Host -Prompt "Do you want to proceed with transferring $($fromUserObject.Email) account (Yes/No)? > "
if (Test-InteractiveSession) {
Write-Output "This action cannot be undone.`n"
$answer = Read-Host -Prompt "Do you want to proceed with transferring $($fromUserObject.Email) account (Yes/No)? > "
} else {
Write-Output('Non-interactive session. Use -Force parameter')
$answer = 'no'
}
if ($answer -ne 'yes' -and $answer -ne 'y') {
return
}
}
}
$transferResult = $enterprise.enterpriseData.TransferUserAccount($enterprise.roleData, $fromUserObject, $targetUserObject).GetAwaiter().GetResult()
if ($transferResult) {
Write-Information "Successfully Transfered:"
Expand Down
4 changes: 2 additions & 2 deletions PowerCommander/PowerCommander.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RootModule = 'PowerCommander.psm1'

# Version number of this module.
ModuleVersion = '0.9.18'
ModuleVersion = '0.9.19'

# Supported PSEditions
CompatiblePSEditions = @('Desktop')
Expand Down Expand Up @@ -117,7 +117,7 @@
LicenseUri = 'https://github.com/Keeper-Security/keeper-sdk-dotnet/blob/master/LICENSE'
ProjectUri = 'https://github.com/Keeper-Security/keeper-sdk-dotnet'
IconUri = 'https://keeper-email-images.s3.amazonaws.com/common/powershell.png'
ReleaseNotes = 'Update Keeper SDL to v1.0.6'
ReleaseNotes = 'Handle non-interactive Powershell environments'
}
}

Expand Down

0 comments on commit 4047242

Please sign in to comment.