HTTP basic auth for Rack::Static app on Heroku - ruby

I have a simple Rack app hosted on Heroku. config.ru:
use Rack::Static,
:urls => ["/stylesheets", "/images", "/javascripts"],
:root => "public"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
How can I add HTTP Basic Auth to this? Bonus points if it only works in the production environment.
Thanks

use Rack::Static,
:urls => ["/stylesheets", "/images", "/javascripts"],
:root => "public"
#SOLUTION:
use Rack::Auth::Basic, "Restricted Area" do |username, password|
[username, password] == ['admin', 'admin']
end
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}

If you want to also protect images, stylesheets and javascripts behind basic auth, you need to put Rack::Auth::Basic first:
use Rack::Auth::Basic, "Restricted Area" do |username, password|
[username, password] == ['admin', 'admin']
end
use Rack::Static,
:urls => ["/stylesheets", "/images", "/javascripts"],
:root => "public"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}

Related

Fedex Ruby Gem: Customs Value is required - how to add?

I'm working with the Ruby gem 'FedEx', https://github.com/jazminschroeder/fedex.
I've set up my code for a development mode and I'm testing making a shipment.
However, I get stuck with the following error:
C:/Ruby22/lib/ruby/gems/2.2.0/gems/fedex-.10.1/lib/fedex/request/shipment.rb:134:in 'failure_response': Customs Value is required. (Fedex:: RateError) from C: /Ruby22/lib/ruby/gems/2.2.0/gems/fedex-.10.1/lib/fedex/request/shipment.rb:32:in 'process_request' from C: /Ruby22/lib/ruby/gems/2.2.0/gems/fedex-3.10.1/lib/fedex/shipment.rb:57:in 'ship' from C: /Ruby22/bin/css_fedex_v1.rb:92:in ''
It seems that I need to parse a 'Customs Value', probably as part of my 'packages' hash. However, I'm unable to find the relevant field for me to enter this in. Anyone who's experienced this and found a solution?
My code is as below:
require 'fedex'
fedex = Fedex::Shipment.new(:key => '***',
:password => '***',
:account_number => '***',
:meter => '***',
:mode => 'development')
shipper = { :name => "***",
:company => "***",
:phone_number => "***",
:address => "***",
:city => "***",
:postal_code => "***",
:country_code => "DK" }
recipient = { :name => "***",
:company => "***",
:phone_number => "***",
:address => "***",
:city => "***",
:postal_code => "***",
:country_code => "GB",
:residential => "false" }
packages = []
packages << {:weight => {:units => "LB", :value => 1}}
shipping_options = {:packaging_type => "YOUR_PACKAGING",
:drop_off_type => "REGULAR_PICKUP"}
rate = fedex.rate(:shipper=>shipper,
:recipient => recipient,
:packages => packages,
:shipping_options => shipping_options)
ship = fedex.ship(:shipper=>shipper,
:recipient => recipient,
:packages => packages,
:service_type => "INTERNATIONAL_PRIORITY",
:shipping_options => shipping_options)
puts ship[:completed_shipment_detail][:operational_detail][:transit_time]
Customs value is declared in their docs:
https://github.com/jazminschroeder/fedex/commit/9f1d4c67b829aaa4eeba9090c1a45d3bd507aab3#diff-4f122efb7c0d98120d8b7f0cd00998e4R106
customs_value = { :currency => "USD",
:amount => "200" }
As I understand you can pass it into the commodities hash or keep it separate.

How do I Work Around " Encoding::UndefinedConversionError: ""\x9D"" from Windows-1252 to UTF-8"?

Here's the code that generates the error:
issues.each
{
|issue|
response = RestClient::Request.new
(
:method => :get,
:url => "http://wls-eng1:8080/rest/api/2/issue/{issue['key']}/comment",
:user => username,
:password => password,
:headers => { :accept => :json, :content_type => :json },
:ssl_ca_file => :temp
).execute
}

How do I create an AWS ELB listener with frontend security policy? How do I replace the policy?

