Skip to content

Commit

Permalink
Allow executing commands as a different user
Browse files Browse the repository at this point in the history
  • Loading branch information
ianballou committed Jul 23, 2024
1 parent 1d0a3f5 commit 5814c0e
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 18 deletions.
9 changes: 3 additions & 6 deletions definitions/features/container_gateway_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ def load_configuration
connection_string = config[:db_connection_string]
if connection_string
uri = URI.parse(connection_string)
@configuration['adapter'] = uri.scheme
@configuration['host'] = uri.host
@configuration['port'] = uri.port
@configuration['database'] = uri.path[1..-1]
@configuration['username'] = uri.user
@configuration['password'] = uri.password
@configuration['connection_string'] = connection_string
@configuration['user'] = 'foreman-proxy'
@configuration['database'] = uri.path[1..]
end
@configuration
end
Expand Down
8 changes: 8 additions & 0 deletions definitions/procedures/restore/drop_databases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def run
drop_foreman(backup, spinner)
drop_candlepin(backup, spinner)
drop_pulpcore(backup, spinner)
drop_container_gateway(backup, spinner)
end
end

Expand All @@ -43,5 +44,12 @@ def drop_pulpcore(backup, spinner)
feature(:pulpcore_database).dropdb
end
end

def drop_container_gateway(backup, spinner)
if backup.file_map[:container_gateway_dump][:present]
spinner.update('Dropping container gateway database')
feature(:container_gateway_database).dropdb
end
end
end
end
4 changes: 4 additions & 0 deletions definitions/scenarios/restore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def restore_sql_dumps(backup)
if backup.file_map[:pulpcore_dump][:present]
add_steps_with_context(Procedures::Restore::PulpcoreDump)
end
if backup.file_map[:container_gateway_dump][:present]
add_steps_with_context(Procedures::Restore::ContainerGatewayDump)
end
if feature(:instance).postgresql_local?
add_step(Procedures::Service::Stop.new(:only => ['postgresql']))
end
Expand All @@ -82,6 +85,7 @@ def set_context_mapping
Procedures::Restore::CandlepinDump => :backup_dir,
Procedures::Restore::ForemanDump => :backup_dir,
Procedures::Restore::PulpcoreDump => :backup_dir,
Procedures::Restore::ContainerGatewayDump => :backup_dir,
Procedures::Restore::ExtractFiles => :backup_dir)
end
end
Expand Down
36 changes: 26 additions & 10 deletions lib/foreman_maintain/concerns/base_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def configuration
end

def local?(config = configuration)
['localhost', '127.0.0.1', `hostname`.strip].include?(config['host'])
['localhost', '127.0.0.1', `hostname`.strip].include?(config['host']) ||
config['host'].nil?
end

def query(sql, config = configuration)
Expand All @@ -57,7 +58,8 @@ def psql(query, config = configuration)
if ping(config)
execute(psql_command(config),
:stdin => query,
:hidden_patterns => [config['password']])
:hidden_patterns => [config['password']],
:config => user)
else
raise_service_error
end
Expand All @@ -66,11 +68,12 @@ def psql(query, config = configuration)
def ping(config = configuration)
execute?(psql_command(config),
:stdin => 'SELECT 1 as ping',
:hidden_patterns => [config['password']])
:hidden_patterns => [config['password']],
:user => config['user'])
end

def dump_db(file, config = configuration)
execute!(dump_command(config) + " > #{file}", :hidden_patterns => [config['password']])
execute!(dump_command(config) + " > #{file}", :hidden_patterns => [config['password']], :user => config['user'])

Check failure on line 76 in lib/foreman_maintain/concerns/base_database.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Layout/LineLength: Line is too long. [120/100]
end

def restore_dump(file, localdb, config = configuration)
Expand All @@ -80,11 +83,16 @@ def restore_dump(file, localdb, config = configuration)
else
# TODO: figure out how to completely ignore errors. Currently this
# sometimes exits with 1 even though errors are ignored by pg_restore
dump_cmd = base_command(config, 'pg_restore') +
' --no-privileges --clean --disable-triggers -n public ' \
"-d #{config['database']} #{file}"
dump_cmd = ''
if config['connection_string']
dump_cmd = "pg_restore --no-privileges --clean --disable-triggers -n public -d #{config['database']} #{file}"

