ActiveAdmin.register ColorGroup do
index :download_links => [:csv] do
selectable_column
column(:id) { |it| auto_link it, it.id }
column(:name) { |it| auto_link it, it.name }
column(:slug) { |it| auto_link it, it.slug }
column(:family) { |it| auto_link it.color_family, it.color_family.name if it.color_family.present? }
column :asset_url
column :order
column :image do |it|
if it.nil?
"N/A"
else
# Use image_tag here...
link_to sanitize("<img src=\"#{it.asset_url}\" class=\"cgs-img\" width=\"101\" height=\"24\" />"), it.asset_url
end
end
actions
end
end
Friday, October 24, 2014
Images in Active Admin Index pages.
Monday, October 13, 2014
Block IP Addresses Using iptables
This post describes the steps to block the IP address that is the origin of a brute-force attack against a WordPress blog.
WARNING!
Make sure you DO NOT block your own IP addresses, or apply and global rules which would block the SSH port (port 22).Identify IP Addresses to Block (Blog Server)
Tail the blog server apache logs:
sudo tail -f /var/log/httpd/access_log
199.188.70.163 - - [12/Oct/2014:16:29:03 +0000] "POST /wp-login.php HTTP/1.0" 200 4432 "-" "-" 37.59.125.22 - - [12/Oct/2014:16:29:03 +0000] "POST /wp-login.php HTTP/1.0" 200 4432 "-" "-" 199.188.70.163 - - [12/Oct/2014:16:29:04 +0000] "POST /wp-login.php HTTP/1.0" 200 4432 "-" "-" 37.59.125.22 - - [12/Oct/2014:16:29:05 +0000] "POST /wp-login.php HTTP/1.0" 200 4432 "-" "-" 37.59.125.22 - - [12/Oct/2014:16:29:05 +0000] "POST /wp-login.php HTTP/1.0" 200 4432 "-" "-"
This shows two ip addresses, 199.188.70.163 and 37.59.125.22 attempting to brute-force the blog's Word Press login page:
It is worth checking the Apache error log:
sudo tail -f /var/log/httpd/access_log
Blocking IP Addresses Using iptables
List existing iptables rules:
# List iptables rules sudo iptables -S -P INPUT ACCEPT -P FORWARD ACCEPT -P OUTPUT ACCEPT
The output of the -S switch can be used to re-create the given rule from the command line or a rule script. To block a single IP addresses 199.188.70.163 and 37.59.125.22 do the following:
sudo iptables -A INPUT -s 199.188.70.163 -j DROP sudo iptables -A INPUT -s 37.59.125.22 -j DROP
Listing the iptables rules once more should show the DROP rules that were added in the previous step:
# List iptables rules sudo iptables -S -P INPUT ACCEPT -P FORWARD ACCEPT -P OUTPUT ACCEPT -A INPUT -s 199.188.70.163 -j DROP -A INPUT -s 37.59.125.22 -j DROP
The two rules that were added have not taken effect yet. To save them do the following:
# Save the iptables rules sudo /etc/init.d/iptables save
NOTE: The /sbin/service command is available on the blog server, so you can use that to save iptables rules:
sudo /sbin/service iptables save The service command runs /etc/init.d/ scripts without passing on environment variables defined by the user.
When successfully saved, it should display a message such as:
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
Removing iptables Rules
List iptables Rules:
sudo iptables -S
-P INPUT ACCEPT -P FORWARD ACCEPT -P OUTPUT ACCEPT -A INPUT -s 199.188.70.0/24 -j DROP -A INPUT -s 37.59.125.0/24 -j DROP
The following lists the form in a more readable form:
# List rules with line number in verbose mode sudo iptables -L -n -v --line-numbers
In the following output the "num" column represents the line number within each Chain (INPUT, FORWARD, and OUTPUT). This number will be will be used to remove the rules.
Chain INPUT (policy ACCEPT 31267 packets, 2159K bytes) num pkts bytes target prot opt in out source destination 1 206 12472 DROP all -- * * 199.188.70.0/24 0.0.0.0/0 2 313 20047 DROP all -- * * 37.59.125.0/24 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 14419 packets, 75M bytes) num pkts bytes target prot opt in out source destination
Look at the number on the left in the "num" column, then use number to delete the rule. For example delete line number 2 (subnet 37.59.125.0/24), enter:
sudo iptables -D INPUT 2
This will not take effect until it the rule is saved:
# Save the iptables rules sudo /etc/init.d/iptables save
Identify Other IP Addresses Performing Similar Attacks
Use the following command to identify IP addresses making large number of login attempts the WordPress blog:
# IP addresses making POST request to the WordPress login page
grep wp-login /var/log/httpd/access_log | grep POST | awk '{print $1}'| sort | uniq -c | sort -nr
20154 199.188.70.163
14105 194.65.224.242
3732 37.59.125.22
40 37.1.222.114
25 192.187.99.194
Make sure that you EXCLUDE remove your own IP addresses and other legitimate IP addresses from the above list before blocking them. In this case we should probably block the first three ( 20154 199.188.70.163, 194.65.224.242, and 37.59.125.22) and not the last two (37.1.222.114 and 192.187.99.194).
You may want to use an IP address lookup tool such as MaxMind GeoIP Lookup to see where the IP address is located:
Finding Your Public IP Address on the Command Line
The following is a Bash command that can be used to quickly find the public IP address your internet service provider (ISP) has given you. This is not the same as the IP address seen by your broadband router, which is usually an private IP address within the ISPs network, or the IP address given to your computer, which is a private IP address issued by my home router.
This command simply makes a "what is my ip" query to Google, which in turn displays the IP address it sees the request originating from. The awk command parses the IP address from the rest of the HTML and prints it to the command line.
curl -s 'https://www.google.com/search?q=what+is+my+ip' | awk 'match($0, /\(Client.*: (.*)\)/){print substr($0, RSTART, RLENGTH)}'
(Client IP address: 50.x.x.x)
Saturday, May 18, 2013
Using Gnuplot in Bash Shell Scripts
This is an example of how Gnuplot scripts can be embedded within Bash scripts using Bash Here Documents (heredocs):
#!/bin/bash
filename="log/load_me.log.1368725550"
awk '/for 10000000/ {c+=1; if(c > 46){n+=1; printf "%s\t%s\n", n, $2} }' $filename > log/values.dat
window_size=50
ruby running_average.rb log/values.dat $window_size > log/averages.dat
gnuplot -p <<EOSCRIPT
set title 'Time to Insert Relationships in Batches of 10M in Neo4j batch-import'
set xlabel 'Batches of 10M Rels on `date "+%Y-%m-%d at %H:%M:%S %Z"`'
set ylabel 'Time Taken (s)'
set grid
# Draw trend line
f(x) = a*x**3 + b*x**2 + c*x + d
set fit quiet
fit f(x) 'log/values.dat' using 1:(\$2/1000) via a,b,c,d
unset fit
# Scale column No.2 by 1000 to turn ms in s.
plot 'log/values.dat' using 1:(\$2/1000) title 'Time to Insert Rels' with lines, f(x) title 'Fit', 'log/averages.dat' using 1:5 title "Average $window_size" with lines
pause -1 "\n\nHit return to continue\n\n"
EOSCRIPT
num_stats=`wc -l log/values.dat | awk '{print $1}'`
tail -$window_size log/values.dat | awk -v num_stats="$num_stats" '{n+=1; s+=$2}
END{
avg=s/(n*1000);
print "Average of last", n, "is", avg, "(s)";
print "Num Rels Stats =", num_stats;
print "Hours Remaining =", (3300-num_stats+46)*avg/3600;
printf "Percentage complete = %.2f%%\n", (num_stats * 100 / 3300.0);
}'
echo -e "Completed at `date`\n\n"
References
- Gnuplot fit command
- http://www.manpagez.com/info/gnuplot/gnuplot-4.6.0/gnuplot_263.php
- Bash Here Documents
- https://www.tldp.org/LDP/abs/html/here-docs.html
- Wikipedia Here Document
- https://en.wikipedia.org/wiki/Here_document
Monday, September 17, 2012
Installing percona-tools Using Homebrew
Try installing percona-tools using Homebrew:
brew install percona-tools
and it will throw the following error:
indika$ brew install percona-toolkit Unsatisfied dependency: DBD::mysql Homebrew does not provide Perl dependencies; install with: cpan -i DBD::mysql
The solution is to first install the DBD:mysql CPAN module manually:
cpan -i DBD::mysql
When prompted for parameters for Makefile.PL, enter the following:
Parameters for the 'perl Makefile.PL' command? [] --testuser=root
Here "root" refers to the DB user, not the system user by the same name. When I tried to install DBD:mysql without the "--testuser=<username>" option on my computer, it resulted in the following error:
t/80procs.t ................. 1/29 DBD::mysql::db do failed: alter routine command denied to user ''@'localhost' for routine 'test.testproc' at t/80procs.t line 41. DBD::mysql::db do failed: alter routine command denied to user ''@'localhost' for routine 'test.testproc' at t/80procs.t line 41. # Looks like you planned 29 tests but ran 2. # Looks like your test exited with 255 just after 2.
Now CD into your CPAN build directory:
cp ~/.cpan/build ls -l drwxr-xr-x 28 indika staff 952 Sep 16 23:32 DBD-mysql-4.022-Sl9Tjw drwxr-xr-x 28 indika staff 952 Sep 16 22:59 DBD-mysql-4.022-XOJVB7 drwxr-xr-x 28 indika staff 952 Sep 16 23:30 DBD-mysql-4.022-mnhn3t
... and make the package:
cd DBD-mysql-4.022-mnhn3t make make test sudo make install
Now install percona-tools using Homebrew:
brew install percona-tools
You should see it successfully:
==> Downloading http://www.percona.com/redir/downloads/percona-toolkit/2.1.2/percona-toolkit-2.1.2.tar.gz ######################################################################## 100.0% ==> perl Makefile.PL PREFIX=/usr/local/Cellar/percona-toolkit/2.1.2 ==> make ==> make test ==> make install /usr/local/Cellar/percona-toolkit/2.1.2: 79 files, 5.6M, built in 11 seconds
Tuesday, December 7, 2010
Turning Over a New Leaf
This changed today when I realized that the new templates could have a maximum width of 1000px while the old templates had a fixed width. This is something I had desperately needed. The usable area for a text of a blog post was around 460px. The source code snippets and console output dumps which I include in my posts as pre-formatted text often wrap around or need to be scrolled due to the limited width of the post area.
With the new template the usable area for the blog post text is approximately 670px. It doesn't seem like much of an improvement, but it makes a huge difference!
The new design took effect at 11:47pm PST today. It is a slightly modified version of one of the new stock designs. I am not yet done tweaking the new blog design, but I feel that this is a good starting point... for now.
Here is a screenshot of this post with the previous blog template I used, included here more for the sake of nostalgia than anything else.
Tuesday, November 23, 2010
Configuring Aptana to Treat Gemfiles as Ruby Files
- Open the "Window Menu" --> Preferences
- Go to General --> "Content Types"
- Expand "Text" node under "Content types"
- Select "Ruby Source File"
- Click the "Add..." button and enter "Gemfile" as the content type.
- Click the "OK" button to save your changes.
The above procedure can be used to add files for the following content types:
- CSS Source Files
- ERB Source Files
- HAML Source Files
- HTML Source Files
- JAR Manifest Files
- Java Property Files
- Java Source Files
- JavaScript Source Files
- SASS Source Files
- XML Build Files (Ant, etc)
- XML Source Files (xml, xslt, etc)
Friday, March 26, 2010
Nondiscriptive Rail Error "Errors running test:units!"
Errors Running test:units!
The "Errors running test:units!" error while creating a brand new Rails application under Cygwin:
rails world_app -d mysql cd world_app $ rake test
This results in the following error message:
(in /cygdrive/d/workspace/tt/world_app) /usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" /usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" Errors running test:units!
Attempting to execute the Rails tests results in the same eror:
$ rake test --trace (in /cygdrive/d/workspace/tt/world_app) ** Invoke test (first_time) ** Execute test ** Invoke test:units (first_time) ** Invoke db:test:prepare (first_time) ** Invoke db:abort_if_pending_migrations (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:abort_if_pending_migrations ** Invoke test:functionals (first_time) ** Invoke db:test:prepare ** Execute test:functionals /usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" ** Invoke test:integration (first_time) ** Invoke db:test:prepare ** Execute test:integration /usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" Errors running test:units!
Fixing the Error
The following steps fixed the error:
- Edit config/database.yml
- Make sure the db password is correct.
- Make sure the db is created.
- Change "host: localhost" to "host: 127.0.0.1" if need be.
$ rake test --trace (in /cygdrive/d/workspace/tt/world_app) ** Invoke test (first_time) ** Execute test ** Invoke test:units (first_time) ** Invoke db:test:prepare (first_time) ** Invoke db:abort_if_pending_migrations (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:abort_if_pending_migrations ** Execute db:test:prepare ** Invoke db:test:load (first_time) ** Invoke db:test:purge (first_time) ** Invoke environment ** Execute db:test:purge ** Execute db:test:load ** Invoke db:schema:load (first_time) ** Invoke environment ** Execute db:schema:load ** Execute test:units /usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" ** Invoke test:functionals (first_time) ** Invoke db:test:prepare ** Execute test:functionals /usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" ** Invoke test:integration (first_time) ** Invoke db:test:prepare ** Execute test:integration /usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"

