Top Description Inners Fields Constructors Methods
org.eclipse.jgit.internal.transport.ssh

public Class OpenSshConfigFile

extends Object
implements SshConfigStore
Class Inheritance
All Implemented Interfaces
org.eclipse.jgit.transport.SshConfigStore
Static Imports
java.nio.charset.StandardCharsets.UTF_8

Fairly complete configuration parser for the openssh ~/.ssh/config file.

Both JSch 0.1.54 and Apache MINA sshd 2.1.0 have parsers for this, but both are buggy. Therefore we implement our own parser to read an openssh configuration file.

Limitations compared to the full openssh 7.5 parser:

Note that openssh's readconf.c is a validating parser; this parser does not validate entries.

This config does %-substitutions for the following tokens:

%i is not handled; Java has no concept of a "user ID". %T is always replaced by NONE.

See Also
man ssh-config

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
OpenSshConfigFile.HostEntry

A host entry from the ssh config file.

private static class
private static class
OpenSshConfigFile.State

Encapsulates entries read out of the configuration file, and a cache of fully resolved entries created from that.

Field Summary

Modifier and TypeField and Description
private final File
configFile

The .ssh/config file we read and monitor for updates.

private final File
home

The user's home directory, as key files may be relative to here.

private Instant
lastModified

Modification time of configFile when it was last loaded.

private final String
localUserName

User name of the user on the host OS.

private OpenSshConfigFile.State
state

State read from the config file, plus the cache.

Constructor Summary

AccessConstructor and Description
public
OpenSshConfigFile(File
user's home directory for the purpose of ~ replacement
home
,
File
file to load.
config
,
String
user name of the current user on the local host OS
localUserName
)

Creates a new OpenSshConfigFile that will read the config from file config use the given file home as "home" directory.

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true if value is "yes", "on", or "true"; false otherwise
flag
(String
to convert
value
)

Converts a ssh config flag value (yes/true/on - no/false/off) into an boolean.

public String

Returns:

the user name
getLocalUserName
()

Retrieves the local user name as given in the constructor.

public OpenSshConfigFile.HostEntry

Returns:

the configuration for the requested name.
lookup
(String
the name the user has supplied to the SSH tool. This may be a real host name, or it may just be a "Host" block in the configuration file.
hostName
,
int
the user supplied; <= 0 if none
port
,
String
the user supplied, may be null or empty if none given
userName
)

Implements org.eclipse.jgit.transport.SshConfigStore.lookup.

Locate the configuration for a specific host request.

private OpenSshConfigFile.HostEntry
lookup(String hostName, int port, String userName, boolean fillDefaults)

public OpenSshConfigFile.HostEntry
lookupDefault(String
host name to look up
hostName
,
int
port number; <= 0 if none
port
,
String
the user name, may be null or empty if none given
userName
)

Implements org.eclipse.jgit.transport.SshConfigStore.lookupDefault.

Locate the configuration for a specific host request and if the configuration has no values for SshConstants#HOST_NAME, SshConstants#PORT, SshConstants#USER, or SshConstants#CONNECTION_ATTEMPTS, fill those values with defaults from the arguments:

Description of arguments ssh config key value from argument HostNamehostNamePortport > 0 ?
private List<OpenSshConfigFile.HostEntry>
private static List<String>

Returns:

a List of elements, possibly empty and possibly containing empty elements, but not containing null
parseList
(String
argument part of the configuration line as read from the config file
argument
)

Splits the argument into a list of whitespace-separated elements.

private static int

Returns:

the index after the token
parseToken
(String
to parse the token out of
argument
,
int
index at the beginning of the token
from
,
int
index one after the last character to look at
to
,
List<String>
a list collecting tokens to which the parsed token is added
result
)

Parses a token up to the next whitespace not inside a string quoted by single or double quotes.

public static boolean

Returns:

true if the name matches at least one of the non-negative patterns and none of the negative patterns, false otherwise
patternMatch
(Iterable<String>
to test name against; any pattern starting with an exclamation mark is a negative pattern
patterns
,
String
to test
name
)

Tells whether a given name matches the given list of patterns, accounting for negative matches.

private static boolean
patternMatches(String pattern, String name)

public static int

Returns:

the value, or -1 if it wasn't a positive integral value
positive
(String
to convert
value
)

Converts a positive value into an int.

private synchronized OpenSshConfigFile.State
private static String
public static int

Returns:

the parsed value as a number of seconds, or -1 if the value is not a valid OpenSSH time value
timeSpec
(String
to convert
value
)

Converts an OpenSSH time value into a number of seconds.

private String
toCacheKey(String hostName, int port, String userName)

private static File
toFile(String path, File home)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

protected String

Returns:

the validated and possibly sanitized value
validate
(String
of the entry
key
,
String
as read from the config file
value
)

