1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*
* Copyright (C) 2020, Thomas Wolf <thomas.wolf@paranor.ch> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.transport;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.eclipse.jgit.annotations.NonNull;
/**
* An abstraction for a SSH config storage, like the OpenSSH ~/.ssh/config file.
*
* @since 5.8
*/
public interface SshConfigStore {
/**
* Locate the configuration for a specific host request.
*
* @param hostName
* to look up
* @param port
* the user supplied; <= 0 if none
* @param userName
* the user supplied, may be {@code null} or empty if none given
* @return the configuration for the requested name.
*/
@NonNull
HostConfig lookup(@NonNull String hostName, int port, String userName);
/**
* Locate the configuration for a specific host request and if the
* configuration has no values for {@link SshConstants#HOST_NAME},
* {@link SshConstants#PORT}, {@link SshConstants#USER}, or
* {@link SshConstants#CONNECTION_ATTEMPTS}, fill those values with defaults
* from the arguments:
* <table>
* <caption>Description of arguments</caption>
* <tr>
* <th>ssh config key</th>
* <th>value from argument</th>
* </tr>
* <tr>
* <td>{@code HostName}</td>
* <td>{@code hostName}</td>
* </tr>
* <tr>
* <td>{@code Port}</td>
* <td>{@code port > 0 ? port : 22}</td>
* </tr>
* <tr>
* <td>{@code User}</td>
* <td>{@code userName}</td>
* </tr>
* <tr>
* <td>{@code ConnectionAttempts}</td>
* <td>{@code 1}</td>
* </tr>
* </table>
*
* @param hostName
* host name to look up
* @param port
* port number; <= 0 if none
* @param userName
* the user name, may be {@code null} or empty if none given
* @return the configuration for the requested name.
* @since 6.0
*/
@NonNull
HostConfig lookupDefault(@NonNull String hostName, int port,
String userName);
/**
* A host entry from the ssh config. Any merging of global values and of
* several matching host entries, %-substitutions, and ~ replacement have
* all been done.
*/
interface HostConfig {
/**
* Retrieves the value of a single-valued key, or the first if the key
* has multiple values. Keys are case-insensitive, so
* {@code getValue("HostName") == getValue("HOSTNAME")}.
*
* @param key
* to get the value of
* @return the value, or {@code null} if none
*/
String getValue(String key);
/**
* Retrieves the values of a multi- or list-valued key. Keys are
* case-insensitive, so
* {@code getValue("HostName") == getValue("HOSTNAME")}.
*
* @param key
* to get the values of
* @return a possibly empty list of values
*/
List<String> getValues(String key);
/**
* Retrieves an unmodifiable map of all single-valued options, with
* case-insensitive lookup by keys.
*
* @return all single-valued options
*/
@NonNull
Map<String, String> getOptions();
/**
* Retrieves an unmodifiable map of all multi- or list-valued options,
* with case-insensitive lookup by keys.
*
* @return all multi-valued options
*/
@NonNull
Map<String, List<String>> getMultiValuedOptions();
}
/**
* An empty {@link HostConfig}.
*/
static final HostConfig EMPTY_CONFIG = new HostConfig() {
@Override
public String getValue(String key) {
return null;
}
@Override
public List<String> getValues(String key) {
return Collections.emptyList();
}
@Override
public Map<String, String> getOptions() {
return Collections.emptyMap();
}
@Override
public Map<String, List<String>> getMultiValuedOptions() {
return Collections.emptyMap();
}
};
}
|