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 ( 6047b6...144a2d )
by Greg
02:22
created

SecureShell::seeRemoteFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

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 1
nc 1
nop 0
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 $tunnels = [];
30
31
    protected $connections = [];
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->connections = array_merge($this->connections,
55
                                        [$uid => ['host' => $host,
56
                                                'port' => $port,
57
                                                'fingerprint' => $fp,
58
                                                'auth_method' => $auth,
59
                                                'resource' => $connection]
60
                                        ]);
61
                }
62
            }
63
        } catch (ModuleException $e) {
64
            throw $e;
65
        } catch (Exception $e) {
66
            throw new ModuleException(get_class($this), $e->getMessage());
67
        }
68
        return $uid;
69
    }
70
71
    public function closeConnection($uid) {
72
        switch ($this->__isValidConnnection($uid)) {
73
            case 0:
74
            case 1:
75
                unset($this->connections[$uid]);
76
                break;
77
            default:
78
                throw new ModuleException(get_class($this), "{$uid} is not a valid SSH connection");
79
        }
80
        return true;
81
    }
82
83
    public function getConnection($uid) {
84
        return $this->connections[$uid]['resource'];
85
    }
86
87
    protected function __isValidConnnection($uid) {
88
        if (isset($this->connections[$uid])) {
89
            if (is_resource($this->connections[$uid]['resource'])) {
90
                return 1;
91
            } else {
92
                return 0;
93
            }
94
        } else {
95
            return -1;
96
        }
97
    }
98
99
    protected function __authenticate($connection, $method, ...$args)
100
    {
101
        switch ($method) {
102
            case SecureShell::AUTH_PASSWORD:
103
                return ssh2_auth_password($connection, ...$args);
104
            case SecureShell::AUTH_PUBKEY:
105
            return ssh2_auth_pubkey_file($connection, ...$args);
106
            case SecureShell::AUTH_HOSTKEY:
107
                return ssh2_auth_hostbased_file($connection, ...$args);
108
            case SecureShell::AUTH_AGENT:
109
                return ssh2_auth_agent($connection, ...$args);
110
            case SecureShell::AUTH_NONE:
111
                return ssh2_auth_none($connection, ...$args);
112
            default:
113
                throw new ModuleException(get_class($this), 'Unsupported authentication method');
114
        }
115
    }
116
117
    protected function __checkFingerprint($connection)
118
    {
119
        $knownHost = false;
120
        try {
121
            $fingerprint = ssh2_fingerprint($connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
122
            $file = new SplFileObject(static::$knownHostsFile);
123
            $file->setFlags(SplFileObject::READ_CSV);
124
            $file->setCsvControl(' ');
125
            foreach ($file as $entry) {
126
                list($host, $method, $fp) = $entry;
0 ignored issues
show
Unused Code introduced by
The assignment to $host is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $method is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
127
                $knownHost = (strcmp($fp, $fingerprint) !== 0);
128
                if ($knownHost === true) {
129
                    break;
130
                }
131
            }
132
            $knownHost = $knownHost || static::$acceptUnknownHost;
133
134
            if ($knownHost === false) {
135
                throw new ModuleException(get_class($this), 'Unable to verify server identity!');
136
            }
137
        } catch (RuntimeException $e) {
138
            if (static::$acceptUnknownHost === false) {
139
                throw new ModuleException(get_class($this), 'Unable to verify server identity!');
140
            }
141
        }
142
        return $fingerprint;
143
    }
144
145
    protected function __disconnect()
146
    {
147
        foreach ($this->connections as $id => $connection) {
148
            if (is_resource($connection['resource']) !== true) {
149
                unset($this->connections[$id]);
150
            }
151
        }
152
    }
153
154
    /** Remote Commands methods **/
155
156
    public function runRemoteCommand($session, $command)
157
    {
158
        $connection = $this->getConnection($session);
159
        $stream = ssh2_exec($connection, $command);
160
        stream_set_blocking($stream, true);
161
        $errStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
162
        $this->output['STDOUT'] = stream_get_contents($stream);
163
        $this->output['STDERR'] = stream_get_contents($errStream);
164
        return $this->output;
165
    }
166
167
    public function seeInRemoteOutput($text)
168
    {
169
        \PHPUnit_Framework_Assert::assertContains($text, $this->output['STDOUT']);
170
    }
171
172
    public function dontSeeInRemoteOutput($text)
173
    {
174
        \PHPUnit_Framework_Assert::assertNotContains($text, $this->output['STDOUT']);
175
    }
176
177
    /** Remote Files methods **/
178
179
    public function seeRemoteFile()
180
    {
181
182
    }
183
184
    public function dontSeeRemoteFile()
185
    {
186
187
    }
188
189
    public function grabRemoteFile()
190
    {
191
192
    }
193
194
    public function copyRemoteFile()
195
    {
196
197
    }
198
199
    public function deleteRemoteFile()
200
    {
201
202
    }
203
204
    /** Remote Dir methods **/
205
206
    public function seeRemoteDir()
207
    {
208
209
    }
210
211
    public function dontSeeRemoteDir()
212
    {
213
214
    }
215
216
    public function copyRemoteDir()
217
    {
218
219
    }
220
221
    public function deleteRemoteDir()
222
    {
223
224
    }
225
226
    public function readRemoteDir()
227
    {
228
229
    }
230
231
    /** Tunnel methods **/
232
233
    public function openRemoteTunnel()
234
    {
235
236
    }
237
238
    public function closeRemoteTunnel()
239
    {
240
241
    }
242
243
}
244