Skip to content

Enhancement: add -Credential support to Connect-ZertoZVM, Get-ZertoAuthToken #6

Open

Description

@mtboren

Still enjoying the module, thanks!

To allow for some non-interactive (or less interactive) connection to a Zerto ZVM, it would be useful to have the -Credential parameter on the Connect-ZertoZVM (and, subsequently, the Get-ZertoAuthToken) cmdlets.

Then, a consumer could, say, import some encrypted creds into a PSCredential object, pass them as the value to -Credential, and not need to enter the creds at every Connect operation.

I made a quick commit with some code to this effect at the fork at https://github.com/mtboren/ZertoModule_fork/tree/dev. The Compare & Pull Request comparison view shows the updates nicely.

How does such an enhancement sound to you?

Cheers

Activity

  1. gregklee commented on Apr 30, 2018

    @gregklee

    I updated the functions to allow passing credentials. Here's the code:

    Function newGet-ZertoAuthToken {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$false, HelpMessage = 'Zerto Server or ENV:\ZertoServer')] [string] $ZertoServer = ( Get-EnvZertoServer ) ,
    [Parameter(Mandatory=$false, HelpMessage = 'Zerto Server URL Port')] [string] $ZertoPort = ( Get-EnvZertoPort ),
    [Parameter( HelpMessage = 'Credentials to connect to Zerto')] $ZertoCredentials
    )

        Set-SSLCertByPass
    
        if ([String]::IsNullOrEmpty($ZertoServer) ) {
            throw "Missing Zerto Server"
        }
    
        $baseURL = "https://" + $ZertoServer + ":"+$ZertoPort+"/v1/"
        $FullURL = $baseURL + "session/add"
        $TypeJSON = "application/json"
        Write-Verbose $FullURL
    
        if ([String]::IsNullOrEmpty($ZertoCredentials) ) 
            {
            $ZertoCredentials = Get-Credential -Message "Enter your Zerto credentials for '$ZertoServer'"
            } 
    
        If ($ZertoCredentials -NE $null) {
            #Remove  our Zerto Version
            Remove-Item ENV:ZertoToken -Force -ErrorAction Ignore
            Remove-Item ENV:ZertoVersion -Force -ErrorAction Ignore
    
            # Authenticating with Zerto APIs - Basic AUTH over SSL
            $authInfo = ("{0}\{1}:{2}" -f  $ZertoCredentials.GetNetworkCredential().domain ,  $ZertoCredentials.GetNetworkCredential().UserName,  $ZertoCredentials.GetNetworkCredential().Password )
            $authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
            $authInfo = [System.Convert]::ToBase64String($authInfo)
            $headers = @{Authorization=("Basic {0}" -f $authInfo)}
            $sessionBody = '{"AuthenticationMethod": "1"}'
    
            #Need to check our Response.
            try { 
                $xZertoSessionResponse = Invoke-WebRequest -Uri $FullURL -Headers $headers -Method POST -Body $sessionBody -ContentType $TypeJSON             
            } catch {
                $xZertoSessionResponse = $_.Exception.Response
            }
    
            if ($xZertoSessionResponse -eq $null  ) {
                Throw "Zerto Server ${ZertoServer}:${ZertoPort} not responding."
            } elseif ($xZertoSessionResponse.StatusCode -eq "200") {
                $xZertoSession = $xZertoSessionResponse.headers.get_item("x-zerto-session")
                $ZertoSessionHeader = @{"x-zerto-session"=$xZertoSession}
                return $ZertoSessionHeader 
            } else {
                if ($xZertoSessionResponse.StatusCode.value__ -eq "401") {
                    Throw "User $ZertoUser not authorized or invalid password."
                }
                return $null
            }
        } else {
            return $null
        }
    }
    

    Function newConnect-ZertoZVM {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true, HelpMessage = 'Zerto Server or ENV:\ZertoServer')] [string] $ZertoServer ,
    [Parameter(Mandatory=$false, HelpMessage = 'Zerto Server URL Port')] [string] $ZertoPort = 9669 ,
    [Parameter(Mandatory=$false, HelpMessage = 'Credentials to connect to Zerto')] $ZertoCredentials
    )

    Set-Item ENV:ZertoServer $ZertoServer
    Set-Item ENV:ZertoPort  $ZertoPort 
    Set-Item ENV:ZertoToken ((newGet-ZertoAuthToken -ZertoServer $ZertoServer -ZertoPort $ZertoPort -ZertoCredentials $ZertoCredentials) | ConvertTo-Json -Compress) 
    Set-Item ENV:ZertoVersion (Get-ZertoLocalSite).version
    

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions