vigilion-rails 1.0.0

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.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +33 -0
  4. data/app/controllers/vigilion/vigilion_controller.rb +21 -0
  5. data/config/routes.rb +3 -0
  6. data/lib/generators/vigilion/install_generator.rb +22 -0
  7. data/lib/generators/vigilion/scan/USAGE +7 -0
  8. data/lib/generators/vigilion/scan/scan_generator.rb +20 -0
  9. data/lib/generators/vigilion/scan/templates/add_scan_results_columns.rb.erb +5 -0
  10. data/lib/tasks/vigilion.rake +12 -0
  11. data/lib/vigilion-rails.rb +55 -0
  12. data/lib/vigilion-rails/configuration.rb +13 -0
  13. data/lib/vigilion-rails/engine.rb +10 -0
  14. data/lib/vigilion-rails/integrations/local_integration.rb +7 -0
  15. data/lib/vigilion-rails/integrations/url_integration.rb +7 -0
  16. data/lib/vigilion-rails/version.rb +3 -0
  17. data/spec/dummy/README.rdoc +28 -0
  18. data/spec/dummy/Rakefile +6 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  20. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  22. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  23. data/spec/dummy/app/models/agnostic_document.rb +7 -0
  24. data/spec/dummy/app/models/carrierwave_document.rb +4 -0
  25. data/spec/dummy/app/models/dragonfly_document.rb +4 -0
  26. data/spec/dummy/app/models/paperclip_document.rb +4 -0
  27. data/spec/dummy/app/uploaders/attachment_uploader.rb +50 -0
  28. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/spec/dummy/bin/bundle +3 -0
  30. data/spec/dummy/bin/rails +4 -0
  31. data/spec/dummy/bin/rake +4 -0
  32. data/spec/dummy/bin/setup +29 -0
  33. data/spec/dummy/config.ru +4 -0
  34. data/spec/dummy/config/application.rb +32 -0
  35. data/spec/dummy/config/boot.rb +5 -0
  36. data/spec/dummy/config/database.yml +25 -0
  37. data/spec/dummy/config/environment.rb +5 -0
  38. data/spec/dummy/config/environments/development.rb +41 -0
  39. data/spec/dummy/config/environments/production.rb +79 -0
  40. data/spec/dummy/config/environments/test.rb +42 -0
  41. data/spec/dummy/config/initializers/assets.rb +11 -0
  42. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  44. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/spec/dummy/config/initializers/inflections.rb +16 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  47. data/spec/dummy/config/initializers/session_store.rb +3 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +23 -0
  50. data/spec/dummy/config/routes.rb +3 -0
  51. data/spec/dummy/config/secrets.yml +22 -0
  52. data/spec/dummy/db/migrate/20150616144220_create_carrierwave_documents.rb +8 -0
  53. data/spec/dummy/db/migrate/20150616144227_create_paperclip_documents.rb +8 -0
  54. data/spec/dummy/db/migrate/20150616144235_create_dragonfly_documents.rb +8 -0
  55. data/spec/dummy/db/migrate/20150616144544_create_agnostic_documents.rb +8 -0
  56. data/spec/dummy/db/schema.rb +61 -0
  57. data/spec/dummy/public/404.html +67 -0
  58. data/spec/dummy/public/422.html +67 -0
  59. data/spec/dummy/public/500.html +66 -0
  60. data/spec/dummy/public/favicon.ico +0 -0
  61. data/spec/helper_methods.rb +11 -0
  62. data/spec/lib/integrations/local_integration_spec.rb +13 -0
  63. data/spec/lib/vigilion_rails_spec.rb +122 -0
  64. data/spec/routing/vigilion_routing_spec.rb +9 -0
  65. data/spec/spec_helper.rb +87 -0
  66. data/spec/vigilion_rails_helper.rb +34 -0
  67. metadata +229 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01e901bc9f20315c84b9cbbb0b6c71ed82fc4ab8
