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

Commit

Permalink
Adding basic auth capability to downloadfile
Browse files Browse the repository at this point in the history
  • Loading branch information
scottpecnik committed May 22, 2018
1 parent 1cbaf3e commit 6669f0d
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ The full path to the directory on the system where the file will be downloaded t

The optional name of the file to download onto the system.

#### `user`

The optional userid is used when authenticating via Basic Auth with the server.
`user` and `password` must be specified or this has no effect.

#### `password`

The optional password is used when authenticating via Basic Auth with the server.
`user` and `password` must be specified or this has no effect.

#### `proxy_address`

The optional http proxy address to use when downloading the file
Expand Down
2 changes: 2 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
String $destination_directory,
Optional[String] $destination_file = undef,
$proxy_address = undef,
$user = '',
$password = '',
$proxy_user = '',
$proxy_password = '',
$is_password_secure = true,
Expand Down
107 changes: 106 additions & 1 deletion spec/defines/download_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@

ps1 = <<-PS1.gsub(%r{^ {6}}, '')
$webclient = New-Object System.Net.WebClient
$user = ''
$password = ''
$proxyAddress = ''
$proxyUser = ''
$proxyPassword = ''
# This is potentially not secure if not using ssl
if (($user -ne '') -and ($password -ne '')) {
# Basic authentication encoding.
$basic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($user + ":" + $password));
# Set Authorization HTTP header with Basic authentication information.
$webclient.Headers["Authorization"] = "Basic $basic"
}
if ($proxyAddress -ne '') {
if (!($proxyAddress.StartsWith('http://') -or $proxyAddress.StartsWith('https://'))) {
Expand Down Expand Up @@ -113,10 +122,19 @@

ps1 = <<-PS1.gsub(%r{^ {6}}, '')
$webclient = New-Object System.Net.WebClient
$user = ''
$password = ''
$proxyAddress = 'test-proxy-01:8888'
$proxyUser = ''
$proxyPassword = ''
# This is potentially not secure if not using ssl
if (($user -ne '') -and ($password -ne '')) {
# Basic authentication encoding.
$basic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($user + ":" + $password));
# Set Authorization HTTP header with Basic authentication information.
$webclient.Headers["Authorization"] = "Basic $basic"
}
if ($proxyAddress -ne '') {
if (!($proxyAddress.StartsWith('http://') -or $proxyAddress.StartsWith('https://'))) {
Expand Down Expand Up @@ -185,10 +203,19 @@

ps1 = <<-PS1.gsub(%r{^ {6}}, '')
$webclient = New-Object System.Net.WebClient
$user = ''
$password = ''
$proxyAddress = 'test-proxy-01:8888'
$proxyUser = 'test-user'
$proxyPassword = 'test-secure'
# This is potentially not secure if not using ssl
if (($user -ne '') -and ($password -ne '')) {
# Basic authentication encoding.
$basic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($user + ":" + $password));
# Set Authorization HTTP header with Basic authentication information.
$webclient.Headers["Authorization"] = "Basic $basic"
}
if ($proxyAddress -ne '') {
if (!($proxyAddress.StartsWith('http://') -or $proxyAddress.StartsWith('https://'))) {
Expand Down Expand Up @@ -259,10 +286,19 @@

ps1 = <<-PS1.gsub(%r{^ {6}}, '')
$webclient = New-Object System.Net.WebClient
$user = ''
$password = ''
$proxyAddress = 'test-proxy-01:8888'
$proxyUser = 'test-user'
$proxyPassword = 'test'
# This is potentially not secure if not using ssl
if (($user -ne '') -and ($password -ne '')) {
# Basic authentication encoding.
$basic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($user + ":" + $password));
# Set Authorization HTTP header with Basic authentication information.
$webclient.Headers["Authorization"] = "Basic $basic"
}
if ($proxyAddress -ne '') {
if (!($proxyAddress.StartsWith('http://') -or $proxyAddress.StartsWith('https://'))) {
Expand Down Expand Up @@ -309,10 +345,19 @@

ps1 = <<-PS1.gsub(%r{^ {6}}, '')
$webclient = New-Object System.Net.WebClient
$user = ''
$password = ''
$proxyAddress = ''
$proxyUser = ''
$proxyPassword = ''
# This is potentially not secure if not using ssl
if (($user -ne '') -and ($password -ne '')) {
# Basic authentication encoding.
$basic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($user + ":" + $password));
# Set Authorization HTTP header with Basic authentication information.
$webclient.Headers["Authorization"] = "Basic $basic"
}
if ($proxyAddress -ne '') {
if (!($proxyAddress.StartsWith('http://') -or $proxyAddress.StartsWith('https://'))) {
Expand Down Expand Up @@ -494,4 +539,64 @@
end
end
end

describe 'when downloading a file with basic authenitcation 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',
user: 'testuser',
password: 'testpass'
}
end

ps1 = <<-PS1.gsub(%r{^ {6}}, '')
$webclient = New-Object System.Net.WebClient
$user = 'testuser'
$password = 'testpass'
$proxyAddress = ''
$proxyUser = ''
$proxyPassword = ''
# This is potentially not secure if not using ssl
if (($user -ne '') -and ($password -ne '')) {
# Basic authentication encoding.
$basic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($user + ":" + $password));
# Set Authorization HTTP header with Basic authentication information.
$webclient.Headers["Authorization"] = "Basic $basic"
}
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
}
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
end
9 changes: 9 additions & 0 deletions templates/download.ps1.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
$webclient = New-Object System.Net.WebClient
$user = '<%= @user %>'
$password = '<%= @password %>'
$proxyAddress = '<%= @proxy_address %>'
$proxyUser = '<%= @proxy_user %>'
$proxyPassword = '<%= @proxy_password %>'
<% if @user_agent %>
$webclient.Headers['User-Agent'] = '<%= @user_agent %>'
<% end %>
# This is potentially not secure if not using ssl
if (($user -ne '') -and ($password -ne '')) {
# Basic authentication encoding.
$basic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($user + ":" + $password));
# Set Authorization HTTP header with Basic authentication information.
$webclient.Headers["Authorization"] = "Basic $basic"
}

if ($proxyAddress -ne '') {
if (!($proxyAddress.StartsWith('http://') -or $proxyAddress.StartsWith('https://'))) {
Expand Down

0 comments on commit 6669f0d

Please sign in to comment.