Hook to perform validation on a single value, or to sanitize it.

protected List<String>

Returns:

a List of values, possibly empty and possibly containing empty elements, but not containing null
validate
(String
of the entry
key
,
List<String>
list of arguments as read from the config file
value
)

Hook to perform validation on values, or to sanitize them.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

configFileback to summary
private final File configFile

The .ssh/config file we read and monitor for updates.

homeback to summary
private final File home

The user's home directory, as key files may be relative to here.

lastModifiedback to summary
private Instant lastModified

Modification time of configFile when it was last loaded.

localUserNameback to summary
private final String localUserName

User name of the user on the host OS.

stateback to summary
private OpenSshConfigFile.State state

State read from the config file, plus the cache.

Constructor Detail

OpenSshConfigFileback to summary
public OpenSshConfigFile(File home, File config, String localUserName)

Creates a new OpenSshConfigFile that will read the config from file config use the given file home as "home" directory.

Parameters
home:File

user's home directory for the purpose of ~ replacement

config:File

file to load.

localUserName:String

user name of the current user on the local host OS

Method Detail

flagback to summary
public static boolean flag(String value)

Converts a ssh config flag value (yes/true/on - no/false/off) into an boolean.

Parameters
value:String

to convert

Returns:boolean

true if value is "yes", "on", or "true"; false otherwise

getLocalUserNameback to summary
public String getLocalUserName()

Retrieves the local user name as given in the constructor.

Returns:String

the user name

lookupback to summary
public OpenSshConfigFile.HostEntry lookup(String hostName, int port, String userName)

Implements org.eclipse.jgit.transport.SshConfigStore.lookup.

Locate the configuration for a specific host request.

Parameters
hostName:String

the name the user has supplied to the SSH tool. This may be a real host name, or it may just be a "Host" block in the configuration file.

port:int

the user supplied; <= 0 if none

userName:String

the user supplied, may be null or empty if none given

Returns:OpenSshConfigFile.HostEntry

the configuration for the requested name.

Annotations
@Override
@NonNull
lookupback to summary
private OpenSshConfigFile.HostEntry lookup(String hostName, int port, String userName, boolean fillDefaults)
lookupDefaultback to summary
public OpenSshConfigFile.HostEntry lookupDefault(String hostName, int port, String userName)

Implements org.eclipse.jgit.transport.SshConfigStore.lookupDefault.

Doc from org.eclipse.jgit.transport.SshConfigStore.lookupDefault.

Locate the configuration for a specific host request and if the configuration has no values for SshConstants#HOST_NAME, SshConstants#PORT, SshConstants#USER, or SshConstants#CONNECTION_ATTEMPTS, fill those values with defaults from the arguments:

Description of arguments
ssh config key value from argument
HostName hostName
Port port > 0 ? port : 22
User userName
ConnectionAttempts 1
Parameters
hostName:String

host name to look up

port:int

port number; <= 0 if none

userName:String

the user name, may be null or empty if none given

Returns:OpenSshConfigFile.HostEntry

the configuration for the requested name.

Annotations
@Override
@NonNull
parseback to summary
private List<OpenSshConfigFile.HostEntry> parse(BufferedReader reader) throws IOException
parseListback to summary
private 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:String

argument part of the configuration line as read from the config file

Returns:List<String>

a List of elements, possibly empty and possibly containing empty elements, but not containing null

parseTokenback to summary
private 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:String

to parse the token out of

from:int

index at the beginning of the token

to:int

index one after the last character to look at

result:List<String>

a list collecting tokens to which the parsed token is added

Returns:int

the index after the token

patternMatchback to summary
public static boolean patternMatch(Iterable<String> patterns, String name)

Tells whether a given name matches the given list of patterns, accounting for negative matches.

Parameters
patterns:Iterable<String>

to test name against; any pattern starting with an exclamation mark is a negative pattern

name:String

to test

Returns:boolean

true if the name matches at least one of the non-negative patterns and none of the negative patterns, false otherwise

Since
7.1
patternMatchesback to summary
private static boolean patternMatches(String pattern, String name)
positiveback to summary
public static int positive(String value)

Converts a positive value into an int.

Parameters
value:String

to convert

Returns:int

the value, or -1 if it wasn't a positive integral value

refreshback to summary
private synchronized OpenSshConfigFile.State refresh()
stripWhitespaceback to summary
private static String stripWhitespace(String value)
timeSpecback to summary
public static int timeSpec(String value)

Converts an OpenSSH time value into a number of seconds. The format is defined by OpenSSH as a sequence of (positive) integers with suffixes for seconds, minutes, hours, days, and weeks.

Parameters
value:String

to convert

Returns:int

the parsed value as a number of seconds, or -1 if the value is not a valid OpenSSH time value

