laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.21k stars 10.9k forks source link

the privateKey could not use on SFTP #26826

Closed Yi-Frank closed 5 years ago

Yi-Frank commented 5 years ago

Description:

the privateKey could not use on SFTP, but password can do

Steps To Reproduce:

config/filesystems.php

        'main_dir' => [
            'driver' => 'sftp',
            'host' => '***',
            'username' => 'root',
            'privateKey' => '/var/id_rsa',  // file permissions was ok
            'password' => '*****',
        ],

that could not login... but use sftp root@**** -i/var/id_rsa ok

but use password could login...

        'main_dir' => [
            'driver' => 'sftp',
            'host' => '***',
            'username' => 'root',
            //'privateKey' => '/var/id_rsa',  // file permissions was ok
            'password' => '*****',
        ],
driesvints commented 5 years ago

If your privateKey isn't encrypted you can just omit the password setting. Can you try this:

        'main_dir' => [
            'driver' => 'sftp',
            'host' => '***',
            'username' => 'root',
            'privateKey' => '/var/id_rsa',
        ],
Yi-Frank commented 5 years ago

yes, I tried that, but could not login too... And I look at the source

league/flysystem-sftp SftpAdapter.php Line 182

/**
     * Login.
     *
     * @throws LogicException
     */
    protected function login()
    {
        if ($this->hostFingerprint) {
            $actualFingerprint = $this->getHexFingerprintFromSshPublicKey($this->connection->getServerPublicHostKey());

            if (0 !== strcasecmp($this->hostFingerprint, $actualFingerprint)) {
                throw new LogicException('The authenticity of host '.$this->host.' can\'t be established.');
            }
        }

        $authentication = $this->getAuthentication();
        // Report error here :  login()
        if (! $this->connection->login($this->getUsername(), $authentication)) {
            throw new LogicException('Could not login with username: '.$this->getUsername().', host: '.$this->host);
        }

        if ($authentication instanceof Agent) {
            $authentication->startSSHForwarding($this->connection);
        }
    }

phpseclib/Net SFTP.php Line 393

    /**
     * Login
     *
     * @param string $username
     * @param string $password
     * @return bool
     * @access public
     */
    function login($username)

the param just was password.....

Is that the mistake here?

driesvints commented 5 years ago

What libraries are you using? Can you link them here? How are you installing/using them? Can you post more code examples?

Yi-Frank commented 5 years ago

I using composer to install libraries...

Composer.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^7.1.3",
        "barryvdh/laravel-ide-helper": "v2.5.2",
        "fideloper/proxy": "^4.0",
        "guzzlehttp/guzzle": "6.3.0",
        "laravel/framework": "5.7.*",
        "laravel/tinker": "~1.0",
        "maddhatter/laravel-fullcalendar": "v1.3.0",
        "overtrue/laravel-wechat": "~4.0",
        "predis/predis": "v1.1.1",
        "simplesoftwareio/simple-qrcode": "2.0.0",
        "toplan/laravel-sms": "2.6.5",
        "league/flysystem-sftp": "~1.0"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": [
            ]
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://packagist.phpcomposer.com"
        }
    }
}

