Module: Puppet::Util

Extended by:
POSIX, SymbolicFileMode
Included in:
Puppet, Application, Application, Configurer, Confine, Interface, Network::AuthStore::Declaration, Node::Exec, Parameter, Parameter, Parser::Compiler, Parser::Functions, Parser::Resource, Parser::Resource::Param, Parser::TemplateWrapper, Provider, Provider, Resource::Catalog::Compiler, Transaction, Type, Type, ClassGen, FileParsing, FileParsing::FileRecord, InstanceLoader, Log, Log, ProviderFeatures::ProviderFeature, Reference, Storage
Defined in:
lib/puppet/util/multi_match.rb,
lib/puppet/util.rb,
lib/puppet/util/json.rb,
lib/puppet/util/logging.rb,
lib/puppet/util/platform.rb,
lib/puppet/util/run_mode.rb,
lib/puppet/util/connection.rb,
lib/puppet/util/command_line.rb,
lib/puppet/util/execution_stub.rb,
lib/puppet/util/windows/eventlog.rb,
lib/puppet/util/constant_inflector.rb,
lib/puppet/util/symbolic_file_mode.rb,
lib/puppet/util/command_line/trollop.rb,
lib/puppet/util/command_line/puppet_option_parser.rb

Overview

MultiMatch allows multiple values to be tested at once in a case expression. This class is needed since Array does not implement the === operator to mean “each v === other.each v”.

This class is useful in situations when the Puppet Type System cannot be used (e.g. in Logging, since it needs to be able to log very early in the initialization cycle of puppet)

Typically used with the constants NOT_NIL TUPLE TRIPLE

which test against single NOT_NIL value, Array with two NOT_NIL, and Array with three NOT_NIL

Defined Under Namespace

Modules: AtFork, Backups, CharacterEncoding, Checksums, ClassGen, Colors, Connection, ConstantInflector, Diff, Docs, Errors, Execution, FileParsing, HttpProxy, IniConfig, InstanceLoader, Json, Ldap, Libuser, Limits, Logging, MonkeyPatches, POSIX, Package, Platform, Plist, Profiler, ProviderFeatures, PsychSupport, RDoc, RetryAction, RpmCompare, RubyGems, SELinux, SSL, SUIDManager, Splayer, SymbolicFileMode, Tagging, Terminal, Warnings, Watcher, Windows, Yaml Classes: Autoload, CommandLine, ExecutionStub, Feature, FileNamespaceAdapter, FileType, FileWatcher, JsonLockfile, Lockfile, Log, Metric, ModuleDirectoriesAdapter, MultiMatch, NetworkDevice, Pidlock, Reference, ResourceTemplate, RunMode, SkipTags, Storage, TagSet, UnixRunMode, WatchedFile, WindowsRunMode

Constant Summary collapse

