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

(#1021) Use credentials for downloads #3283

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ from the url resource.
OPTIONAL switch to force download of file every time, even if the file
already exists.

.PARAMETER Credentials
OPTIONAL A System.Net.ICredentials-Object that can be used for downloading files from a server which requires user authentication.

.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.

Expand Down Expand Up @@ -198,6 +201,7 @@ Get-FtpFile
[parameter(Mandatory = $false)][hashtable] $options = @{Headers = @{} },
[parameter(Mandatory = $false)][switch] $getOriginalFileName,
[parameter(Mandatory = $false)][switch] $forceDownload,
[parameter(Mandatory = $false)][System.Net.ICredentials] $credentials = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)

Expand Down Expand Up @@ -287,7 +291,7 @@ Get-FtpFile
if ($url.StartsWith('http:')) {
try {
$httpsUrl = $url.Replace("http://", "https://")
Get-WebHeaders -Url $httpsUrl -ErrorAction "Stop" | Out-Null
Get-WebHeaders -Url $httpsUrl -Credentials $credentials -ErrorAction "Stop" | Out-Null
$url = $httpsUrl
Write-Warning "Url has SSL/TLS available, switching to HTTPS for download"
}
Expand All @@ -301,7 +305,7 @@ Get-FtpFile
$fileFullPath = $fileFullPath -replace '\\chocolatey\\chocolatey\\', '\chocolatey\'
$fileDirectory = [System.IO.Path]::GetDirectoryName($fileFullPath)
$originalFileName = [System.IO.Path]::GetFileName($fileFullPath)
$fileFullPath = Get-WebFileName -Url $url -DefaultName $originalFileName
$fileFullPath = Get-WebFileName -Url $url -DefaultName $originalFileName -Credentials $credentials
$fileFullPath = Join-Path $fileDirectory $fileFullPath
$fileFullPath = [System.IO.Path]::GetFullPath($fileFullPath)
}
Expand All @@ -324,7 +328,7 @@ Get-FtpFile
$headers = @{}
if ($url.StartsWith('http')) {
try {
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop"
$headers = Get-WebHeaders -Url $url -Credentials $credentials -ErrorAction "Stop"
}
catch {
if ($PSVersionTable.PSVersion -lt (New-Object 'Version' 3, 0)) {
Expand All @@ -333,7 +337,7 @@ Get-FtpFile
$originalProtocol = [System.Net.ServicePointManager]::SecurityProtocol
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Ssl3
try {
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop"
$headers = Get-WebHeaders -Url $url -Credentials $credentials -ErrorAction "Stop"
}
catch {
Write-Host "Attempt to get headers for $url failed.`n $($_.Exception.Message)"
Expand Down Expand Up @@ -369,7 +373,7 @@ Get-FtpFile
if ($needsDownload) {
Write-Host "Downloading $packageName $bitPackage
from `'$url`'"
Get-WebFile -Url $url -FileName $fileFullPath -Options $options
Get-WebFile -Url $url -FileName $fileFullPath -Options $options -Credentials $credentials
}
else {
Write-Debug "$($packageName)'s requested file has already been downloaded. Using cached copy at
Expand Down
9 changes: 8 additions & 1 deletion src/chocolatey.resources/helpers/functions/Get-WebFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Silences the progress output.
.PARAMETER Options
OPTIONAL - Specify custom headers.

.PARAMETER Credentials
OPTIONAL A System.Net.ICredentials-Object that can be used for downloading files from a server which requires user authentication.

.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.

Expand All @@ -88,6 +91,7 @@ Get-WebFileName
[parameter(Mandatory = $false)][switch] $Passthru,
[parameter(Mandatory = $false)][switch] $quiet,
[parameter(Mandatory = $false)][hashtable] $options = @{Headers = @{} },
[parameter(Mandatory = $false)][System.Net.ICredentials] $credentials = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)

Expand All @@ -110,7 +114,10 @@ Get-WebFileName

$req = [System.Net.HttpWebRequest]::Create($url);
$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
if ($defaultCreds -ne $null) {
if ($null -ne $credentials) {
$req.Credentials = $credentials
}
elseif ($defaultCreds -ne $null) {
$req.Credentials = $defaultCreds
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ from the url response.
The user agent to use as part of the request. Defaults to 'chocolatey
command line'.

.PARAMETER Credentials
OPTIONAL A System.Net.ICredentials-Object that can be used for downloading files from a server which requires user authentication.

.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.

Expand All @@ -69,6 +72,7 @@ Get-ChocolateyWebFile
[parameter(Mandatory = $false, Position = 0)][string] $url = '',
[parameter(Mandatory = $true, Position = 1)][string] $defaultName,
[parameter(Mandatory = $false)][string] $userAgent = 'chocolatey command line',
[parameter(Mandatory = $false)][System.Net.ICredentials] $credentials = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)

Expand Down Expand Up @@ -107,7 +111,10 @@ Get-ChocolateyWebFile
}

$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
if ($defaultCreds -ne $null) {
if ($null -ne $credentials) {
$request.Credentials = $credentials
}
elseif ($defaultCreds -ne $null) {
$request.Credentials = $defaultCreds
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ This is the url to get a request/response from.
The user agent to use as part of the request. Defaults to 'chocolatey
command line'.

.PARAMETER Credentials
OPTIONAL A System.Net.ICredentials-Object that can be used for downloading files from a server which requires user authentication.

.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.

Expand All @@ -52,6 +55,7 @@ Get-WebFile
param(
[parameter(Mandatory = $false, Position = 0)][string] $url = '',
[parameter(Mandatory = $false, Position = 1)][string] $userAgent = 'chocolatey command line',
[parameter(Mandatory = $false)][System.Net.ICredentials] $credentials = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)

Expand All @@ -63,7 +67,10 @@ Get-WebFile

$request = [System.Net.HttpWebRequest]::Create($url);
$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
if ($defaultCreds -ne $null) {
if ($null -ne $credentials) {
$request.Credentials = $credentials
}
elseif ($defaultCreds -ne $null) {
$request.Credentials = $defaultCreds
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ be used in place.
NOTE: You can also use `Install-ChocolateyInstallPackage` for the same
functionality (see links).

.PARAMETER Credentials
OPTIONAL A System.Net.ICredentials-Object that can be used for downloading files from a server which requires user authentication.

.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.

Expand Down Expand Up @@ -364,6 +367,7 @@ Install-ChocolateyZipPackage
[alias("useOnlyPackageSilentArgs")][switch] $useOnlyPackageSilentArguments = $false,
[parameter(Mandatory = $false)][switch]$useOriginalLocation,
[parameter(Mandatory = $false)][scriptblock] $beforeInstall,
[parameter(Mandatory = $false)][System.Net.ICredentials] $credentials = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)
[string]$silentArgs = $silentArgs -join ' '
Expand Down Expand Up @@ -413,6 +417,7 @@ Install-ChocolateyZipPackage
-Checksum64 $checksum64 `
-ChecksumType64 $checksumType64 `
-Options $options `
-Credentials $credentials `
-GetOriginalFileName
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ The recommendation is to use at least SHA256.
.PARAMETER Options
OPTIONAL - Specify custom headers.

.PARAMETER Credentials
OPTIONAL A System.Net.ICredentials-Object that can be used for downloading files from a server which requires user authentication.

.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.

Expand Down Expand Up @@ -187,13 +190,14 @@ Install-ChocolateyZipPackage
[parameter(Mandatory = $false)][string] $checksum64 = '',
[parameter(Mandatory = $false)][string] $checksumType64 = '',
[parameter(Mandatory = $false)][hashtable] $options = @{Headers = @{} },
[parameter(Mandatory = $false)][System.Net.ICredentials] $credentials = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)

Write-FunctionCallLogMessage -Invocation $MyInvocation -Parameters $PSBoundParameters

if ($url -ne '') {
Get-ChocolateyWebFile $packageName $psFileFullPath $url $url64bit -checksum $checksum -checksumType $checksumType -checksum64 $checksum64 -checksumType64 $checksumType64 -Options $options
Get-ChocolateyWebFile $packageName $psFileFullPath $url $url64bit -checksum $checksum -checksumType $checksumType -checksum64 $checksum64 -checksumType64 $checksumType64 -Options $options -Credentials $credentials
}

if ($env:chocolateyPackageName -ne $null -and $env:chocolateyPackageName -eq $env:ChocolateyInstallDirectoryPackage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ Will be used for VsixUrl if VsixUrl is empty.
This parameter provides compatibility, but should not be used directly
and not with the community package repository until January 2018.

.PARAMETER Credentials
OPTIONAL A System.Net.ICredentials-Object that can be used for downloading files from a server which requires user authentication.

.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.

Expand Down Expand Up @@ -144,6 +147,7 @@ Install-ChocolateyZipPackage
[parameter(Mandatory = $false)][string] $checksumType = '',
[parameter(Mandatory = $false)][hashtable] $options = @{Headers = @{} },
[alias("fileFullPath")][parameter(Mandatory = $false)][string] $file = '',
[parameter(Mandatory = $false)][System.Net.ICredentials] $credentials = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)

Expand Down Expand Up @@ -193,7 +197,7 @@ Install-ChocolateyZipPackage
if ($installer) {
$download = "$env:TEMP\$($packageName.Replace(' ','')).vsix"
try {
Get-ChocolateyWebFile $packageName $download $vsixUrl -checksum $checksum -checksumType $checksumType -Options $options
Get-ChocolateyWebFile $packageName $download $vsixUrl -checksum $checksum -checksumType $checksumType -Options $options -Credentials $credentials
}
catch {
throw "There were errors attempting to retrieve the vsix from $vsixUrl. The error message was '$_'."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ Usage of this parameter will prevent Uninstall-ChocolateyZipPackage
from working, extracted files will have to be cleaned up with
Remove-Item or a similar command instead.

.PARAMETER Credentials
OPTIONAL A System.Net.ICredentials-Object that can be used for downloading files from a server which requires user authentication.

.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.

Expand Down Expand Up @@ -200,6 +203,7 @@ Get-ChocolateyUnzip
[alias("fileFullPath")][parameter(Mandatory = $false)][string] $file = '',
[alias("fileFullPath64")][parameter(Mandatory = $false)][string] $file64 = '',
[parameter(Mandatory = $false)][switch] $disableLogging,
[parameter(Mandatory = $false)][System.Net.ICredentials] $credentials = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)

Expand All @@ -225,6 +229,6 @@ Get-ChocolateyUnzip
$url64bit = $file64
}

$filePath = Get-ChocolateyWebFile $packageName $downloadFilePath $url $url64bit -checkSum $checkSum -checksumType $checksumType -checkSum64 $checkSum64 -checksumType64 $checksumType64 -options $options -getOriginalFileName
$filePath = Get-ChocolateyWebFile $packageName $downloadFilePath $url $url64bit -checkSum $checkSum -checksumType $checksumType -checkSum64 $checkSum64 -checksumType64 $checksumType64 -options $options -getOriginalFileName -Credentials $credentials
Get-ChocolateyUnzip "$filePath" $unzipLocation $specificFolder $packageName -disableLogging:$disableLogging
}