phpseclib/Net SFTP.php Lin 393

    /**
     * Login
     *
     * @param string $username
     * @param string $password
     * @return bool
     * @access public
     */
    function login($username)
    {
        $args = func_get_args();
        if (!call_user_func_array(array(&$this, '_login'), $args)) {
            return false;
        }

        $this->window_size_server_to_client[self::CHANNEL] = $this->window_size;

        $packet = pack(
            'CNa*N3',
            NET_SSH2_MSG_CHANNEL_OPEN,
            strlen('session'),
            'session',
            self::CHANNEL,
            $this->window_size,
            0x4000
        );

        if (!$this->_send_binary_packet($packet)) {
            return false;
        }

        $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_OPEN;

        $response = $this->_get_channel_packet(self::CHANNEL, true);
        if ($response === false) {
            return false;
        }

        $packet = pack(
            'CNNa*CNa*',
            NET_SSH2_MSG_CHANNEL_REQUEST,
            $this->server_channels[self::CHANNEL],
            strlen('subsystem'),
            'subsystem',
            1,
            strlen('sftp'),
            'sftp'
        );
        if (!$this->_send_binary_packet($packet)) {
            return false;
        }

        $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST;

        $response = $this->_get_channel_packet(self::CHANNEL, true);
        if ($response === false) {
            // from PuTTY's psftp.exe
            $command = "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n" .
                       "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n" .
                       "exec sftp-server";
            // we don't do $this->exec($command, false) because exec() operates on a different channel and plus the SSH_MSG_CHANNEL_OPEN that exec() does
            // is redundant
            $packet = pack(
                'CNNa*CNa*',
                NET_SSH2_MSG_CHANNEL_REQUEST,
                $this->server_channels[self::CHANNEL],
                strlen('exec'),
                'exec',
                1,
                strlen($command),
                $command
            );
            if (!$this->_send_binary_packet($packet)) {
                return false;
            }

            $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST;

            $response = $this->_get_channel_packet(self::CHANNEL, true);
            if ($response === false) {
                return false;
            }
        }

        $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_DATA;

        if (!$this->_send_sftp_packet(NET_SFTP_INIT, "\0\0\0\3")) {
            return false;
        }

        $response = $this->_get_sftp_packet();
        if ($this->packet_type != NET_SFTP_VERSION) {
            user_error('Expected SSH_FXP_VERSION');
            return false;
        }

        if (strlen($response) < 4) {
            return false;
        }
        extract(unpack('Nversion', $this->_string_shift($response, 4)));
        $this->version = $version;
        while (!empty($response)) {
            if (strlen($response) < 4) {
                return false;
            }
            extract(unpack('Nlength', $this->_string_shift($response, 4)));
            $key = $this->_string_shift($response, $length);
            if (strlen($response) < 4) {
                return false;
            }
            extract(unpack('Nlength', $this->_string_shift($response, 4)));
            $value = $this->_string_shift($response, $length);
            $this->extensions[$key] = $value;
        }

        /*
         SFTPv4+ defines a 'newline' extension.  SFTPv3 seems to have unofficial support for it via 'newline@vandyke.com',
         however, I'm not sure what 'newline@vandyke.com' is supposed to do (the fact that it's unofficial means that it's
         not in the official SFTPv3 specs) and 'newline@vandyke.com' / 'newline' are likely not drop-in substitutes for
         one another due to the fact that 'newline' comes with a SSH_FXF_TEXT bitmask whereas it seems unlikely that
         'newline@vandyke.com' would.
        */
        /*
        if (isset($this->extensions['newline@vandyke.com'])) {
            $this->extensions['newline'] = $this->extensions['newline@vandyke.com'];
            unset($this->extensions['newline@vandyke.com']);
        }
        */

        $this->request_id = 1;

        /*
         A Note on SFTPv4/5/6 support:
         <http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-5.1> states the following:

         "If the client wishes to interoperate with servers that support noncontiguous version
          numbers it SHOULD send '3'"

         Given that the server only sends its version number after the client has already done so, the above
         seems to be suggesting that v3 should be the default version.  This makes sense given that v3 is the
         most popular.

         <http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-5.5> states the following;

         "If the server did not send the "versions" extension, or the version-from-list was not included, the
          server MAY send a status response describing the failure, but MUST then close the channel without
          processing any further requests."

         So what do you do if you have a client whose initial SSH_FXP_INIT packet says it implements v3 and
         a server whose initial SSH_FXP_VERSION reply says it implements v4 and only v4?  If it only implements
         v4, the "versions" extension is likely not going to have been sent so version re-negotiation as discussed
         in draft-ietf-secsh-filexfer-13 would be quite impossible.  As such, what \phpseclib\Net\SFTP would do is close the
         channel and reopen it with a new and updated SSH_FXP_INIT packet.
        */
        switch ($this->version) {
            case 2:
            case 3:
                break;
            default:
                return false;
        }

        $this->pwd = $this->_realpath('.');

        $this->_update_stat_cache($this->pwd, array());

        return true;
    }

These are the code and configuration. Do you need others?

And SFTP.php Comment

