class Object

Public Instance Methods

describe_command(*argv, &block) click to toggle source
# File lib/conjur/command/rspec/describe_command.rb, line 2
def describe_command *argv, &block
  describe *argv do
    let(:cert_store) { double(:cert_store) }
      
    before do
      allow(cert_store).to receive(:add_file)
      # Stub the constant OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE which is
      # implicitly used in many places in the CLI and in conjur-api-ruby as the de facto
      # cert store.
      stub_const 'OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE', cert_store

      # Reset the rest_client_options defaults to avoid using expired rspec doubles.
      #
      # Conjur.configuration is a lazy-loaded singleton. There is single CLI instance
      # shared across this test suite. When Conjur.configuration is loaded for the first
      # time it assumes the defaults value for Conjur.configuration.rest_client_options
      # of:
      # {
      #  :ssl_cert_store => OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE
      # }
      #
      # Notice above that each test case stubs the constant
      # OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE with a double. Without further
      # modification this means the first time the CLI is run and Conjur.configuration
      # is loaded Conjur.configuration.rest_client_options[:ssl_cert_store] it is set to
      # the double associated with the test case at that point in time. Since
      # Conjur.configuration is only loaded once, without modification, that double will
      # be retained and its usage will result in a RSpec::Mocks::ExpiredTestDoubleError.
      # To avoid this for each test case we must reset
      # Conjur.configuration.rest_client_options[:ssl_cert_store] with the double for
      # the current test case.
      Conjur.configuration.rest_client_options[:ssl_cert_store] = cert_store
    end

    let(:invoke) do
      Conjur::CLI.error_device = $stderr
      # TODO: allow proper handling of description like "audit:send 'hello world'"
      Conjur::CLI.run argv.first.split(' ')
    end
    instance_eval &block
  end
end
install_plugin(name, version) click to toggle source
# File lib/conjur/command/plugin.rb, line 92
def install_plugin(name, version)
  uninstall_plugin(name) rescue Exception

  gem_name = name.start_with?('conjur-asset-') ? name : "conjur-asset-#{name}"

  cmd = Gem::Commands::InstallCommand.new
  cmd.handle_options ['--no-ri', '--no-rdoc', gem_name, '--version', "#{version}"]

  begin
    cmd.execute
  rescue Gem::SystemExitException => e
    if e.exit_code != 0
      raise e
    end
  end

  modify_plugin_list('add', name)
end
io() click to toggle source

default IO is standard output

# File lib/conjur/command/rspec/output_matchers.rb, line 51
def io
  @io ||= :stdout
end
modify_plugin_list(op, plugin_name) click to toggle source
# File lib/conjur/command/plugin.rb, line 123
def modify_plugin_list(op, plugin_name)
  config_found = false
  # Check the dedicated plugin config file first
  (Conjur::Config.plugin_config_files + Conjur::Config.default_config_files).uniq!.each do |f|
    if File.exists?(f) and not config_found  # only write to the first file found
      config_found = true
      config = YAML.load(IO.read(f)).stringify_keys rescue {}
      config['plugins'] ||= []
      config['plugins'] += [plugin_name] if op == 'add'
      config['plugins'] -= [plugin_name] if op == 'remove'
      config['plugins'].uniq!

      File.write(f, YAML.dump(config))
    end
  end
  exit_now! 'No Conjur config file found for plugin installation' unless config_found
end
uninstall_plugin(name) click to toggle source
# File lib/conjur/command/plugin.rb, line 111
def uninstall_plugin(name)
  if Conjur::Config.plugins.include?(name)
    gem_name = name.start_with?('conjur-asset-') ? name : "conjur-asset-#{name}"

    cmd = Gem::Commands::UninstallCommand.new
    cmd.handle_options ['-x', '-I', '-a', gem_name]
    cmd.execute

    modify_plugin_list('remove', name)
  end
end