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; <= 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.      * A host entry from the ssh config. Any merging of global values and of
  36.      * several matching host entries, %-substitutions, and ~ replacement have
  37.      * all been done.
  38.      */
  39.     interface HostConfig {

  40.         /**
  41.          * Retrieves the value of a single-valued key, or the first if the key
  42.          * has multiple values. Keys are case-insensitive, so
  43.          * {@code getValue("HostName") == getValue("HOSTNAME")}.
  44.          *
  45.          * @param key
  46.          *            to get the value of
  47.          * @return the value, or {@code null} if none
  48.          */
  49.         String getValue(String key);

  50.         /**
  51.          * Retrieves the values of a multi- or list-valued key. Keys are
  52.          * case-insensitive, so
  53.          * {@code getValue("HostName") == getValue("HOSTNAME")}.
  54.          *
  55.          * @param key
  56.          *            to get the values of
  57.          * @return a possibly empty list of values
  58.          */
  59.         List<String> getValues(String key);

  60.         /**
  61.          * Retrieves an unmodifiable map of all single-valued options, with
  62.          * case-insensitive lookup by keys.
  63.          *
  64.          * @return all single-valued options
  65.          */
  66.         @NonNull
  67.         Map<String, String> getOptions();

  68.         /**
  69.          * Retrieves an unmodifiable map of all multi- or list-valued options,
  70.          * with case-insensitive lookup by keys.
  71.          *
  72.          * @return all multi-valued options
  73.          */
  74.         @NonNull
  75.         Map<String, List<String>> getMultiValuedOptions();

  76.     }

  77.     /**
  78.      * An empty {@link HostConfig}.
  79.      */
  80.     static final HostConfig EMPTY_CONFIG = new HostConfig() {

  81.         @Override
  82.         public String getValue(String key) {
  83.             return null;
  84.         }

  85.         @Override
  86.         public List<String> getValues(String key) {
  87.             return Collections.emptyList();
  88.         }

  89.         @Override
  90.         public Map<String, String> getOptions() {
  91.             return Collections.emptyMap();
  92.         }

  93.         @Override
  94.         public Map<String, List<String>> getMultiValuedOptions() {
  95.             return Collections.emptyMap();
  96.         }

  97.     };
  98. }