Class: HammerCLI::SSLOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/ssloptions.rb

Constant Summary collapse

DEFAULT_LOCAL_CA_STORE_PATH =
"~/.hammer/certs"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SSLOptions

Returns a new instance of SSLOptions.



5
6
7
8
9
# File 'lib/hammer_cli/ssloptions.rb', line 5

def initialize(options={})
  @settings = options.fetch(:settings, HammerCLI::Settings)
  @logger = options.fetch(:logger, Logging.logger['SSLoptions'])
  @ca_manager = options.fetch(:ca_manager, HammerCLI::CACertManager.new(DEFAULT_LOCAL_CA_STORE_PATH))
end

Instance Method Details

#get_local_ca_store_pathObject



11
12
13
# File 'lib/hammer_cli/ssloptions.rb', line 11

def get_local_ca_store_path
  @settings.get(:ssl, :local_ca_store_path) || DEFAULT_LOCAL_CA_STORE_PATH
end

#get_options(uri = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hammer_cli/ssloptions.rb', line 15

def get_options(uri = nil)
  ssl_options = {}
  for sslopt in [:ssl_ca_file, :ssl_ca_path, :verify_ssl, :ssl_version] do
    ssloptval = read_ssl_option(sslopt)
    ssl_options[sslopt] = ssloptval unless ssloptval.nil?
  end
  ssl_options.merge!(cert_key_options)

  # enable ssl verification
  ssl_options[:verify_ssl] = true if ssl_options[:verify_ssl].nil?

  if ssl_options[:verify_ssl] && uri && !ssl_options[:ssl_ca_file] && !ssl_options[:ssl_ca_path]
    uri = URI.parse(uri) if uri.is_a?(String)
    if @ca_manager.cert_exist?(uri)
      ssl_options[:ssl_ca_file] = @ca_manager.cert_file_name(uri)
      @logger.info("Matching CA cert was found in local CA store #{ssl_options[:ssl_ca_file]}")
    end
  end

  [:ssl_ca_file, :ssl_ca_path].each do |opt|
    ssl_options[opt] = File.expand_path(ssl_options[opt]) unless ssl_options[opt].nil?
  end

  @logger.debug("SSL options: #{ApipieBindings::Utils::inspect_data(ssl_options)}")
  ssl_options
end