Module: Chef::FileAccessControl::Unix

Included in:
Chef::FileAccessControl
Defined in:
lib/chef/file_access_control/unix.rb

Constant Summary collapse

UINT =
(1 << 32)
UID_MAX =
(1 << 32) - 10

Instance Method Summary collapse

Instance Method Details

#current_gidObject



103
104
105
# File 'lib/chef/file_access_control/unix.rb', line 103

def current_gid
  gid_from_resource(current_resource)
end

#current_modeObject



171
172
173
# File 'lib/chef/file_access_control/unix.rb', line 171

def current_mode
  mode_from_resource(current_resource)
end

#current_uidObject



63
64
65
# File 'lib/chef/file_access_control/unix.rb', line 63

def current_uid
  uid_from_resource(current_resource)
end

#define_resource_requirementsObject



46
47
48
49
# File 'lib/chef/file_access_control/unix.rb', line 46

def define_resource_requirements
  uid_from_resource(resource)
  gid_from_resource(resource)
end

#describe_changesObject



51
52
53
54
55
56
57
# File 'lib/chef/file_access_control/unix.rb', line 51

def describe_changes
  changes = []
  changes << "change mode from '#{mode_to_s(current_mode)}' to '#{mode_to_s(target_mode)}'" if should_update_mode?
  changes << "change owner from '#{current_resource.owner}' to '#{resource.owner}'" if should_update_owner?
  changes << "change group from '#{current_resource.group}' to '#{resource.group}'" if should_update_group?
  changes
end

#gid_from_resource(resource) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/chef/file_access_control/unix.rb', line 107

def gid_from_resource(resource)
  return nil if resource == nil or resource.group.nil?
  if resource.group.kind_of?(String)
    diminished_radix_complement( Etc.getgrnam(resource.group).gid )
  elsif resource.group.kind_of?(Integer)
    resource.group
  else
    Chef::Log.error("The `group` parameter of the #@resource resource is set to an invalid value (#{resource.owner.inspect})")
    raise ArgumentError, "cannot resolve #{resource.group.inspect} to gid, group must be a string or integer"
  end
rescue ArgumentError
  provider.requirements.assert(:create, :create_if_missing, :touch) do |a|
    a.assertion { false }
    a.failure_message(Chef::Exceptions::GroupIDNotFound, "cannot determine group id for '#{resource.group}', does the group exist on this system?")
    a.whyrun("Assuming group #{resource.group} would have been created")
  end
  return nil
end

Returns:

  • (Boolean)


215
216
217
# File 'lib/chef/file_access_control/unix.rb', line 215

def manage_symlink_attrs?
  @provider.manage_symlink_access?
end

#mode_from_resource(res) ⇒ Object



158
159
160
161
# File 'lib/chef/file_access_control/unix.rb', line 158

def mode_from_resource(res)
  return nil if res == nil or res.mode.nil?
  (res.mode.respond_to?(:oct) ? res.mode.oct : res.mode.to_i) & 007777
end

#mode_to_s(mode) ⇒ Object



167
168
169
# File 'lib/chef/file_access_control/unix.rb', line 167

def mode_to_s(mode)
  mode.nil? ? "" : "0#{mode.to_s(8)}"
end

#requires_changes?Boolean

TODO factor this up

Returns:

  • (Boolean)


42
43
44
# File 'lib/chef/file_access_control/unix.rb', line 42

def requires_changes?
  should_update_mode? || should_update_owner? || should_update_group?
end

#set_allObject



35
36
37
38
39
# File 'lib/chef/file_access_control/unix.rb', line 35

def set_all
  set_owner
  set_group
  set_mode
end

#set_all!Object



29
30
31
32
33
# File 'lib/chef/file_access_control/unix.rb', line 29

def set_all!
  set_owner!
  set_group!
  set_mode!
end

#set_groupObject



154
155
156
# File 'lib/chef/file_access_control/unix.rb', line 154

def set_group
  set_group! if should_update_group?
end

#set_group!Object



146
147
148
149
150
151
152
# File 'lib/chef/file_access_control/unix.rb', line 146

def set_group!
  unless target_gid.nil?
    chown(nil, target_gid, file)
    Chef::Log.info("#{log_string} group changed to #{target_gid}")
    modified
  end