AbsolutePathWindows =
%r!^(?:(?:[A-Z]:#{slash})|(?:#{slash}#{slash}#{label}#{slash}#{label})|(?:#{slash}#{slash}\?#{slash}#{label}))!io
AbsolutePathPosix =
%r!^/!
RFC_3986_URI_REGEX =
/^(?<scheme>([^:\/?#]+):)?(?<authority>\/\/([^\/?#]*))?(?<path>[^?#]*)(\?(?<query>[^#]*))?(#(?<fragment>.*))?$/
PUPPET_STACK_INSERTION_FRAME =
/.*puppet_stack\.rb.*in.*`stack'/
DEFAULT_POSIX_MODE =

Replace a file, securely. This takes a block, and passes it the file handle of a file open for writing. Write the replacement content inside the block and it will safely replace the target file.

This method will make no changes to the target file until the content is successfully written and the block returns without raising an error.

As far as possible the state of the existing file, such as mode, is preserved. This works hard to avoid loss of any metadata, but will result in an inode change for the file.

Arguments: `filename`, `default_mode`, `staging_location`

The filename is the file we are going to replace.

The default_mode is the mode to use when the target file doesn't already exist; if the file is present we copy the existing mode/owner/group values across. The default_mode can be expressed as an octal integer, a numeric string (ie '0664') or a symbolic file mode.

The staging_location is a location to render the temporary file before moving the file to it's final location.

0644
DEFAULT_WINDOWS_MODE =
nil

Constants included from SymbolicFileMode

SymbolicFileMode::SetGIDBit, SymbolicFileMode::SetUIDBit, SymbolicFileMode::StickyBit, SymbolicFileMode::SymbolicMode, SymbolicFileMode::SymbolicSpecialToBit

Constants included from POSIX

POSIX::LOCALE_ENV_VARS, POSIX::USER_ENV_VARS

Class Method Summary collapse

Methods included from SymbolicFileMode

normalize_symbolic_mode, symbolic_mode_to_int, valid_symbolic_mode?

Methods included from POSIX

get_posix_field, gid, groups_of, idfield, methodbyid, methodbyname, search_posix_field, uid

Class Method Details

.absolute_path?(path, platform = nil) ⇒ Boolean

Returns:

  • (Boolean)

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/puppet/util.rb', line 298

def absolute_path?(path, platform=nil)
  unless path.is_a?(String)
    Puppet.warning("Cannot check if #{path} is an absolute path because it is a '#{path.class}' and not a String'")
    return false
  end

  # Ruby only sets File::ALT_SEPARATOR on Windows and the Ruby standard
  # library uses that to test what platform it's on.  Normally in Puppet we
  # would use Puppet.features.microsoft_windows?, but this method needs to
  # be called during the initialization of features so it can't depend on
  # that.
  #
  # @deprecated Use ruby's built-in methods to determine if a path is absolute.
  platform ||= Puppet::Util::Platform.windows? ? :windows : :posix
  regex = case platform
          when :windows
            AbsolutePathWindows
          when :posix
            AbsolutePathPosix
          else
            raise Puppet::DevError, _("unknown platform %{platform} in absolute_path") % { platform: platform }
          end

  !! (path =~ regex)
end

.benchmark(*args) ⇒ Object

execute a block of work and based on the logging level provided, log the provided message with the seconds taken The message 'msg' should include string ' in %seconds seconds' as part of the message and any content should escape any percent signs '%' so that they are not interpreted as formatting commands

escaped_str = str.gsub(/%/, '%%')

Parameters:

  • msg (String)

    the message to be formated to assigned the %seconds seconds take to execute, other percent signs '%' need to be escaped

  • level (Symbol)

    the logging level for this message

  • object (Object)

    The object use for logging the message

Raises:


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/puppet/util.rb', line 210

def benchmark(*args)
  msg = args.pop
  level = args.pop
  object = if args.empty?
             if respond_to?(level)
               self
             else
               Puppet
             end
           else
             args.pop
           end

  #TRANSLATORS 'benchmark' is a method name and should not be translated
  raise Puppet::DevError, _("Failed to provide level to benchmark") unless level

  unless level == :none or object.respond_to? level
    raise Puppet::DevError, _("Benchmarked object does not respond to %{value}") % { value: level }
  end

  # Only benchmark if our log level is high enough
  if level != :none and Puppet::Util::Log.sendlevel?(level)
    seconds = Benchmark.realtime {
      yield
    }
    object.send(level, msg % { seconds: "%0.2f" % seconds })
    return seconds
  else
    yield
  end
end

.chuserObject

Change the process to a different user


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/puppet/util.rb', line 150

def self.chuser
  group = Puppet[:group]
  if group
    begin
      Puppet::Util::SUIDManager.change_group(group, true)
    rescue => detail
      Puppet.warning _("could not change to group %{group}: %{detail}") % { group: group.inspect, detail: detail }
      $stderr.puts _("could not change to group %{group}") % { group: group.inspect }

      # Don't exit on failed group changes, since it's
      # not fatal
      #exit(74)
    end
  end

  user = Puppet[:user]
  if user
    begin
      Puppet::Util::SUIDManager.change_user(user, true)
    rescue => detail
      $stderr.puts _("Could not change to user %{user}: %{detail}") % { user: user, detail: detail }
      exit(74)
    end
  end
end

.clear_environment(mode = default_env) ⇒ 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.

Removes all environment variables

Parameters:

  • mode (Symbol) (defaults to: default_env)

    Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/util.rb', line 72

def clear_environment(mode = default_env)
  case mode
    when :posix
      ENV.clear
    when :windows
      Puppet::Util::Windows::Process.get_environment_strings.each do |key, _|
        Puppet::Util::Windows::Process.set_environment_variable(key, nil)
      end
    else
      raise _("Unable to clear the environment for mode %{mode}") % { mode: mode }
  end
end

.default_envObject


29
30
31
32
33
# File 'lib/puppet/util.rb', line 29

def default_env
  Puppet.features.microsoft_windows? ?
    :windows :
    :posix
end

.deterministic_rand(seed, max) ⇒ Object


753
754
755
# File 'lib/puppet/util.rb', line 753

def deterministic_rand(seed,max)
  deterministic_rand_int(seed, max).to_s
end

.deterministic_rand_int(seed, max) ⇒ Object


758
759
760
# File 'lib/puppet/util.rb', line 758

def deterministic_rand_int(seed,max)
  Random.new(seed).rand(max)
end

.exit_on_fail(message, code = 1) { ... } ⇒ Object

Executes a block of code, wrapped with some special exception handling. Causes the ruby interpreter to

exit if the block throws an exception.

Parameters:

  • message (String)

    a message to log if the block fails

  • code (Integer) (defaults to: 1)

    the exit code that the ruby interpreter should return if the block fails

Yields:


734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/puppet/util.rb', line 734

def exit_on_fail(message, code = 1)
  yield
# First, we need to check and see if we are catching a SystemExit error.  These will be raised
#  when we daemonize/fork, and they do not necessarily indicate a failure case.
rescue SystemExit => err
  raise err

# Now we need to catch *any* other kind of exception, because we may be calling third-party
#  code (e.g. webrick), and we have no idea what they might throw.
rescue Exception => err
  ## NOTE: when debugging spec failures, these two lines can be very useful
  #puts err.inspect
  #puts Puppet::Util.pretty_backtrace(err.backtrace)
  Puppet.log_exception(err, "#{message}: #{err}")
  Puppet::Util::Log.force_flushqueue()
  exit(code)
end

.format_backtrace_array(primary_stack, puppetstack = []) ⇒ Object

arguments may be a Ruby stack, with an optional Puppet stack argument, or just a Puppet stack. stacks may be an Array of Strings “/foo.rb:0 in `blah'” or an Array of Arrays that represent a frame: [“/foo.pp”, 0]


566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/puppet/util.rb', line 566

def self.format_backtrace_array(primary_stack, puppetstack = [])
  primary_stack.flat_map do |frame|
    frame = format_puppetstack_frame(frame) if frame.is_a?(Array)
    primary_frame = resolve_stackframe(frame)

    if primary_frame =~ PUPPET_STACK_INSERTION_FRAME && !puppetstack.empty?
      [resolve_stackframe(format_puppetstack_frame(puppetstack.shift)),
       primary_frame]
    else
      primary_frame
    end
  end
end

.format_puppetstack_frame(file_and_lineno) ⇒ Object


590
591
592
# File 'lib/puppet/util.rb', line 590

def self.format_puppetstack_frame(file_and_lineno)
  file_and_lineno.join(':')
end

.get_env(name, mode = default_env) ⇒ String

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.

Returns Value of the specified environment variable. nil if it does not exist.

Parameters:

  • name (String)

    The name of the environment variable to retrieve

  • mode (Symbol) (defaults to: default_env)

    Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect

Returns:

  • (String)

    Value of the specified environment variable. nil if it does not exist


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet/util.rb', line 40

def get_env(name, mode = default_env)
  if mode == :windows
    Puppet::Util::Windows::Process.get_environment_strings.each do |key, value |
      if name.casecmp(key) == 0 then
        return value
      end
    end
    return nil
  else
    ENV[name]
  end
end

.get_environment(mode = default_env) ⇒ Hash

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.

Returns A hashtable of all environment variables.

Parameters:

  • mode (Symbol) (defaults to: default_env)

    Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect

Returns:

  • (Hash)

    A hashtable of all environment variables


57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/util.rb', line 57

def get_environment(mode = default_env)
  case mode
    when :posix
      ENV.to_hash
    when :windows
      Puppet::Util::Windows::Process.get_environment_strings
    else
      raise _("Unable to retrieve the environment for mode %{mode}") % { mode: mode }
  end
end

.logmethods(klass, useself = true) ⇒ Object

Create instance methods for each of the log levels. This allows the messages to be a little richer. Most classes will be calling this method.


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/puppet/util.rb', line 179

def self.logmethods(klass, useself = true)
  Puppet::Util::Log.eachlevel { |level|
    klass.send(:define_method, level, proc { |args|
      args = args.join(" ") if args.is_a?(Array)
      if useself

        Puppet::Util::Log.create(
          :level => level,
          :source => self,
          :message => args
        )
      else

        Puppet::Util::Log.create(
          :level => level,
          :message => args
        )
      end
    })
  }
end

.merge_environment(env_hash, mode = default_env) ⇒ 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.

Parameters:

  • name (Hash)

    Environment variables to merge into the existing environment. nil values will remove the variable

  • mode (Symbol) (defaults to: default_env)

    Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect


105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/puppet/util.rb', line 105

def merge_environment(env_hash, mode = default_env)
  case mode
    when :posix
      env_hash.each { |name, val| ENV[name.to_s] = val }
    when :windows
      env_hash.each do |name, val|
        Puppet::Util::Windows::Process.set_environment_variable(name.to_s, val)
      end
    else
      raise _("Unable to merge given values into the current environment for mode %{mode}") % { mode: mode }
  end
end

.path_to_uri(path) ⇒ Object

Convert a path to a file URI


326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/puppet/util.rb', line 326

def path_to_uri(path)
  return unless path

  params = { :scheme => 'file' }

  if Puppet::Util::Platform.windows?
    path = path.tr('\\', '/')

    unc = /^\/\/([^\/]+)(\/.+)/.match(path)
    if unc
      params[:host] = unc[1]
      path = unc[2]
    elsif path =~ /^[a-z]:\//i
      path = '/' + path
    end
  end

  # have to split *after* any relevant escaping
  params[:path], params[:query] = uri_encode(path).split('?')
  search_for_fragment = params[:query] ? :query : :path
  if params[search_for_fragment].include?('#')
    params[search_for_fragment], _, params[:fragment] = params[search_for_fragment].rpartition('#')
  end

  begin
    URI::Generic.build(params)
  rescue => detail
    raise Puppet::Error, _("Failed to convert '%{path}' to URI: %{detail}") % { path: path, detail: detail }, detail.backtrace
  end
end

.pretty_backtrace(backtrace = caller(1), puppetstack = []) ⇒ Object

utility method to get the current call stack and format it to a human-readable string (which some IDEs/editors will recognize as links to the line numbers in the trace)


558
559
560
# File 'lib/puppet/util.rb', line 558

def self.pretty_backtrace(backtrace = caller(1), puppetstack = [])
  format_backtrace_array(backtrace, puppetstack).join("\n")
end

.replace_file(file, default_mode, staging_location: nil, validate_callback: nil, &block) ⇒ Object

Raises:


620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/puppet/util.rb', line 620

def replace_file(file, default_mode, staging_location: nil, validate_callback: nil, &block)
  raise Puppet::DevError, _("replace_file requires a block") unless block_given?

  if default_mode
    unless valid_symbolic_mode?(default_mode)
      raise Puppet::DevError, _("replace_file default_mode: %{default_mode} is invalid") % { default_mode: default_mode }
    end

    mode = symbolic_mode_to_int(normalize_symbolic_mode(default_mode))
  else
    if Puppet::Util::Platform.windows?
      mode = DEFAULT_WINDOWS_MODE
    else
      mode = DEFAULT_POSIX_MODE
    end
  end

  begin
    file = Puppet::FileSystem.pathname(file)

    # encoding for Uniquefile is not important here because the caller writes to it as it sees fit
    if staging_location
      tempfile = Puppet::FileSystem::Uniquefile.new(Puppet::FileSystem.basename_string(file), staging_location)
    else
      tempfile = Puppet::FileSystem::Uniquefile.new(Puppet::FileSystem.basename_string(file), Puppet::FileSystem.dir_string(file))
    end


    effective_mode =
    if !Puppet::Util::Platform.windows?
      # Grab the current file mode, and fall back to the defaults.

      if Puppet::FileSystem.exist?(file)
        stat = Puppet::FileSystem.lstat(file)
        tempfile.chown(stat.uid, stat.gid)
        stat.mode
      else
        mode
      end
    end

    # OK, now allow the caller to write the content of the file.
    yield tempfile

    if effective_mode
      # We only care about the bottom four slots, which make the real mode,
      # and not the rest of the platform stat call fluff and stuff.
      tempfile.chmod(effective_mode & 07777)
    end

    # Now, make sure the data (which includes the mode) is safe on disk.
    tempfile.flush
    begin
      tempfile.fsync
    rescue NotImplementedError
      # fsync may not be implemented by Ruby on all platforms, but
      # there is absolutely no recovery path if we detect that.  So, we just
      # ignore the return code.
      #
      # However, don't be fooled: that is accepting that we are running in
      # an unsafe fashion.  If you are porting to a new platform don't stub
      # that out.
    end

    tempfile.close

    if validate_callback
      validate_callback.call(tempfile.path)
    end

    if Puppet::Util::Platform.windows?
      # Windows ReplaceFile needs a file to exist, so touch handles this
      if !Puppet::FileSystem.exist?(file)
        Puppet::FileSystem.touch(file)
        if mode
          Puppet::Util::Windows::Security.set_mode(mode, Puppet::FileSystem.path_string(file))
        end
      end
      # Yes, the arguments are reversed compared to the rename in the rest
      # of the world.
      Puppet::Util::Windows::File.replace_file(FileSystem.path_string(file), tempfile.path)

    else
      # MRI Ruby checks for this and raises an error, while JRuby removes the directory
      # and replaces it with a file. This makes the our version of replace_file() consistent
      if Puppet::FileSystem.exist?(file) && Puppet::FileSystem.directory?(file)
        raise Errno::EISDIR, _("Is a directory: %{directory}") % { directory: file }
      end
      File.rename(tempfile.path, Puppet::FileSystem.path_string(file))
    end
  ensure
    # in case an error occurred before we renamed the temp file, make sure it
    # gets deleted
    if tempfile
      tempfile.close!
    end
  end


  # Ideally, we would now fsync the directory as well, but Ruby doesn't
  # have support for that, and it doesn't matter /that/ much...

  # Return something true, and possibly useful.
  file
end

.resolve_stackframe(frame) ⇒ Object


580
581
582
583
584
585
586
587
588
# File 'lib/puppet/util.rb', line 580

def self.resolve_stackframe(frame)
  _, path, rest = /^(.*):(\d+.*)$/.match(frame).to_a
  if path
    path = Pathname(path).realpath rescue path
    "#{path}:#{rest}"
  else
    frame
  end
end

.safe_posix_fork(stdin = $stdin, stdout = $stdout, stderr = $stderr, &block) ⇒ Object


507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/puppet/util.rb', line 507

def safe_posix_fork(stdin=$stdin, stdout=$stdout, stderr=$stderr, &block)
  child_pid = Kernel.fork do
    STDIN.reopen(stdin)
    STDOUT.reopen(stdout)
    STDERR.reopen(stderr)

    $stdin = STDIN
    $stdout = STDOUT
    $stderr = STDERR

    begin
      Dir.foreach('/proc/self/fd') do |f|
        if f != '.' && f != '..' && f.to_i >= 3
          IO::new(f.to_i).close rescue nil
        end
      end
    rescue Errno::ENOENT # /proc/self/fd not found
      3.upto(256){|fd| IO::new(fd).close rescue nil}
    end

    block.call if block
  end
  child_pid
end

.set_env(name, value = nil, mode = default_env) ⇒ 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.

Parameters:

  • name (String)

    The name of the environment variable to set

  • value (String) (defaults to: nil)

    The value to set the variable to. nil deletes the environment variable

  • mode (Symbol) (defaults to: default_env)

    Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect


90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet/util.rb', line 90

def set_env(name, value = nil, mode = default_env)
  case mode
    when :posix
      ENV[name] = value
    when :windows
      Puppet::Util::Windows::Process.set_environment_variable(name,value)
    else
      raise _("Unable to set the environment variable %{name} for mode %{mode}") % { name: name, mode: mode }
  end
end

.skip_external_factsObject

Executes a block of code, wrapped around Facter.load_external(false) and Facter.load_external(true) which will cause Facter to not evaluate external facts.


765
766
767
768
769
770
771
772
773
# File 'lib/puppet/util.rb', line 765

def skip_external_facts
  return yield unless Facter.respond_to? :load_external
  begin
    Facter.load_external(false)
    yield
  ensure
    Facter.load_external(true)
  end
end

.symbolizehash(hash) ⇒ Object


533
534
535
536
537
538
539
540
# File 'lib/puppet/util.rb', line 533

def symbolizehash(hash)
  newhash = {}
  hash.each do |name, val|
    name = name.intern if name.respond_to? :intern
    newhash[name] = val
  end
  newhash
end

.thinmarkObject

Just benchmark, with no logging.


544
545
546
547
548
549
550
# File 'lib/puppet/util.rb', line 544

def thinmark
  seconds = Benchmark.realtime {
    yield
  }

  seconds
end

.uri_encode(path, opts = { :allow_fragment => false }) ⇒ String

Percent-encodes a URI string per RFC3986 - tools.ietf.org/html/rfc3986

Properly handles escaping rules for paths, query strings and fragments independently

The output is safe to pass to URI.parse or URI::Generic.build and will correctly round-trip through URI.unescape

Parameters:

  • A (String path)

    URI string that may be in the form of:

    foo.com/bar?query file://tmp/foo bar //foo.com/bar?query /bar?query bar?query bar . C:WindowsTemp

    Note that with no specified scheme, authority or query parameter delimiter ? that a naked string will be treated as a path.

    Note that if query parameters need to contain data such as & or = that this method should not be used, as there is no way to differentiate query parameter data from query delimiters when multiple parameters are specified

  • Options (Hash{Symbol=>String} opts)

    to alter encoding

  • opts (Hash) (defaults to: { :allow_fragment => false })

    a customizable set of options

Options Hash (opts):

  • :allow_fragment (Array<Symbol>)

    defaults to false. When false will treat # as part of a path or query and not a fragment delimiter

Returns:

  • (String)

    a new string containing appropriate portions of the URI encoded per the rules of RFC3986. In particular, path will not encode , but will encode space as %20 query will encode as %2B and space as %20 fragment behaves like query

Raises:

  • (ArgumentError)

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/puppet/util.rb', line 452

def uri_encode(path, opts = { :allow_fragment => false })
  raise ArgumentError.new(_('path may not be nil')) if path.nil?

  # ensure string starts as UTF-8 for the sake of Ruby 1.9.3
  encoded = ''.encode!(Encoding::UTF_8)

  # parse uri into named matches, then reassemble properly encoded
  parts = path.match(RFC_3986_URI_REGEX)

  encoded += parts[:scheme] unless parts[:scheme].nil?
  encoded += parts[:authority] unless parts[:authority].nil?

  # path requires space to be encoded as %20 (NEVER +)
  # + should be left unencoded
  # URI::parse and URI::Generic.build don't like paths encoded with CGI.escape
  # URI.escape does not change / to %2F and : to %3A like CGI.escape
  #
  # URI.escape is obsolete in Ruby 2.7. Ignore this error until we're able to
  # switch to a different escape mechanism. If this is JRuby, we can't mask
  # the error message, because this isn't thread safe. JRuby shouldn't be
  # using Ruby 2.7 or raising the warning anyway.
  orig_verbose = $VERBOSE
  $VERBOSE = nil unless Puppet::Util::Platform.jruby?
  begin
    encoded += URI.escape(parts[:path]) unless parts[:path].nil?
  ensure
    $VERBOSE = orig_verbose unless Puppet::Util::Platform.jruby?
  end

  # each query parameter
  if !parts[:query].nil?
    query_string = parts[:query].split('&').map do |pair|
      # can optionally be separated by an =
      pair.split('=').map do |v|
        uri_query_encode(v)
      end.join('=')
    end.join('&')
    encoded += '?' + query_string
  end

  encoded += ((opts[:allow_fragment] ? '#' : '%23') + uri_query_encode(parts[:fragment])) unless parts[:fragment].nil?

  encoded
end

.uri_query_encode(query_string) ⇒ String

Percent-encodes a URI query parameter per RFC3986 - tools.ietf.org/html/rfc3986

The output will correctly round-trip through URI.unescape

Parameters:

  • A (String query_string)

    URI query parameter that may contain reserved characters that must be percent encoded for the key or value to be properly decoded as part of a larger query string:

    query encodes as : query

    query_with_special=chars like&and * and# plus+this encodes as: query_with_special%3Dchars%20like%26and%20%2A%20and%23%20plus%2Bthis

    Note: Also usable by fragments, but not suitable for paths

Returns:

  • (String)

    a new string containing an encoded query string per the rules of RFC3986.

    In particular, query will encode + as %2B and space as %20


402
403
404
405
406
407
408
409
410
411
412
# File 'lib/puppet/util.rb', line 402

def uri_query_encode(query_string)
  return nil if query_string.nil?

  # query can encode space to %20 OR +
  # + MUST be encoded as %2B
  # in RFC3968 both query and fragment are defined as:
  # = *( pchar / "/" / "?" )
  # CGI.escape turns space into + which is the most backward compatible
  # however it doesn't roundtrip through URI.unescape which prefers %20
  CGI.escape(query_string).gsub('+', '%20')
end

.uri_to_path(uri) ⇒ Object

Get the path component of a URI


359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/puppet/util.rb', line 359

def uri_to_path(uri)
  return unless uri.is_a?(URI)

  # CGI.unescape doesn't handle space rules properly in uri paths
  # URI.unescape does, but returns strings in their original encoding
  path = uri_unescape(uri.path.encode(Encoding::UTF_8))

  if Puppet::Util::Platform.windows? && uri.scheme == 'file'
    if uri.host && !uri.host.empty?
      path = "//#{uri.host}" + path # UNC
    else
      path.sub!(/^\//, '')
    end
  end

  path
end

.uri_unescape(path) ⇒ Object


498
499
500
501
502
503
504
# File 'lib/puppet/util.rb', line 498

def uri_unescape(path)
  orig_verbose = $VERBOSE
  $VERBOSE = nil unless Puppet::Util::Platform.jruby?
  return URI.unescape(path)
ensure
  $VERBOSE = orig_verbose unless Puppet::Util::Platform.jruby?
end

.which(bin) ⇒ String

Resolve a path for an executable to the absolute path. This tries to behave in the same manner as the unix `which` command and uses the `PATH` environment variable.

Parameters:

  • bin (String)

    the name of the executable to find.

Returns:

  • (String)

    the absolute path to the found executable.


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/puppet/util.rb', line 250

def which(bin)
  if absolute_path?(bin)
    return bin if FileTest.file? bin and FileTest.executable? bin
  else
    exts = Puppet::Util.get_env('PATHEXT')
    exts = exts ? exts.split(File::PATH_SEPARATOR) : %w[.COM .EXE .BAT .CMD]
    Puppet::Util.get_env('PATH').split(File::PATH_SEPARATOR).each do |dir|
      begin
        dest = File.expand_path(File.join(dir, bin))
      rescue ArgumentError => e
        # if the user's PATH contains a literal tilde (~) character and HOME is not set, we may get
        # an ArgumentError here.  Let's check to see if that is the case; if not, re-raise whatever error
        # was thrown.
        if e.to_s =~ /HOME/ and (Puppet::Util.get_env('HOME').nil? || Puppet::Util.get_env('HOME') == "")
          # if we get here they have a tilde in their PATH.  We'll issue a single warning about this and then
          # ignore this path element and carry on with our lives.
          #TRANSLATORS PATH and HOME are environment variables and should not be translated
          Puppet::Util::Warnings.warnonce(_("PATH contains a ~ character, and HOME is not set; ignoring PATH element '%{dir}'.") % { dir: dir })
        elsif e.to_s =~ /doesn't exist|can't find user/
          # ...otherwise, we just skip the non-existent entry, and do nothing.
          #TRANSLATORS PATH is an environment variable and should not be translated
          Puppet::Util::Warnings.warnonce(_("Couldn't expand PATH containing a ~ character; ignoring PATH element '%{dir}'.") % { dir: dir })
        else
          raise
        end
      else
        if Puppet::Util::Platform.windows? && File.extname(dest).empty?
          exts.each do |ext|
            destext = File.expand_path(dest + ext)
            return destext if FileTest.file? destext and FileTest.executable? destext
          end
        end
        return dest if FileTest.file? dest and FileTest.executable? dest
      end
    end
  end
  nil
end

.withenv(hash, mode = :posix) ⇒ Object

Run some code with a specific environment. Resets the environment back to what it was at the end of the code. Windows can store Unicode chars in the environment as keys or values, but Ruby's ENV tries to roundtrip them through the local codepage, which can cause encoding problems - underlying helpers use Windows APIs on Windows see bugs.ruby-lang.org/issues/8822


125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet/util.rb', line 125

def withenv(hash, mode = :posix)
  saved = get_environment(mode)
  merge_environment(hash, mode)
  yield
ensure
  if saved
    clear_environment(mode)
    merge_environment(saved, mode)
  end
end

.withumask(mask) ⇒ Object

Execute a given chunk of code with a new umask.


138
139
140
141
142
143
144
145
146
# File 'lib/puppet/util.rb', line 138

def self.withumask(mask)
  cur = File.umask(mask)

  begin
    yield
  ensure
    File.umask(cur)
  end
end