Skip to content

Rubocop Process Complete #953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ appveyor.yml:
delete: true
spec/spec_helper.rb:
allow_deprecations: true
.travis.yml:
extras:
- rvm: 2.1.9
script: bundle exec rake rubocop
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ matrix:
- rvm: 2.1.9
bundler_args: --without system_tests
env: PUPPET_GEM_VERSION="~> 4.0"
- rvm: 2.1.9
script: bundle exec rake rubocop
notifications:
email: false
49 changes: 24 additions & 25 deletions lib/puppet/parser/functions/postgresql_acls_to_resources_hash.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# postgresql_acls_to_resources_hash.rb
module Puppet::Parser::Functions
newfunction(:postgresql_acls_to_resources_hash, :type => :rvalue, :doc => <<-EOS
newfunction(:postgresql_acls_to_resources_hash, type: :rvalue, doc: <<-EOS
This internal function translates the ipv(4|6)acls format into a resource
suitable for create_resources. It is not intended to be used outside of the
postgresql internal classes/defined resources.
Expand All @@ -14,11 +15,13 @@ module Puppet::Parser::Functions
The third parameter is an order offset, so you can start the order at an
arbitrary starting point.
EOS
) do |args|
func_name = "postgresql_acls_to_resources_hash()"
) do |args|
func_name = 'postgresql_acls_to_resources_hash()'

raise(Puppet::ParseError, "#{func_name}: Wrong number of arguments " +
"given (#{args.size} for 3)") if args.size != 3
if args.size != 3
raise(Puppet::ParseError, "#{func_name}: Wrong number of arguments " \
"given (#{args.size} for 3)")
end

acls = args[0]
raise(Puppet::ParseError, "#{func_name}: first argument must be an array") \
Expand All @@ -38,36 +41,32 @@ module Puppet::Parser::Functions

parts = acl.split

raise(Puppet::ParseError, "#{func_name}: acl line #{index} does not " +
"have enough parts") unless parts.length >= 4
unless parts.length >= 4
raise(Puppet::ParseError, "#{func_name}: acl line #{index} does not " \
'have enough parts')
end

resource = {
'type' => parts[0],
'database' => parts[1],
'user' => parts[2],
'order' => format('%03d', offset + index),
'order' => format('%03d', offset + index), # rubocop:disable Style/FormatString
}
if parts[0] == 'local' then
if parts[0] == 'local'
resource['auth_method'] = parts[3]
if parts.length > 4 then
resource['auth_option'] = parts.last(parts.length - 4).join(" ")
if parts.length > 4
resource['auth_option'] = parts.last(parts.length - 4).join(' ')
end
else
if parts[4] =~ /^\d/
resource['address'] = parts[3] + ' ' + parts[4]
resource['auth_method'] = parts[5]
elsif parts[4] =~ %r{^\d}
resource['address'] = parts[3] + ' ' + parts[4]
resource['auth_method'] = parts[5]

if parts.length > 6 then
resource['auth_option'] = parts.last(parts.length - 6).join(" ")
end
else
resource['address'] = parts[3]
resource['auth_method'] = parts[4]
resource['auth_option'] = parts.last(parts.length - 6).join(' ') if parts.length > 6
else
resource['address'] = parts[3]
resource['auth_method'] = parts[4]

if parts.length > 5 then
resource['auth_option'] = parts.last(parts.length - 5).join(" ")
end
end
resource['auth_option'] = parts.last(parts.length - 5).join(' ') if parts.length > 5
end
resources["postgresql class generated rule #{id} #{index}"] = resource
end
Expand Down
19 changes: 11 additions & 8 deletions lib/puppet/parser/functions/postgresql_escape.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
require 'digest/md5'

# postgresql_escape.rb
module Puppet::Parser::Functions
newfunction(:postgresql_escape, :type => :rvalue, :doc => <<-EOS
newfunction(:postgresql_escape, type: :rvalue, doc: <<-EOS
Safely escapes a string using $$ using a random tag which should be consistent
EOS
) do |args|
) do |args|

