Permalink
Browse files

Initial commit - implemented SecurityGuard::CountryIps

  • Loading branch information...
0 parents commit 614bc809dc44c841fd9dc8d448f652b4a422d46a @fredwu committed Jan 18, 2012
@@ -0,0 +1,18 @@
+.DS_Store
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,3 @@
+source 'https://rubygems.org'
+
+gemspec
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Fred Wu
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,51 @@
+# SecurityGuard
+
+This gem is a collection of useful tools for auditing data and performing security checks.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'security_guard'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install security_guard
+
+## Usage
+
+### Country IPs
+
+Returns a list of the IPs from given country and IP dictionaries. Useful for auditing IPs from higher risk nations.
+
+```ruby
+country_ips = SecurityGuard::CountryIps.new
+country_ips.countries = ['Australia', 'United States']
+country_ips.ips = ['4.4.4.4', '8.8.8.8', '203.206.0.1']
+country_ips.result # => ['203.206.0.1']
+```
+
+You may also pass country and IP data as a line-delimited file by appending `_from_file` at the end of the attributes:
+
+```ruby
+country_ips.countries_from_file = '/path/to/the/file'
+country_ips.ips_from_file = '/path/to/the/file'
+```
+
+## Contributing
+
+1. Fork it
+2. Make sure you add documentation to README.md
+3. Make sure you test all your code
+4. Do your magic!
+5. Create a new Pull Request
+
+## Author
+
+- [Fred Wu](http://fredwu.me/)
+
+Brought to you by [SitePoint](http://www.sitepoint.com/).
@@ -0,0 +1,11 @@
+#!/usr/bin/env rake
+require 'bundler/gem_tasks'
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+ t.libs.push 'lib'
+ t.test_files = FileList['specs/**/*_spec.rb']
+ t.verbose = true
+end
+
+task :default => :test
Binary file not shown.
@@ -0,0 +1,5 @@
+require 'security_guard/version'
+require 'security_guard/utils/files'
+require 'security_guard/utils/accepts_from_file'
+require 'security_guard/utils/geo_ips'
+require 'security_guard/country_ips'
@@ -0,0 +1,20 @@
+module SecurityGuard
+ class CountryIps
+ include Utils::AcceptsFromFile
+
+ accepts_from_file :countries, :ips
+
+ def initialize
+ @geoip ||= Utils::GeoIps.new(Utils::Files.load('GeoIP.dat'))
+ end
+
+ def result
+ country_ips = []
+ ips.each do |ip|
+ @geoip.ip_address = ip
+ country_ips << ip if countries.include?(@geoip.country_name)
+ end
+ country_ips.sort
+ end
+ end
+end
@@ -0,0 +1,20 @@
+module SecurityGuard
+ module Utils
+ module AcceptsFromFile
+ def self.included(klass)
+ klass.extend ClassMethods
+ end
+
+ module ClassMethods
+ def accepts_from_file(*attributes)
+ attributes.each do |attribute|
+ self.send :attr_accessor, attribute
+ self.send :define_method, "#{attribute}_from_file=", lambda { |file|
+ self.send "#{attribute}=", SecurityGuard::Utils::Files.to_array(file)
+ }
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,13 @@
+module SecurityGuard
+ module Utils
+ class Files
+ def self.load(filename)
+ File.expand_path("../../../../data/#{filename}", __FILE__)
+ end
+
+ def self.to_array(line_delimited_file)
+ File.readlines(line_delimited_file).map{ |line| line.sub("\n", '') }
+ end
+ end
+ end
+end
@@ -0,0 +1,22 @@
+require 'geoip'
+
+module SecurityGuard
+ module Utils
+ class GeoIps
+ attr_accessor :ip_address
+
+ def initialize(geo_data_file, ip_address = nil)
+ @geoip ||= GeoIP.new(geo_data_file)
+ self.ip_address = ip_address
+ end
+
+ def country
+ @geoip.country(ip_address)
+ end
+
+ def country_name
+ country.country_name
+ end
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module SecurityGuard
+ VERSION = '0.0.1'
+end
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/security_guard/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ['Fred Wu']
+ gem.email = ['fred.wu@sitepoint.com']
+ gem.summary = %q{A bunch of helpers for security audit.}
+ gem.description = gem.summary
+ gem.homepage = 'http://sitepoint.com/'
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = 'security_guard'
+ gem.require_paths = ['lib']
+ gem.version = SecurityGuard::VERSION
+
+ gem.add_dependency 'geoip'
+ gem.add_development_dependency 'simplecov'
+end
@@ -0,0 +1,17 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe SecurityGuard::CountryIps do
+ before do
+ @country_ips = SecurityGuard::CountryIps.new
+ @country_ips.countries = ['Australia']
+ @country_ips.ips = ['4.4.4.4', '8.8.8.8', '203.206.0.1']
+ end
+
+ it 'contains GeoIP data' do
+ @country_ips.instance_variable_get(:@geoip).must_be_instance_of SecurityGuard::Utils::GeoIps
+ end
+
+ it 'returns all IPs from the given country and IP dictionaries' do
+ @country_ips.result.must_equal ['203.206.0.1']
+ end
+end
@@ -0,0 +1,3 @@
+Australia
+United States
+United Kingdom
@@ -0,0 +1,15 @@
+require 'simplecov'
+SimpleCov.start
+
+require 'minitest/autorun'
+require 'minitest/spec'
+
+require File.expand_path('../../lib/security_guard', __FILE__)
+
+def data_file(filename)
+ SecurityGuard::Utils::Files.load(filename)
+end
+
+def fixture_file(filename)
+ File.expand_path("../fixtures/#{filename}", __FILE__)
+end
@@ -0,0 +1,22 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+class TestA
+ include SecurityGuard::Utils::AcceptsFromFile
+ accepts_from_file :test_attribute
+end
+
+describe SecurityGuard::Utils::AcceptsFromFile do
+ before do
+ @test = TestA.new
+ end
+
+ it 'has #test_attribute as an accessor' do
+ @test.test_attribute = 'test'
+ @test.test_attribute.must_equal 'test'
+ end
+
+ it 'converts data from a file into an array' do
+ @test.test_attribute_from_file = fixture_file('countries.txt')
+ @test.test_attribute.must_equal ['Australia', 'United States', 'United Kingdom']
+ end
+end
@@ -0,0 +1,15 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe SecurityGuard::Utils::Files do
+ describe 'SecurityGuard::Utils::Files#load' do
+ it 'points to the correct file' do
+ SecurityGuard::Utils::Files.load('GeoIP.dat').must_match 'security_guard/data/GeoIP.dat'
+ end
+ end
+
+ describe 'SecurityGuard::Utils::Files#to_array' do
+ it 'returns an array of the line delimited data' do
+ SecurityGuard::Utils::Files.to_array(fixture_file('countries.txt')).must_equal ['Australia', 'United States', 'United Kingdom']
+ end
+ end
+end
@@ -0,0 +1,45 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe SecurityGuard::Utils::GeoIps do
+ describe 'initialise without an ip' do
+ before do
+ @geoip = SecurityGuard::Utils::GeoIps.new(data_file('GeoIP.dat'))
+ end
+
+ it 'initialises with a geo ip database' do
+ @geoip.must_be_instance_of SecurityGuard::Utils::GeoIps
+ end
+
+ it 'sets an ip address' do
+ @geoip.ip_address = '8.8.8.8'
+ @geoip.ip_address.must_equal '8.8.8.8'
+ end
+ end
+
+ describe 'initialise with an ip' do
+ before do
+ @geoip = SecurityGuard::Utils::GeoIps.new(data_file('GeoIP.dat'), '8.8.8.8')
+ end
+
+ it 'has an ip address' do
+ @geoip.ip_address.must_equal '8.8.8.8'
+ end
+
+ it 'detects the country' do
+ @geoip.country.must_be_kind_of Struct
+ @geoip.country.to_hash.must_equal({
+ :request => '8.8.8.8',
+ :ip => '8.8.8.8',
+ :country_code => 225,
+ :country_code2 => 'US',
+ :country_code3 => 'USA',
+ :country_name => 'United States',
+ :continent_code => 'NA'
+ })
+ end
+
+ it 'detects the country name' do
+ @geoip.country_name.must_equal 'United States'
+ end
+ end
+end

0 comments on commit 614bc80

Please sign in to comment.