Module: Puppet::Etc Private

Defined in:
lib/puppet/etc.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

On Ruby 2.1.0 and later, Etc returns strings in variable encoding depending on environment. The string returned will be labeled with the environment's encoding (Encoding.default_external), with one exception: If the environment encoding is 7-bit ASCII, and any individual character bit representation is equal to or greater than 128 - x80 - 0b10000000 - signifying the smallest 8-bit big-endian value, the returned string will be in BINARY encoding instead of environment encoding.

Barring that exception, the returned string will be labeled as encoding Encoding.default_external, regardless of validity or byte-width. For example, ruby will label a string containing a four-byte characters such as “u2070E” as EUC_KR even though EUC_KR is a two-byte width encoding.

On Ruby 2.0.x and earlier, Etc will always return string values in BINARY, ignoring encoding altogether.

For Puppet we specifically want UTF-8 as our input from the Etc module - which is our input for many resource instance 'is' values. The associated 'should' value will basically always be coming from Puppet in UTF-8 - and written to disk as UTF-8. Etc is defined for Windows but the majority calls to it return nil and Puppet does not use it.

That being said, we have cause to retain the original, pre-override string values. `puppet resource user` (Puppet::Resource::User.indirection.search('User', {})) uses self.instances to query for user(s) and then iterates over the results of that query again to obtain state for each user. If we've overridden the original user name and not retained the original, we've lost the ability to query the system for it later. Hence the Puppet::Etc::Passwd and Puppet::Etc::Group structs.

We only use Etc for retrieving existing property values from the system. For setting property values, providers leverage system tools (i.e., `useradd`)

Class Method Summary collapse

Class Method Details

.endgrentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

closes handle to /etc/group file


51
52
53
# File 'lib/puppet/etc.rb', line 51

def endgrent
  ::Etc.endgrent
end

.endpwentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

closes handle to /etc/passwd file


68
69
70
# File 'lib/puppet/etc.rb', line 68

def endpwent
  ::Etc.endpwent
end

.getgrentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Etc::getgrent returns an Etc::Group struct object On first call opens /etc/group and returns parse of first entry. Each subsquent call returns new struct the next entry or nil if EOF. Call ::endgrent to close file.


46
47
48
# File 'lib/puppet/etc.rb', line 46

def getgrent
  override_field_values_to_utf8(::Etc.getgrent)
end

.getgrgid(id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Etc::getgrid searches /etc/group file for an entry corresponding to id. returns an Etc::Group struct corresponding to the entry or raises ArgumentError if none


95
96
97
# File 'lib/puppet/etc.rb', line 95

def getgrgid(id)
  override_field_values_to_utf8(::Etc.getgrgid(id))
end

.getgrnam(groupname) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Etc::getgrnam searches /etc/group file for an entry corresponding to groupname. returns an Etc::Group struct corresponding to the entry or raises ArgumentError if none


88
89
90
# File 'lib/puppet/etc.rb', line 88

def getgrnam(groupname)
  override_field_values_to_utf8(::Etc.getgrnam(groupname))
end

.getpwentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Etc::getpwent returns an Etc::Passwd struct object On first call opens /etc/passwd and returns parse of first entry. Each subsquent call returns new struct for the next entry or nil if EOF. Call ::endgrent to close file.


63
64
65
# File 'lib/puppet/etc.rb', line 63

def getpwent
  override_field_values_to_utf8(::Etc.getpwent)
end

.getpwnam(username) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Etc::getpwnam searches /etc/passwd file for an entry corresponding to username. returns an Etc::Passwd struct corresponding to the entry or raises ArgumentError if none


81
82
83
# File 'lib/puppet/etc.rb', line 81

def getpwnam(username)
  override_field_values_to_utf8(::Etc.getpwnam(username))
end

.getpwuid(id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Etc::getpwuid searches /etc/passwd file for an entry corresponding to id. returns an Etc::Passwd struct corresponding to the entry or raises ArgumentError if none


102
103
104
# File 'lib/puppet/etc.rb', line 102

def getpwuid(id)
  override_field_values_to_utf8(::Etc.getpwuid(id))
end

.groupObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Etc::group returns a Ruby iterator that executes a block for each entry in the /etc/group file. The code-block is passed a Group struct. See getgrent above for more details.


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/puppet/etc.rb', line 109

def group
  # The implementation here duplicates the logic in https://github.com/ruby/etc/blob/master/ext/etc/etc.c#L523-L537
  # Note that we do not call ::Etc.group directly, because we
  # want to use our wrappers for methods like getgrent, setgrent,
  # endgrent, etc.
  return getgrent unless block_given?

  setgrent
  begin
    while cur_group = getgrent #rubocop:disable Lint/AssignmentInCondition
      yield cur_group
    end
  ensure
    endgrent
  end
end

.setgrentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

effectively equivalent to IO#rewind of /etc/group


56
57
58
# File 'lib/puppet/etc.rb', line 56

def setgrent
  ::Etc.setgrent
end

.setpwentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

effectively equivalent to IO#rewind of /etc/passwd


73
74
75
# File 'lib/puppet/etc.rb', line 73

def setpwent
  ::Etc.setpwent
end