Skip to content

Commit

Permalink
Rubocop and cleanup ENV configs
Browse files Browse the repository at this point in the history
  • Loading branch information
sammo1235 committed Oct 4, 2024
1 parent f19c96f commit 741223e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ SENTRY_DSN=SENTRY_DSN
AUTHY_API_KEY=AUTHY_API_KEY
AUTHY_API_URL=https://api.authy.com
DISABLE_VIRUS_SCANNER=false
ENABLE_VIRUS_SCANNER_BUCKETS=false
VIRUS_SCANNER_BUCKETS_ENDPOINT=http://s3service:9000
VIRUS_SCANNER_URL=http://localhost:80
VIRUS_SCANNER_USERNAME=app1
VIRUS_SCANNER_PASSWORD=letmein
Expand Down
12 changes: 6 additions & 6 deletions app/jobs/file_scan_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def perform(key, class_name, record_id, attribute_name)
begin
file_to_scan = get_file_to_scan(file)
scan_result = VirusScanner.scan_file(file_to_scan)
status = scan_result[:malware] ? 'infected' : 'clean'
status = scan_result[:malware] ? "infected" : "clean"
record.send(:"on_scan_#{attribute_name}", status: status)
rescue VirusScanner::AuthenticationError => e
handle_authentication_error(record, attribute_name, e)
Expand All @@ -27,23 +27,23 @@ def perform(key, class_name, record_id, attribute_name)

def get_file_to_scan(file)
if file.is_a?(String)
File.open(file, 'rb')
File.open(file, "rb")
elsif file.respond_to?(:read)
file
elsif file.is_a?(CarrierWave::SanitizedFile)
File.open(file.file, 'rb')
File.open(file.file, "rb")
elsif file.respond_to?(:file)
if file.file.is_a?(CarrierWave::SanitizedFile)
File.open(file.file.file, 'rb')
File.open(file.file.file, "rb")
elsif file.file.respond_to?(:path)
File.open(file.file.path, 'rb')
File.open(file.file.path, "rb")
elsif file.file.respond_to?(:read)
file.file
else
raise ArgumentError, "Don't know how to handle #{file.file.class}"
end
elsif file.respond_to?(:path)
File.open(file.path, 'rb')
File.open(file.path, "rb")
else
raise ArgumentError, "Don't know how to handle #{file.class}"
end
Expand Down
33 changes: 19 additions & 14 deletions app/models/concerns/scan_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def perform_virus_scan
def move_to_clean_bucket(attr_name)
file = send(attr_name)
if file.present?
if Rails.env.production? || ENV['ENABLE_VIRUS_SCANNER_BUCKETS']
if Rails.env.production? || ENV["ENABLE_VIRUS_SCANNER_BUCKETS"]
move_to_permanent_s3_bucket(file)
else
move_to_permanent_local_folder(attr_name)
Expand All @@ -31,30 +31,35 @@ def move_to_clean_bucket(attr_name)
end