/**
 * Pure-PHP implementation of SFTP.
 *
 * PHP version 5
 *
 * Currently only supports SFTPv2 and v3, which, according to wikipedia.org, "is the most widely used version,
 * implemented by the popular OpenSSH SFTP server".  If you want SFTPv4/5/6 support, provide me with access
 * to an SFTPv4/5/6 server.
 *
 * The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.
 *
 * Here's a short example of how to use this library:
 * <code>
 * <?php
 *    include 'vendor/autoload.php';
 *
 *    $sftp = new \phpseclib\Net\SFTP('www.domain.tld');
 *    if (!$sftp->login('username', 'password')) {
 *        exit('Login Failed');
 *    }
 *
 *    echo $sftp->pwd() . "\r\n";
 *    $sftp->put('filename.ext', 'hello, world!');
 *    print_r($sftp->nlist());
 * ?>
 * </code>
 *
 * @category  Net
 * @package   SFTP
 * @author    Jim Wigginton <terrafrost@php.net>
 * @copyright 2009 Jim Wigginton
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link      http://phpseclib.sourceforge.net
 */

Is problem with this library? That for PHP 5....

Thanks so much...

driesvints commented 5 years ago

I don't see that library in your composer.json. Do you know which library requires it? Can you copy/paste your composer.lock file here? https://paste.laravel.io

Yi-Frank commented 5 years ago

Maybe this is it, "league/flysystem-sftp": "~1.0", And I paste composer.lock file in https://paste.laravel.io...And Save... Thanks your help...

driesvints commented 5 years ago

@Yi-Frank I can't open that link.

Yi-Frank commented 5 years ago

@driesvints Sorry...That not save...that limit 50000

https://www.yb-view.com/markdown/editor/5c18fa329a0ab

i saved here

or

https://www.yb-view.com/stroage/composer.lock

driesvints commented 5 years ago

I think you have an old version or something because the signature for that method is different than the one you posted above: https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Net/SFTP.php#L406

Try removing your vendor directory and run a composer install.

fridzema commented 5 years ago

I use it this way:

    'SFTP' => [
      'driver' => 'sftp',
      'host' => '0.0.0.0',
      'username' => 'admin',
      'privateKey' => '-----BEGIN RSA PRIVATE KEY-----000000000000000000000000000000-----END RSA PRIVATE KEY-----',
      'root' => '/',
      'timeout' => 10
    ],

When i use with password it doesn't work for me. Also i couldn't get the privateKey file reference to work so i use the contents of it directly (i think this is also a tiny bit more performant).

I also suggest you use the cache option so the server doesn't have to re-auth every time you call the disk.

composer require league/flysystem-cached-adapter

    'SFTP' => [
      'driver' => 'sftp',
      'host' => '0.0.0.0',
      'username' => 'admin',
      'privateKey' => '-----BEGIN RSA PRIVATE KEY-----000000000000000000000000000000-----END RSA PRIVATE KEY-----',
      'root' => '/',
      'timeout' => 10,
      'cache' => true
    ],
Yi-Frank commented 5 years ago

@driesvints I removed my vendor directory and composr install again... now phpseclib version is 2.0.13... image

but that not worked... phpseclib releases version was 2.0.13 https://github.com/phpseclib/phpseclib/releases Could you download that and see the file?... that method is defferent with master...

Should I try to download a master verison?

Yi-Frank commented 5 years ago

@fridzema thanks for your suggest... And I tried your method... that not worked...

fridzema commented 5 years ago

@Yi-Frank what error did you get?

Yi-Frank commented 5 years ago

@fridzema image

