module Remotus::Auth

Module containing remote authentication classes and modules

Public Class Methods

cache() click to toggle source

Gets the credential cache

@return [Hash{String => Remotus::Auth::Credential}] credential cache with hostname keys

# File lib/remotus/auth.rb, line 35
def self.cache
  @cache ||= {}
end
clear_cache() click to toggle source

Clears all entries in the credential cache

# File lib/remotus/auth.rb, line 42
def self.clear_cache
  @cache = {}
end
credential(connection, **options) click to toggle source

Gets authentication credentials for the given connection and options

@param [Remotus::SshConnection, Remotus::WinrmConnection, host] connection remote connection @param [Hash] options options hash

options may be used by different credential stores.

@return [Remotus::Auth::Credential] found credential

# File lib/remotus/auth.rb, line 20
def self.credential(connection, **options)
  # Only return cached credentials that have a populated user and password, otherwise attempt retrieval
  return cache[connection.host] if cache.key?(connection.host) && cache[connection.host].user && cache[connection.host].password

  found_credential = credential_from_stores(connection, **options)
  return found_credential if found_credential

  raise Remotus::MissingCredential, "Could not find credential for #{connection.host} in any credential store (#{stores.join(", ")})."
end
stores() click to toggle source

Gets the list of associated credential stores

@return [Array<Remotus::Auth::Store>] credential stores

# File lib/remotus/auth.rb, line 51
def self.stores
  @stores ||= []
end
stores=(stores) click to toggle source

Sets the list of associated credential stores

@param [Array<Remotus::Auth::Store>] stores credential stores

# File lib/remotus/auth.rb, line 60
def self.stores=(stores)
  @stores = stores
end

Private Class Methods

credential_from_stores(connection, **options) click to toggle source

Gets authentication credentials for the given connection and options from one of the credential stores

@param [Remotus::SshConnection, Remotus::WinrmConnection, host] connection remote connection @param [Hash] options options hash

options may be used by different credential stores.

@return [Remotus::Auth::Credential, nil] found credential or nil if the credential cannot be found

# File lib/remotus/auth.rb, line 76
def credential_from_stores(connection, **options)
  stores.each do |store|
    Remotus.logger.debug { "Gathering #{connection.host} credentials from #{store} credential store" }
    host_cred = store.credential(connection, **options)
    next unless host_cred

    Remotus.logger.debug { "#{connection.host} credentials found in #{store} credential store" }
    cache[connection.host] = host_cred
    return host_cred
  end
  nil
end