Debian Codegraph @

includes/shell/ShellboxClientFactory.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Shell;
4
5 use GuzzleHttp\Psr7\Uri;
6 use GuzzleHttp\RequestOptions;
7 use MediaWiki\Http\HttpRequestFactory;
8 use RuntimeException;
9 use Shellbox\Client;
10 use Shellbox\RPC\LocalRpcClient;
11 use Shellbox\RPC\RpcClient;
12
13 /**
14 * This is a service which provides a configured client to access a remote
15 * Shellbox installation.
16 *
17 * @since 1.36
18 */
19 class ShellboxClientFactory {
20 /** @var HttpRequestFactory */
21 private $requestFactory;
22 /** @var (string|false|null)[]|null */
23 private $urls;
24 /** @var string|null */
25 private $key;
26
27 /** The default request timeout, in seconds */
28 public const DEFAULT_TIMEOUT = 10;
29
30 /**
31 * @internal Use MediaWikiServices::getShellboxClientFactory()
32 * @param HttpRequestFactory $requestFactory The factory which will be used
33 * to make HTTP clients.
34 * @param (string|false|null)[]|null $urls The Shellbox base URL mapping
35 * @param string|null $key The shared secret key used for HMAC authentication
36 */
37 public function __construct( HttpRequestFactory $requestFactory, $urls, $key ) {
38 $this->requestFactory = $requestFactory;
39 $this->urls = $urls;
40 $this->key = $key;
41 }
42
43 /**
44 * Test whether remote Shellbox is enabled by configuration.
45 *
46 * @param string|null $service Same as the service option for getClient.
47 * @return bool
48 */
49 public function isEnabled( ?string $service = null ): bool {
50 return $this->getUrl( $service ) !== null;
51 }
52
53 /**
54 * Get a Shellbox client with the specified options. If remote Shellbox is
55 * not configured (isEnabled() returns false), an exception will be thrown.
56 *
57 * @param array $options Associative array of options:
58 * - timeout: The request timeout in seconds
59 * - service: the shellbox backend name to get the URL from the mapping
60 * @return Client
61 * @throws RuntimeException
62 */
63 public function getClient( array $options = [] ) {
64 $url = $this->getUrl( $options['service'] ?? null );
65 if ( $url === null ) {
66 throw new RuntimeException( 'To use a remote shellbox to run shell commands, ' .
67 '$wgShellboxUrls and $wgShellboxSecretKey must be configured.' );
68 }
69
70 return new Client(
71 $this->requestFactory->createGuzzleClient( [
72 RequestOptions::TIMEOUT => $options['timeout'] ?? self::DEFAULT_TIMEOUT,
73 RequestOptions::HTTP_ERRORS => false,
74 ] ),
75 new Uri( $url ),
76 $this->key,
77 [ 'allowUrlFiles' => true ]
78 );
79 }
80
81 /**
82 * Get a Shellbox RPC client with the specified options. If remote Shellbox is
83 * not configured (isEnabled() returns false), an exception will be thrown.
84 *
85 * @param array $options Associative array of options:
86 * - timeout: The request timeout in seconds
87 * - service: the shellbox backend name to get the URL from the mapping
88 * @return RpcClient
89 * @throws RuntimeException
90 */
91 public function getRemoteRpcClient( array $options = [] ): RpcClient {
92 return $this->getClient( $options );
93 }
94
95 /**
96 * Get a Shellbox RPC client with specified options. If remote Shellbox is
97 * not configured (isEnabled() returns false), a local fallback is returned.
98 *
99 * @param array $options
100 * @return RpcClient
101 */
102 public function getRpcClient( array $options = [] ): RpcClient {
103 $url = $this->getUrl( $options['service'] ?? null );
104 if ( $url === null ) {
105 return new LocalRpcClient();
106 }
107 return $this->getRemoteRpcClient( $options );
108 }
109
110 private function getUrl( ?string $service ): ?string {
111 if ( $this->urls === null || $this->key === null || $this->key === '' ) {
112 return null;
113 }
114 $url = $this->urls[$service ?? ''] ?? $this->urls['default'] ?? null;
115 if ( !is_string( $url ) ) {
116 return null;
117 }
118 return $url;
119 }
120
121 }