Class: Inspec::Resources::FileResource

Inherits:
Object
  • Object
show all
Includes:
FilePermissionsSelector, LinuxMountParser
Defined in:
lib/resources/file.rb

Direct Known Subclasses

Bond, Directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LinuxMountParser

#parse_mount_options

Methods included from FilePermissionsSelector

#select_file_perms_style

Constructor Details

#initialize(path) ⇒ FileResource

Returns a new instance of FileResource.



39
40
41
42
43
# File 'lib/resources/file.rb', line 39

def initialize(path)
  # select permissions style
  @perms_provider = select_file_perms_style(inspec.os)
  @file = inspec.backend.file(path)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



38
39
40
# File 'lib/resources/file.rb', line 38

def file
  @file
end

#mount_optionsObject (readonly)

Returns the value of attribute mount_options.



38
39
40
# File 'lib/resources/file.rb', line 38

def mount_options
  @mount_options
end

Instance Method Details

#contain(*_) ⇒ Object



63
64
65
# File 'lib/resources/file.rb', line 63

def contain(*_)
  raise 'Contain is not supported. Please use standard RSpec matchers.'
end

#contentObject



57
58
59
60
61
# File 'lib/resources/file.rb', line 57

def content
  res = file.content
  return nil if res.nil?
  res.force_encoding('utf-8')
end

#executable?(by_usergroup, by_specific_user) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/resources/file.rb', line 81

def executable?(by_usergroup, by_specific_user)
  return false unless exist?
  return skip_resource '`executable?` is not supported on your OS yet.' if @perms_provider.nil?

  file_permission_granted?('execute', by_usergroup, by_specific_user)
end

#mounted?(expected_options = nil, identical = false) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/resources/file.rb', line 88

def mounted?(expected_options = nil, identical = false)
  mounted = file.mounted

  # return if no additional parameters have been provided
  return file.mounted? if expected_options.nil?

  # deprecation warning, this functionality will be removed in future version
  warn "[DEPRECATION] `be_mounted.with and be_mounted.only_with` are deprecated.  Please use `mount('#{source_path}')` instead."

  # we cannot read mount data on non-Linux systems
  return nil if !inspec.os.linux?

  # parse content if we are on linux
  @mount_options ||= parse_mount_options(mounted.stdout, true)

  if identical
    # check if the options should be identical
    @mount_options == expected_options
  else
    # otherwise compare the selected values
    @mount_options.contains(expected_options)
  end
end

#readable?(by_usergroup, by_specific_user) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/resources/file.rb', line 67

def readable?(by_usergroup, by_specific_user)
  return false unless exist?
  return skip_resource '`readable?` is not supported on your OS yet.' if @perms_provider.nil?

  file_permission_granted?('read', by_usergroup, by_specific_user)
end

#sgidObject



116
117
118
# File 'lib/resources/file.rb', line 116

def sgid
  (mode & 02000) > 0
end

#stickyObject



120
121
122
# File 'lib/resources/file.rb', line 120

def sticky
  (mode & 01000) > 0
end

#suidObject



112
113
114
# File 'lib/resources/file.rb', line 112

def suid
  (mode & 04000) > 0
end

#to_sObject



124
125
126
# File 'lib/resources/file.rb', line 124

def to_s
  "File #{source_path}"
end

#writable?(by_usergroup, by_specific_user) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/resources/file.rb', line 74

def writable?(by_usergroup, by_specific_user)
  return false unless exist?
  return skip_resource '`writable?` is not supported on your OS yet.' if @perms_provider.nil?

  file_permission_granted?('write', by_usergroup, by_specific_user)
end