Пример #1
0
  @Override
  public void download(String remote, Path local, Collection<Option> options) throws IOException {
    local = ValidateUtils.checkNotNull(local, "Invalid argument local: %s", local);
    remote = ValidateUtils.checkNotNullAndNotEmpty(remote, "Invalid argument remote: %s", remote);

    LinkOption[] opts = IoUtils.getLinkOptions(false);
    if (Files.isDirectory(local, opts)) {
      options = addTargetIsDirectory(options);
    }

    if (options.contains(Option.TargetIsDirectory)) {
      Boolean status = IoUtils.checkFileExists(local, opts);
      if (status == null) {
        throw new SshException("Target directory " + local.toString() + " is probaly inaccesible");
      }

      if (!status.booleanValue()) {
        throw new SshException("Target directory " + local.toString() + " does not exist");
      }

      if (!Files.isDirectory(local, opts)) {
        throw new SshException("Target directory " + local.toString() + " is not a directory");
      }
    }

    download(remote, local.getFileSystem(), local, options);
  }
Пример #2
0
  /**
   * Sets the server's {@link KeyPairProvider} with the loaded identities - if any
   *
   * @param <S> The generic server type
   * @param server The {@link SshServer} to configure
   * @param props The {@link Properties} holding the server's configuration - ignored if {@code
   *     null}/empty
   * @param supportedOnly If {@code true} then ignore identities that are not supported internally
   * @return The updated server
   * @throws IOException If failed to access the file system
   * @throws GeneralSecurityException If failed to load the keys
   * @see #loadKeyPairProvider(Properties, boolean, LinkOption...)
   */
  public static <S extends SshServer> S setKeyPairProvider(
      S server, Properties props, boolean supportedOnly)
      throws IOException, GeneralSecurityException {
    KeyPairProvider provider =
        loadKeyPairProvider(props, supportedOnly, IoUtils.getLinkOptions(false));
    if (provider != null) {
      server.setKeyPairProvider(provider);
    }

    return server;
  }
Пример #3
0
  @Test
  public void testLoadClientIdentities() throws Exception {
    Assume.assumeTrue("BouncyCastle not registered", SecurityUtils.isBouncyCastleRegistered());

    Path resFolder = getClassResourcesFolder(TEST_SUBFOLDER, getClass()).toPath();
    LinkOption[] options = IoUtils.getLinkOptions(false);
    Collection<BuiltinIdentities> expected = EnumSet.noneOf(BuiltinIdentities.class);
    for (BuiltinIdentities type : BuiltinIdentities.VALUES) {
      String fileName = ClientIdentity.getIdentityFileName(type);
      Path file = resFolder.resolve(fileName);
      if (!Files.exists(file, options)) {
        System.out.println("Skip non-existing identity file " + file);
        continue;
      }

      if (!type.isSupported()) {
        System.out.println("Skip unsupported identity file " + file);
        continue;
      }

      expected.add(type);
    }

    Map<String, KeyPair> ids =
        ClientIdentity.loadDefaultIdentities(
            resFolder, false, // don't be strict
            null, // none of the files is password protected
            options);
    assertEquals(
        "Mismatched loaded ids count", GenericUtils.size(expected), GenericUtils.size(ids));

    Collection<KeyPair> pairs = new ArrayList<KeyPair>(ids.size());
    for (BuiltinIdentities type : BuiltinIdentities.VALUES) {
      if (expected.contains(type)) {
        KeyPair kp = ids.get(type.getName());
        assertNotNull("No key pair loaded for " + type, kp);
        pairs.add(kp);
      }
    }

    KeyPairProvider provider = IdentityUtils.createKeyPairProvider(ids, true /* supported only */);
    assertNotNull("No provider generated", provider);

    Iterable<KeyPair> keys = provider.loadKeys();
    for (KeyPair kp : keys) {
      assertTrue("Unexpected loaded key: " + kp, pairs.remove(kp));
    }

    assertEquals("Not all pairs listed", 0, pairs.size());
  }
Пример #4
0
  @Test
  public void testCRLFHandling() throws IOException {
    List<String> lines =
        Arrays.asList(
            getClass().getPackage().getName(),
            getClass().getSimpleName(),
            getCurrentTestName(),
            "(" + mode + ")",
            new Date(System.currentTimeMillis()).toString());
    String content = GenericUtils.join(lines, "\r\n");
    try (ByteArrayInputStream bais =
            new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
        TtyFilterInputStream tty = new TtyFilterInputStream(bais, EnumSet.of(mode))) {
      final AtomicInteger crCount = new AtomicInteger(0);
      final AtomicInteger lfCount = new AtomicInteger(0);

      try (OutputStream output =
          new OutputStream() {
            @Override
            public void write(int b) throws IOException {
              if (b == '\r') {
                crCount.incrementAndGet();
              } else if (b == '\n') {
                lfCount.incrementAndGet();
              }
            }
          }) {
        long copySize = IoUtils.copy(tty, output);
        assertTrue(
            "Copy size (" + copySize + ") above total length (" + content.length() + ")",
            copySize <= content.length());
      }

      assertCRLFCounts(
          mode, lines.size() - 1 /* last line has no NL */, crCount.get(), lfCount.get());
    }
  }