def move_to_permanent_s3_bucket(file)
s3_client = Aws::S3::Client.new(
region: ENV['AWS_REGION'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
endpoint: 'http://s3service:9000',
force_path_style: true
)
options = {
region: ENV["AWS_REGION"],
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
}

if ENV["ENABLE_VIRUS_SCANNER_BUCKETS"]
options[:endpoint] = ENV["VIRUS_SCANNER_BUCKETS_ENDPOINT"]
options[:force_path_style] = true
end

s3_client = Aws::S3::Client.new(options)

s3_client.copy_object(
bucket: ENV['AWS_S3_PERMANENT_BUCKET'],
copy_source: "#{ENV['AWS_S3_TMP_BUCKET']}/#{file.path}",
key: file.permanent_path
bucket: ENV["AWS_S3_PERMANENT_BUCKET"],
copy_source: "#{ENV["AWS_S3_TMP_BUCKET"]}/#{file.path}",
key: file.permanent_path,
)

s3_client.delete_object(
bucket: ENV['AWS_S3_TMP_BUCKET'],
key: file.path
bucket: ENV["AWS_S3_TMP_BUCKET"],
key: file.path,
)
end

def move_to_permanent_local_folder(attribute_name)
file = send(attribute_name)
if file.respond_to?(:path)
new_path = file.path.sub('/tmp/', '/permanent/')
new_path = file.path.sub("/tmp/", "/permanent/")
Rails.logger.debug "Moving file from #{file.path} to #{new_path}"
FileUtils.mkdir_p(File.dirname(new_path))
FileUtils.mv(file.path, new_path) unless File.exist?(new_path)
Expand Down
11 changes: 6 additions & 5 deletions app/uploaders/file_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class FileUploader < CarrierWave::Uploader::Base
storage :custom

def store_dir
base_dir = model.respond_to?(:clean?) && model.clean? ? "permanent" : "tmp"
base_dir = (model.respond_to?(:clean?) && model.clean?) ? "permanent" : "tmp"
"uploads/#{base_dir}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

Expand All @@ -27,19 +27,20 @@ def filename

def fog_directory
if model.respond_to?(:clean?) && model.clean?
ENV['AWS_S3_PERMANENT_BUCKET']
ENV["AWS_S3_PERMANENT_BUCKET"]
else
ENV['AWS_S3_TMP_BUCKET']
ENV["AWS_S3_TMP_BUCKET"]
end
end

def permanent_path
path.sub('tmp', 'permanent')
path.sub("tmp", "permanent")
end

private

def read_from_permanent_storage
if Rails.env.production? || ENV['ENABLE_VIRUS_SCANNER_BUCKETS']
if Rails.env.production? || ENV["ENABLE_VIRUS_SCANNER_BUCKETS"]
permanent_file = CarrierWave::Storage::Fog::File.new(self, permanent_storage, store_path)
permanent_file.read
else
Expand Down
20 changes: 10 additions & 10 deletions config/initializers/carrierwave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class CustomStorage
def self.new(uploader)
if Rails.env.production? || ENV['ENABLE_VIRUS_SCANNER_BUCKETS']
if Rails.env.production? || ENV["ENABLE_VIRUS_SCANNER_BUCKETS"]
CustomFogStorage.new(uploader)
else
CustomFileStorage.new(uploader)
Expand All @@ -19,7 +19,7 @@ class CustomFileStorage < CarrierWave::Storage::File
def retrieve!(identifier)
file = super
if file.respond_to?(:uploader) && file.uploader.model.respond_to?(:clean?) && file.uploader.model.clean?
new_path = file.path.sub('/tmp/', '/permanent/')
new_path = file.path.sub("/tmp/", "/permanent/")
FileUtils.mkdir_p(File.dirname(new_path))
FileUtils.mv(file.path, new_path) unless File.exist?(new_path)
file.instance_variable_set(:@path, new_path)
Expand All @@ -29,19 +29,19 @@ def retrieve!(identifier)
end

CarrierWave.configure do |config|
if Rails.env.production? || ENV['ENABLE_VIRUS_SCANNER_BUCKETS']
if Rails.env.production? || ENV["ENABLE_VIRUS_SCANNER_BUCKETS"]
base_credentials = {
provider: "AWS",
aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
region: ENV["AWS_REGION"],
}
base_credentials.merge!({
endpoint: 'http://s3service:9000',
path_style: true
}) if ENV['ENABLE_VIRUS_SCANNER_BUCKETS']
config.fog_credentials = base_credentials
if ENV["ENABLE_VIRUS_SCANNER_BUCKETS"]
base_credentials[:endpoint] = ENV["VIRUS_SCANNER_BUCKETS_ENDPOINT"]
base_credentials[:path_style] = true
end
config.fog_credentials = base_credentials

config.fog_directory = ENV["AWS_S3_TMP_BUCKET"]
config.storage = :fog
config.fog_public = false
Expand All @@ -50,7 +50,7 @@ def retrieve!(identifier)
else
config.storage = :file
config.enable_processing = false if Rails.env.test?
config.root = Rails.root.join('public')
config.root = Rails.root.join("public")
config.cache_dir = "uploads/tmp"
config.cache_storage = :file
end
Expand Down
6 changes: 3 additions & 3 deletions lib/virus_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ def scan_file(file)
private

def download_to_tempfile(file)
temp_file = Tempfile.new("virus_scan", encoding: 'UTF-8')
temp_file = Tempfile.new("virus_scan", encoding: "UTF-8")
if file.is_a?(String)
File.open(file, "rb") do |f|
IO.copy_stream(f, temp_file)
end
elsif file.class.to_s.include?('Uploader')
elsif file.class.to_s.include?("Uploader")
IO.copy_stream(StringIO.new(file.read), temp_file)
elsif file.respond_to?(:read)
IO.copy_stream(file, temp_file)
elsif file.respond_to?(:file)
if file.file.respond_to?(:path)
File.open(file.file.path, 'rb') do |f|
File.open(file.file.path, "rb") do |f|
IO.copy_stream(f, temp_file)
end
elsif file.file.respond_to?(:read)
Expand Down

0 comments on commit 741223e

Please sign in to comment.