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

As a corporate customer, I would like CVRF downloads to occur via Proxy #5

Open
Xeleema opened this issue Apr 13, 2017 · 12 comments
Open

Comments

@Xeleema
Copy link

Xeleema commented Apr 13, 2017

Within a corporate environment, where servers are restricted from accessing the internet, the cmdlets should have "out-of-the-box" support for specifying Proxy settings.

image

Text-based Error

PS C:\> Get-MsrcCvrfDocument -ID $monthOfInterest -ApiKey $msrcApiKey -Verbose
VERBOSE: Calling https://api.msrc.microsoft.com/cvrf/2017-Apr?api-version=2016-08-01
VERBOSE: GET https://api.msrc.microsoft.com/cvrf/2017-Apr?api-version=2016-08-01 with 0-byte payload
Get-MsrcCvrfDocument : HTTP Get failed with status code ProxyAuthenticationRequired: Proxy Authentication Required
At line:1 char:1
+ Get-MsrcCvrfDocument -ID $monthOfInterest -ApiKey $msrcApiKey -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-MsrcCvrfDocument

:shipit:

@craig-martin
Copy link
Contributor

Good suggestion, Xeleema. The commands in this module are really just wrappers over Invoke-RestMethod (https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/invoke-restmethod) which has parameters to support using a Proxy.

As a workaround, you could do this:

Import-Module MsrcSecurityUpdates

Set-MSRCApiKey -ApiKey Foo

### Get a CVRF document using Proxy Credentials
$apiUrl = "$msrcApiUrl/cvrf/2017-Apr?$msrcApiVersion"
Invoke-RestMethod -Method Get -Uri $apiUrl -Headers @{
    'Api-Key' = $global:MSRCApiKey 
    'Accept'  = 'application/json'
} -Proxy http://myProxy -ProxyCredential (Get-Credential -Message 'Please enter the creds for the proxy' -UserName MyProxyUserName)

