Skip to content

Commit 736f8c2

Browse files
authored
Fixes #38286 - Deploy script with tighter permissions (#124)
* Fixes #38286 - Deploy script with tighter permissions * Refs #38286 - Ensure access for effective user The files are deployed by the connection user, but they need to be readable and executable by the by the effective user. Sadly, a non-root user cannot change ownership of a file to another user so we have to rely on ACLs to grant the permissions to the other user. * Refs #38286 - Tighten permissions on the output file
1 parent c0b9d25 commit 736f8c2

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def preflight_checks
168168
error: 'Failed to execute script on remote machine, exit code: %{exit_code}.'
169169
)
170170
unless @user_method.is_a? NoopUserMethod
171+
ensure_effective_user_access(script)
171172
ensure_remote_command("#{@user_method.cli_command_prefix} #{script}",
172173
error: 'Failed to change to effective user, exit code: %{exit_code}',
173174
tty: true,
@@ -206,8 +207,11 @@ def prepare_start
206207
SCRIPT
207208
@remote_script_wrapper = upload_data(
208209
wrapper,
209-
File.join(File.dirname(@remote_script), 'script-wrapper'),
210-
555)
210+
File.join(File.dirname(@remote_script), 'script-wrapper'))
211+
ensure_effective_user_access(@remote_script_wrapper, @remote_script)
212+
upload_data('', @output_path, 600)
213+
ensure_effective_user_access(@output_path, mode: 'rw')
214+
@remote_script_wrapper
211215
end
212216

213217
# the script that initiates the execution
@@ -354,10 +358,10 @@ def ensure_local_directory(path)
354358
def cp_script_to_remote(script = @script, name = 'script')
355359
path = remote_command_file(name)
356360
@logger.debug("copying script to #{path}:\n#{indent_multiline(script)}")
357-
upload_data(sanitize_script(script), path, 555)
361+
upload_data(sanitize_script(script), path)
358362
end
359363

360-
def upload_data(data, path, permissions = 555)
364+
def upload_data(data, path, permissions = 500)
361365
ensure_remote_directory File.dirname(path)
362366
# We use tee here to pipe stdin coming from ssh to a file at $path, while silencing its output
363367
# This is used to write to $path with elevated permissions, solutions using cat and output redirection
@@ -414,5 +418,11 @@ def check_expecting_disconnect
414418
@expecting_disconnect = true
415419
end
416420
end
421+
422+
def ensure_effective_user_access(*paths, mode: 'rx')
423+
unless @user_method.is_a? NoopUserMethod
424+
ensure_remote_command("setfacl -m u:#{@user_method.effective_user}:#{mode} #{paths.join(' ')}")
425+
end
426+
end
417427
end
418428
end

0 commit comments

Comments
 (0)