In general, this is the command I'd use:
my_elb = elb.create("test", { :availability_zones => ["us-east-1d"],
:listeners => [ { :port => 80, :protocol => "http",
:instance_port => 8082,
:instance_protocol => "http" }
] } )
my_elb.listeners.create( { :port => 443,
:protocol => :https,
:instance_protocol => :http,
:instance_port => 80,
:server_certificate => cert.arn } )
But I can't find the syntax for adding a security policy (e.g. "ELBSecurityPolicy-2014-10")
You should save the listener into a variable then call the policy= method:
my_elb = elb.create("test", { :availability_zones => ["us-east-1d"],
:listeners => [ { :port => 80, :protocol => "http",
:instance_port => 8082,
:instance_protocol => "http" }
] } )
listener = my_elb.listeners.create( { :port => 443,
:protocol => :https,
:instance_protocol => :http,
:instance_port => 80,
:server_certificate => cert.arn } )
listener.policy = 'ELBSecurityPolicy-2014-10'
puts listener.policy.inspect # <AWS::ELB::LoadBalancerPolicy load_balancer_name:test name:ELBSecurityPolicy-2014-10>
Hope this helps
I'm working on the same thing at this time and you can add the security policy into the elb.create object like I'm doing. The example below is for a VPC based ELB, as you can see in the outside of the listener you can add security groups as an array.
webservice = elb.load_balancers.create( "webservice001",
:listeners => [{
:port => 80,
:protocol => :http,
:instance_port => 8080,
:instance_protocol => :http
},
{
:port => 443,
:protocol => :https,
:instance_port => 8443,
:instance_protocol => :https,
:ssl_certificate_id => "arn:aws:iam::xxxxxxxxx:server-certificate/server-certificate"
}],
:subnets =>[ "subnet-id1", "subnet-id2", "subnet-id3" ],
:security_groups => [ "webservice001-elb-sg-id#" ],
:scheme => 'internet-facing'
)

Sinatra, Pony gem: NoMethodError

I am trying to send an e-mail using Pony and get the
NoMethodError at /
undefined method `address' for #Mail::Message:. error. This is my code so far:
post '/' do
Pony.options = { :from => '___#yandex.ru',
:via => :smtp,
:address => 'smtp.yandex.ru',
:port => '465',
:user_name => '___',
:password => '___',
:authentication => :plain,
:domain => "http://127.0.0.1:9393/"
}
Pony.mail(subject: 'Hello', to: "___#yandex.ru", body: 'hi')
redirect '/'
end
when running bundle list it does show pony (1.10). What could go wrong?
:address, :port, etc. go inside a :via_options hash.
Per the documentation:
:via_options => {
:address => 'smtp.yourserver.com',
:port => '25',
:user_name => 'user',
:password => 'password',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
}
Therefore, you'll want:
post '/' do
Pony.options = {
:from => '___#yandex.ru',
:via => :smtp,
:via_options => {
:address => 'smtp.yandex.ru',
:port => '465',
:user_name => '___',
:password => '___',
:authentication => :plain,
:domain => "http://127.0.0.1:9393/"
}
}
Pony.mail(subject: 'Hello', to: "___#yandex.ru", body: 'hi')
redirect '/'
end

Rack: How do you store the URL as a variable?

I am writing a simple static Rack app. Check out the config.ru code below:
use Rack::Static,
:urls => ["/elements", "/img", "/pages", "/users", "/css", "/js"],
:root => "archive"
map '/' do
run Proc.new { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=6400'
},
File.open('archive/splash.html', File::RDONLY)
]
}
end
map '/pages/search.html' do
run Proc.new { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=6400'
},
File.open('archive/pages/search.html', File::RDONLY)
]
}
end
map '/pages/user.html' do
run Proc.new { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=6400'
},
File.open('archive/pages/user.html', File::RDONLY)
]
}
end
# Each map section is repeated for each HTML page served
I'd like to simplify this by storing the URL as variable and creating one map section that says
map url do
run Proc.new { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=6400'
},
File.open('archive' + url, File::RDONLY)
]
}
end
How can I correctly set this url variable?
How about:
static_page_mappings = {
'/' => 'archive/splash.html',
'/pages/search.html' => 'archive/pages/search.html'
'/pages/user.html' => 'archive/pages/user.html',
}
static_page_mappings.each do |req, file|
map req do
run Proc.new { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=6400',
},
File.open(file, File::RDONLY)
]
}
end
end
You shouldn't need the map part.
run Proc.new { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=6400'
},
File.open( 'archive' + env['PATH_INFO'], File::RDONLY)
]
}

Resources