|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'etc' |
| 4 | + |
| 5 | +# class to use in openssl providers to handle file permission (mode, group and owner) |
| 6 | +class Puppet::Provider::Openssl < Puppet::Provider |
| 7 | + include Puppet::Util::POSIX |
| 8 | + |
| 9 | + def owner |
| 10 | + if File.exist?(@resource[:path]) |
| 11 | + Etc.getpwuid(File.stat(@resource[:path]).uid).name |
| 12 | + else |
| 13 | + :absent |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + def owner=(should) |
| 18 | + File.chown(uid(should), nil, resource[:path]) |
| 19 | + rescue StandardError => e |
| 20 | + raise Puppet::Error, _("Failed to set owner to '#{should}': #{e}"), detail.backtrace |
| 21 | + end |
| 22 | + |
| 23 | + def group |
| 24 | + if File.exist?(@resource[:path]) |
| 25 | + Etc.getgrgid(File.stat(@resource[:path]).gid).name |
| 26 | + else |
| 27 | + :absent |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + def group=(should) |
| 32 | + File.chown(nil, gid(should), resource[:path]) |
| 33 | + rescue StandardError => e |
| 34 | + raise Puppet::Error, _("Failed to set group to '#{should}': #{e}"), detail.backtrace |
| 35 | + end |
| 36 | + |
| 37 | + # Return the mode as an octal string, not as an integer. |
| 38 | + def mode |
| 39 | + if File.exist?(@resource[:path]) |
| 40 | + format('0%o', (File.stat(@resource[:path]).mode & 0o07777)) |
| 41 | + else |
| 42 | + :absent |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + # Set the file mode, converting from a string to an integer. |
| 47 | + def mode=(should) |
| 48 | + File.chmod(Integer("0#{should}"), @resource[:path]) |
| 49 | + end |
| 50 | + |
| 51 | + def set_file_perm(filename, owner = nil, group = nil, mode = nil) |
| 52 | + File.chown(uid(owner), nil, resource[:path]) if owner |
| 53 | + File.chown(nil, gid(group), resource[:path]) if group |
| 54 | + File.chmod(Integer("0#{mode}"), filename) if mode |
| 55 | + end |
| 56 | +end |
0 commit comments