Class: Puppet::Indirector::JSON

Inherits:
Terminus show all
Defined in:
lib/puppet/indirector/json.rb

Overview

The base class for JSON indirection terminus implementations.

This should generally be preferred to the YAML base for any future implementations, since it is faster and can load untrusted data safely.

Constant Summary

Constants included from Util

Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

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

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Instance Attribute Summary

Attributes included from Util::Docs

#doc, #nodoc

Instance Method Summary collapse

Methods inherited from Terminus

abstract_terminus?, #allow_remote_requests?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, model, #model, #name, name2const, register_terminus_class, #require_environment?, terminus_class, terminus_classes, #terminus_type, #validate, #validate_key, #validate_model

Methods included from Util::InstanceLoader

#instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from Util::POSIX

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

Methods included from Util::SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Constructor Details

This class inherits a constructor from Puppet::Indirector::Terminus

Instance Method Details

#destroy(request) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/puppet/indirector/json.rb', line 24

def destroy(request)
  Puppet::FileSystem.unlink(path(request.key))
rescue => detail
  unless detail.is_a? Errno::ENOENT
    raise Puppet::Error, _("Could not destroy %{json} %{request}: %{detail}") % { json: name, request: request.key, detail: detail }, detail.backtrace
  end

  1 # emulate success...
end

#find(request) ⇒ Object



11
12
13
# File 'lib/puppet/indirector/json.rb', line 11

def find(request)
  load_json_from_file(path(request.key), request.key)
end

#path(name, ext = '.json') ⇒ Object

Return the path to a given node’s file.



41
42
43
44
45
46
47
48
49
# File 'lib/puppet/indirector/json.rb', line 41

def path(name, ext = '.json')
  if name =~ Puppet::Indirector::BadNameRegexp then
    Puppet.crit(_("directory traversal detected in %{json}: %{name}") % { json: self.class, name: name.inspect })
    raise ArgumentError, _("invalid key")
  end

  base = data_dir
  File.join(base, self.class.indirection_name.to_s, name.to_s + ext)
end

#save(request) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/puppet/indirector/json.rb', line 15

def save(request)
  filename = path(request.key)
  FileUtils.mkdir_p(File.dirname(filename))

  Puppet::FileSystem.replace_file(filename, 0o660) { |f| f.print to_json(request.instance).force_encoding(Encoding::BINARY) }
rescue TypeError => detail
  Puppet.log_exception(detail, _("Could not save %{json} %{request}: %{detail}") % { json: name, request: request.key, detail: detail })
end

#search(request) ⇒ Object



34
35
36
37
38
# File 'lib/puppet/indirector/json.rb', line 34

def search(request)
  Dir.glob(path(request.key)).collect do |file|
    load_json_from_file(file, request.key)
  end
end