Skip to content

Commit

Permalink
Update rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
adamruzicka authored and iNecas committed Jan 7, 2019
1 parent 0d0ee90 commit 51bfa80
Show file tree
Hide file tree
Showing 27 changed files with 187 additions and 146 deletions.
32 changes: 29 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
inherit_from: .rubocop_todo.yml

Rails:
Enabled: false

Expand All @@ -12,17 +14,32 @@ Style/HashSyntax:
- ruby19
- hash_rockets

Bundler/DuplicatedGem:
Enabled: false

Lint/ShadowingOuterLocalVariable:
Enabled: false

Lint/RescueException:
Enabled: false

Metrics/ClassLength:
Exclude:
- 'test/**/*'

Style/TrailingCommaInLiteral:
Naming/AccessorMethodName:
Enabled: false

Style/TrailingCommaInArguments:
Naming/FileName:
Enabled: false

Style/AccessorMethodName:
Naming/HeredocDelimiterNaming:
Enabled: false

Style/TrailingCommaInLiteral:
Enabled: false

Style/TrailingCommaInArguments:
Enabled: false

Metrics/LineLength:
Expand Down Expand Up @@ -72,3 +89,12 @@ Metrics/CyclomaticComplexity:

Metrics/ClassLength:
Enabled: false

Style/RescueStandardError:
Enabled: false

Style/RescueModifier:
Enabled: false

Style/Alias:
Enabled: false
7 changes: 7 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-11-07 15:47:10 +0100 using RuboCop version 0.52.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.
10 changes: 5 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ group :development do
end

group :test do
gem 'smart_proxy_dynflow', :path => '.'
gem 'smart_proxy', :git => "https://github.com/theforeman/smart-proxy", :branch => "develop"
gem 'smart_proxy_dynflow', :path => '.'

if RUBY_VERSION < '2.1'
gem 'public_suffix', '< 3'
gem 'rubocop', '< 0.51.0'
gem 'rainbow', '< 3'
gem 'rubocop', '< 0.51.0'
else
gem 'rubocop', '~> 0.52.1'
gem 'public_suffix'
gem 'rubocop', '~> 0.52.1'
end

if RUBY_VERSION < '2.2'
Expand All @@ -27,11 +27,11 @@ group :test do
end

if RUBY_VERSION < '2.2'
gem 'sinatra', '< 2'
gem 'rack', '>= 1.1', '< 2.0.0'
gem 'sinatra', '< 2'
else
gem 'sinatra'
gem 'rack', '>= 1.1'
gem 'sinatra'
end

# load bundler.d
Expand Down
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ desc 'Default: run unit tests.'
task :default => :test

namespace :test do

desc 'Test Dynflow core plugin'
Rake::TestTask.new(:core) do |t|
ENV['DYNFLOW_DB_CONN_STRING'] = 'sqlite:/'
Expand Down
8 changes: 4 additions & 4 deletions lib/smart_proxy_dynflow/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ class Api < ::Sinatra::Base
end
end


# TODO: move this to foreman-proxy to reduce code duplicities
def do_authorize_with_trusted_hosts
# When :trusted_hosts is given, we check the client against the list
# HTTPS: test the certificate CN
# HTTP: test the reverse DNS entry of the remote IP
trusted_hosts = Proxy::SETTINGS.trusted_hosts
if trusted_hosts
if [ 'yes', 'on', 1 ].include? request.env['HTTPS'].to_s
if ['yes', 'on', 1].include? request.env['HTTPS'].to_s
fqdn = https_cert_cn
source = 'SSL_CLIENT_CERT'
else
Expand All @@ -39,13 +38,14 @@ def do_authorize_with_trusted_hosts
logger.debug "verifying remote client #{fqdn} (based on #{source}) against trusted_hosts #{trusted_hosts}"

unless Proxy::SETTINGS.trusted_hosts.include?(fqdn)
log_halt 403, "Untrusted client #{fqdn} attempted to access #{request.path_info}. Check :trusted_hosts: in settings.yml"
log_halt 403, "Untrusted client #{fqdn} attempted " \
"to access #{request.path_info}. Check :trusted_hosts: in settings.yml"
end
end
end

def do_authorize_with_ssl_client
if ['yes', 'on', '1'].include? request.env['HTTPS'].to_s
if %w[yes on 1].include? request.env['HTTPS'].to_s
if request.env['SSL_CLIENT_CERT'].to_s.empty?
log_halt 403, "No client SSL certificate supplied"
end
Expand Down
11 changes: 6 additions & 5 deletions lib/smart_proxy_dynflow/callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ def uri

