Class: Chef::ConfigFetcher

Inherits:
Object show all
Defined in:
lib/chef/config_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_location, config_file_jail = nil) ⇒ ConfigFetcher

Returns a new instance of ConfigFetcher.



12
13
14
15
# File 'lib/chef/config_fetcher.rb', line 12

def initialize(config_location, config_file_jail=nil)
  @config_location = config_location
  @config_file_jail = config_file_jail
end

Instance Attribute Details

#config_file_jailObject (readonly)

Returns the value of attribute config_file_jail.



10
11
12
# File 'lib/chef/config_fetcher.rb', line 10

def config_file_jail
  @config_file_jail
end

#config_locationObject (readonly)

Returns the value of attribute config_location.



9
10
11
# File 'lib/chef/config_fetcher.rb', line 9

def config_location
  @config_location
end

Instance Method Details

#config_missing?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chef/config_fetcher.rb', line 48

def config_missing?
  return false if remote_config?

  # Check if the config file exists, and check if it is underneath the config file jail
  begin
    real_config_file = Pathname.new(config_location).realpath.to_s
  rescue Errno::ENOENT
    return true
  end

  # If realpath succeeded, the file exists
  return false if !config_file_jail

  begin
    real_jail = Pathname.new(config_file_jail).realpath.to_s
  rescue Errno::ENOENT
    Chef::Log.warn("Config file jail #{config_file_jail} does not exist: will not load any config file.")
    return true
  end

  !Chef::ChefFS::PathUtils.descendant_of?(real_config_file, real_jail)
end

#fetch_jsonObject



17
18
19
20
21
22
23
24
# File 'lib/chef/config_fetcher.rb', line 17

def fetch_json
  config_data = read_config
  begin
    Chef::JSONCompat.from_json(config_data)
  rescue JSON::ParserError => error
    Chef::Application.fatal!("Could not parse the provided JSON file (#{config_location}): " + error.message, 2)
  end
end

#fetch_remote_configObject



34
35
36
37
38
# File 'lib/chef/config_fetcher.rb', line 34

def fetch_remote_config
  http.get("")
rescue SocketError, SystemCallError, Net::HTTPServerException => error
  Chef::Application.fatal!("Cannot fetch config '#{config_location}': '#{error.class}: #{error.message}", 2)
end

#httpObject



71
72
73
# File 'lib/chef/config_fetcher.rb', line 71

def http
  Chef::HTTP::Simple.new(config_location)
end

#read_configObject



26
27
28
29
30
31
32
# File 'lib/chef/config_fetcher.rb', line 26

def read_config
  if remote_config?
    fetch_remote_config
  else
    read_local_config
  end
end

#read_local_configObject



40
41
42
43
44
45
46
# File 'lib/chef/config_fetcher.rb', line 40

def read_local_config
  ::File.read(config_location)
rescue Errno::ENOENT => error
  Chef::Application.fatal!("Cannot load configuration from #{config_location}", 2)
rescue Errno::EACCES => error
  Chef::Application.fatal!("Permissions are incorrect on #{config_location}. Please chmod a+r #{config_location}", 2)
end

#remote_config?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/chef/config_fetcher.rb', line 75

def remote_config?
  !!(config_location =~ %r{^(http|https)://})
end