-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
328 lines (300 loc) · 11.2 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php namespace MyENA\PHPIPAMAPI;
use DCarbone\Go\Time;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request as PSR7Request;
use GuzzleHttp\Psr7\Uri as PSR7Uri;
use GuzzleHttp\RequestOptions;
use MyENA\PHPIPAMAPI\Chain\AddressesController;
use MyENA\PHPIPAMAPI\Chain\CircuitsController;
use MyENA\PHPIPAMAPI\Chain\DevicesController;
use MyENA\PHPIPAMAPI\Chain\L2DomainsController;
use MyENA\PHPIPAMAPI\Chain\PrefixController;
use MyENA\PHPIPAMAPI\Chain\SectionsController;
use MyENA\PHPIPAMAPI\Chain\SubnetsController;
use MyENA\PHPIPAMAPI\Chain\ToolsController;
use MyENA\PHPIPAMAPI\Chain\UserController;
use MyENA\PHPIPAMAPI\Chain\VlansController;
use MyENA\PHPIPAMAPI\Chain\VrfsController;
use MyENA\PHPIPAMAPI\Error\ApiError;
use MyENA\PHPIPAMAPI\Error\TransportError;
use MyENA\PHPIPAMAPI\Models\UserSession;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* Class Client
* @package MyENA\PHPIPAMAPI
*/
class Client implements LoggerAwareInterface {
use LoggerAwareTrait;
/** @var \MyENA\PHPIPAMAPI\Config */
private $config;
/** @var string */
private $serviceAddress;
/** @var \MyENA\PHPIPAMAPI\Models\UserSession */
private $clientUserSession;
/** @var bool */
private $silent;
/** @var array */
private static $requestOptions = [
RequestOptions::HTTP_ERRORS => false,
RequestOptions::DECODE_CONTENT => false,
RequestOptions::ALLOW_REDIRECTS => true,
];
/**
* Client constructor.
* @param \MyENA\PHPIPAMAPI\Config $config
* @param \Psr\Log\LoggerInterface|null $logger
*/
public function __construct(Config $config, LoggerInterface $logger = null) {
if (null === $logger) {
$logger = new NullLogger();
}
$this->logger = $logger;
$this->config = $config;
$this->silent = $this->config->silent();
// TODO: is this needed in conjunction with __destruct?
// $t = $this;
// register_shutdown_function(function() use ($t) {
// if (isset($t->clientUserSession)) {
// $t->User()->DELETE()->execute();
// }
// });
}
/**
* On destruction of class, terminate session token
*/
public function __destruct() {
if (isset($this->clientUserSession)) {
$this->User()->DELETE()->execute();
}
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\AddressesController
*/
public function Addresses(): AddressesController {
return new AddressesController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\CircuitsController
*/
public function Circuits(): CircuitsController {
return new CircuitsController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\DevicesController
*/
public function Devices(): DevicesController {
return new DevicesController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\L2DomainsController
*/
public function L2Domains(): L2DomainsController {
return new L2DomainsController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\PrefixController
*/
public function Prefix(): PrefixController {
return new PrefixController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\SectionsController
*/
public function Sections(): SectionsController {
return new SectionsController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\SubnetsController
*/
public function Subnets(): SubnetsController {
return new SubnetsController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\ToolsController
*/
public function ToolsController(): ToolsController {
return new ToolsController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\UserController
*/
public function User(): UserController {
return new UserController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\VlansController
*/
public function Vlans(): VlansController {
return new VlansController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Chain\VrfsController
*/
public function Vrfs(): VrfsController {
return new VrfsController($this, $this->logger);
}
/**
* @return \MyENA\PHPIPAMAPI\Config
*/
public function getConfig(): Config {
return $this->config;
}
/**
* Returns the current token of this client
*
* @return \MyENA\PHPIPAMAPI\Models\UserSession|null
*/
public function getClientUserSession(): ?UserSession {
return $this->clientUserSession ?? null;
}
/**
* @param \MyENA\PHPIPAMAPI\Request $request
* @return array(
* @type \Psr\Http\Message\ResponseInterface Response, if we received one
* @type \MyENA\PHPIPAMAPI\Error Error, if we saw one
* )
*/
public function do(Request $request): array {
$psrRequest = $this->compilePSR7($request);
if (!$this->silent) {
$this->logger->debug("Executing: {$psrRequest->getMethod()} {$psrRequest->getUri()}");
}
try {
$resp = $this->config->getClient()->send($psrRequest, self::$requestOptions);
} catch (GuzzleException $e) {
if (!$this->silent) {
$this->logger->error("Query returned {$e->getCode()}: {$e->getMessage()}");
}
// TODO: do something different here?
return [null, new TransportError($e->getCode(), $e->getMessage())];
} catch (\Exception $e) {
if (!$this->silent) {
$this->logger->error("Query returned {$e->getCode()}: {$e->getMessage()}");
}
return [null, new TransportError($e->getCode(), $e->getMessage())];
}
if (!$this->silent) {
$this->logger->debug("Query returned {$resp->getStatusCode()}: {$resp->getReasonPhrase()}");
}
$code = $resp->getStatusCode();
if (200 > $code || $code >= 300) {
return [$resp, new ApiError($code, $resp->getBody()->getContents())];
}
// if this request modifies user session information, try to prevent weirdness.
if (UserController::PATH === $request->uri()) {
$this->updateSession($psrRequest, $resp);
}
return [$resp, null];
}
/**
* @return string
*/
private function serviceAddress(): string {
if (!isset($this->serviceAddress)) {
$this->serviceAddress = sprintf(
'%s://%s%s/api/%s/',
$this->config->useHTTPS() ? 'https' : 'http',
$this->config->getHost(),
(0 === ($port = $this->config->getPort()) ? '' : ":{$port}"),
$this->config->getAppID()
);
}
return $this->serviceAddress;
}
/**
* Will attempt return the current user session for this client, attempting to procure one first if not set
*
* @return \MyENA\PHPIPAMAPI\Models\UserSession
*/
private function clientUserSession(): ?UserSession {
if (!isset($this->clientUserSession)) {
/** @var \MyENA\PHPIPAMAPI\Chain\User\POSTResponse $resp */
/** @var \MyENA\PHPIPAMAPI\Error $err */
[$resp, $err] = $this->User()->POST()->execute();
if (null !== $err) {
throw new \RuntimeException(sprintf(
'Unable to authenticate user %s: %s',
$this->config->getUsername(),
$err
));
}
$this->clientUserSession = new UserSession($resp->getData()->getToken(), $resp->getData()->getExpires());
}
return $this->clientUserSession ?? null;
}
/**
* There are probably many, many situations this will miss.
*
* @param \Psr\Http\Message\RequestInterface
* @param \Psr\Http\Message\ResponseInterface $response
*/
private function updateSession(RequestInterface $request, ResponseInterface $response): void {
$method = $request->getMethod();
if ('DELETE' === $method) {
if (isset($this->clientUserSession) &&
$request->getHeaderLine(PHPIPAM_TOKEN_HEADER) === $this->clientUserSession->getToken()) {
if (!$this->silent) {
$this->logger->info('Client token invalidated');
}
$this->clientUserSession = null;
}
} else if ('PATCH' === $method) {
if (isset($this->clientUserSession) &&
$request->getHeaderLine(PHPIPAM_TOKEN_HEADER) === $this->clientUserSession->getToken()) {
$this->clientUserSession->refreshFromPSR7Response($response);
if (!$this->silent) {
$this->logger->info('Client token has been refreshed');
}
}
} else if ('POST' === $method) {
if ($request->getHeaderLine('Authorization') === sprintf(
'Basic %s',
base64_encode("{$this->getConfig()->getUsername()}:{$this->getConfig()->getPassword()}")
)) {
if (isset($this->clientUserSession) && $this->clientUserSession->getExpiresTime()->After(Time::Now())) {
if (!$this->silent) {
$this->logger->warning(sprintf(
'Login call made using configured credentials despite existing session not expiring for another "%s", will attempt to cleanup existing session...',
$this->clientUserSession->getExpiresTime()->UnixNanoDuration()
));
}
[$_, $err] = $this->User()->DELETE()->execute();
if (null !== $err) {
if (!$this->silent) {
$this->logger->warning(sprintf('Error cleaning up existing session: %s', $err));
}
}
}
if (!$this->silent) {
$this->logger->info('Client token created');
}
$this->clientUserSession = UserSession::fromPSR7Response($response);
}
}
}
/**
* @param \MyENA\PHPIPAMAPI\Request $r
* @return \Psr\Http\Message\RequestInterface
*/
private function compilePSR7(Request $r): RequestInterface {
$uri = new PSR7Uri("{$this->serviceAddress()}{$r->uri()}");
if (0 < (count($r->parameters()))) {
$params = [];
foreach ($r->parameters() as $k => $v) {
$params[] = "{$k}={$v}";
}
$uri = $uri->withQuery(implode('&', $params));
}
return new PSR7Request(
$r->method(),
$uri,
$r->headers() +
($r->authenticated() ? [PHPIPAM_TOKEN_HEADER => $this->clientUserSession()->getToken()] : []),
$r->body());
}
}