Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Token validation for Connect-Rubrik - Issue 374 #380

Merged
merged 8 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
* Added new `Get-RubrikBootstrap` function that Connects to the Rubrik cluster and retrieves the bootstrap process progress
* Created a templates folder with examples of Rubrik bootstrap

## 2019-07-14

### Changed [Connect-Rubrik] - Will validate if token is correct

* Added validation step for token, a query is executed against the cluster endpoint to validate the token
* Get-RubrikAPIToken pwsh 5 bug fixed

## 2019-07-13

### Changed [Submit-Request] private function
Expand Down
17 changes: 12 additions & 5 deletions Rubrik/Public/Connect-Rubrik.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ function Connect-Rubrik {
# Optionally, use the Username and Password parameters
[Parameter(ParameterSetName='Credential',Mandatory=$true, Position = 1)]
[System.Management.Automation.CredentialAttribute()]$Credential,
# Provide the Rubrik API Token instead, these are specificially created API token for authentication.
[Parameter(ParameterSetName='Token',Mandatory=$true, Position = 1)]
[ValidateNotNullOrEmpty()]
[String]$Token,
#Organization to connect with, assuming the user has multiple organizations
[Alias('organization_id')]
Expand Down Expand Up @@ -99,8 +101,7 @@ function Connect-Rubrik {
$UserAgent = "Rubrik-Powershell-SDK/$($MyInvocation.MyCommand.ScriptBlock.Module.Version.ToString())"
Write-Verbose -Message "Using User Agent $($UserAgent)"

if($Token)
{
if($Token) {
$head = @{'Authorization' = "Bearer $($Token)";'User-Agent' = $UserAgent}
Write-Verbose -Message 'Storing all connection details into $global:rubrikConnection'
$global:rubrikConnection = @{
Expand All @@ -114,9 +115,15 @@ function Connect-Rubrik {
version = Get-RubrikSoftwareVersion -Server $Server
authType = 'Token'
}
}
else
{

try {
$null = Get-RubrikVersion -ErrorAction Stop
} catch {
Write-Verbose -Message 'Removing API token from $RubrikConnection using Disconnect-Rubrik'
Disconnect-Rubrik
throw 'Invalid API Token provided, please provide correct token'
}
} else {
$Credential = Test-RubrikCredential -Username $Username -Password $Password -Credential $Credential

$uri = New-URIString -server $Server -endpoint ($resources.URI) -id $id
Expand Down
4 changes: 3 additions & 1 deletion Rubrik/Public/Get-RubrikAPIToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ function Get-RubrikAPIToken
# Remove any api tokens generated for usage with web
$result = $result | Where-Object {$_.sessionType -ne 'Web'}

$result.ForEach{$_.PSObject.TypeNames.Insert(0,'Rubrik.APIToken')}
if ($null -ne $result) {
@($result).ForEach{$_.PSObject.TypeNames.Insert(0,'Rubrik.APIToken')}
}
return $result

} # End of process
Expand Down