SshConfigStore.java

  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. import java.util.Collections;
  12. import java.util.List;
  13. import java.util.Map;

  14. import org.eclipse.jgit.annotations.NonNull;

  15. /**
  16.  * An abstraction for a SSH config storage, like the OpenSSH ~/.ssh/config file.
  17.  *
  18.  * @since 5.8
  19.  */
  20. public interface SshConfigStore {

  21.     /**
  22.      * Locate the configuration for a specific host request.
  23.      *
  24.      * @param hostName
  25.      *            to look up
  26.      * @param port
  27.      *            the user supplied; &lt;= 0 if none
  28.      * @param userName
  29.      *            the user supplied, may be {@code null} or empty if none given
  30.      * @return the configuration for the requested name.
  31.      */
  32.     @NonNull
  33.     HostConfig lookup(@NonNull String hostName, int port, String userName);

  34.     /**
  35.      * Locate the configuration for a specific host request and if the
  36.      * configuration has no values for {@link SshConstants#HOST_NAME},
  37.      * {@link SshConstants#PORT}, {@link SshConstants#USER}, or
  38.      * {@link SshConstants#CONNECTION_ATTEMPTS}, fill those values with defaults
  39.      * from the arguments:
  40.      * <table>
  41.      * <tr>
  42.      * <th>ssh config key</th>
  43.      * <th>value from argument</th>
  44.      * </tr>
  45.      * <tr>
  46.      * <td>{@code HostName}</td>
  47.      * <td>{@code hostName}</td>
  48.      * </tr>
  49.      * <tr>
  50.      * <td>{@code Port}</td>
  51.      * <td>{@code port > 0 ? port : 22}</td>
  52.      * </tr>
  53.      * <tr>
  54.      * <td>{@code User}</td>
  55.      * <td>{@code userName}</td>
  56.      * </tr>
  57.      * <tr>
  58.      * <td>{@code ConnectionAttempts}</td>
  59.      * <td>{@code 1}</td>
  60.      * </tr>
  61.      * </table>
  62.      *
  63.      * @param hostName
  64.      *            host name to look up
  65.      * @param port
  66.      *            port number; &lt;= 0 if none
  67.      * @param userName
  68.      *            the user name, may be {@code null} or empty if none given
  69.      * @return the configuration for the requested name.
  70.      * @since 6.0
  71.      */
  72.     @NonNull
  73.     HostConfig lookupDefault(@NonNull String hostName, int port,
  74.             String userName);

  75.     /**
  76.      * A host entry from the ssh config. Any merging of global values and of
  77.      * several matching host entries, %-substitutions, and ~ replacement have
  78.      * all been done.
  79.      */
  80.     interface HostConfig {

  81.         /**
  82.          * Retrieves the value of a single-valued key, or the first if the key
  83.          * has multiple values. Keys are case-insensitive, so
  84.          * {@code getValue("HostName") == getValue("HOSTNAME")}.
  85.          *
  86.          * @param key
  87.          *            to get the value of
  88.          * @return the value, or {@code null} if none
  89.          */
  90.         String getValue(String key);

  91.         /**
  92.          * Retrieves the values of a multi- or list-valued key. Keys are
  93.          * case-insensitive, so
  94.          * {@code getValue("HostName") == getValue("HOSTNAME")}.
  95.          *
  96.          * @param key
  97.          *            to get the values of
  98.          * @return a possibly empty list of values
  99.          */
  100.         List<String> getValues(String key);

  101.         /**
  102.          * Retrieves an unmodifiable map of all single-valued options, with
  103.          * case-insensitive lookup by keys.
  104.          *
  105.          * @return all single-valued options
  106.          */
  107.         @NonNull
  108.         Map<String, String> getOptions();

  109.         /**
  110.          * Retrieves an unmodifiable map of all multi- or list-valued options,
  111.          * with case-insensitive lookup by keys.
  112.          *
  113.          * @return all multi-valued options
  114.          */
  115.         @NonNull
  116.         Map<String, List<String>> getMultiValuedOptions();

  117.     }

  118.     /**
  119.      * An empty {@link HostConfig}.
  120.      */
  121.     static final HostConfig EMPTY_CONFIG = new HostConfig() {

  122.         @Override
  123.         public String getValue(String key) {
  124.             return null;
  125.         }

  126.         @Override
  127.         public List<String> getValues(String key) {
  128.             return Collections.emptyList();
  129.         }

  130.         @Override
  131.         public Map<String, String> getOptions() {
  132.             return Collections.emptyMap();
  133.         }

  134.         @Override
  135.         public Map<String, List<String>> getMultiValuedOptions() {
  136.             return Collections.emptyMap();
  137.         }

  138.     };
  139. }