Class HostConfigEntry

java.lang.Object
org.apache.sshd.client.config.hosts.HostPatternsHolder
org.apache.sshd.client.config.hosts.HostConfigEntry
All Implemented Interfaces:
MutableUserHolder, UsernameHolder

public class HostConfigEntry extends HostPatternsHolder implements MutableUserHolder
Represents an entry in the client's configuration file as defined by the ssh_config configuration file format
Author:
Apache MINA SSHD Project
See Also:
  • Field Details

    • STD_CONFIG_FILENAME

      public static final String STD_CONFIG_FILENAME
      Standard OpenSSH config file name
      See Also:
    • HOST_CONFIG_PROP

      public static final String HOST_CONFIG_PROP
      See Also:
    • MATCH_CONFIG_PROP

      public static final String MATCH_CONFIG_PROP
      See Also:
    • HOST_NAME_CONFIG_PROP

      public static final String HOST_NAME_CONFIG_PROP
      See Also:
    • PORT_CONFIG_PROP

      public static final String PORT_CONFIG_PROP
      See Also:
    • USER_CONFIG_PROP

      public static final String USER_CONFIG_PROP
      See Also:
    • PROXY_JUMP_CONFIG_PROP

      public static final String PROXY_JUMP_CONFIG_PROP
      See Also:
    • IDENTITY_FILE_CONFIG_PROP

      public static final String IDENTITY_FILE_CONFIG_PROP
      See Also:
    • CERTIFICATE_FILE_CONFIG_PROP

      public static final String CERTIFICATE_FILE_CONFIG_PROP
      See Also:
    • LOCAL_FORWARD_CONFIG_PROP

      public static final String LOCAL_FORWARD_CONFIG_PROP
      See Also:
    • REMOTE_FORWARD_CONFIG_PROP

      public static final String REMOTE_FORWARD_CONFIG_PROP
      See Also:
    • SEND_ENV_CONFIG_PROP

      public static final String SEND_ENV_CONFIG_PROP
      See Also:
    • SET_ENV_CONFIG_PROP

      public static final String SET_ENV_CONFIG_PROP
      See Also:
    • PUBKEY_ACCEPTED_ALGORITHMS_CONFIG_PROP

      public static final String PUBKEY_ACCEPTED_ALGORITHMS_CONFIG_PROP
      See Also:
    • ADD_KEYS_TO_AGENT_CONFIG_PROP

      public static final String ADD_KEYS_TO_AGENT_CONFIG_PROP
      See Also:
    • CANONICAL_DOMAINS_CONFIG_PROP

      public static final String CANONICAL_DOMAINS_CONFIG_PROP
      See Also:
    • GLOBAL_KNOWN_HOSTS_CONFIG_PROP

      public static final String GLOBAL_KNOWN_HOSTS_CONFIG_PROP
      See Also:
    • USER_KNOWN_HOSTS_CONFIG_PROP

      public static final String USER_KNOWN_HOSTS_CONFIG_PROP
      See Also:
    • EXCLUSIVE_IDENTITIES_CONFIG_PROP

      public static final String EXCLUSIVE_IDENTITIES_CONFIG_PROP
      Use only the identities specified in the host entry (if any)
      See Also:
    • DEFAULT_EXCLUSIVE_IDENTITIES

      public static final boolean DEFAULT_EXCLUSIVE_IDENTITIES
      See Also:
    • IDENTITY_AGENT

      public static final String IDENTITY_AGENT
      The IdentityAgent configuration. If not set in the HostConfigEntry, the value of this property is null, which means that a default SSH agent is to be used, if it is running. Other values defined by OpenSSH are:
        none
        No SHH agent is to be used at all, even if one is running.
        SSH_AUTH_SOCK
        The SSH agent listening on the Unix domain socket given by the environment variable SSH_AUTH_SOCK shall be used. If the environment variable is not set, no SSH agent is used.
        other
        For OpenSSH, the value shall resolve to the file name of a Unix domain socket to use to connect to an SSH agent.
      See Also:
    • MULTI_VALUE_SEPARATORS

      public static final String MULTI_VALUE_SEPARATORS
      See Also:
    • PATH_MACRO_CHAR

      public static final char PATH_MACRO_CHAR
      See Also:
    • LOCAL_HOME_MACRO

      public static final char LOCAL_HOME_MACRO
      See Also:
    • LOCAL_USER_MACRO

      public static final char LOCAL_USER_MACRO
      See Also:
    • LOCAL_HOST_MACRO

      public static final char LOCAL_HOST_MACRO
      See Also:
    • REMOTE_HOST_MACRO

      public static final char REMOTE_HOST_MACRO
      See Also:
    • REMOTE_USER_MACRO

      public static final char REMOTE_USER_MACRO
      See Also:
    • REMOTE_PORT_MACRO

      public static final char REMOTE_PORT_MACRO
      See Also:
    • ADDITIVE_KEYS

      public static final Set<String> ADDITIVE_KEYS
      Unmodifiable set of OpenSSH config file keys that can be specified multiple times building up a list. All other keys follow a "first match wins" rule.
    • LIST_KEYS

      public static final Set<String> LIST_KEYS
      Unmodifiable set of OpenSSH config file keys that take a whitespace-separated list of values.
    • KEY_ALIASES

      public static final Map<String,String> KEY_ALIASES
      A modifiable map of config key aliases, mapping aliases to a canonical name. Keys are aliases, values are the canonical names.
    • hostPatterns

      protected String hostPatterns
    • properties

      protected final Map<String, List<String>> properties
  • Constructor Details

    • HostConfigEntry

      public HostConfigEntry()
    • HostConfigEntry

      public HostConfigEntry(String pattern, String host, int port, String username)
    • HostConfigEntry

      public HostConfigEntry(String pattern, String host, int port, String username, String proxyJump)
  • Method Details

    • collate

      public void collate(HostConfigEntry that)
      Merges that into this via underride. That is, any value present in this entry takes precedence over the given entry. Only this object is modified. The given entry remains unchanged.
      Parameters:
      that - The HostConfigEntry to merge.
    • getHost

      public String getHost()
      Returns:
      The pattern(s) represented by this entry
    • setHost

      public void setHost(String host)
    • setHost

      public void setHost(Collection<String> patterns)
    • getHostName

      public String getHostName()
      Returns:
      The effective host name to connect to if the pattern matches
    • setHostName

      public void setHostName(String hostName)
    • getPort

      public int getPort()
      Returns:
      A port override - if positive
    • setPort

      public void setPort(int port)
    • getUsername

      public String getUsername()
      Specified by:
      getUsername in interface UsernameHolder
      Returns:
      A username override - if not null/empty
    • setUsername

      public void setUsername(String username)
      Specified by:
      setUsername in interface MutableUserHolder
    • getProxyJump

      public String getProxyJump()
      Returns:
      the host to use as a proxy
    • setProxyJump

      public void setProxyJump(String proxyJump)
    • getIdentities

      public Collection<String> getIdentities()
      Returns:
      The current identities file paths; may be empty
    • addIdentity

      public void addIdentity(Path path)
      Parameters:
      path - A Path to a file that contains an identity key - never null
    • addIdentity

      public void addIdentity(String id)
      Adds a path to an identity file
      Parameters:
      id - The identity path to add - never null
    • setIdentities

      public void setIdentities(Collection<String> identities)
    • isIdentitiesOnly

      public boolean isIdentitiesOnly()
      Returns:
      true if must use only the identities in this entry
    • setIdentitiesOnly

      public void setIdentitiesOnly(boolean identitiesOnly)
    • getProperties

      public Map<String, List<String>> getProperties()
      Retrieves the raw Map of properties.
    • clear

      public void clear()
    • getValues

      public List<String> getValues(String name)
      Retrieves all the values of a property. If the property an additive or list-valued property, the result may contain more than one value.
      Parameters:
      name - of the property to get the values of; must not be null or empty
      Returns:
      the values, as an unmodifiable list, may be null
    • getProperty

      public String getProperty(String name)
      Retrieves the single value of a property. If called for an additive or list-valued key that has several values, only the first one is returned.
      Parameters:
      name - of the property to get the values of; must not be null or empty
      Returns:
      the property value or null if not set
      See Also:
    • getProperty

      public String getProperty(String name, String defaultValue)
      Retrieves the single value of a property. If called for an additive or list-valued key that has several values, only the first one is returned. If the property is not set, returns the given defaultValue.
      Parameters:
      name - of the property to get the values of; must not be null or empty
      defaultValue - value to return if not set, may be null
      Returns:
      the property value or defaultValue if not set
    • setProperty

      public void setProperty(String name, String value)
      Sets or replaces the property value. If the value is null or empty, the property is removed. Otherwise, if it's a additive property, the new value is added to any previously added values. Otherwise, the existing value is replaced by the new value.
      Parameters:
      name - of the property to set the value; must not be null or empty
      value - to set; if null or empty, the property is removed
    • setProperty

      public void setProperty(String name, List<String> value)
      Sets or replaces the property value. If the value is null or empty, the property is removed. Otherwise, the existing value is replaced by the new value.
      Parameters:
      name - of the property to set the value; must not be null or empty
      value - to set; if null or empty, the property is removed
    • removeProperty

      public List<String> removeProperty(String name)
      Removes a property.
      Parameters:
      name - Property name - never null/empty
      Returns:
      The removed property value - null if no such property name
    • append

      public <A extends Appendable> A append(A sb) throws IOException
      Writes a string representation with each property on a line to the given Appendable, using System.lineSeparator() to end each line.
      Type Parameters:
      A - The Appendable type
      Parameters:
      sb - The Appendable to write to
      Returns:
      sb
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • appendNonEmptyProperties

      public static <A extends Appendable> A appendNonEmptyProperties(A sb, Map<String, List<String>> props) throws IOException
      Appends the properties.
      Type Parameters:
      A - The Appendable type
      Parameters:
      sb - The target appender
      props - The Map of properties - ignored if null/empty
      Returns:
      the target appender
      Throws:
      IOException
    • appendNonEmptyProperty

      public static <A extends Appendable> A appendNonEmptyProperty(A sb, String name, List<String> value) throws IOException
      Type Parameters:
      A - The Appendable type
      Parameters:
      sb - The target appender
      name - The property name - never null/empty
      value - The property value - ignored if null. Note: if the string representation of the value contains any commas, they are assumed to indicate a multi-valued property which is broken down to individual lines - one per value.
      Returns:
      The target appender after having appended (or not) the value
      Throws:
      IOException
    • appendValue

      public static <A extends Appendable> A appendValue(A sb, String value) throws IOException
      Throws:
      IOException
    • findMatchingEntries

      public static List<HostConfigEntry> findMatchingEntries(String host, HostConfigEntry... entries)
      Locates all the matching entries for a give host name / address
      Parameters:
      host - The host name / address - ignored if null/empty
      entries - The HostConfigEntry-ies to scan - ignored if null/empty
      Returns:
      A List of all the matching entries
      See Also:
    • findMatchingEntries

      public static List<HostConfigEntry> findMatchingEntries(String host, Collection<? extends HostConfigEntry> entries)
      Locates all the matching entries for a give host name / address
      Parameters:
      host - The host name / address - ignored if null/empty
      entries - The HostConfigEntry-ies to scan - ignored if null/empty
      Returns:
      A List of all the matching entries
      See Also:
    • toHostConfigEntryResolver

      public static HostConfigEntryResolver toHostConfigEntryResolver(Collection<? extends HostConfigEntry> entries)
      Parameters:
      entries - The entries - ignored if null/empty
      Returns:
      A HostConfigEntryResolver wrapper using the entries
    • readHostConfigEntries

      public static List<HostConfigEntry> readHostConfigEntries(Path path, OpenOption... options) throws IOException
      Throws:
      IOException
    • readHostConfigEntries

      public static List<HostConfigEntry> readHostConfigEntries(URL url) throws IOException
      Throws:
      IOException
    • readHostConfigEntries

      public static List<HostConfigEntry> readHostConfigEntries(InputStream inStream, boolean okToClose) throws IOException
      Throws:
      IOException
    • readHostConfigEntries

      public static List<HostConfigEntry> readHostConfigEntries(Reader rdr, boolean okToClose) throws IOException
      Throws:
      IOException
    • readHostConfigEntries

      public static List<HostConfigEntry> readHostConfigEntries(BufferedReader rdr) throws IOException
      Reads configuration entries
      Parameters:
      rdr - The BufferedReader to use
      Returns:
      The List of read HostConfigEntry-ies
      Throws:
      IOException - If failed to parse the read configuration
    • parseList

      public static List<String> parseList(String argument)
      Splits the argument into a list of whitespace-separated elements. Elements containing whitespace must be quoted and will be de-quoted. Backslash-escapes are handled for quotes and blanks.
      Parameters:
      argument - argument part of the configuration line as read from the config file
      Returns:
      a List of elements, possibly empty and possibly containing empty elements, but not containing null
    • parseToken

      public static int parseToken(String argument, int from, int to, List<String> result)
      Parses a token up to the next whitespace not inside a string quoted by single or double quotes. Inside a string, quotes can be escaped by backslash characters. Outside of a string, "\ " can be used to include a space in a token; inside a string "\ " is taken literally as '\' followed by ' '.
      Parameters:
      argument - to parse the token out of
      from - index at the beginning of the token
      to - index one after the last character to look at
      result - a list collecting tokens to which the parsed token is added
      Returns:
      the index after the token
    • writeHostConfigEntries

      public static void writeHostConfigEntries(Path path, Collection<? extends HostConfigEntry> entries, OpenOption... options) throws IOException
      Throws:
      IOException
    • writeHostConfigEntries

      public static void writeHostConfigEntries(OutputStream outputStream, boolean okToClose, Collection<? extends HostConfigEntry> entries) throws IOException
      Throws:
      IOException
    • appendHostConfigEntries

      public static <A extends Appendable> A appendHostConfigEntries(A sb, Collection<? extends HostConfigEntry> entries) throws IOException
      Writes all given entries to the given Appendable.
      Type Parameters:
      A - The Appendable type
      Parameters:
      sb - The Appendable to write to
      entries - the entries to write
      Returns:
      sb
      Throws:
      IOException
    • parseConfigValue

      public static List<String> parseConfigValue(String value)
      Parses a host config value in a list of whitespace-separated elements, handling OpenSSH-style quoting.
      Parameters:
      value - The value - ignored if null/empty (after trimming)
      Returns:
      A List of the encountered values
    • resolveIdentityFilePath

      public static String resolveIdentityFilePath(String id, String host, int port, String username) throws IOException
      Throws:
      IOException
    • getDefaultHostConfigFile

      public static Path getDefaultHostConfigFile()
      Returns:
      The default Path location of the OpenSSH hosts entries configuration file