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 ( 616c91...14312a )
by Greg
02:43
created

SecureShell::dontSeeRemoteDir()   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 \SplFileObject;
6
use \RuntimeException;
7
8
class SecureShell extends \Codeception\Platform\Extension
9
{
10
11
    const DEFAULT_PORT  = 22;
12
    const AUTH_PASSWORD = 1;
13
    const AUTH_PUBKEY   = 2;
14
    const AUTH_HOSTKEY  = 3;
15
    const AUTH_AGENT    = 4;
16
    const AUTH_NONE     = 0;
17
18
    // list events to listen to
19
    public static $events = [];
20
21
    protected static $knownHostsFile = '~/.ssh/known_hosts';
22
23
    protected static $tunnels = [];
24
25
    protected static $connections = [];
26
27
    public function openConnection($host,
28
                                    $port = SecureShell::DEFAULT_PORT,
29
                                    $auth = SecureShell::AUTH_PASSWORD,
30
                                    ...$args)
31
    {
32
        $uid = null;
0 ignored issues
show
Unused Code introduced by
$uid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
        $callbacks = array('disconnect' => [$this, '_disconnect']);
34
35
        if (!($connection = ssh2_connect($host, $port, $callbacks))) {
36
            throw new ModuleException("Cannot connect to server {$host}:{$port}");
0 ignored issues
show
Bug introduced by
The call to ModuleException::__construct() misses a required argument $message.

This check looks for function calls that miss required arguments.

Loading history...
37
        } else {
38
            $this->__checkFingerprint($connection);
39
40
            if ($this->__authenticate($connection, ...$args) === false) {
41
                throw new ModuleException("Authentication failed on server {$host}:{$port}");
0 ignored issues
show
Bug introduced by
The call to ModuleException::__construct() misses a required argument $message.

This check looks for function calls that miss required arguments.

Loading history...
42
            } else {
43
                $uid = uniqid('ssh_');
44
                $this->connections[$uid] = ['host' => $host,
45
                                            'port' => $port,
46
                                            'fingerprint' => $fp,
0 ignored issues
show
Bug introduced by
The variable $fp does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47
                                            'auth_method' => $auth,
48
                                            'resource' => $connection];
49
            }
50
        }
51
        return $uid;
52
    }
53
54
    public function closeConnection($uid) {
55
        switch ($this->__isValidConnnection($uid)) {
56
            case 0:
57
            case 1:
58
                unset($this->connections[$uid]);
59
                break;
60
            default:
61
                throw new ModuleException("{$uid} is not a valid SSH connection");
0 ignored issues
show
Bug introduced by
The call to ModuleException::__construct() misses a required argument $message.

This check looks for function calls that miss required arguments.

Loading history...
62
        }
63
    }
64
65
    protected function __isValidConnnection($uid) {
66
        if (isset($this->connections[$uid])) {
67
            if (is_resource($this->connections[$uid]['resource'])) {
68
                return 1;
69
            } else {
70
                return 0;
71
            }
72
        } else {
73
            return -1;
74
        }
75
    }
76
77
    protected function __authenticate($connection, $method, ...$args)
78
    {
79
        switch ($method) {
80
            case SecureShell::AUTH_PASSWORD:
81
                return ssh2_auth_password($connection, ...$args);
82
            case SecureShell::AUTH_PUBKEY:
83
            return ssh2_auth_pubkey_file($connection, ...$args);
84
            case SecureShell::AUTH_HOSTKEY:
85
                return ssh2_auth_hostbased_file($connection, ...$args);
86
            case SecureShell::AUTH_AGENT:
87
                return ssh2_auth_agent($connection, ...$args);
88
            case SecureShell::AUTH_NONE:
89
                return ssh2_auth_none($connection, ...$args);
90
            default:
91
                throw new ModuleException('Unsupported authentication method');
0 ignored issues
show
Bug introduced by
The call to ModuleException::__construct() misses a required argument $message.

This check looks for function calls that miss required arguments.

Loading history...
92
        }
93
    }
94
95
    protected function __checkFingerprint($connection, $acceptUnknown = false)
96
    {
97
        $fingerprint = ssh2_fingerprint($connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
98
        $knownHost = false;
99
        try {
100
            $file = new SplFileObject($this->knownHostsFile);
101
            $file->setFlags(SplFileObject::READ_CSV);
102
            $file->setCsvControl(' ');
103
            foreach ($file as $entry) {
104
                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...
105
                $knownHost = (strcmp($fp, $fingerprint) !== 0);
106
                if ($knownHost === true) {
107
                    break;
108
                }
109
            }
110
            $knownHost = $knownHost || $acceptUnknown;
111
112
            if ($knownHost === false) {
113
                throw new ModuleException('Unable to verify server identity!');
0 ignored issues
show
Bug introduced by
The call to ModuleException::__construct() misses a required argument $message.

This check looks for function calls that miss required arguments.

Loading history...
114
            }
115
        } catch (RuntimeException $e) {
116
            if ($acceptUnknown === false) {
117
                throw new ModuleException('Unable to verify server identity!');
0 ignored issues
show
Bug introduced by
The call to ModuleException::__construct() misses a required argument $message.

This check looks for function calls that miss required arguments.

Loading history...
118
            }
119
        }
120
        return true;
121
    }
122
123
    protected function __disconnect()
124
    {
125
        foreach ($this->connections as $id => $connection) {
126
            if (is_resource($connection['resource']) !== true) {
127
                unset($this->connections[$id]);
128
            }
129
        }
130
    }
131
132
    /** Remote Commands methods **/
133
134
    public function runRemoteCommand()
135
    {
136
137
    }
138
139
    public function seeRemoteOutput()
140
    {
141
142
    }
143
144
    public function dontSeeRemoteOutput()
145
    {
146
147
    }
148
149
    /** Remote Files methods **/
150
151
    public function seeRemoteFile()
152
    {
153
154
    }
155
156
    public function dontSeeRemoteFile()
157
    {
158
159
    }
160
161
    public function grabRemoteFile()
162
    {
163
164
    }
165
166
    public function copyRemoteFile()
167
    {
168
169
    }
170
171
    public function deleteRemoteFile()
172
    {
173
174
    }
175
176
    /** Remote Dir methods **/
177
178
    public function seeRemoteDir()
179
    {
180
181
    }
182
183
    public function dontSeeRemoteDir()
184
    {
185
186
    }
187
188
    public function copyRemoteDir()
189
    {
190
191
    }
192
193
    public function deleteRemoteDir()
194
    {
195
196
    }
197
198
    public function readRemoteDir()
199
    {
200
201
    }
202
203
    /** Tunnel methods **/
204
205
    public function openRemoteTunnel()
206
    {
207
208
    }
209
210
    public function closeRemoteTunnel()
211
    {
212
213
    }
214
215
}
216