Class: Puppet::Indirector::SslFile

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

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::RFC_3986_URI_REGEX

Constants included from Util::SymbolicFileMode

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

Constants included from Util::POSIX

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

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Class Attribute Summary collapse

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Terminus

abstract_terminus?, #allow_remote_requests?, const2name, #indirection, indirection_name, inherited, mark_as_abstract_terminus, #model, model, #name, name2const, register_terminus_class, 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, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, which, withenv, withumask

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::POSIX

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

Methods included from Util::Docs

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

Constructor Details

#initializeSslFile


31
32
33
34
35
# File 'lib/puppet/indirector/ssl_file.rb', line 31

def initialize
  Puppet.settings.use(:main, :ssl)

  (collection_directory || file_location) or raise Puppet::DevError, _("No file or directory setting provided; terminus %{class_name} cannot function") % { class_name: self.class.name }
end

Class Attribute Details

.directory_settingObject (readonly)

Returns the value of attribute directory_setting


16
17
18
# File 'lib/puppet/indirector/ssl_file.rb', line 16

def directory_setting
  @directory_setting
end

.file_settingObject (readonly)

Returns the value of attribute file_setting


16
17
18
# File 'lib/puppet/indirector/ssl_file.rb', line 16

def file_setting
  @file_setting
end

Class Method Details

.collection_directoryObject

The full path to where we should store our files.


20
21
22
23
# File 'lib/puppet/indirector/ssl_file.rb', line 20

def self.collection_directory
  return nil unless directory_setting
  Puppet.settings[directory_setting]
end

.file_locationObject

The full path to an individual file we would be managing.


26
27
28
29
# File 'lib/puppet/indirector/ssl_file.rb', line 26

def self.file_location
  return nil unless file_setting
  Puppet.settings[file_setting]
end

.store_at(setting) ⇒ Object

Specify a single file location for storing just one file. This is used for things like the CRL.


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

def self.store_at(setting)
  @file_setting = setting
end

.store_in(setting) ⇒ Object

Specify the directory in which multiple files are stored.


5
6
7
# File 'lib/puppet/indirector/ssl_file.rb', line 5

def self.store_in(setting)
  @directory_setting = setting
end

Instance Method Details

#destroy(request) ⇒ Object

Remove our file.


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet/indirector/ssl_file.rb', line 51

def destroy(request)
  path = Puppet::FileSystem.pathname(path(request.key))
  return false unless Puppet::FileSystem.exist?(path)

  Puppet.notice _("Removing file %{model} %{request} at '%{path}'") % { model: model, request: request.key, path: path }
  begin
    Puppet::FileSystem.unlink(path)
  rescue => detail
    raise Puppet::Error, _("Could not remove %{request}: %{detail}") % { request: request.key, detail: detail }, detail.backtrace
  end
end

#find(request) ⇒ Object

Find the file on disk, returning an instance of the model.


64
65
66
67
68
# File 'lib/puppet/indirector/ssl_file.rb', line 64

def find(request)
  filename = rename_files_with_uppercase(path(request.key))

  filename ? create_model(request.key, filename) : nil
end

#path(name) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet/indirector/ssl_file.rb', line 37

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

  if collection_directory
    File.join(collection_directory, name.to_s + ".pem")
  else
    file_location
  end
end

#save(request) ⇒ Object

Save our file to disk.

Raises:


71
72
73
74
75
76
77
78
79
# File 'lib/puppet/indirector/ssl_file.rb', line 71

def save(request)
  path = path(request.key)
  dir = File.dirname(path)

  raise Puppet::Error.new(_("Cannot save %{request}; parent directory %{dir} does not exist") % { request: request.key, dir: dir }) unless FileTest.directory?(dir)
  raise Puppet::Error.new(_("Cannot save %{request}; parent directory %{dir} is not writable") % { request: request.key, dir: dir }) unless FileTest.writable?(dir)

  write(request.key, path) { |f| f.print request.instance.to_s }
end

#search(request) ⇒ Object

Search for more than one file. At this point, it just returns an instance for every file in the directory.


83
84
85
86
87
88
89
# File 'lib/puppet/indirector/ssl_file.rb', line 83

def search(request)
  dir = collection_directory
  Dir.entries(dir).
    select  { |file| file =~ /\.pem$/ }.
    collect { |file| create_model(file.sub(/\.pem$/, ''), File.join(dir, file)) }.
    compact
end