raise(Puppet::ParseError, "postgresql_escape(): Wrong number of arguments " +
"given (#{args.size} for 1)") if args.size != 1
if args.size != 1
raise(Puppet::ParseError, 'postgresql_escape(): Wrong number of arguments ' \
"given (#{args.size} for 1)")
end

password = args[0]

if password !~ /\$\$/ and password[-1] != '$'
if password !~ %r{\$\$} && password[-1] != '$'
retval = "$$#{password}$$"
else
escape = Digest::MD5.hexdigest(password)[0..5].gsub(/\d/,'')
until password !~ /#{escape}/
escape = Digest::MD5.hexdigest(escape)[0..5].gsub(/\d/,'')
escape = Digest::MD5.hexdigest(password)[0..5].gsub(%r{\d}, '')
until password !~ %r{#{escape}}
escape = Digest::MD5.hexdigest(escape)[0..5].gsub(%r{\d}, '')
end
retval = "$#{escape}$#{password}$#{escape}$"
end
Expand Down
11 changes: 7 additions & 4 deletions lib/puppet/parser/functions/postgresql_password.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# hash a string as mysql's "PASSWORD()" function would do it
require 'digest/md5'

# postgresql_password.rb
module Puppet::Parser::Functions
newfunction(:postgresql_password, :type => :rvalue, :doc => <<-EOS
newfunction(:postgresql_password, type: :rvalue, doc: <<-EOS
Returns the postgresql password hash from the clear text username / password.
EOS
) do |args|
) do |args|

raise(Puppet::ParseError, "postgresql_password(): Wrong number of arguments " +
"given (#{args.size} for 2)") if args.size != 2
if args.size != 2
raise(Puppet::ParseError, 'postgresql_password(): Wrong number of arguments ' \
"given (#{args.size} for 2)")
end

username = args[0]
password = args[1]
Expand Down
62 changes: 30 additions & 32 deletions lib/puppet/provider/postgresql_conf/parsed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,39 @@

Puppet::Type.type(:postgresql_conf).provide(
:parsed,
:parent => Puppet::Provider::ParsedFile,
:default_target => '/etc/postgresql.conf',
:filetype => :flat
parent: Puppet::Provider::ParsedFile,
default_target: '/etc/postgresql.conf',
filetype: :flat,
) do
desc "Set key/values in postgresql.conf."
desc 'Set key/values in postgresql.conf.'

text_line :comment, :match => /^\s*#/
text_line :blank, :match => /^\s*$/
text_line :comment, match: %r{^\s*#}
text_line :blank, match: %r{^\s*$}

record_line :parsed,
:fields => %w{name value comment},
:optional => %w{comment},
:match => /^\s*([\w\.]+)\s*=?\s*(.*?)(?:\s*#\s*(.*))?\s*$/,
:to_line => proc { |h|

# simple string and numeric values don't need to be enclosed in quotes
if h[:value].is_a?(Numeric)
val = h[:value].to_s
else
val = h[:value]
end
dontneedquote = val.match(/^(\d+.?\d+|\w+)$/)
dontneedequal = h[:name].match(/^(include|include_if_exists)$/i)

str = h[:name].downcase # normalize case
str += dontneedequal ? ' ' : ' = '
str += "'" unless dontneedquote && !dontneedequal
str += val
str += "'" unless dontneedquote && !dontneedequal
str += " # #{h[:comment]}" unless (h[:comment].nil? or h[:comment] == :absent)
str
},
:post_parse => proc { |h|
h[:name].downcase! # normalize case
h[:value].gsub!(/(^'|'$)/, '') # strip out quotes
}
fields: %w[name value comment],
optional: %w[comment],
match: %r{^\s*([\w\.]+)\s*=?\s*(.*?)(?:\s*#\s*(.*))?\s*$},
to_line: proc { |h|
# simple string and numeric values don't need to be enclosed in quotes
val = if h[:value].is_a?(Numeric)
h[:value].to_s
else
h[:value]
end
dontneedquote = val.match(%r{^(\d+.?\d+|\w+)$})
dontneedequal = h[:name].match(%r{^(include|include_if_exists)$}i)

str = h[:name].downcase # normalize case
str += dontneedequal ? ' ' : ' = '
str += "'" unless dontneedquote && !dontneedequal
str += val
str += "'" unless dontneedquote && !dontneedequal
str += " # #{h[:comment]}" unless h[:comment].nil? || h[:comment] == :absent
str
},
post_parse: proc { |h|
h[:name].downcase! # normalize case
h[:value].gsub!(%r{(^'|'$)}, '') # strip out quotes
}
end
4 changes: 1 addition & 3 deletions lib/puppet/provider/postgresql_conn_validator/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),"..","..",".."))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', '..'))
require 'puppet/util/postgresql_validator'

# This file contains a provider for the resource type `postgresql_conn_validator`,
Expand Down Expand Up @@ -38,6 +38,4 @@ def create
def validator
@validator ||= Puppet::Util::PostgresqlValidator.new(resource)
end

end

59 changes: 28 additions & 31 deletions lib/puppet/provider/postgresql_psql/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
Puppet::Type.type(:postgresql_psql).provide(:ruby) do

def run_unless_sql_command(sql)
# for the 'unless' queries, we wrap the user's query in a 'SELECT COUNT',
# which makes it easier to parse and process the output.
run_sql_command('SELECT COUNT(*) FROM (' << sql << ') count')
run_sql_command('SELECT COUNT(*) FROM (' << sql << ') count')
end

def run_sql_command(sql)
Expand All @@ -12,9 +11,9 @@ def run_sql_command(sql)
end

command = [resource[:psql_path]]
command.push("-d", resource[:db]) if resource[:db]
command.push("-p", resource[:port]) if resource[:port]
command.push("-t", "-c", '"' + sql.gsub('"', '\"') + '"')
command.push('-d', resource[:db]) if resource[:db]
command.push('-p', resource[:port]) if resource[:port]
command.push('-t', '-c', '"' + sql.gsub('"', '\"') + '"')

environment = get_environment

Expand All @@ -29,28 +28,29 @@ def run_sql_command(sql)

private

def get_environment
def get_environment # rubocop:disable Style/AccessorMethodName : Refactor does not work correctly
environment = (resource[:connect_settings] || {}).dup
if envlist = resource[:environment]
envlist = [envlist] unless envlist.is_a? Array
envlist.each do |setting|
if setting =~ /^(\w+)=((.|\n)+)$/
env_name = $1
value = $2
if environment.include?(env_name) || environment.include?(env_name.to_sym)
if env_name == 'NEWPGPASSWD'
warning "Overriding environment setting '#{env_name}' with '****'"
else
warning "Overriding environment setting '#{env_name}' with '#{value}'"
end
envlist = resource[:environment]
return environment unless envlist

envlist = [envlist] unless envlist.is_a? Array
envlist.each do |setting|
if setting =~ %r{^(\w+)=((.|\n)+)$}
env_name = Regexp.last_match(1)
value = Regexp.last_match(2)
if environment.include?(env_name) || environment.include?(env_name.to_sym)
if env_name == 'NEWPGPASSWD'
warning "Overriding environment setting '#{env_name}' with '****'"
else
warning "Overriding environment setting '#{env_name}' with '#{value}'"
end
environment[env_name] = value
else
warning "Cannot understand environment setting #{setting.inspect}"
end
environment[env_name] = value
else
warning "Cannot understand environment setting #{setting.inspect}"
end
end
return environment
environment
end

def run_command(command, user, group, environment)
Expand All @@ -65,16 +65,13 @@ def run_command(command, user, group, environment)
Puppet::Util::SUIDManager.run_and_capture(command, user, group)
end
else
output = Puppet::Util::Execution.execute(command, {
:uid => user,
:gid => group,
:failonfail => false,
:combine => true,
:override_locale => true,
:custom_environment => environment,
})
output = Puppet::Util::Execution.execute(command, uid: user,
gid: group,
failonfail: false,
combine: true,
override_locale: true,
custom_environment: environment)
[output, $CHILD_STATUS.dup]
end
end

end
Loading