Monday, February 23, 2009

SSL and RubyonRails

Hi guys and gals too :),

Am back after a fair long interval this time, ya ya same old reason was busy with office and all. But recently did something good(if not unusual).
And the heading is telling it very correct, did implemented the SSL and used https for some of my pages in rubyonrails.

As many of my readers know, i am new to ubuntu so is new to apache too(initially i used to think, these dealings are sort of system admin stuff) but hey after all it is a software and some bit of configurations.. so i thought lets do it.. and i did it...

So now trimming all the conversation.. the aim of my application was to implement https(SSL) for the "payment gateway" and "pick package" page
and here is the solution
for the prerequisite purpose i assume that you have apache and ruby on rails setup in your system and you have an ROR application which needs https protocol for some of the pages

Step 00: Create a ssl certificate signed by yourself (by following these steps)
install the ssl-cert package
sudo aptitude install ssl-cert

# to create a self-signed certificate.. it will open several dialog boxes, keep on answering the question in correct format(some 7-8 inquiries are there) and at the end you will have your self-signed certificate
sudo make-ssl-cert /usr/share/ssl-cert/ssleay.cnf /path/to/ssl/certictare/selfsigned.pem

Step 01: first of all enable the modes(for apache)
sudo a2enmod ssl
sudo a2enmod proxy
sudo a2enmod rewrite
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod headers


Step 02: set the virtual host
goto apache root directory(which is at /etc/apache2 in my case) and execute following commands
#to switch to apache directory
cd /etc/apache2

#to disable the default site(which have the default configuration of apache)
sudo a2dissite default
#create a new configuration for virtual host by copying the default site configuration and (rename 'ourapplication' with your application name)
sudo cp sites-available/default sites-available/ourapplication
#to enable our application site configuration in apache
sudo a2ensite ourapplication

Step 03: edit the newly created ourapplication configuration, which is available at /etc/apache2/sites-available/ourapplication using any of your favorite editor
sudo gedit /etc/apache2/sites-available/ourapplication


and it should look something lie this
<VirtualHost *:80>
ServerName ourapplication
ProxyPass / http://somename.com:3000/
ProxyPassReverse / http://somename.com:3000/
</VirtualHost>

<VirtualHost *:443>
ServerName ourapplication
ProxyPass / http://somename.com:3000/
ProxyPassReverse / http://somename.com:3000/
ProxyPreserveHost On
RequestHeader set X_FORWARDED_PROTO 'https'

SSLEngine On
SSLProxyEngine On
SSLCertificateFile /path/to/self/signed/certificate/selfsigned.pem
SSLProxyMachineCertificateFile /path/to/self/signed/certificate/selfsigned.pem
</VirtualHost>


Step 04: We also have to change the proxy configuration, so that proxy request can be handled as we desire it to do
sudo gedit /etc/apache2/mods-available/proxy.conf

change the setting from
previous proxy setting
<proxy>
AddDefaultCharset off
Order deny,allow
Deny from all
Allow from .example.com
</proxy>
to new proxy setting
<proxy>
AddDefaultCharset off
Order deny,allow
Allow from all
</proxy>

Step 05: To reload the new setting so that apache can follow our rule..
sudo /etc/init.d/apache2 force-reload
(but it should not give any error, and it will not if no wrong is done to it form the above mentioned procedures)
if it gives some ouitpit like this
* Reloading web server config apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

Step 06: Now all apache work is done, you had to do small amount of effort in your rails application and here they
a) install a plugin
ruby script/plugin install ssl_requirement

b) include it in the application controller so that it can use ssl using
include SslRequirement

c) to use https on any particular action of some controller use
class EcommerceController < ApplicationController
ssl_required :action_name_1, :action_name_2, :action_name_3
# some more codes..............
end


if you have any query just drop me a comment, otherwise all is well as expected :)

Monday, February 16, 2009

visit to surajkund mela

Recently visited the surajkund mela @ faridabad, India.
I am as such reluctant to visit places with crowd but this time it was different[it was the valentine day after all :) ]. i have just uploaded the picture slideshow on slideshare.net

Tuesday, February 3, 2009

form inside form. is it possible?

Hi guys,

whatever i can recall i think i got hit by this problem earlier too, but at that time certainly i was not a blogger, in fact i was lazy of writing too :)) and see it came again, this issue is of placing a html form inside another.
It sounds messy right?? ya ya i know, but some how this was one of the requirement for one of the application that i was developing.

was having a html form with list item as radio box option but later it came to the requirement that label of radio button too should be update able so i tried putting up form_remote_tag(the rails method) for labels but it was not working in fact html form was not coming on to the page(as i was loading it through partials).

So went in for :with parameter in using the remote functions for prototype helper

the host rhtml
<% form_tag url_for(:controller => "controller_name", :action => "action_name") do %>
<%@some_collection_hash.each do |key,value| -%>
<span><%=key%></span>
<%value.each do |single_item|-%>
<div class="sbc" style="width:30px;">
<%=radio_button_tag("publisher[#{key}]", single_item.id,false,:disabled => (single_item.status != "valid")) -%>
</div>
<div class="sbc" id="<%=single_item.id-%>_batch">
<%=render :partial => "/xyz/partial_one", :locals => {:single_item => single_item, :key => key} -%>
</div>
<%end-%>
<%end-%>
<%= submit_tag "Get Summary"%>
<% end %>

The view partial
<ul>
<li class="head_li">Satus:</li>
<li class="dotless_li">
<%=select_tag("#{single_item_id}_status", options_for_select(["valid","invalid","pending","running","deleted","summarize"],@single_item.status)) %>
</li>
</ul>
<ul>
<li class="head_li">Description:</li>
<li class="dotless_li"><%=text_area_tag "#{single_item_id}_desc", @single_item.comments, :rows => 3, :cols => 30 -%></li>
</ul>
<input type="button" value="Close" onclick="<%= remote_function(:update => "options",
:url => {:controller=> :controller_name, :action => :show_single_iteminfo ,:single_item_id => single_item_id, :key => key},
:update => { :success => "#{single_item_id}_single_item", :failure => "#{single_item_id}_single_item" }) -%>" />

<input type="button" value="Save" onclick="<%= remote_function(:update => "options",
:url => {:controller=> :controller_name, :action => :save_single_iteminfo ,:single_item_id => single_item_id, :key => key},
:with => "'description='+$F('#{single_item_id}_desc')+'&'+'status='+$F('#{single_item_id}_status')",
:update => { :success => "#{single_item_id}_single_item", :failure => "#{single_item_id}_single_item" }) -%>" />

Honda Civic & A/C problems.

Hello Friends, Got a post again on to Honda Civic (The good old favorite commuter of mine). This car has been doing great except for so...