end

#set_modeObject



203
204
205
# File 'lib/chef/file_access_control/unix.rb', line 203

def set_mode
  set_mode! if should_update_mode?
end

#set_mode!Object



195
196
197
198
199
200
201
# File 'lib/chef/file_access_control/unix.rb', line 195

def set_mode!
  unless target_mode.nil?
    chmod(target_mode, file)
    Chef::Log.info("#{log_string} mode changed to #{target_mode.to_s(8)}")
    modified
  end
end

#set_ownerObject



95
96
97
# File 'lib/chef/file_access_control/unix.rb', line 95

def set_owner
  set_owner! if should_update_owner?
end

#set_owner!Object



87
88
89
90
91
92
93
# File 'lib/chef/file_access_control/unix.rb', line 87

def set_owner!
  unless target_uid.nil?
    chown(target_uid, nil, file)
    Chef::Log.info("#{log_string} owner changed to #{target_uid}")
    modified
  end
end

#should_update_group?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/chef/file_access_control/unix.rb', line 126

def should_update_group?
  if target_gid.nil?
    # the user has not specified a permission on the new resource, so we never manage it with FAC
    Chef::Log.debug("found target_gid == nil, so no group was specified on resource, not managing group")
    return false
  elsif current_gid.nil?
    # the user has specified a permission, and we are creating a file, so always enforce permissions
    Chef::Log.debug("found current_gid == nil, so we are creating a new file, updating group")
    return true
  elsif target_gid != current_gid
    # the user has specified a permission, and it does not match the file, so fix the permission
    Chef::Log.debug("found target_gid != current_gid, updating group")
    return true
  else
    Chef::Log.debug("found target_gid == current_gid, not updating group")
    # the user has specified a permission, but it matches the file, so behave idempotently
    return false
  end
end

#should_update_mode?Boolean

Returns:

  • (Boolean)


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/chef/file_access_control/unix.rb', line 175

def should_update_mode?
  if target_mode.nil?
    # the user has not specified a permission on the new resource, so we never manage it with FAC
    Chef::Log.debug("found target_mode == nil, so no mode was specified on resource, not managing mode")
    return false
  elsif current_mode.nil?
    # the user has specified a permission, and we are creating a file, so always enforce permissions
    Chef::Log.debug("found current_mode == nil, so we are creating a new file, updating mode")
    return true
  elsif target_mode != current_mode
    # the user has specified a permission, and it does not match the file, so fix the permission
    Chef::Log.debug("found target_mode != current_mode, updating mode")
    return true
  else
    Chef::Log.debug("found target_mode == current_mode, not updating mode")
    # the user has specified a permission, but it matches the file, so behave idempotently
    return false
  end
end

#should_update_owner?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/file_access_control/unix.rb', line 67

def should_update_owner?
  if target_uid.nil?
    # the user has not specified a permission on the new resource, so we never manage it with FAC
    Chef::Log.debug("found target_uid == nil, so no owner was specified on resource, not managing owner")
    return false
  elsif current_uid.nil?
    # the user has specified a permission, and we are creating a file, so always enforce permissions
    Chef::Log.debug("found current_uid == nil, so we are creating a new file, updating owner")
    return true
  elsif target_uid != current_uid
    # the user has specified a permission, and it does not match the file, so fix the permission
    Chef::Log.debug("found target_uid != current_uid, updating owner")
    return true
  else
    Chef::Log.debug("found target_uid == current_uid, not updating owner")
    # the user has specified a permission, but it matches the file, so behave idempotently
    return false
  end
end

#statObject



207
208
209
210
211
212
213
# File 'lib/chef/file_access_control/unix.rb', line 207

def stat
  if manage_symlink_attrs?
    @stat ||= File.lstat(file)
  else
    @stat ||= File.stat(file)
  end
end

#target_gidObject



99
100
101
# File 'lib/chef/file_access_control/unix.rb', line 99

def target_gid
  gid_from_resource(resource)
end

#target_modeObject



163
164
165
# File 'lib/chef/file_access_control/unix.rb', line 163

def target_mode
  mode_from_resource(resource)
end

#target_uidObject



59
60
61
# File 'lib/chef/file_access_control/unix.rb', line 59

def target_uid
  uid_from_resource(resource)
end