LogicException: Could not login with username: root, host: 172.16.0.5 in file /var/www/html/mgr/vendor/league/flysystem-sftp/src/SftpAdapter.php on line 200
Stack trace:
  1. LogicException-&gt;() /var/www/html/mgr/vendor/league/flysystem-sftp/src/SftpAdapter.php:200
  2. League\Flysystem\Sftp\SftpAdapter-&gt;login() /var/www/html/mgr/vendor/league/flysystem-sftp/src/SftpAdapter.php:178
  3. League\Flysystem\Sftp\SftpAdapter-&gt;connect() /var/www/html/mgr/vendor/league/flysystem/src/Adapter/AbstractFtpAdapter.php:573
  4. League\Flysystem\Adapter\AbstractFtpAdapter-&gt;getConnection() /var/www/html/mgr/vendor/league/flysystem-sftp/src/SftpAdapter.php:300
  5. League\Flysystem\Sftp\SftpAdapter-&gt;listDirectoryContents() /var/www/html/mgr/vendor/league/flysystem/src/Adapter/AbstractFtpAdapter.php:317
  6. League\Flysystem\Adapter\AbstractFtpAdapter-&gt;listContents() /var/www/html/mgr/vendor/league/flysystem/src/Filesystem.php:271
  7. League\Flysystem\Filesystem-&gt;listContents() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:603
  8. Illuminate\Filesystem\FilesystemAdapter-&gt;directories() /var/www/html/mgr/app/Http/Controllers/TestController.php:19
  9. App\Http\Controllers\TestController-&gt;index() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
 10. call_user_func_array() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
 11. Illuminate\Routing\Controller-&gt;callAction() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45
 12. Illuminate\Routing\ControllerDispatcher-&gt;dispatch() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Route.php:212
 13. Illuminate\Routing\Route-&gt;runController() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Route.php:169
 14. Illuminate\Routing\Route-&gt;run() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Router.php:682
 15. Illuminate\Routing\Router-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
 16. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
 17. Illuminate\Routing\Middleware\SubstituteBindings-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 18. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 19. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:75
 20. Illuminate\Foundation\Http\Middleware\VerifyCsrfToken-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 21. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 22. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49
 23. Illuminate\View\Middleware\ShareErrorsFromSession-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 24. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 25. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:63
 26. Illuminate\Session\Middleware\StartSession-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 27. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 28. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php:37
 29. Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 30. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 31. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:66
 32. Illuminate\Cookie\Middleware\EncryptCookies-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 33. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 34. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
 35. Illuminate\Pipeline\Pipeline-&gt;then() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Router.php:684
 36. Illuminate\Routing\Router-&gt;runRouteWithinStack() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Router.php:659
 37. Illuminate\Routing\Router-&gt;runRoute() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Router.php:625
 38. Illuminate\Routing\Router-&gt;dispatchToRoute() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Router.php:614
 39. Illuminate\Routing\Router-&gt;dispatch() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
 40. Illuminate\Foundation\Http\Kernel-&gt;Illuminate\Foundation\Http\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
 41. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/fideloper/proxy/src/TrustProxies.php:57
 42. Fideloper\Proxy\TrustProxies-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 43. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 44. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:31
 45. Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 46. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 47. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:31
 48. Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 49. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 50. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
 51. Illuminate\Foundation\Http\Middleware\ValidatePostSize-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 52. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 53. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:62
 54. Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode-&gt;handle() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 55. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 56. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
 57. Illuminate\Pipeline\Pipeline-&gt;then() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:151
 58. Illuminate\Foundation\Http\Kernel-&gt;sendRequestThroughRouter() /var/www/html/mgr/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
 59. Illuminate\Foundation\Http\Kernel-&gt;handle() /var/www/html/mgr/public/index.php:55
driesvints commented 5 years ago

Can you show us what you have in TestController.php?

fridzema commented 5 years ago

Hmmmm looks like the connection can be made, no timeout. Can you login with this credentials in a ftp client (transmit, cyberduck, filezilla etc)?

And check the ssh key: in your filesystem you must have the local ssh key in the authorized ssh keys on the remote server to make a connection.

I have struggled with this quite some time for me the problem was just comment out the password rule.

There must be something small now, almost there!

Yi-Frank commented 5 years ago

@driesvints

<?php
/**
 * Created by PhpStorm.
 * User: ybaig
 * Date: 2018-01-04
 * Time: 17:46
 */
namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class TestController extends Controller
{
    public function index(Request $request){

        $dirs = Storage::disk('main_dir')->directories('/');
        return $dirs;
    }
}

That was TestController.php....Just show the directory...

Yi-Frank commented 5 years ago

@fridzema I could login using commands...

