Skip to content

Commit e7f96c1

Browse files
committed
updates
1 parent 3769e0f commit e7f96c1

19 files changed

+606
-189
lines changed

Modules/ibPS-Main.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ if ($ENV:IBPSDevelopment -eq "Enabled") {
3535
}
3636
if ($ENV:IBPSDebug -eq "Enabled") {
3737
$DebugPreference = 'Continue'
38+
$VerbosePreference = 'Continue'
3839
} else {
3940
$DebugPreference = 'SilentlyContinue'
41+
$VerbosePreference = 'SilentlyContinue'
4042
}
4143

4244
Initialize-NIOSConfig

Modules/ibPS/Functions/CSP/Authentication/API/Switch-B1ConnectionProfile.ps1

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,16 @@
4242
$ContextConfig.CurrentContext = $Name
4343
$ContextConfig | ConvertTo-Json -Depth 5 | Out-File $Script:B1ConfigFile -Force
4444

45-
if ($ENV:B1APIKey -or $ENV:B1CSPUrl) {
45+
if ($ENV:B1CSPUrl) {
4646
$Platform = Detect-OS
4747
if ($Platform -eq "Windows") {
48-
[System.Environment]::SetEnvironmentVariable('B1APIKey',$null,[System.EnvironmentVariableTarget]::User)
4948
[System.Environment]::SetEnvironmentVariable('B1CSPUrl',$null,[System.EnvironmentVariableTarget]::User)
50-
$ENV:B1APIKey = $null
5149
$ENV:B1CSPUrl = $null
5250
}
5351
if ($Platform -eq "Mac" -or $Platform -eq "Unix") {
5452
if (Test-Path ~/.zshenv) {
55-
sed -i '' -e '/B1APIKey/d' ~/.zshenv
5653
sed -i '' -e '/B1CSPUrl/d' ~/.zshenv
5754
}
58-
$ENV:B1APIKey = $null
5955
$ENV:B1CSPUrl = $null
6056
}
6157
}

Modules/ibPS/Functions/CSP/Authentication/JWT/Connect-B1Account.ps1

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function Connect-B1Account {
1515
.PARAMETER Email
1616
The email address of the Infoblox Portal account to use when connecting.
1717
18-
.PARAMETER Password
19-
The password of the Infoblox Portal account to use when connecting.
18+
.PARAMETER SecurePassword
19+
The password of the Infoblox Portal account to use when connecting, in SecureString format.
2020
2121
.EXAMPLE
2222
PS> Connect-B1Account -Email "[email protected]" -Password "mySuperSecurePassword"
@@ -33,63 +33,49 @@ function Connect-B1Account {
3333
Authentication
3434
#>
3535
param(
36-
[Parameter(Mandatory = $true)]
36+
[Parameter(Mandatory = $true,ParameterSetName="JWT")]
3737
[string]$Email,
38+
[Parameter(Mandatory = $true,ParameterSetName="API")]
39+
[switch]$APIKey,
3840
[ValidateSet("US","EU")]
3941
[String]$CSPRegion = 'US',
40-
[String]$CSPUrl,
41-
[Parameter(Mandatory = $false)]
42-
[SecureString]$SecurePassword
42+
[Parameter(Mandatory = $false,ParameterSetName="JWT")]
43+
[SecureString]$SecurePassword,
44+
[Parameter(Mandatory = $false,ParameterSetName="API")]
45+
[SecureString]$SecureAPIKey
4346
)
4447

45-
if (-not $SecurePassword) {
46-
$Password = Read-Host -Prompt "Enter your password for $Email" -AsSecureString
47-
} else {
48-
$Password = $SecurePassword
49-
}
50-
51-
switch ($CSPRegion) {
52-
"US" {
53-
$CSPUrl = "https://csp.infoblox.com"
54-
}
55-
"EU" {
56-
$CSPUrl = "https://csp.eu.infoblox.com"
57-
}
58-
}
59-
$ENV:B1CSPUrl = $CSPUrl
60-
61-
$Body = @{
62-
email = $Email
63-
password = ConvertFrom-SecureString -SecureString $Password -AsPlainText
64-
} | ConvertTo-Json
65-
66-
$Headers = @{
67-
"Content-Type" = "application/json"
68-
}
69-
7048
try {
71-
$Result = Invoke-RestMethod -Method POST -Uri "$($CSPUrl)/v2/session/users/sign_in" -Body $Body -Headers $Headers -ContentType "application/json"
72-
if ($Result.jwt -ne $null) {
73-
$ENV:B1Bearer = $Result.jwt
74-
if ($CU = Get-B1CSPCurrentUser) {
75-
$CA = Get-B1CSPCurrentUser -Account
76-
Write-Host "Successfully connected to $($CA.name) using: $($CU.email)." -ForegroundColor Green
49+
$AuthManager = [AuthManager]::new($CSPRegion)
50+
if ($Email) {
51+
if (-not $SecurePassword) {
52+
$Password = Read-Host -Prompt "Enter your password for $Email" -AsSecureString
7753
} else {
78-
Write-Error "Successfully retrieved JWT but no active user details were returned."
54+
$Password = $SecurePassword
7955
}
80-
} else {
81-
if ($Result.error.message) {
82-
Write-Error "$($Result.error.message)"
83-
} else {
84-
Write-Error "An unknown error occurred while connecting to the Infoblox Portal. Please check your credentials and try again."
56+
$AuthManager.ConnectJWT($Email,$Password)
57+
if ($AuthManager.JWT) {
58+
$Script:AuthManager = $AuthManager
59+
}
60+
} elseif ($APIKey) {
61+
if (-not $SecureAPIKey) {
62+
$SecureAPIKey = Read-Host -Prompt "Enter your API Key" -AsSecureString
63+
}
64+
$AuthManager.ConnectAPIKey($SecureAPIKey)
65+
if ($AuthManager.APIKey) {
66+
$Script:AuthManager = $AuthManager
67+
$CU = Get-B1CSPCurrentUser -ErrorAction SilentlyContinue
68+
if ($CU) {
69+
Write-Host "Connected using API Key as: $($CU.name)" -ForegroundColor Green
70+
} else {
71+
Write-Error "Failed to connect using API Key. Please check your API Key and try again."
72+
$Script:AuthManager = $null
73+
return
74+
}
8575
}
8676
}
8777
} catch {
88-
$json = $_ | ConvertFrom-Json -ErrorAction SilentlyContinue
89-
if ($json.error) {
90-
Write-Error "$($json.error.message)"
91-
} else {
92-
Write-Error "An unknown error occurred while connecting to the Infoblox Portal. Please check your credentials and try again."
93-
}
78+
Write-Error $_
79+
$Script:AuthManager = $null
9480
}
9581
}

Modules/ibPS/Functions/CSP/Authentication/JWT/Disconnect-B1Account.ps1

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,13 @@ function Disconnect-B1Account {
2121
Authentication
2222
#>
2323
[CmdletBinding()]
24-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
2524
param()
26-
if ($ENV:B1Bearer) {
27-
$CU = Get-B1CSPCurrentUser
28-
$CA = Get-B1CSPCurrentUser -Account
29-
if ($CU -and $CA) {
30-
Write-Host "Disconnecting from $($CA.name) using: $($CU.email)." -ForegroundColor Green
31-
} else {
32-
Write-Host "Already disconnected from the Infoblox Portal." -ForegroundColor Green
33-
return
34-
}
25+
if ($Script:AuthManager) {
26+
$AuthManager = $Script:AuthManager
27+
$AuthManager.Disconnect()
28+
$Script:AuthManager = $null
3529
} else {
3630
Write-Error "You are not currently connected to the Infoblox Portal. Please use Connect-B1Account first."
3731
return
3832
}
39-
40-
$Headers = @{
41-
"Authorization" = "Bearer $($ENV:B1Bearer)"
42-
}
43-
44-
try {
45-
$ENV:B1Bearer = $null
46-
$Result = Invoke-RestMethod -Method DELETE -Uri "$($ENV:B1CSPUrl)/v2/session/users/sign_out" -Headers $Headers
47-
Write-Host "Disconnected Successfully." -ForegroundColor Green
48-
} catch {
49-
Write-Error "An unknown error occurred while disconnecting from the Infoblox Portal."
50-
return $_
51-
}
5233
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function Get-B1AccountSession {
2+
<#
3+
.SYNOPSIS
4+
5+
6+
.DESCRIPTION
7+
8+
9+
.EXAMPLE
10+
PS> Get-B1AccountSession
11+
12+
.FUNCTIONALITY
13+
Infoblox Portal
14+
15+
.FUNCTIONALITY
16+
Core
17+
18+
.FUNCTIONALITY
19+
Authentication
20+
#>
21+
[CmdletBinding()]
22+
param()
23+
if ($Script:AuthManager) {
24+
$AuthManager = $Script:AuthManager
25+
$AuthManager.GetSessionInfo()
26+
} else {
27+
Write-Error "You are not currently connected to the Infoblox Portal. Please use Connect-B1Account first."
28+
return
29+
}
30+
}
Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
function Switch-B1Account {
22
<#
33
.SYNOPSIS
4-
Switches the interactive JWT session token to a different Infoblox Portal account.
4+
Switches an interactive JWT session token to a different Infoblox Portal account.
55
66
.DESCRIPTION
7-
Switches the interactive JWT session token to a different Infoblox Portal account. This can be used to switch into the context of Sandboxes/Subtenants using the parent account's JWT session token.
7+
Switches an interactive JWT session token to a different Infoblox Portal account. This can be used to switch into the context of Sandboxes/Subtenants using the parent account's JWT session token.
8+
9+
This only works when connected to the Infoblox Portal using Connect-B1Account and an Email / Password. API Keys do not support account switching.
810
911
.PARAMETER Name
1012
The name of the Infoblox Portal account to switch to. This is the name as displayed in the Infoblox Portal.
@@ -33,8 +35,8 @@ function Switch-B1Account {
3335
[string]$id
3436
)
3537

36-
if (!$ENV:B1Bearer) {
37-
Write-Error "You must be connected to a BloxOne account before switching accounts. Please use Connect-B1Account first."
38+
if (!$Script:AuthManager) {
39+
Write-Error "You must be connected to the Infoblox Portal before switching accounts. Please use Connect-B1Account first."
3840
return
3941
}
4042

@@ -47,38 +49,13 @@ function Switch-B1Account {
4749
$id = $Account.id
4850
}
4951

50-
$Body = @{
51-
id = $id
52-
} | ConvertTo-Json
53-
54-
$Headers = @{
55-
"Authorization" = "Bearer $ENV:B1Bearer"
56-
}
57-
5852
try {
59-
$Result = Invoke-RestMethod -Method POST -Uri "https://csp.infoblox.com/v2/session/account_switch" -Body $Body -Headers $Headers -ContentType "application/json"
60-
61-
if ($Result.jwt -ne $null) {
62-
$ENV:B1Bearer = $Result.jwt
63-
if ($CU = Get-B1CSPCurrentUser) {
64-
$CA = Get-B1CSPCurrentUser -Account
65-
Write-Host "Successfully switched to $($CA.name) using: $($CU.email)." -ForegroundColor Green
66-
} else {
67-
Write-Error "Successfully retrieved new JWT but no active user details were returned."
68-
}
69-
} else {
70-
if ($Result.error) {
71-
Write-Error "$($Result.error)"
72-
} else {
73-
Write-Error "An unknown error occurred while switching accounts."
74-
}
53+
if (!($Script:AuthManager).SwitchSession($id)) {
54+
Write-Error "Failed to switch accounts. Please check the account ID and try again."
55+
return
7556
}
7657
} catch {
77-
$json = $_ | ConvertFrom-Json
78-
if ($json.error) {
79-
Write-Error "$($json.error.message)"
80-
} else {
81-
Write-Error "An unknown error occurred while switching accounts."
82-
}
58+
Write-Error $_
59+
return
8360
}
8461
}

Modules/ibPS/Functions/CSP/Private/ArgCompleter.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ Register-ArgumentCompleter -CommandName Get-B1Service,New-B1Service -ParameterNa
3737

3838
$B1Accounts = {
3939
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
40-
if (!$ENV:B1Bearer) {
41-
Write-Host "`nYou must be connected to a BloxOne account before switching accounts. Please use Connect-B1Account first." -ForegroundColor Red
40+
if (!$Script:AuthManager) {
41+
Write-Host "`nYou must be connected to the Infoblox Portal before switching accounts. Please use Connect-B1Account first." -ForegroundColor Red
4242
return
4343
}
4444
(Get-B1CSPCurrentUser -Accounts | Where-Object {$_.name -like "$wordToComplete*"}).name

0 commit comments

Comments
 (0)