See Also
OpenBSD man 5 sshd_config, section TIME FORMATS
toCacheKeyback to summary
private String toCacheKey(String hostName, int port, String userName)
Annotations
@NonNull
toFileback to summary
private static File toFile(String path, File home)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
@SuppressWarnings:nls
validateback to summary
protected String validate(String key, String value)

Hook to perform validation on a single value, or to sanitize it. If this throws an (unchecked) exception, parsing of the file is abandoned.

Parameters
key:String

of the entry

value:String

as read from the config file

Returns:String

the validated and possibly sanitized value

validateback to summary
protected List<String> validate(String key, List<String> value)

Hook to perform validation on values, or to sanitize them. If this throws an (unchecked) exception, parsing of the file is abandoned.

Parameters
key:String

of the entry

value:List<String>

list of arguments as read from the config file

Returns:List<String>

a List of values, possibly empty and possibly containing empty elements, but not containing null

org.eclipse.jgit.internal.transport.ssh back to summary

public Class OpenSshConfigFile.HostEntry

extends Object
implements HostConfig
Class Inheritance
  • java.lang.Object
  • org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.HostEntry
All Implemented Interfaces
org.eclipse.jgit.transport.SshConfigStore.HostConfig

A host entry from the ssh config file. Any merging of global values and of several matching host entries, %-substitutions, and ~ replacement have all been done.

Field Summary

Modifier and TypeField and Description
private static final Map<String, String>
ALIASES

OpenSSH has renamed some config keys.

private static final Set<String>
LIST_KEYS

Keys that take a whitespace-separated list of elements as argument.

private Map<String, List<String>>
private static final Set<String>
MULTI_KEYS

Keys that can be specified multiple times, building up a list.

private Map<String, List<String>>
private Map<String, String>
private final List<String>

Constructor Summary

AccessConstructor and Description
public
HostEntry()

Constructor used to build the merged entry; never matches anything

public
HostEntry(List<String>
to be used in matching against host name.
patterns
)

Method Summary

Modifier and TypeMethod and Description
public Map<String, List<String>>

Returns:

all multi-valued options
getMultiValuedOptions
()

Implements org.eclipse.jgit.transport.SshConfigStore.HostConfig.getMultiValuedOptions.

Retrieves an unmodifiable map of all multi-valued options, with case-insensitive lookup by keys.

public Map<String, String>

Returns:

all single-valued options
getOptions
()

Implements org.eclipse.jgit.transport.SshConfigStore.HostConfig.getOptions.

Retrieves an unmodifiable map of all single-valued options, with case-insensitive lookup by keys.

public String

Returns:

the value, or null if none
getValue
(String
to get the value of
key
)

Implements org.eclipse.jgit.transport.SshConfigStore.HostConfig.getValue.

Retrieves the value of a single-valued key, or the first if the key has multiple values.

public List<String>

Returns:

a possibly empty list of values
getValues
(String
to get the values of
key
)

Implements org.eclipse.jgit.transport.SshConfigStore.HostConfig.getValues.

Retrieves the values of a multi or list-valued key.

public static boolean

Returns:

true if the key is a list-valued key.
isListKey
(String
to check
key
)

Does the key take a whitespace-separated list of values?

pack-priv boolean
matches(String hostName)

pack-priv void
private List<String>
replaceTilde(List<String> values, File home)

public void
setValue(String
to modify
key
,
String
to set or add
value
)

Sets the value of a single-valued key if it not set yet, or adds a value to a multi-valued key.

public void
setValue(String
to set
key
,
List<String>
a non-empty list of values
values
)

Sets the values of a multi- or list-valued key.

private List<String>
substitute(List<String> values, String allowed, OpenSshConfigFile.Replacer r, boolean withEnv)

pack-priv void
substitute(String originalHostName, int port, String userName, String localUserName, File home, boolean fillDefaults)

private static String
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

ALIASESback to summary
private static final Map<String, String> ALIASES

OpenSSH has renamed some config keys. This maps old names to new names.

LIST_KEYSback to summary
private static final Set<String> LIST_KEYS

Keys that take a whitespace-separated list of elements as argument. Because the dequote-handling is different, we must handle those in the parser. There are a few other keys that take comma-separated lists as arguments, but for the parser those are single arguments that must be quoted if they contain whitespace, and taking them apart is the responsibility of the user of those keys.

listOptionsback to summary
private Map<String, List<String>> listOptions
MULTI_KEYSback to summary
private static final Set<String> MULTI_KEYS

Keys that can be specified multiple times, building up a list. (I.e., those are the keys that do not follow the general rule of "first occurrence wins".)

multiOptionsback to summary
private Map<String, List<String>> multiOptions
optionsback to summary
private Map<String, String> options
patternsback to summary
private final List<String> patterns

