1 /* 2 * Copyright (C) 2020, Thomas Wolf <thomas.wolf@paranor.ch> and others 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the Eclipse Distribution License v. 1.0 which is available at 6 * https://www.eclipse.org/org/documents/edl-v10.php. 7 * 8 * SPDX-License-Identifier: BSD-3-Clause 9 */ 10 package org.eclipse.jgit.transport; 11 12 import java.util.Collections; 13 import java.util.List; 14 import java.util.Map; 15 16 import org.eclipse.jgit.annotations.NonNull; 17 18 /** 19 * An abstraction for a SSH config storage, like the OpenSSH ~/.ssh/config file. 20 * 21 * @since 5.8 22 */ 23 public interface SshConfigStore { 24 25 /** 26 * Locate the configuration for a specific host request. 27 * 28 * @param hostName 29 * to look up 30 * @param port 31 * the user supplied; <= 0 if none 32 * @param userName 33 * the user supplied, may be {@code null} or empty if none given 34 * @return the configuration for the requested name. 35 */ 36 @NonNull 37 HostConfig lookup(@NonNull String hostName, int port, String userName); 38 39 /** 40 * A host entry from the ssh config. Any merging of global values and of 41 * several matching host entries, %-substitutions, and ~ replacement have 42 * all been done. 43 */ 44 interface HostConfig { 45 46 /** 47 * Retrieves the value of a single-valued key, or the first if the key 48 * has multiple values. Keys are case-insensitive, so 49 * {@code getValue("HostName") == getValue("HOSTNAME")}. 50 * 51 * @param key 52 * to get the value of 53 * @return the value, or {@code null} if none 54 */ 55 String getValue(String key); 56 57 /** 58 * Retrieves the values of a multi- or list-valued key. Keys are 59 * case-insensitive, so 60 * {@code getValue("HostName") == getValue("HOSTNAME")}. 61 * 62 * @param key 63 * to get the values of 64 * @return a possibly empty list of values 65 */ 66 List<String> getValues(String key); 67 68 /** 69 * Retrieves an unmodifiable map of all single-valued options, with 70 * case-insensitive lookup by keys. 71 * 72 * @return all single-valued options 73 */ 74 @NonNull 75 Map<String, String> getOptions(); 76 77 /** 78 * Retrieves an unmodifiable map of all multi- or list-valued options, 79 * with case-insensitive lookup by keys. 80 * 81 * @return all multi-valued options 82 */ 83 @NonNull 84 Map<String, List<String>> getMultiValuedOptions(); 85 86 } 87 88 /** 89 * An empty {@link HostConfig}. 90 */ 91 static final HostConfig EMPTY_CONFIG = new HostConfig() { 92 93 @Override 94 public String getValue(String key) { 95 return null; 96 } 97 98 @Override 99 public List<String> getValues(String key) { 100 return Collections.emptyList(); 101 } 102 103 @Override 104 public Map<String, String> getOptions() { 105 return Collections.emptyMap(); 106 } 107 108 @Override 109 public Map<String, List<String>> getMultiValuedOptions() { 110 return Collections.emptyMap(); 111 } 112 113 }; 114 }