def relay(request, from, to)
path = request.path.gsub(from, to)
Proxy::LogBuffer::Decorator.instance.debug "Proxy request from #{request.host_with_port}#{request.path} to #{uri.to_s}#{path}"
message = "Proxy request from #{request.host_with_port}#{request.path} to #{uri}#{path}"
Proxy::LogBuffer::Decorator.instance.debug message
req = case request.env['REQUEST_METHOD']
when 'GET'
request_factory.create_get path, request.env['rack.request.query_hash']
when 'POST'
request_factory.create_post path, request.body.read
when 'GET'
request_factory.create_get path, request.env['rack.request.query_hash']
when 'POST'
request_factory.create_post path, request.body.read
end
req['X-Forwarded-For'] = request.env['HTTP_HOST']
req['AUTHORIZATION'] = request.env['HTTP_AUTHORIZATION']
Expand Down
2 changes: 1 addition & 1 deletion lib/smart_proxy_dynflow/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Proxy
class Dynflow
module Helpers
def relay_request(from = /^\/dynflow/, to = '')
def relay_request(from = %r{^/dynflow}, to = '')
response = Proxy::Dynflow::Callback::Core.relay(request, from, to)
content_type response.content_type
status response.code
Expand Down
2 changes: 1 addition & 1 deletion lib/smart_proxy_dynflow/http_config.ru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'smart_proxy_dynflow/api'

map "/dynflow" do
map '/'do
map '/' do
run Proxy::Dynflow::Api
end
end
4 changes: 3 additions & 1 deletion lib/smart_proxy_dynflow/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class Plugin < Proxy::Plugin
default_settings :core_url => 'http://localhost:8008'
plugin :dynflow, Proxy::Dynflow::VERSION

# rubocop:disable Lint/HandleExceptions
after_activation do
begin
require 'smart_proxy_dynflow_core'
rescue LoadError => e
rescue LoadError
# Dynflow core is not available in the proxy, will be handled
# by standalone Dynflow core
end
end
# rubocop:enable Lint/HandleExceptions
end
end
2 changes: 1 addition & 1 deletion lib/smart_proxy_dynflow/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Proxy
class Dynflow
VERSION = '0.2.1'
VERSION = '0.2.1'.freeze
end
end
4 changes: 2 additions & 2 deletions lib/smart_proxy_dynflow_core/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Api < ::Sinatra::Base
helpers Helpers

before do
logger = Log.instance
authorize_with_token || authorize_with_ssl_client
content_type :json
end
Expand Down Expand Up @@ -58,7 +57,8 @@ class Api < ::Sinatra::Base
private

def callback_host(params, request)
params.fetch('action_input', {})['proxy_url'] || request.env.values_at('HTTP_X_FORWARDED_FOR', 'HTTP_HOST').compact.first
params.fetch('action_input', {})['proxy_url'] ||
request.env.values_at('HTTP_X_FORWARDED_FOR', 'HTTP_HOST').compact.first
end

def launcher_class(params)
Expand Down
2 changes: 2 additions & 0 deletions lib/smart_proxy_dynflow_core/bundler_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module SmartProxyDynflowCore
class BundlerHelper
# rubocop:disable Metrics/PerceivedComplexity
def self.require_groups(*groups)
if File.exist?(File.expand_path('../../../Gemfile.in', __FILE__))
# If there is a Gemfile.in file, we will not use Bundler but BundlerExt
Expand All @@ -25,5 +26,6 @@ def self.require_groups(*groups)
Bundler.require(*groups)
end
end
# rubocop:enable Metrics/PerceivedComplexity
end
end
76 changes: 37 additions & 39 deletions lib/smart_proxy_dynflow_core/callback.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
require 'rest-client'

# rubocop:disable Lint/HandleExceptions
begin
require 'smart_proxy_dynflow/callback'
rescue LoadError
end
# rubocop:enable Lint/HandleExceptions

module SmartProxyDynflowCore
module Callback
class Action < Dynflow::Action
def plan(callback, data)
plan_self(:callback => callback, :data => data)
end
class Request
class << self
def send_to_foreman_tasks(callback_info, data)
self.new.callback(prepare_payload(callback_info, data))
end

def run
Request.send_to_foreman_tasks(input[:callback], input[:data])
# rubocop:disable Metrics/PerceivedComplexity
def ssl_options
return @ssl_options if defined? @ssl_options
@ssl_options = {}
settings = Settings.instance
return @ssl_options unless URI.parse(settings.foreman_url).scheme == 'https'

@ssl_options[:verify_ssl] = OpenSSL::SSL::VERIFY_PEER

