GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 447b48...c57848 )
by Greg
02:13
created

SecureShell::dontSeeInRemoteOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Codeception\Extension;
3
4
use Codeception\Exception\ModuleException;
5
use Codeception\Exception\ModuleConfigException;
6
use Codeception\Module;
7
use \SplFileObject;
8
use \RuntimeException;
9
use \Exception;
10
11
class SecureShell extends Module
12
{
13
14
    const DEFAULT_PORT  = 22;
15
    const AUTH_PASSWORD = 1;
16
    const AUTH_PUBKEY   = 2;
17
    const AUTH_HOSTKEY  = 3;
18
    const AUTH_AGENT    = 4;
19
    const AUTH_NONE     = 0;
20
21
    protected $config = [];
22
23
    protected $requiredFields = [];
24
25
    protected static $knownHostsFile = '~/.ssh/known_hosts'; // configuration
26
27
    protected static $acceptUnknownHost = true; // configuration
28
29
    protected $tunnel;
30
31
    protected $connection;
32
33
    private $output;
34
35
    public function openConnection($host,
36
                                    $port = SecureShell::DEFAULT_PORT,
37
                                    $auth = SecureShell::AUTH_PASSWORD,
38
                                    ...$args)
39
    {
40
        $uid = null;
41
        $callbacks = array('disconnect' => [$this, '_disconnect']);
42
43
        try {
44
            $connection = ssh2_connect($host, $port, $callbacks);
45
            if (!$connection) {
46
                throw new ModuleException(get_class($this), "Unable to connect to {$host} on port {$port}");
47
            } else {
48
                $fp = $this->__checkFingerprint($connection);
49
50
                if ($this->__authenticate($connection, $auth, ...$args) === false) {
51
                    throw new ModuleException(get_class($this), "Authentication failed on server {$host}:{$port}");
52
                } else {
53
                    $uid = hash('crc32', uniqid($fp), false);
54
                    $this->connection = $connection;
55
                }
56
            }
57
        } catch (ModuleException $e) {
58
            throw $e;
59
        } catch (Exception $e) {
60
            throw new ModuleException(get_class($this), $e->getMessage());
61
        }
62
        return $uid;
63
    }
64
65
    public function closeConnection() {
66
        $this->connection = null;
67
        return true;
68
    }
69
70
    public function getConnection() {
71
        return $this->connection;
72
    }
73
74
    protected function __authenticate($connection, $method, ...$args)
75
    {
76
        switch ($method) {
77
            case SecureShell::AUTH_PASSWORD:
78
                return ssh2_auth_password($connection, ...$args);
79
            case SecureShell::AUTH_PUBKEY:
80
                return ssh2_auth_pubkey_file($connection, ...$args);
81
            case SecureShell::AUTH_HOSTKEY:
82
                return ssh2_auth_hostbased_file($connection, ...$args);
83
            case SecureShell::AUTH_AGENT:
84
                return ssh2_auth_agent($connection, ...$args);
85
            case SecureShell::AUTH_NONE:
86
                return ssh2_auth_none($connection, ...$args);
87
            default:
88
                throw new ModuleException(get_class($this), 'Unsupported authentication method');
89
        }
90
    }
91
92
    protected function __checkFingerprint($connection)
93
    {
94
        $knownHost = false;
95
        try {
96
            $fingerprint = ssh2_fingerprint($connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
97
            $file = new SplFileObject(static::$knownHostsFile);
98
            $file->setFlags(SplFileObject::READ_CSV);
99
            $file->setCsvControl(' ');
100
            foreach ($file as $entry) {
101
                list(,, $fp) = $entry;
102
                $knownHost = (strcmp($fp, $fingerprint) !== 0);
103
                if ($knownHost === true) {
104
                    break;
105
                }
106
            }
107
            $knownHost = $knownHost || static::$acceptUnknownHost;
108
109
            if ($knownHost === false) {
110
                throw new ModuleException(get_class($this), 'Unable to verify server identity!');
111
            }
112
        } catch (RuntimeException $e) {
113
            if (static::$acceptUnknownHost === false) {
114
                throw new ModuleException(get_class($this), 'Unable to verify server identity!');
115
            }
116
        }
117
        return $fingerprint;
118
    }
119
120
    /** Remote Commands methods **/
121
122
    public function runRemoteCommand($command)
123
    {
124
        try {
125
            $stream = ssh2_exec($this->connection, $command);
126
            stream_set_blocking($stream, true);
127
            $errStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
128
            $this->output['STDOUT'] = stream_get_contents($stream);
129
            $this->output['STDERR'] = stream_get_contents($errStream);
130
            return $this->output;
131
        } catch (Exception $e) {
132
            throw new ModuleException(get_class($this), $e->getMessage());
133
        }
134
    }
135
136
    public function seeInRemoteOutput($text)
137
    {
138
        \PHPUnit_Framework_Assert::assertContains($text, $this->output['STDOUT']);
139
    }
140
141
    public function dontSeeInRemoteOutput($text)
142
    {
143
        \PHPUnit_Framework_Assert::assertNotContains($text, $this->output['STDOUT']);
144
    }
145
146
    /** Remote Files methods **/
147
148
    public function seeRemoteFile($filename)
149
    {
150
        $sftp = ssh2_sftp($this->connection);
151
        $res = ssh2_sftp_stat($sftp, $filename);
152
        \PHPUnit_Framework_Assert::assertNotEmpty($res);
153
    }
154
155
    public function dontSeeRemoteFile($filename)
156
    {
157
        $sftp = ssh2_sftp($this->connection);
158
        try {
159
            $res = (bool) ssh2_sftp_stat($sftp, $filename);
160
        } catch (Exception $e) {
161
            $res = false;
162
        }
163
        \PHPUnit_Framework_Assert::assertFalse($res);
164
    }
165
166
    public function grabRemoteFile($filename)
167
    {
168
        try {
169
            $sftp = ssh2_sftp($this->connection);
170
            return file_get_contents("ssh2.sftp://{$sftp}/{$filename}");
171
        } catch (Exception $e) {
172
            throw new ModuleException(get_class($this), $e->getMessage());
173
        }
174
    }
175
176
    /** Remote Dir methods **/
177
178 View Code Duplication
    public function seeRemoteDir($dirname)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    {
180
        try {
181
            $res = (bool) $this->grabRemoteDir($dirname);
182
        } catch (Exception $e) {
183
            $res = false;
184
        }
185
        \PHPUnit_Framework_Assert::assertTrue($res);
186
    }
187
188 View Code Duplication
    public function dontSeeRemoteDir($dirname)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
    {
190
        try {
191
            $res = (bool) $this->grabRemoteDir($dirname);
192
        } catch (Exception $e) {
193
            $res = false;
194
        }
195
        \PHPUnit_Framework_Assert::assertFalse($res);
196
    }
197
198
    public function grabRemoteDir($dirname)
199
    {
200
        $res = null;
201
        try {
202
            $sftp = ssh2_sftp($this->connection);
203
            $res = scandir("ssh2.sftp://{$sftp}/{$dirname}");
204
        } catch (Exception $e) {
205
            throw new ModuleException(get_class($this), $e->getMessage());
206
        }
207
        return $res;
208
    }
209
210
}
211