root@Cpvrt-Parent:/var/www/html/cgs# sftp root@172.16.0.4 -i/var/id_rsa
Connected to 172.16.0.4.
sftp> exit

the key could be used...

And I output the authentication...

SftpAdapter.php

protected function login()
    {
        if ($this->hostFingerprint) {
            $actualFingerprint = $this->getHexFingerprintFromSshPublicKey($this->connection->getServerPublicHostKey());

            if (0 !== strcasecmp($this->hostFingerprint, $actualFingerprint)) {
                throw new LogicException('The authenticity of host '.$this->host.' can\'t be established.');
            }
        }

        $authentication = $this->getAuthentication();

        var_dump($authentication);

        if (! $this->connection->login($this->getUsername(), $authentication)) {
            throw new LogicException('Could not login with username: '.$this->getUsername().', host: '.$this->host);
        }

        if ($authentication instanceof Agent) {
            $authentication->startSSHForwarding($this->connection);
        }
    }

Output

object(phpseclib\Crypt\RSA)#380 (23) {
  ["zero"]=>
  object(phpseclib\Math\BigInteger)#381 (2) {
    ["value"]=>
    string(2) "0x"
    ["engine"]=>
    string(26) "internal (64-bit, OpenSSL)"
  }
  ["one"]=>
  object(phpseclib\Math\BigInteger)#382 (2) {
    ["value"]=>
    string(4) "0x01"
    ["engine"]=>
    string(26) "internal (64-bit, OpenSSL)"
  }
  ["privateKeyFormat"]=>
  int(0)
  ["publicKeyFormat"]=>
  int(7)
  ["modulus"]=>
  NULL
  ["k"]=>
  NULL
  ["exponent"]=>
  NULL
  ["primes"]=>
  NULL
  ["exponents"]=>
  NULL
  ["coefficients"]=>
  NULL
  ["hashName"]=>
  string(4) "sha1"
  ["hash"]=>
  object(phpseclib\Crypt\Hash)#384 (8) {
    ["hashParam"]=>
    string(4) "sha1"
    ["b"]=>
    int(64)
    ["l"]=>
    int(20)
    ["hash"]=>
    string(4) "sha1"
    ["key"]=>
    bool(false)
    ["computedKey"]=>
    bool(false)
    ["opad"]=>
    NULL
    ["ipad"]=>
    NULL
  }
  ["hLen"]=>
  int(20)
  ["sLen"]=>
  NULL
  ["mgfHash"]=>
  object(phpseclib\Crypt\Hash)#386 (8) {
    ["hashParam"]=>
    string(4) "sha1"
    ["b"]=>
    int(64)
    ["l"]=>
    int(20)
    ["hash"]=>
    string(4) "sha1"
    ["key"]=>
    bool(false)
    ["computedKey"]=>
    bool(false)
    ["opad"]=>
    NULL
    ["ipad"]=>
    NULL
  }
  ["mgfHLen"]=>
  int(20)
  ["encryptionMode"]=>
  int(1)
  ["signatureMode"]=>
  int(1)
  ["publicExponent"]=>
  NULL
  ["password"]=>
  bool(false)
  ["components"]=>
  array(0) {
  }
  ["configFile"]=>
  string(75) "/var/www/html/mgr/vendor/phpseclib/phpseclib/phpseclib/Crypt/../openssl.cnf"
  ["comment"]=>
  NULL
}

That seem no problem....

Yi-Frank commented 5 years ago

....The key was wrong.....That my mistakes..... I change an other key...That worded....Sorry for wasting your time..... Thanks for your help.......

Yi-Frank commented 5 years ago

@fridzema Maybe you can check file permissions...or user?

fridzema commented 5 years ago

@Yi-Frank for me everything is working fine now! Glad to help, no problem!

mikaeledstrom commented 5 years ago

I have the exact same issue. Logging in with password works fine from code. Logging in from terminal with private key works fine too. But not logging in with private key from code. To avoid any permission issue, I have pasted the whole contents of the private key into the config/filesystems.php.

One thing I am investigating is that $this->hostFingerprint never returns true.

$this->getAuthentication() gives the RSA object. How can I investigate this more? Where should I see the private key if the assumption is that the private key is not injected/used.