private_key_file = settings.foreman_ssl_key || settings.ssl_private_key
if private_key_file
private_key = File.read(private_key_file)
@ssl_options[:ssl_client_key] = OpenSSL::PKey::RSA.new(private_key)
end
certificate_file = settings.foreman_ssl_cert || settings.ssl_certificate
if certificate_file
certificate = File.read(certificate_file)
@ssl_options[:ssl_client_cert] = OpenSSL::X509::Certificate.new(certificate)
end
ca_file = settings.foreman_ssl_ca || settings.ssl_ca_file
@ssl_options[:ssl_ca_file] = ca_file if ca_file
@ssl_options
end
# rubocop:enable Metrics/PerceivedComplexity

private

def prepare_payload(callback, data)
{ :callback => callback, :data => data }.to_json
end
end
end

class Request
def callback(payload)
response = callback_resource.post(payload, :content_type => :json)
if response.code.to_s != "200"
Expand All @@ -26,43 +55,12 @@ def callback(payload)
response
end

def self.send_to_foreman_tasks(callback_info, data)
self.new.callback(self.prepare_payload(callback_info, data))
end

private

def self.prepare_payload(callback, data)
{ :callback => callback, :data => data }.to_json
end

def callback_resource
@resource ||= RestClient::Resource.new(Settings.instance.foreman_url + '/foreman_tasks/api/tasks/callback',
self.class.ssl_options)
end

def self.ssl_options
return @ssl_options if defined? @ssl_options
@ssl_options = {}
settings = Settings.instance
return @ssl_options unless URI.parse(settings.foreman_url).scheme == 'https'

@ssl_options[:verify_ssl] = OpenSSL::SSL::VERIFY_PEER

private_key_file = settings.foreman_ssl_key || settings.ssl_private_key
if private_key_file
private_key = File.read(private_key_file)
@ssl_options[:ssl_client_key] = OpenSSL::PKey::RSA.new(private_key)
end
certificate_file = settings.foreman_ssl_cert || settings.ssl_certificate
if certificate_file
certificate = File.read(certificate_file)
@ssl_options[:ssl_client_cert] = OpenSSL::X509::Certificate.new(certificate)
end
ca_file = settings.foreman_ssl_ca || settings.ssl_ca_file
@ssl_options[:ssl_ca_file] = ca_file if ca_file
@ssl_options
end
end

class Action < ::Dynflow::Action
Expand Down
6 changes: 3 additions & 3 deletions lib/smart_proxy_dynflow_core/core.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module SmartProxyDynflowCore
class Core

attr_accessor :world, :accepted_cert_serial

def initialize
Expand All @@ -25,7 +24,8 @@ def persistence_conn_string

db_file = Settings.instance.database
if db_file.nil? || db_file.empty?
Log.instance.warn "Could not open DB for dynflow at '#{db_file}', will keep data in memory. Restart will drop all dynflow data."
Log.instance.warn "Could not open DB for dynflow at '#{db_file}', " \
"will keep data in memory. Restart will drop all dynflow data."
else
db_conn_string += "/#{db_file}"
end
Expand All @@ -44,7 +44,7 @@ def default_world_config
config.persistence_adapter = persistence_adapter
config.execution_plan_cleaner = execution_plan_cleaner
# TODO: There has to be a better way
matchers = config.silent_dead_letter_matchers.call().concat(self.class.silencer_matchers)
matchers = config.silent_dead_letter_matchers.call.concat(self.class.silencer_matchers)
config.silent_dead_letter_matchers = matchers
yield config if block_given?
end
Expand Down
4 changes: 2 additions & 2 deletions lib/smart_proxy_dynflow_core/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def authorize_with_token
auth = request.env['HTTP_AUTHORIZATION']
basic_prefix = /\ABasic /
if !auth.to_s.empty? && auth =~ basic_prefix &&
ForemanTasksCore::OtpManager.authenticate(auth.gsub(basic_prefix, ''))
ForemanTasksCore::OtpManager.authenticate(auth.gsub(basic_prefix, ''))
Log.instance.debug('authorized with token')
return true
end
Expand All @@ -21,7 +21,7 @@ def authorize_with_token
end

def authorize_with_ssl_client
if %w(yes on 1).include? request.env['HTTPS'].to_s
if %w[yes on 1].include? request.env['HTTPS'].to_s
if request.env['SSL_CLIENT_CERT'].to_s.empty?
Log.instance.error "No client SSL certificate supplied"
halt 403, MultiJson.dump(:error => "No client SSL certificate supplied")
Expand Down
Loading

0 comments on commit 51bfa80

Please sign in to comment.