Check failure on line 88 in lib/foreman_maintain/concerns/base_database.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Layout/LineLength: Line is too long. [121/100]
else
dump_cmd = base_command(config, 'pg_restore') +
' --no-privileges --clean --disable-triggers -n public ' \

Check failure on line 91 in lib/foreman_maintain/concerns/base_database.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Layout/MultilineOperationIndentation: Align the operands of an expression in an assignment spanning multiple lines.
"-d #{config['database']} #{file}"
end
execute!(dump_cmd, :hidden_patterns => [config['password']],
:valid_exit_statuses => [0, 1])
:valid_exit_statuses => [0, 1], :user => config['user'])
end
end

Expand Down Expand Up @@ -152,11 +160,19 @@ def base_command(config, command = 'psql')
end

def psql_command(config)
base_command(config, 'psql') + " -d #{config['database']}"
if config['connection_string']
"psql #{config['connection_string']}"
else
base_command(config, 'psql') + " -d #{config['database']}"
end
end

def dump_command(config)
base_command(config, 'pg_dump') + " -Fc #{config['database']}"
if config['connection_string']
"pg_dump -Fc #{config['connection_string']}"
else
base_command(config, 'pg_dump') + " -Fc #{config['database']}"
end
end

def raise_service_error
Expand Down
9 changes: 7 additions & 2 deletions lib/foreman_maintain/utils/command_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ class CommandRunner
attr_reader :logger, :command

def initialize(logger, command, options)
options.validate_options!(:stdin, :hidden_patterns, :interactive, :valid_exit_statuses)
options.validate_options!(:stdin, :hidden_patterns, :interactive, :valid_exit_statuses, :user)

Check failure on line 11 in lib/foreman_maintain/utils/command_runner.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Layout/LineLength: Line is too long. [102/100]
options[:valid_exit_statuses] ||= [0]
@logger = logger
@command = command
@user = options[:user]
if @user && !@user.empty?

Check failure on line 15 in lib/foreman_maintain/utils/command_runner.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.
@command = "sudo -u #{@user} -- " + command
else
@command = command
end
@stdin = options[:stdin]
@hidden_patterns = Array(options[:hidden_patterns]).compact
@interactive = options[:interactive]
Expand Down
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions test/lib/utils/backup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def feature_with_local_method(label, return_value)

it 'Validates fpc standard backup' do
assume_feature_present(:pulpcore_database)
assume_feature_present(:container_gateway_database)
fpc_standard_backup = subject.new(fpc_standard)
refute fpc_standard_backup.katello_online_backup?
refute fpc_standard_backup.katello_logical_backup?
Expand All @@ -181,6 +182,7 @@ def feature_with_local_method(label, return_value)

it 'Validates fpc online backup' do
assume_feature_present(:pulpcore_database)
assume_feature_present(:container_gateway_database)
fpc_online_backup = subject.new(fpc_online)
refute fpc_online_backup.katello_standard_backup?
refute fpc_online_backup.katello_online_backup?
Expand All @@ -195,6 +197,7 @@ def feature_with_local_method(label, return_value)

it 'Validates fpc logical backup' do
assume_feature_present(:pulpcore_database)
assume_feature_present(:container_gateway_database)
fpc_logical_backup = subject.new(fpc_logical)
refute fpc_logical_backup.katello_standard_backup?
refute fpc_logical_backup.katello_online_backup?
Expand All @@ -209,6 +212,7 @@ def feature_with_local_method(label, return_value)

it 'Validates fpc hybrid db backup' do
feature_with_local_method(:pulpcore_database, true)
feature_with_local_method(:container_gateway_database, true)
fpc_hybrid_db_backup = subject.new(fpc_hybrid)
assert fpc_hybrid_db_backup.katello_standard_backup?
refute fpc_hybrid_db_backup.katello_online_backup?
Expand Down

0 comments on commit 5814c0e

Please sign in to comment.