security_guard 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode
7
+ - jruby-19mode
8
+ - ruby-head
9
+ - jruby-head
10
+ - ree
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # SecurityGuard
1
+ # SecurityGuard [![Build Status](https://secure.travis-ci.org/fredwu/security_guard.png?branch=master)](http://travis-ci.org/fredwu/security_guard) [![Dependency Status](https://gemnasium.com/fredwu/security_guard.png)](https://gemnasium.com/fredwu/security_guard)
2
2
 
3
3
  This gem is a collection of useful tools for auditing data and performing security checks.
4
4
 
@@ -48,11 +48,10 @@ country_ips = SecurityGuard::CountryIps.new(
48
48
  Returns a list of the IPs from given country and IP dictionaries. Useful for auditing IPs from higher risk nations.
49
49
 
50
50
  ```ruby
51
- country_ips = SecurityGuard::CountryIps.new(
51
+ SecurityGuard::CountryIps.new(
52
52
  :countries => ['Australia'],
53
53
  :ips => ['4.4.4.4', '8.8.8.8', '203.206.0.1']
54
- )
55
- country_ips.result # => ['203.206.0.1']
54
+ ).result # => ['203.206.0.1']
56
55
  ```
57
56
 
58
57
  You may also pass country and IP data as a line-delimited file by appending `_from_file` at the end of the attributes:
@@ -73,6 +72,17 @@ SecurityGuard::Deduplication.new(
73
72
  ).process
74
73
  ```
75
74
 
75
+ ### Sequences
76
+
77
+ Prepends line-delimited content with a comma-delimited sequence column (1, 2, 3...) for counting.
78
+
79
+ ```ruby
80
+ SecurityGuard::Sequences.new(
81
+ :input_folder => '/path/to/the/input/folder',
82
+ :output_folder => '/path/to/the/output/folder'
83
+ ).process
84
+ ```
85
+
76
86
  ## Changelog
77
87
 
78
88
  ### v0.0.6 [2012-03-07]
data/bin/sguard CHANGED
@@ -39,6 +39,20 @@ module SecurityGuard
39
39
  end
40
40
  end
41
41
 
42
+ class SequencesCommand < Clamp::Command
43
+ parameter 'input_folder', 'folder containing the input files'
44
+ parameter 'output_folder', 'folder for outputing deduped files'
45
+
46
+ def execute
47
+ SecurityGuard::Sequences.new(
48
+ :input_folder => input_folder,
49
+ :output_folder => output_folder
50
+ ).process
51
+
52
+ ap "Sequences results published to '#{output_folder}'."
53
+ end
54
+ end
55
+
42
56
  class MainCommand < Clamp::Command
43
57
  def self.output_result(result, output_path)
44
58
  `echo #{result} > #{output_path}`
@@ -52,6 +66,10 @@ module SecurityGuard
52
66
  subcommand 'deduplication',
53
67
  'Deduplicates content contained within a list of files.',
54
68
  SecurityGuard::DeduplicationCommand
69
+
70
+ subcommand 'sequences',
71
+ 'Prepends line-delimited content with a sequence column (1, 2, 3...) for counting.',
72
+ SecurityGuard::SequencesCommand
55
73
  end
56
74
  end
57
75
 
@@ -3,5 +3,7 @@ require 'security_guard/utils/files'
3
3
  require 'security_guard/utils/geo_ips'
4
4
  require 'security_guard/concerns/accepts_from_file'
5
5
  require 'security_guard/concerns/initializable'
6
+ require 'security_guard/concerns/input_to_output'
6
7
  require 'security_guard/country_ips'
7
8
  require 'security_guard/deduplication'
9
+ require 'security_guard/sequences'
@@ -0,0 +1,41 @@
1
+ module SecurityGuard
2
+ module Concerns
3
+ module InputToOutput
4
+ def input_to_output(args)
5
+ input_data, filenames = read_data_from(args[:input])
6
+
7
+ output_data = send(args[:process], input_data)
8
+
9
+ write_data_to args[:output],
10
+ :output_data => output_data,
11
+ :filenames => filenames
12
+ end
13
+
14
+ private
15
+
16
+ def read_data_from(folder)
17
+ files = Dir["#{folder}/*"].sort
18
+
19
+ raise Exception.new('Input folder is invalid or is empty.') if files.empty?
20
+
21
+ input_data = []
22
+ filenames = []
23
+
24
+ files.each do |file|
25
+ input_data << File.readlines(file).map{ |line| line.downcase.strip }
26
+ filenames << File.basename(file)
27
+ end
28
+
29
+ [input_data, filenames]
30
+ end
31
+
32
+ def write_data_to(folder, opts)
33
+ opts[:output_data].each_with_index do |array, index|
34
+ File.open("#{folder}/#{opts[:filenames][index]}", 'w') do |f|
35
+ f.puts array
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,45 +1,21 @@
1
1
  module SecurityGuard
2
2
  class Deduplication
3
3
  include Concerns::Initializable
4
+ include Concerns::InputToOutput
4
5
 
5
6
  initializable :input_folder, :output_folder
6
- attr_accessor :source_data, :deduped_data, :filenames
7
-
8
- def initialize(args = nil)
9
- @source_data ||= []
10
- @deduped_data ||= []
11
- @filenames ||= []
12
- initializable_attrs args
13
- end
14
7
 
15
8
  def process
16
- read_data_from input_folder
17
- dedupe source_data
18
- write_data_to output_folder
9
+ input_to_output :input => input_folder,
10
+ :output => output_folder,
11
+ :process => :dedupe
19
12
  end
20
13
 
21
14
  private
22
15
 
23
- def read_data_from(folder)
24
- files = Dir["#{folder}/*"].sort
25
-
26
- raise Exception.new('Input folder is invalid or is empty.') if files.empty?
27
-
28
- files.each do |file|
29
- filenames << File.basename(file)
30
- source_data << File.readlines(file).map{ |line| line.downcase.strip }
31
- end
32
- end
33
-
34
- def write_data_to(folder)
35
- deduped_data.each_with_index do |array, index|
36
- File.open("#{folder}/#{filenames[index]}", 'w') do |f|
37
- f.puts array
38
- end
39
- end
40
- end
41
-
42
16
  def dedupe(data)
17
+ deduped_data = []
18
+
43
19
  # start from the lowest array (in terms of dedupe priority)
44
20
  data.reverse!
45
21
  data_original = data.clone
@@ -47,9 +23,10 @@ module SecurityGuard
47
23
  data.shift
48
24
  deduped_data << _deduped_multi(array, data)
49
25
  end
26
+
50
27
  # the top array doesn't need to be compared, just needs to be unique
51
28
  deduped_data.last.uniq!
52
- deduped_data.reverse!
29
+ deduped_data.reverse
53
30
  end
54
31
 
55
32
  def _deduped_multi(target, others)
@@ -0,0 +1,27 @@
1
+ module SecurityGuard
2
+ class Sequences
3
+ include Concerns::Initializable
4
+ include Concerns::InputToOutput
5
+
6
+ initializable :input_folder, :output_folder
7
+
8
+ def process
9
+ input_to_output :input => input_folder,
10
+ :output => output_folder,
11
+ :process => :prepend_sequencial_numbers
12
+ end
13
+
14
+ private
15
+
16
+ def prepend_sequencial_numbers(data)
17
+ data_with_sequences = []
18
+ data.each_with_index do |array, index|
19
+ data_with_sequences[index] = []
20
+ array.each_with_index do |entry, i|
21
+ data_with_sequences[index] << "#{i}, #{entry}"
22
+ end
23
+ end
24
+ data_with_sequences
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module SecurityGuard
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -3,10 +3,10 @@ require File.expand_path('../lib/security_guard/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ['Fred Wu']
6
- gem.email = ['fred.wu@sitepoint.com']
6
+ gem.email = ['ifredwu@gmail.com']
7
7
  gem.summary = %q{A collection of useful tools for auditing data and performing security checks.}
8
8
  gem.description = gem.summary
9
- gem.homepage = 'https://github.com/sitepoint/security_guard'
9
+ gem.homepage = 'https://github.com/fredwu/security_guard'
10
10
 
11
11
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
12
  gem.files = `git ls-files`.split("\n")
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ class TestInputToOutput
4
+ include SecurityGuard::Concerns::Initializable
5
+ include SecurityGuard::Concerns::InputToOutput
6
+
7
+ initializable :input_folder, :output_folder
8
+
9
+ def process
10
+ input_to_output :input => input_folder,
11
+ :output => output_folder,
12
+ :process => :reverse
13
+ end
14
+
15
+ def reverse(data)
16
+ data.each do |lines|
17
+ lines.reverse!
18
+ end
19
+ data
20
+ end
21
+ end
22
+
23
+ describe SecurityGuard::Concerns::InputToOutput do
24
+ let :test_io do
25
+ TestInputToOutput.new(
26
+ :input_folder => fixture_file('dedupe_lists/'),
27
+ :output_folder => fixture_file('../tmp/')
28
+ )
29
+ end
30
+
31
+ let :input_data do
32
+ [
33
+ [1, 2, 3],
34
+ [4, 5],
35
+ [6, 7],
36
+ ]
37
+ end
38
+
39
+ before do
40
+ `rm -rf #{fixture_file('../tmp/*')}`
41
+ end
42
+
43
+ it 'reads data from the input folder' do
44
+ input_data, filenames = test_io.send :read_data_from, fixture_file('dedupe_lists/')
45
+ input_data.must_equal input_data
46
+ end
47
+
48
+ it 'records filenames' do
49
+ input_data, filenames = test_io.send :read_data_from, fixture_file('dedupe_lists/')
50
+ filenames.must_equal ['a.txt', 'b.txt', 'c.txt']
51
+ end
52
+
53
+ it 'writes data to the output folder' do
54
+ test_io.send(:write_data_to,
55
+ fixture_file('../tmp/'),
56
+ :filenames => ['a.txt', 'b.txt'],
57
+ :output_data => [
58
+ [1, 2, 3],
59
+ [4, 5, 6],
60
+ ]
61
+ )
62
+
63
+ File.read(fixture_file('../tmp/a.txt')).must_equal "1\n2\n3\n"
64
+ File.read(fixture_file('../tmp/b.txt')).must_equal "4\n5\n6\n"
65
+ end
66
+
67
+ it 'reads input and writes output' do
68
+ test_io.process
69
+
70
+ File.read(fixture_file('../tmp/a.txt')).must_equal "c@example.com\nc@example.com\nb@example.com\na@example.com\n"
71
+ File.read(fixture_file('../tmp/b.txt')).must_equal "d@example.com\nb@example.com\n"
72
+ File.read(fixture_file('../tmp/c.txt')).must_equal "e@example.com\nd@example.com\nc@example.com\n"
73
+ end
74
+ end
@@ -20,28 +20,6 @@ describe SecurityGuard::Deduplication do
20
20
  `rm -rf #{fixture_file('../tmp/*')}`
21
21
  end
22
22
 
23
- it 'reads data from the input folder' do
24
- dedupe.send :read_data_from, fixture_file('dedupe_lists/')
25
- dedupe.source_data.must_equal source_data
26
- end
27
-
28
- it 'records filenames' do
29
- dedupe.send :read_data_from, fixture_file('dedupe_lists/')
30
- dedupe.send(:filenames).must_equal ['a.txt', 'b.txt', 'c.txt']
31
- end
32
-
33
- it 'writes data to the output folder' do
34
- dedupe.send :filenames=, ['a.txt', 'b.txt']
35
- dedupe.deduped_data = [
36
- [1, 2, 3],
37
- [4, 5, 6],
38
- ]
39
- dedupe.send :write_data_to, fixture_file('../tmp/')
40
-
41
- File.read(fixture_file('../tmp/a.txt')).must_equal "1\n2\n3\n"
42
- File.read(fixture_file('../tmp/b.txt')).must_equal "4\n5\n6\n"
43
- end
44
-
45
23
  it 'reads input and writes output' do
46
24
  dedupe.process
47
25
 
@@ -53,7 +31,7 @@ describe SecurityGuard::Deduplication do
53
31
  it 'dedupes an array with another array' do
54
32
  dedupe.send(:_deduped,
55
33
  ['a@example.com', 'b@example.com', 'c@example.com', 'c@example.com'],
56
- ['b@example.com', 'd@example.com'],
34
+ ['b@example.com', 'd@example.com']
57
35
  ).must_equal ['a@example.com', 'c@example.com']
58
36
  end
59
37
 
@@ -68,8 +46,7 @@ describe SecurityGuard::Deduplication do
68
46
  end
69
47
 
70
48
  it 'dedupes the source data' do
71
- dedupe.send :dedupe, source_data
72
- dedupe.deduped_data.must_equal [
49
+ dedupe.send(:dedupe, source_data).must_equal [
73
50
  ['a@example.com', 'b@example.com', 'c@example.com'],
74
51
  ['d@example.com'],
75
52
  ['e@example.com'],
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SecurityGuard::Sequences do
4
+ let :sequences do
5
+ SecurityGuard::Sequences.new(
6
+ :input_folder => fixture_file('dedupe_lists/'),
7
+ :output_folder => fixture_file('../tmp/')
8
+ )
9
+ end
10
+
11
+ let :source_data do
12
+ [
13
+ ['a@example.com', 'b@example.com', 'c@example.com', 'c@example.com'],
14
+ ['b@example.com', 'd@example.com'],
15
+ ['c@example.com', 'd@example.com', 'e@example.com'],
16
+ ]
17
+ end
18
+
19
+ before do
20
+ `rm -rf #{fixture_file('../tmp/*')}`
21
+ end
22
+
23
+ it 'reads input and writes output' do
24
+ sequences.process
25
+
26
+ File.read(fixture_file('../tmp/a.txt')).must_equal "0, a@example.com\n1, b@example.com\n2, c@example.com\n3, c@example.com\n"
27
+ File.read(fixture_file('../tmp/b.txt')).must_equal "0, b@example.com\n1, d@example.com\n"
28
+ File.read(fixture_file('../tmp/c.txt')).must_equal "0, c@example.com\n1, d@example.com\n2, e@example.com\n"
29
+ end
30
+
31
+ it 'prepends sequencial numbers to each item' do
32
+ sequences.send(:prepend_sequencial_numbers, source_data).must_equal [
33
+ ['0, a@example.com', '1, b@example.com', '2, c@example.com', '3, c@example.com'],
34
+ ['0, b@example.com', '1, d@example.com'],
35
+ ['0, c@example.com', '1, d@example.com', '2, e@example.com'],
36
+ ]
37
+ end
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: security_guard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-07 00:00:00.000000000 Z
12
+ date: 2012-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: clamp
16
- requirement: &70344773855680 !ruby/object:Gem::Requirement
16
+ requirement: &70245692555440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70344773855680
24
+ version_requirements: *70245692555440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: awesome_print
27
- requirement: &70344773855260 !ruby/object:Gem::Requirement
27
+ requirement: &70245692554980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70344773855260
35
+ version_requirements: *70245692554980
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: geoip
38
- requirement: &70344773854840 !ruby/object:Gem::Requirement
38
+ requirement: &70245692554480 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70344773854840
46
+ version_requirements: *70245692554480
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70344773854420 !ruby/object:Gem::Requirement
49
+ requirement: &70245692553840 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70344773854420
57
+ version_requirements: *70245692553840
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &70344773854000 !ruby/object:Gem::Requirement
60
+ requirement: &70245692553160 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70344773854000
68
+ version_requirements: *70245692553160
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: minitest-colorize
71
- requirement: &70344773853580 !ruby/object:Gem::Requirement
71
+ requirement: &70245692551760 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,17 +76,18 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70344773853580
79
+ version_requirements: *70245692551760
80
80
  description: A collection of useful tools for auditing data and performing security
81
81
  checks.
82
82
  email:
83
- - fred.wu@sitepoint.com
83
+ - ifredwu@gmail.com
84
84
  executables:
85
85
  - sguard
86
86
  extensions: []
87
87
  extra_rdoc_files: []
88
88
  files:
89
89
  - .gitignore
90
+ - .travis.yml
90
91
  - Gemfile
91
92
  - LICENSE
92
93
  - README.md
@@ -96,14 +97,17 @@ files:
96
97
  - lib/security_guard.rb
97
98
  - lib/security_guard/concerns/accepts_from_file.rb
98
99
  - lib/security_guard/concerns/initializable.rb
100
+ - lib/security_guard/concerns/input_to_output.rb
99
101
  - lib/security_guard/country_ips.rb
100
102
  - lib/security_guard/deduplication.rb
103
+ - lib/security_guard/sequences.rb
101
104
  - lib/security_guard/utils/files.rb
102
105
  - lib/security_guard/utils/geo_ips.rb
103
106
  - lib/security_guard/version.rb
104
107
  - security_guard.gemspec
105
108
  - specs/concerns/accepts_from_file_spec.rb
106
109
  - specs/concerns/initializable_spec.rb
110
+ - specs/concerns/input_to_output_spec.rb
107
111
  - specs/country_ips_spec.rb
108
112
  - specs/deduplication_spec.rb
109
113
  - specs/fixtures/countries.txt
@@ -111,11 +115,12 @@ files:
111
115
  - specs/fixtures/dedupe_lists/b.txt
112
116
  - specs/fixtures/dedupe_lists/c.txt
113
117
  - specs/fixtures/ip_addresses.txt
118
+ - specs/sequences_spec.rb
114
119
  - specs/spec_helper.rb
115
120
  - specs/tmp/.gitkeep
116
121
  - specs/utils/files_spec.rb
117
122
  - specs/utils/geo_ips_spec.rb
118
- homepage: https://github.com/sitepoint/security_guard
123
+ homepage: https://github.com/fredwu/security_guard
119
124
  licenses: []
120
125
  post_install_message:
121
126
  rdoc_options: []
@@ -129,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
134
  version: '0'
130
135
  segments:
131
136
  - 0
132
- hash: 2907249137378969452
137
+ hash: -3598287692275573134
133
138
  required_rubygems_version: !ruby/object:Gem::Requirement
134
139
  none: false
135
140
  requirements:
@@ -138,10 +143,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
143
  version: '0'
139
144
  segments:
140
145
  - 0
141
- hash: 2907249137378969452
146
+ hash: -3598287692275573134
142
147
  requirements: []
143
148
  rubyforge_project:
144
- rubygems_version: 1.8.16
149
+ rubygems_version: 1.8.17
145
150
  signing_key:
146
151
  specification_version: 3
147
152
  summary: A collection of useful tools for auditing data and performing security checks.