Пример #1
0
  @Override
  public T getPath(String first, String... more) {
    StringBuilder sb = new StringBuilder();
    if (!GenericUtils.isEmpty(first)) {
      appendDedupSep(sb, first.replace('\\', '/')); // in case we are running on Windows
    }

    if (GenericUtils.length(more) > 0) {
      for (String segment : more) {
        if ((sb.length() > 0) && (sb.charAt(sb.length() - 1) != '/')) {
          sb.append('/');
        }
        // in case we are running on Windows
        appendDedupSep(sb, segment.replace('\\', '/'));
      }
    }

    if ((sb.length() > 1) && (sb.charAt(sb.length() - 1) == '/')) {
      sb.setLength(sb.length() - 1);
    }

    String path = sb.toString();
    String root = null;
    if (path.startsWith("/")) {
      root = "/";
      path = path.substring(1);
    }

    String[] names = GenericUtils.split(path, '/');
    return create(root, names);
  }
Пример #2
0
  /**
   * @param props The {@link Properties} holding the server's configuration - ignored if {@code
   *     null}/empty
   * @param options The {@link LinkOption}s to use when checking files existence
   * @return A {@link Map} of the found identities where key=the identity type (case
   *     <U>insensitive</U>) and value=the {@link Path} of the file holding the specific type key
   * @throws IOException If failed to access the file system
   * @see #getIdentityType(String)
   * @see #HOST_KEY_CONFIG_PROP
   * @see org.apache.sshd.common.config.SshConfigFileReader#readConfigFile(File)
   */
  public static Map<String, Path> findIdentities(Properties props, LinkOption... options)
      throws IOException {
    if (GenericUtils.isEmpty(props)) {
      return Collections.emptyMap();
    }

    String keyList = props.getProperty(HOST_KEY_CONFIG_PROP);
    String[] paths = GenericUtils.split(keyList, ',');
    if (GenericUtils.isEmpty(paths)) {
      return Collections.emptyMap();
    }

    Map<String, Path> ids = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    for (String p : paths) {
      File file = new File(p);
      Path path = file.toPath();
      if (!Files.exists(path, options)) {
        continue;
      }

      String type = getIdentityType(path.getFileName().toString());
      if (GenericUtils.isEmpty(type)) {
        type = p; // just in case the file name does not adhere to the standard naming convention
      }
      Path prev = ids.put(type, path);
      ValidateUtils.checkTrue(prev == null, "Multiple mappings for type=%s", type);
    }

    return ids;
  }
Пример #3
0
  public static <E extends KnownHostEntry> E parseKnownHostEntry(E entry, String data) {
    String line = data;
    if (GenericUtils.isEmpty(line) || (line.charAt(0) == PublicKeyEntry.COMMENT_CHAR)) {
      return entry;
    }

    entry.setConfigLine(line);

    if (line.charAt(0) == MARKER_INDICATOR) {
      int pos = line.indexOf(' ');
      ValidateUtils.checkTrue(pos > 0, "Missing marker name end delimiter in line=%s", data);
      ValidateUtils.checkTrue(pos > 1, "No marker name after indicator in line=%s", data);
      entry.setMarker(line.substring(1, pos));
      line = line.substring(pos + 1).trim();
    } else {
      entry.setMarker(null);
    }

    int pos = line.indexOf(' ');
    ValidateUtils.checkTrue(pos > 0, "Missing host patterns end delimiter in line=%s", data);
    String hostPattern = line.substring(0, pos);
    line = line.substring(pos + 1).trim();

    if (hostPattern.charAt(0) == KnownHostHashValue.HASHED_HOST_DELIMITER) {
      KnownHostHashValue hash =
          ValidateUtils.checkNotNull(
              KnownHostHashValue.parse(hostPattern),
              "Failed to extract host hash value from line=%s",
              data);
      entry.setHashedEntry(hash);
      entry.setPatterns(null);
    } else {
      entry.setHashedEntry(null);
      entry.setPatterns(parsePatterns(GenericUtils.split(hostPattern, ',')));
    }

    AuthorizedKeyEntry key =
        ValidateUtils.checkNotNull(
            AuthorizedKeyEntry.parseAuthorizedKeyEntry(line),
            "No valid key entry recovered from line=%s",
            data);
    entry.setKeyEntry(key);
    return entry;
  }
Пример #4
0
 /**
  * @param sigs A comma-separated list of signatures' names - ignored if {@code null}/empty
  * @return A {@link ParseResult} of all the {@link NamedFactory} whose name appears in the string
  *     and represent a built-in signature. Any unknown name is <U>ignored</U>. The order of the
  *     returned result is the same as the original order - bar the unknown signatures.
  *     <B>Note:</B> it is up to caller to ensure that the list does not contain duplicates
  */
 public static ParseResult parseSignatureList(String sigs) {
   return parseSignatureList(GenericUtils.split(sigs, ','));
 }