Module: ActiveJob::Temporal::TLSFile

Defined in:
lib/activejob/temporal/tls_file.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

OPEN_FLAGS =
File::RDONLY | (File.const_defined?(:NOFOLLOW) ? File::NOFOLLOW : 0)
USES_NOFOLLOW =
File.const_defined?(:NOFOLLOW)

Class Method Summary collapse

Class Method Details

.read(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/activejob/temporal/tls_file.rb', line 22

def read(path)
  return nil if path.nil? || path.to_s.empty?

  expanded_path = File.expand_path(path)
  reject_symlink!(expanded_path) unless USES_NOFOLLOW
  File.open(expanded_path, OPEN_FLAGS) do |file|
    raise Error, "TLS file path must point to a regular file: #{path}" unless file.stat.file?

    file.read
  end
rescue Errno::ELOOP
  raise Error, "TLS file path must not be a symlink: #{path}"
rescue Errno::ENOENT, Errno::ENOTDIR, Errno::EACCES
  raise Error, "TLS file path is not readable: #{path}"
end

.readable_regular_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
# File 'lib/activejob/temporal/tls_file.rb', line 12

def readable_regular_file?(path)
  expanded_path = File.expand_path(path)
  stat = File.lstat(expanded_path)
  return false if stat.symlink?

  stat.file? && File.readable?(expanded_path)
rescue Errno::ENOENT, Errno::ENOTDIR, Errno::EACCES
  false
end

.reject_symlink!(path) ⇒ Object

Raises:



38
39
40
41
42
# File 'lib/activejob/temporal/tls_file.rb', line 38

def reject_symlink!(path)
  return unless File.lstat(path).symlink?

  raise Error, "TLS file path must not be a symlink: #{path}"
end