### Get security Updates using Proxy Credentials
$apiUrl = "$msrcApiUrl/updates?$msrcApiVersion"
Invoke-RestMethod -Method Get -Uri $apiUrl -Headers @{
    'Api-Key' = $global:MSRCApiKey 
    'Accept'  = 'application/json'
} -Proxy http://myProxy -ProxyCredential (Get-Credential -Message 'Please enter the creds for the proxy' -UserName MyProxyUserName) | 
Select-Object -ExpandProperty Value```

@vrdse
Copy link

vrdse commented Apr 27, 2017

You could also set the proxy as devault values like this:
$PSDefaultParameterValues += @{'*:ProxyUseDefaultCredentials'=$true;'*:Proxy'='http://proxy:port'}

@craig-martin
Copy link
Contributor

That's a great workaround, thanks for the suggestion! Given that the workaround exists, I don't think we should plumb the parameters through to the Get-Msrc* commands.

@craig-martin
Copy link
Contributor

Fixed by @NicholasBn in #10

@Xeleema
Copy link
Author

Xeleema commented May 11, 2017

Work-around mentioned by vrdse doesn't work in my case. However it may be something in the 1.6.4 version of the module. The private function Get-CVRFID works when called directly, however when called by Get-MsrccvrfDocument, Get-CVRFID throws "Cannot retrieve the dynamic parameters for the cmdlet. Unable to get online the list of CVRF ID".

Digging into $Error shows Invoke-RestMethod caught an "Access Denied (authentication_failed)" from the proxy. So something isn't quite right with $PSDefaultParameterValues (or $global:PSDefaultParameterValues, even when specifying 'Invoke-RestMethod instead of '*').

I'll grab 1.6.6 and give that a shot. :)

Edit: $PSDefaultParameterValues suggestion works, then things trip and stumble when Get-MsrccvrfDocument calls Get-CVRFID.

@Xeleema
Copy link
Author

Xeleema commented May 11, 2017

Moved comment to Issue #9

@craig-martin
Copy link
Contributor

Have you tried using Set-MSRCApiKey with the Proxy and ProxyCredential parameters?

Set-MSRCApiKey -ApiKey foo -Proxy http://myProxy -ProxyCredential (Get-Credential)

@vrdse
Copy link

vrdse commented May 12, 2017

@craig-martin Yes, I tried the new parameters. Works as good as or as bad as setting $PSDefaultParameterValues.
In both cases I have the issue #9. Not 100% sure if it's proxy related though.

@craig-martin
Copy link
Contributor

Thanks for the extra detail. Can you try using Invoke-RestMethod on its own? This module is basically a wrapper over that command so I'd like to confirm that it works.

Here is a sample command line without proxy details:

Invoke-RestMethod -Uri https://api.msrc.microsoft.com/cvrf/2016-Sep?api-Version=2016 -Headers @{'Api-Key'='YOUR KEY GOES HERE';Accept='application/json'}

Here is a sample command line with proxy details:

Invoke-RestMethod -Uri https://api.msrc.microsoft.com/cvrf/2016-Sep?api-Version=2016 -Headers @{'Api-Key'='YOUR KEY GOES HERE';Accept='application/json'} -Proxy http://YourProxyServer -ProxyCredential (Get-Credential)

@craig-martin craig-martin reopened this May 12, 2017
@Xeleema
Copy link
Author

Xeleema commented May 23, 2017

@craig-martin
When I execute the following, I can get through our internal proxy and obtain data from the RESTful API;

# $MyAPIKey= ## redacted for post
# $MyProxyAndPort= ## redacted for post
$PSDefaultParameterValues   += @{'Invoke-RestMethod:ProxyUseDefaultCredentials'=$true}
Invoke-RestMethod -Uri https://api.msrc.microsoft.com/cvrf/2016-Sep?api-Version=2016 -Headers @{'Api-Key'=$MyAPIKey;Accept='application/json'} -Proxy $MyProxyAndPort

When I dot-source and call Get-CVRFID directly, I'm able to get data through our proxy;

. C:\CVRF\MsrcSecurityUpdates\Private\Get-CVRFID.ps1
Get-CVRFID -ID "2017-May"

However, I catch an error when running ; Get-MsrcCvrfDocument -ID "2017-May" -Verbose

##
## TEST BLOCK (Base) - C:\CVRF\MsrcSecurityUpdates
##
$PSDefaultParameterValues   += @{'Invoke-RestMethod:ProxyUseDefaultCredentials'=$true}
Import-Module C:\CVRF\MsrcSecurityUpdates
# $MyAPIKey = ## redacted for post
# $MyProxyAndPort = ## redacted for post
Set-MSRCApiKey -ApiKey $MyAPIKey -Proxy $MyProxyAndPort
Get-MsrcCvrfDocument -ID "2017-May" -Verbose
##
## TEST BLOCK base (end)
##

The run of Invoke-RestMethod (called from Get-CVRFID) is getting Access Denied because it assumes that if $Proxy is specificed, that $ProxyCredentials (rather than $DefaultCredentials) will be used.

Every call to Invoke-RestMethod is affected.

NOTE: However, this was as it stood on May 11th with version 1.6.7. I'll upgrade to PowerShell v5.0 and grab the latest *.zip file blob.

@Xeleema
Copy link
Author

Xeleema commented May 23, 2017

Update: Since there doesn't appear to be an applicable code-change since May 11th, I've dug deeper and discovered the following;

Within \MsrcSecurityUpdates\Private\Get-CVRFID.ps1

Regarding the if-statement for $global:msrcProxyCredential (Lines 21 thru 23)

Remove this;

    if ($global:msrcProxyCredential){
        $RestMethod.Add('ProxyCredential',$global:msrcProxyCredential)
    }

Then incorporate it into the if-statement for $global:msrcProxy ;

    if ($global:msrcProxy){
        $RestMethod.Add('Proxy' , $global:msrcProxy)
        if ($global:msrcProxyCredential){
            $RestMethod.Add('ProxyCredential',$global:msrcProxyCredential)
        } else {
            $RestMethod.Add('ProxyUseDefaultCredentials',$true)
        }
    }

Note the "if no msrcproxyCredential then set ProxyUseDefaultCredentials to $true".
My test-block now results in the following output;

PS C:\CG\HFS>
PS C:\CG\HFS> cd \
PS C:\> $error.clear()
PS C:\> ##
PS C:\> ## TEST BLOCK (Base) - C:\CVRF\MsrcSecurityUpdates
PS C:\> ##
PS C:\>
PS C:\> $PSDefaultParameterValues   += @{'Invoke-RestMethod:ProxyUseDefaultCredentials'=$true}
PS C:\> Import-Module C:\CVRF\MsrcSecurityUpdates
PS C:\>
PS C:\> Set-MSRCApiKey -ApiKey $MyAPIKey -Proxy $MyProxyAndPort
PS C:\>
PS C:\> Get-MsrcCvrfDocument -ID "2017-May" -Verbose
VERBOSE: Calling https://api.msrc.microsoft.com/cvrf/2017-May?api-version=2016-08-01
VERBOSE: GET https://api.msrc.microsoft.com/cvrf/2017-May?api-version=2016-08-01 with 0-byte payload
VERBOSE: received 1298134-byte response of content type application/json; charset=utf-8
DocumentTitle     : @{Value=May 2017 Security Updates}
DocumentType      : @{Value=Security Update}
DocumentPublisher : @{ContactDetails=; IssuingAuthority=; Type=0}
DocumentTracking  : (lots of good stuff)
DocumentNotes     : (even more good stuff)
ProductTree       : (good stuff)
Vulnerability     : (good stuff)
PS C:\>
PS C:\> ##
PS C:\> ## TEST BLOCK base (end)
PS C:\> ##
PS C:\>

@emikulic1
Copy link

We have run into the same issue at our corporation. We need to access this data via proxy (MiM) that is required for our govt customer base.

mdressman pushed a commit that referenced this issue Jan 21, 2021
Merge pull request #77 from p0w3rsh3ll/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants