Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #54 from spacepants/cookie-support
Browse files Browse the repository at this point in the history
Add cookie support
  • Loading branch information
alexjfisher authored Mar 30, 2017
2 parents 7044a3d + 80b5e4c commit e6cacaa
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ The optional switch to change the way that `proxyPassword` is interpreted from
secure string to plaintext. This will send the password in plaintext to the
machine being provisioned, which may be a security concern.

#### `cookies`

An optional array of cookies to add to the HTTP request for the download.

## Limitations

This module is tested on the following platforms:
Expand Down
10 changes: 9 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
# [*timeout*]
# The optional timeout(in seconds) in case you expect to download big and slow file
#
# [*cookies*]
# An optional array of cookies to add to the HTTP request for the download.
#
# === Examples
#
# To download dotnet 4.0
Expand All @@ -63,7 +66,8 @@
$proxy_user='',
$proxy_password='',
$is_password_secure=true,
$timeout = undef
$timeout = undef,
Optional[Array[String]] $cookies = undef
) {

if "x${destination_file}x" == 'xx' {
Expand All @@ -77,6 +81,10 @@
Exec { timeout => $timeout }
}

if $cookies {
$cookie_string = join($cookies, ';')
}

$powershell_filename = regsubst($url, '^(.*\/)(.+?)(?:\.[^\.]*$|$)$', '\2')

validate_re($url, '.+')
Expand Down
51 changes: 51 additions & 0 deletions spec/defines/download_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,57 @@
it { is_expected.to contain_file('download-test.exe.ps1').with_content(ps1) }
end

describe 'when downloading a file with provided cookies we want to check that the erb gets evaluated correctly' do
let(:title) { 'Download DotNet 4.0' }
let(:params) do
{
url: 'http://myserver.com/test.exe',
destination_directory: 'c:\temp',
cookies: ['my_cookie=something-secure', 'this_too=something-else']
}
end

ps1 = <<-PS1.gsub(%r{^ {6}}, '')
$webclient = New-Object System.Net.WebClient
$proxyAddress = ''
$proxyUser = ''
$proxyPassword = ''
if ($proxyAddress -ne '') {
if (!($proxyAddress.StartsWith('http://') -or $proxyAddress.StartsWith('https://'))) {
$proxyAddress = 'http://' + $proxyAddress
}
$proxy = new-object System.Net.WebProxy
$proxy.Address = $proxyAddress
if (($proxyPassword -ne '') -and ($proxyUser -ne '')) {
$password = ConvertTo-SecureString -string $proxyPassword
$proxy.Credentials = New-Object System.Management.Automation.PSCredential($proxyUser, $password)
$webclient.UseDefaultCredentials = $true
}
$webclient.proxy = $proxy
}
$webclient.Headers.Add([System.Net.HttpRequestHeader]::Cookie, "my_cookie=something-secure;this_too=something-else")
try {
$webclient.DownloadFile('http://myserver.com/test.exe', 'c:\\temp\\test.exe')
}
catch [Exception] {
write-host $_.Exception.GetType().FullName
write-host $_.Exception.Message
write-host $_.Exception.InnerException.Message
throw $_.Exception
}
PS1

it { is_expected.to contain_file('download-test.exe.ps1').with_content(ps1) }
end

describe 'when not passing a destination url to the download define' do
let(:title) { 'Download DotNet 4.0' }
let(:params) do
Expand Down
4 changes: 3 additions & 1 deletion templates/download.ps1.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ if ($proxyAddress -ne '') {
}
$webclient.proxy = $proxy
}

<% if @cookies %>
<%= '$webclient.Headers.Add([System.Net.HttpRequestHeader]::Cookie, "' + @cookie_string + '")' %>
<% end %>
try {
$webclient.DownloadFile('<%= @url %>', '<%= @destination_directory %>\<%= @filename %>')
}
Expand Down

0 comments on commit e6cacaa

Please sign in to comment.