4
+ data.tar.gz: b5c38585a91c7da6c76de2456c4654f3023b900c
5
+ SHA512:
6
+ metadata.gz: 388400a8bc15441dfc68415f24dafb069c9e736514f5b5e4e9ba4d1a21abece700e66aeaf527185fa3cfff98de7e411252550f9f699a3fe11f84a4cf8039c3d4
7
+ data.tar.gz: 310542b30ee4d339a470e0c9fec30dbbc28b793d1bda9ac3d89c30449114d33c10f41c309631fcde9fe48a4895aa824271259e8ff59ef549cf8f9fbf5c19d6a8
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Bit Zesty Ltd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ begin
2
+ require "bundler/setup"
3
+ rescue LoadError
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
+ end
6
+
7
+ require "rdoc/task"
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = "rdoc"
11
+ rdoc.title = "VigilionRails"
12
+ rdoc.options << "--line-numbers"
13
+ rdoc.rdoc_files.include("README.rdoc")
14
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load "rails/tasks/engine.rake"
19
+
20
+ load "rails/tasks/statistics.rake"
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require "rake/testtask"
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << "lib"
28
+ t.libs << "test"
29
+ t.pattern = "test/**/*_test.rb"
30
+ t.verbose = false
31
+ end
32
+
33
+ task default: :test
@@ -0,0 +1,21 @@
1
+ class Vigilion::VigilionController < ActionController::Base
2
+ skip_before_filter :verify_authenticity_token
3
+ before_action :verify_api_auth
4
+
5
+ def callback
6
+ identity = JSON.parse(params[:key])
7
+ model = identity["model"].constantize.find(identity["id"])
8
+ on_scan = "on_scan_#{identity["column"]}"
9
+ if model.present? && model.respond_to?(on_scan)
10
+ model.send(on_scan, params)
11
+ end
12
+ head :ok
13
+ end
14
+
15
+ private
16
+ def verify_api_auth
17
+ unless Vigilion::HTTP.digest(request.raw_post) == request.headers["X-Request-Signature"]
18
+ render json: { message: "unauthorized" }, status: 401
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ post "/vigilion/callback", to: "vigilion/vigilion#callback"
3
+ end
@@ -0,0 +1,22 @@
1
+ class Vigilion::InstallGenerator < Rails::Generators::Base
2
+ def create_initializer_file
3
+ create_file "config/initializers/vigilion.rb", <<INITIALIZER
4
+ Vigilion.configure do |config|
5
+ config.access_key_id = ENV["VIGILION_ACCESS_KEY_ID"]
6
+ config.secret_access_key = ENV["VIGILION_SECRET_ACCESS_KEY"]
7
+
8
+ # Integration strategy (default is :url)
9
+ # config.integration = :local
10
+
11
+ # By default vigilion will be bypassed in development and test environments.
12
+ # Disable vigilion scanning entirely even in production environments:
13
+ # config.loopback = true
14
+ # Enable vigilion scanning even in development and test environments:
15
+ # (Note that the callback URL probably won't be reached)
16
+ # config.loopback = false
17
+ # Specify different loopback_response (default is 'clean')
18
+ # config.loopback_response = 'infected'
19
+ end
20
+ INITIALIZER
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ Description:
2
+ Generates a migration to prepare the database schema to support virus scanning for a particular model.
3
+
4
+ Example:
5
+ rails generate scan Model attachment
6
+
7
+ This will create a migration which adds a `attachment_scan_results' column to `models' table.
@@ -0,0 +1,20 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class Vigilion::ScanGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ argument :model_name, :type => :string
8
+ argument :attribute_name, :type => :string
9
+
10
+ def self.next_migration_number(path)
11
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ def create_model_file
15
+ @table_name = model_name.tableize
16
+ @column_name = "#{attribute_name}_scan_results"
17
+ @migration_name = "vigilion_scan_#{@table_name}_#{attribute_name}"
18
+ migration_template "add_scan_results_columns.rb.erb", "db/migrate/#{@migration_name}.rb"
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ class <%= @migration_name.classify %> < ActiveRecord::Migration
2
+ def change
3
+ add_column :<%= @table_name %>, :<%= @column_name %>, :string
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ namespace :vigilion do
2
+ desc "Ensure vigilion credentials are valid and can connect to the server"
3
+ task test: :environment do
4
+ project = Vigilion::HTTP.new.validate
5
+
6
+ puts "Credentials OK!"
7
+ puts " Project: #{project['name']}"
8
+ puts " Callback: #{project['callback_url']}"
9
+
10
+ end
11
+
12
+ end
@@ -0,0 +1,55 @@
1
+ require "vigilion-rails/engine"
2
+ require "vigilion-rails/integrations/url_integration"
3
+ require "vigilion-rails/integrations/local_integration"
4
+ require "vigilion-rails/configuration"
5
+
6
+ module VigilionRails
7
+
8
+ module ActiveRecord
9
+ def scan_file column, options={}
10
+ default = { scan_column: "#{column}_scan_results", integration: Vigilion::Configuration.integration }
11
+ options = default.merge(options)
12
+ integration_class = "VigilionRails::#{options[:integration].to_s.camelize}Integration"
13
+ class_eval <<-RUBY, __FILE__, __LINE__+1
14
+
15
+ def scan_#{column}!
16
+ key = { model: self.class.name, column: '#{column}', id: id }.to_json
17
+
18
+ if Vigilion::Configuration.loopback
19
+ logger.info 'vigilion scan request was simulated'
20
+
21
+ self.class.find(id).on_scan_#{column} status: Vigilion::Configuration.loopback_response
22
+ else
23
+ #{integration_class}.new.scan key, self, :#{column}
24
+ end
25
+ @#{column}_old_url = #{column}.url
26
+ return true
27
+ end
28
+
29
+ # Vigilion service callback
30
+ def on_scan_#{column} params
31
+ update_attribute('#{options[:scan_column]}', params[:status])
32
+ end
33
+
34
+ after_initialize :remember_#{column}_url
35
+ after_save :check_scan_#{column}
36
+
37
+ def remember_#{column}_url
38
+ @#{column}_old_url = #{column}.try(:url) unless new_record?
39
+ end
40
+
41
+ def check_scan_#{column}
42
+ if @#{column}_old_url != #{column}.url
43
+ scan_#{column}!
44
+ end
45
+ end
46
+
47
+ def clean?
48
+ #{options[:scan_column]} == "clean"
49
+ end
50
+ RUBY
51
+ end
52
+ end
53
+ end
54
+
55
+ ActiveRecord::Base.extend VigilionRails::ActiveRecord
@@ -0,0 +1,13 @@
1
+ module VigilionRails
2
+ class Configuration < Rails::Railtie
3
+ initializer "configuration.configure_default_values" do
4
+ # Sets default configuration.
5
+ # Please see https://github.com/vigilion/vigilion-ruby/blob/master/lib/vigilion/configuration.rb
6
+ Vigilion.configure do |config|
7
+ config.integration = :url
8
+ config.loopback = Rails.env.development? || Rails.env.test?
9
+ config.loopback_response = 'clean'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module VigilionRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace VigilionRails
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module VigilionRails
2
+ class LocalIntegration
3
+ def scan(key, model, column)
4
+ Vigilion.scan_path(key, model.send(column).path)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module VigilionRails
2
+ class UrlIntegration
3
+ def scan(key, model, column)
4
+ Vigilion.scan_url(key, model.send(column).url)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module VigilionRails
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,7 @@
1
+ class AgnosticDocument < ActiveRecord::Base
2
+ scan_file :attachment
3
+
4
+ def attachment
5
+ OpenStruct.new(url: attachment_url)
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ class CarrierwaveDocument < ActiveRecord::Base
2
+ mount_uploader :attachment, AttachmentUploader
3
+ scan_file :attachment, integration: :local
4
+ end
@@ -0,0 +1,4 @@
1
+ class DragonflyDocument < ActiveRecord::Base
2
+ dragonfly_accessor :attachment
3
+ scan_file :attachment
4
+ end
@@ -0,0 +1,4 @@
1
+ class PaperclipDocument < ActiveRecord::Base
2
+ has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
3
+ scan_file :attachment
4
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ class AttachmentUploader < CarrierWave::Uploader::Base
4
+
5
+ # Include RMagick or MiniMagick support:
6
+ # include CarrierWave::RMagick
7
+ # include CarrierWave::MiniMagick
8
+
9
+ # Choose what kind of storage to use for this uploader:
10
+ # storage :fog
11
+
12
+ # Override the directory where uploaded files will be stored.
13
+ # This is a sensible default for uploaders that are meant to be mounted:
14
+ def store_dir
15
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
16
+ end
17
+
18
+ # Provide a default URL as a default if there hasn't been a file uploaded:
19
+ # def default_url
20
+ # # For Rails 3.1+ asset pipeline compatibility:
21
+ # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
22
+ #
23
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
24
+ # end
25
+
26
+ # Process files as they are uploaded:
27
+ # process :scale => [200, 300]
28
+ #
29
+ # def scale(width, height)
30
+ # # do something
31
+ # end
32
+
33
+ # Create different versions of your uploaded files:
34
+ # version :thumb do
35
+ # process :resize_to_fit => [50, 50]
36
+ # end
37
+
38
+ # Add a white list of extensions which are allowed to be uploaded.
39
+ # For images you might use something like this:
40
+ # def extension_white_list
41
+ # %w(jpg jpeg gif png)
42
+ # end
43
+
44
+ # Override the filename of the uploaded files:
45
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
46
+ # def filename
47
+ # "something.jpg" if original_filename
48
+ # end
49
+
50
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>