Skip to content

Commit 5bce4cf

Browse files
committed
fix: gracefully handle missing CAP_CHOWN in rootless containers
Wrap all FileUtils.chown calls in a rescue Errno::EPERM block so that openvoxserver-ca no longer crashes when running inside rootless containers (e.g. podman rootless, OpenShift with arbitrary UIDs) where the process lacks the CAP_CHOWN capability. In these environments file ownership is typically managed through SGID bits and g=u permission patterns instead of explicit chown calls. Signed-off-by: Simon Lauger <simon@lauger.de>
1 parent c53628d commit 5bce4cf

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

lib/puppetserver/ca/utils/file_system.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ def self.forcibly_symlink(source, link_target)
6060
# Symlink permissions are ignored in favor of the source's permissions,
6161
# so we don't have to change those.
6262
source_info = File.stat(source)
63-
FileUtils.chown(source_info.uid, source_info.gid, link_target)
63+
begin
64+
FileUtils.chown(source_info.uid, source_info.gid, link_target)
65+
rescue Errno::EPERM # rubocop:disable Lint/SuppressedException
66+
# In rootless containers the process may lack CAP_CHOWN.
67+
end
6468
end
6569

6670
def initialize
@@ -93,14 +97,22 @@ def write_file(path, one_or_more_objects, mode)
9397
f.puts object.to_s
9498
end
9599
end
96-
FileUtils.chown(@user, @group, path)
100+
begin
101+
FileUtils.chown(@user, @group, path)
102+
rescue Errno::EPERM # rubocop:disable Lint/SuppressedException
103+
# In rootless containers the process may lack CAP_CHOWN.
104+
end
97105
end
98106

99107
# Warning: directory mode should be specified in DIR_MODES above
100108
def ensure_dir(directory)
101109
if !File.exist?(directory)
102110
FileUtils.mkdir_p(directory, mode: DIR_MODES[directory])
103-
FileUtils.chown(@user, @group, directory)
111+
begin
112+
FileUtils.chown(@user, @group, directory)
113+
rescue Errno::EPERM # rubocop:disable Lint/SuppressedException
114+
# In rootless containers the process may lack CAP_CHOWN.
115+
end
104116
end
105117
end
106118
end

0 commit comments

Comments
 (0)