clam_chowder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7c1bbdb6150fe272295b9d2c3bd648a7c93aa20d
4
+ data.tar.gz: fdf79465aead5a27612b15aa223f94d182a096e2
5
+ SHA512:
6
+ metadata.gz: 7d621c58a0e4bd9a41fca632f34f8dd0a976b365826bfa0f5820153b75cb051c7b6317af2756a819a0f9079b2e6bc0c666e227b570efbb5dc7340dd24660aaec
7
+ data.tar.gz: c16eb0847811416a172010df2441d220cc71df5f4c3e4c3d6b13b41f7485973db173fe218a0787ab35c7ff9513322fa09a61ee8c53487db9c1239da012835d32
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ vendor/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p247
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in clam_chowder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 NEXWAY Co.,ltd.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # README
2
+
3
+ ## Name
4
+ clam_chower
5
+
6
+ ## Description
7
+ Nicely application-level wrapper for anti-virus software.
8
+ now supports on clamd only.
9
+
10
+ ## Installing
11
+ Put this line in your Gemfile:
12
+ ```
13
+ gem 'clam_chowder'
14
+ ```
15
+
16
+ then bundle:
17
+ ```
18
+ % bundle
19
+ ```
20
+
21
+ ## Usage
22
+ ```ruby
23
+ response = ClamChowder::Scanner.new.scan_io(file_stream)
24
+ ```
25
+
26
+ ### Infected?
27
+ When the file was infected with a virus, that return true and non-it return false.
28
+
29
+ ```ruby
30
+ response.infected?
31
+ # => true
32
+ ```
33
+
34
+ ### Virus Name
35
+ ```ruby
36
+ response.virus_name
37
+ # => 'Eicar-Test-Signature'
38
+ ```
39
+
40
+ ### Status
41
+ When the file was infected with a virus, that return 'OK' and non-it return 'FOUND'.
42
+
43
+ ```ruby
44
+ response.status
45
+ # => 'OK'
46
+ ```
47
+
48
+ ### Stub Mode
49
+ clam_chowder prepares a stub mode.
50
+ in stub mode consider infected a virus with written as virus in file.
51
+
52
+ #### How to use
53
+ ```ruby
54
+ ClamChowder.default_backend = :stub
55
+ ```
56
+
57
+ if you use stub-mode when with the production environment on rails.
58
+ ```ruby
59
+ # config/initializers/clam_chowder.rb
60
+ ClamChowder.default_backend = Rails.env.production? ? :clamd : :stub
61
+ ```
62
+
63
+ ## Supported versions
64
+ - Ruby 2.0
65
+ - Ruby 2.1
66
+
67
+ ## Dependencies
68
+ - clamd >= 1.0.1
69
+
70
+ ## Tested environments
71
+ - MacOSX Mountain Lion
72
+ - Ubuntu 12.04 LTS
73
+
74
+ ## License
75
+ clam_chowder is available under the [MIT LICENSE](https://github.com/nexway/clam_chowder/blob/develop/LICENSE.txt)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/clam_chowder ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'clam_chowder'
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clam_chowder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "clam_chowder"
8
+ spec.version = ClamChowder::VERSION
9
+ spec.authors = ["NEXWAY", "Iori ONDA"]
10
+ spec.email = ["n2developer@nexway.co.jp", "iori.onda@gmail.com"]
11
+ spec.summary = %q{Nicely application-level wrapper for anti-virus software.}
12
+ spec.description = %q{Nicely application-level wrapper for anti-virus software.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '~> 2.0'
22
+
23
+ spec.add_dependency "clamd"
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,18 @@
1
+ require 'clam_chowder/scanner'
2
+
3
+ module ClamChowder
4
+ class ScanException < StandardError; end
5
+
6
+ class << self
7
+ attr_accessor :default_backend
8
+
9
+ def infected_stream?(io)
10
+ response = Scanner.new(default_backend).scan_io(io)
11
+ response.infected?
12
+ rescue => e
13
+ raise ScanException.new(e)
14
+ end
15
+ end
16
+
17
+ self.default_backend = :clamd
18
+ end
@@ -0,0 +1,21 @@
1
+ require 'clam_chowder/backend/clamd'
2
+ require 'clam_chowder/backend/stub'
3
+
4
+ module ClamChowder
5
+ module Backend
6
+ class << self
7
+ def add(name, klass)
8
+ @backends ||= {}
9
+
10
+ @backends[name] = klass
11
+ end
12
+
13
+ def lookup(name)
14
+ @backends[name]
15
+ end
16
+ end
17
+
18
+ add(:clamd, ::ClamChowder::Backend::Clamd)
19
+ add(:stub, ::ClamChowder::Backend::Stub)
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ require 'clam_chowder/backend'
2
+ require 'clam_chowder/response'
3
+
4
+ require 'clamd'
5
+
6
+ module ClamChowder
7
+ module Backend
8
+ class Clamd
9
+ def initialize
10
+ @clamd = ::Clamd::Client.new
11
+ end
12
+
13
+ def scan_file(path)
14
+ parse_response(@clamd.scan(path))
15
+ end
16
+
17
+ private
18
+
19
+ def parse_response(str)
20
+ /^(?<file_path>.*): (?<virus_name>.*)\s?(?<status>(OK|FOUND))$/ =~ str
21
+ case status
22
+ when 'OK'
23
+ ClamChowder::Response.new(status, file_path)
24
+ when "FOUND"
25
+ ClamChowder::Response.new(status, file_path, virus_name.strip)
26
+ else
27
+ raise ScanException.new(str)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,16 @@
1
+ require 'clam_chowder/backend/clamd'
2
+ require 'clam_chowder/response'
3
+
4
+ module ClamChowder
5
+ module Backend
6
+ class Stub
7
+ def scan_file(path)
8
+ if File.open(path, encoding: 'BINARY') {|f| f.each_line.grep(/virus/).empty? }
9
+ ClamChowder::Response.new('OK', path)
10
+ else
11
+ ClamChowder::Response.new('FOUND', path, 'stub-virus')
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module ClamChowder
2
+ class Response
3
+ attr_reader :virus_name, :file_path
4
+
5
+ def initialize(status, file_path, virus_name = nil)
6
+ @status = status
7
+ @file_path = file_path
8
+ @virus_name = virus_name
9
+ end
10
+
11
+ def infected?
12
+ @status == 'FOUND'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ require 'tempfile'
2
+ require 'clam_chowder'
3
+ require 'clam_chowder/backend'
4
+ require 'clam_chowder/response'
5
+
6
+ module ClamChowder
7
+ class Scanner
8
+ def self.infected_stream?(io)
9
+ ::ClamChowder.infected_stream?(io) # XXX compatibility
10
+ end
11
+
12
+ def initialize(backend = ClamChowder.default_backend)
13
+ @backend ||= ClamChowder::Backend.lookup(backend).new
14
+ end
15
+
16
+ def scan_io(readable)
17
+ begin
18
+ Tempfile.open('clam_chowder_temp', encoding: 'BINARY') do |file|
19
+ while bytes = readable.read(1024 * 32)
20
+ file.write(bytes.strip)
21
+ end
22
+ file.rewind
23
+ file.chmod(0644)
24
+ @backend.scan_file(file.path)
25
+ end
26
+ rescue => e
27
+ raise ScanException.new(e)
28
+ end
29
+ ensure
30
+ readable.rewind if readable.respond_to?(:rewind)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module ClamChowder
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,117 @@
1
+ require './lib/clam_chowder.rb'
2
+ require 'tempfile'
3
+
4
+ def create_virus_file
5
+ Tempfile.open('eicar.com', encoding: 'BINARY').tap do |file|
6
+ # [note] - EICAR Test Virus
7
+ # http://www.eicar.org/86-0-Intended-use.html
8
+ file.write('X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*')
9
+ file.rewind
10
+ end
11
+ end
12
+
13
+ describe 'Scanner' do
14
+ describe '.infected_stream?' do
15
+ subject { ClamChowder::Scanner.infected_stream?(File::open(file_path)) }
16
+
17
+ context 'ウイルスが未検知の場合' do
18
+ let(:file_path) { 'spec/fixtures/innocent.txt' }
19
+
20
+ it { should be_false }
21
+ end
22
+
23
+ context 'ウイルスが検知された場合' do
24
+ let(:file) { create_virus_file }
25
+ let(:file_path) { file.path }
26
+
27
+ after { file.close }
28
+
29
+ it { should be_true }
30
+ end
31
+
32
+ context '検知/未検知以外の場合' do
33
+ let(:file_path) { 'path/to/file' }
34
+
35
+ specify 'ScanException 例外が発生すること' do
36
+ expect { ClamChowder::Scanner.infected_stream?(file_path) }.to raise_error(ClamChowder::ScanException)
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '#scan_io' do
42
+ context 'Backend が clamd の場合' do
43
+ before { ClamChowder.default_backend = :clamd }
44
+
45
+ let(:scanner) { ClamChowder::Scanner.new }
46
+ let(:scan_result) { scanner.scan_io File::open(file_path) }
47
+
48
+ context 'ウィルス未検知の場合' do
49
+ let(:file_path) { 'spec/fixtures/innocent.txt' }
50
+
51
+ specify do
52
+ expect(scan_result).to_not be_infected
53
+ end
54
+ end
55
+
56
+ context 'ウィルス検知の場合' do
57
+ let(:file) { create_virus_file }
58
+ let(:file_path) { file.path }
59
+
60
+ after { file.close }
61
+
62
+ specify do
63
+ expect(scan_result).to be_infected
64
+ expect(scan_result.virus_name).to eq 'Eicar-Test-Signature'
65
+ end
66
+ end
67
+
68
+ context '検知/未検知以外の場合' do
69
+ let(:file_path) { "/path/to/file" }
70
+
71
+ specify "例外が発生すること" do
72
+ expect { ClamChowder::Scanner.new.scan_io(file_path) }.to raise_error(ClamChowder::ScanException)
73
+ end
74
+ end
75
+ end
76
+
77
+ context 'Backend が stub の場合' do
78
+ before { ClamChowder.default_backend = :stub }
79
+
80
+ let(:scanner) { ClamChowder::Scanner.new }
81
+ let(:scan_result) { scanner.scan_io File::open(file_path) }
82
+
83
+ context 'ウィルス未検知の場合' do
84
+ let(:file_path) { 'spec/fixtures/innocent.txt' }
85
+
86
+ specify do
87
+ expect(scan_result).to_not be_infected
88
+ end
89
+ end
90
+
91
+ context 'ウィルス検知の場合' do
92
+ let(:file) do
93
+ Tempfile.open('stub_virus.txt', encoding: 'BINARY').tap do |file|
94
+ file.write('virus')
95
+ file.rewind
96
+ end
97
+ end
98
+ let(:file_path) { file.path }
99
+
100
+ after { file.close }
101
+
102
+ specify do
103
+ expect(scan_result).to be_infected
104
+ expect(scan_result.virus_name).to eq 'stub-virus'
105
+ end
106
+ end
107
+
108
+ context '検知/未検知以外の場合' do
109
+ let(:file_path) { "/path/to/file" }
110
+
111
+ specify "例外が発生すること" do
112
+ expect { ClamChowder::Scanner.new.scan_io(file_path) }.to raise_error(ClamChowder::ScanException)
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
File without changes
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clam_chowder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - NEXWAY
8
+ - Iori ONDA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: clamd
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '1.6'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '1.6'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: Nicely application-level wrapper for anti-virus software.
71
+ email:
72
+ - n2developer@nexway.co.jp
73
+ - iori.onda@gmail.com
74
+ executables:
75
+ - clam_chowder
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - .gitignore
80
+ - .ruby-version
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/clam_chowder
86
+ - clam_chowder.gemspec
87
+ - lib/clam_chowder.rb
88
+ - lib/clam_chowder/backend.rb
89
+ - lib/clam_chowder/backend/clamd.rb
90
+ - lib/clam_chowder/backend/stub.rb
91
+ - lib/clam_chowder/response.rb
92
+ - lib/clam_chowder/scanner.rb
93
+ - lib/clam_chowder/version.rb
94
+ - spec/clam_chowder_spec.rb
95
+ - spec/fixtures/innocent.txt
96
+ homepage: ''
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ~>
107
+ - !ruby/object:Gem::Version
108
+ version: '2.0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.2.2
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Nicely application-level wrapper for anti-virus software.
120
+ test_files:
121
+ - spec/clam_chowder_spec.rb
122
+ - spec/fixtures/innocent.txt