Constructor Detail

HostEntryback to summary
public HostEntry()

Constructor used to build the merged entry; never matches anything

HostEntryback to summary
public HostEntry(List<String> patterns)
Parameters
patterns:List<String>

to be used in matching against host name.

Method Detail

getMultiValuedOptionsback to summary
public Map<String, List<String>> getMultiValuedOptions()

Implements org.eclipse.jgit.transport.SshConfigStore.HostConfig.getMultiValuedOptions.

Retrieves an unmodifiable map of all multi-valued options, with case-insensitive lookup by keys.

Returns:Map<String, List<String>>

all multi-valued options

Annotations
@Override
@NonNull
getOptionsback to summary
public Map<String, String> getOptions()

Implements org.eclipse.jgit.transport.SshConfigStore.HostConfig.getOptions.

Retrieves an unmodifiable map of all single-valued options, with case-insensitive lookup by keys.

Returns:Map<String, String>

all single-valued options

Annotations
@Override
@NonNull
getValueback to summary
public String getValue(String key)

Implements org.eclipse.jgit.transport.SshConfigStore.HostConfig.getValue.

Retrieves the value of a single-valued key, or the first if the key has multiple values. Keys are case-insensitive, so getValue("HostName") == getValue("HOSTNAME").

Parameters
key:String

to get the value of

Returns:String

the value, or null if none

Annotations
@Override
getValuesback to summary
public List<String> getValues(String key)

Implements org.eclipse.jgit.transport.SshConfigStore.HostConfig.getValues.

Retrieves the values of a multi or list-valued key. Keys are case-insensitive, so getValue("HostName") == getValue("HOSTNAME").

Parameters
key:String

to get the values of

Returns:List<String>

a possibly empty list of values

Annotations
@Override
isListKeyback to summary
public static boolean isListKey(String key)

Does the key take a whitespace-separated list of values?

Parameters
key:String

to check

Returns:boolean

true if the key is a list-valued key.

matchesback to summary
pack-priv boolean matches(String hostName)
mergeback to summary
pack-priv void merge(OpenSshConfigFile.HostEntry entry)
replaceTildeback to summary
private List<String> replaceTilde(List<String> values, File home)
setValueback to summary
public void setValue(String key, String value)

Sets the value of a single-valued key if it not set yet, or adds a value to a multi-valued key. If the value is null, the key is removed altogether, whether it is single-, list-, or multi-valued.

Parameters
key:String

to modify

value:String

to set or add

setValueback to summary
public void setValue(String key, List<String> values)

Sets the values of a multi- or list-valued key.

Parameters
key:String

to set

values:List<String>

a non-empty list of values

substituteback to summary
private List<String> substitute(List<String> values, String allowed, OpenSshConfigFile.Replacer r, boolean withEnv)
substituteback to summary
pack-priv void substitute(String originalHostName, int port, String userName, String localUserName, File home, boolean fillDefaults)
toKeyback to summary
private static String toKey(String key)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
@SuppressWarnings:nls
org.eclipse.jgit.internal.transport.ssh back to summary

private Class OpenSshConfigFile.Replacer

extends Object
Class Inheritance
  • java.lang.Object
  • org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.Replacer

Field Summary

Modifier and TypeField and Description
public static final String
DEFAULT_TOKENS

Tokens applicable to most keys.

private final Map<Character, String>

Constructor Summary

AccessConstructor and Description
public
Replacer(String host, int port, String user, String localUserName, File home)

Method Summary

Modifier and TypeMethod and Description
public String
substitute(String input, String allowed, boolean withEnv)

public void
update(char key, String value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_TOKENSback to summary
public static final String DEFAULT_TOKENS

Tokens applicable to most keys.

See Also
man ssh_config
replacementsback to summary
private final Map<Character, String> replacements

Constructor Detail

Replacerback to summary
public Replacer(String host, int port, String user, String localUserName, File home)

Method Detail

substituteback to summary
public String substitute(String input, String allowed, boolean withEnv)
updateback to summary
public void update(char key, String value)
org.eclipse.jgit.internal.transport.ssh back to summary

private Class OpenSshConfigFile.State

extends Object
Class Inheritance
  • java.lang.Object
  • org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.State

Encapsulates entries read out of the configuration file, and a cache of fully resolved entries created from that.

Field Summary

Modifier and TypeField and Description
pack-priv List<OpenSshConfigFile.HostEntry>
pack-priv Map<String, OpenSshConfigFile.HostEntry>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

entriesback to summary
pack-priv List<OpenSshConfigFile.HostEntry> entries
hostsback to summary
pack-priv Map<String, OpenSshConfigFile.HostEntry> hosts

Constructor Detail

Stateback to summary
private State()

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
@SuppressWarnings:nls