Debian Codegraph @

vendor/wikimedia/shellbox/src/Command/RemoteBoxedExecutor.php

raw
1 <?php
2
3 namespace Shellbox\Command;
4
5 use Psr\Log\LoggerInterface;
6 use Psr\Log\NullLogger;
7 use Shellbox\Client;
8 use Shellbox\Shellbox;
9 use Shellbox\ShellboxError;
10
11 /**
12 * A BoxedExecutor which works by running the command on a remote server via
13 * HTTP/HTTPS.
14 */
15 class RemoteBoxedExecutor extends BoxedExecutor {
16 /** @var Client */
17 private $client;
18
19 /** @var LoggerInterface */
20 private $logger;
21
22 /**
23 * @param Client $client
24 */
25 public function __construct( Client $client ) {
26 $this->client = $client;
27 $this->logger = new NullLogger;
28 }
29
30 /**
31 * Set the logger
32 *
33 * @param LoggerInterface $logger
34 */
35 public function setLogger( LoggerInterface $logger ) {
36 $this->logger = $logger;
37 }
38
39 public function executeValid( BoxedCommand $command ) {
40 $this->checkForUrlFiles( $command );
41
42 // Compile the binary parts
43 $parts = [ [
44 'name' => 'json-data',
45 'headers' => [
46 'Content-Disposition' => "json-data",
47 'Content-Type' => 'application/json',
48 ],
49 'contents' => Shellbox::jsonEncode( [
50 'action' => 'shell',
51 'command' => $command->getClientData(),
52 ] )
53 ] ];
54 foreach ( $command->getInputFiles() as $boxedName => $file ) {
55 if ( $file instanceof InputFileWithContents ) {
56 $parts[] = [
57 'name' => $boxedName,
58 'headers' => [
59 'Content-Disposition' =>
60 "attachment; name=\"$boxedName\"; filename=\"$boxedName\"",
61 'Content-Type' => 'application/octet-stream',
62 ],
63 'contents' => $file->getStreamOrString()
64 ];
65 }
66 }
67 $stdin = $command->getStdin();
68 if ( $stdin !== '' ) {
69 $parts[] = [
70 'name' => 'stdin',
71 'headers' => [
72 'Content-Disposition' => "form-data; name=stdin",
73 'Content-Type' => 'application/octet-stream'
74 ],
75 'contents' => $stdin
76 ];
77 }
78 $files = $command->getOutputFiles();
79 $globs = $command->getOutputGlobs();
80
81 // Send the request
82 $resultData = $this->client->sendRequest(
83 'shell/' . $command->getRouteName(),
84 $parts,
85 $files,
86 $globs
87 );
88
89 // Validate the response
90 if ( !isset( $resultData['exitCode'] ) ) {
91 throw new ShellboxError( 'Server result is missing the exit code' );
92 }
93
94 // Forward log entries
95 foreach ( $resultData['log'] ?? [] as $logEntry ) {
96 $this->logger->log(
97 $logEntry['level'],
98 $logEntry['message'],
99 $logEntry['context']
100 );
101 }
102
103 // Construct the result
104 $result = ( new BoxedResult )
105 ->exitCode( $resultData['exitCode'] )
106 ->stdout( $resultData['stdout'] ?? null )
107 ->stderr( $resultData['stderr'] ?? null );
108
109 $uploadedFiles = array_fill_keys( $resultData['uploadedFiles'] ?? [], true );
110 foreach ( $files as $boxedName => $file ) {
111 if ( $file->wasReceived() || isset( $uploadedFiles[$boxedName] ) ) {
112 $result->addOutputFile( $boxedName, $file );
113 }
114 }
115
116 foreach ( $globs as $glob ) {
117 foreach ( $glob->getFiles() as $boxedName => $file ) {
118 $result->addOutputFile( $boxedName, $file );
119 }
120 }
121
122 return $result;
123 }
124
125 public function areUrlFilesAllowed() {
126 return $this->client->areUrlFilesAllowed();
127 }
128
129 /**
130 * If URL files are not allowed by client configuration, throw an exception
131 * if the specified command has any such files.
132 *
133 * Applications should check areUrlFilesAllowed() before calling
134 * inputFileFromUrl() etc. to avoid an exception from this method.
135 *
136 * @param BoxedCommand $command
137 * @throws ShellboxError
138 */
139 private function checkForUrlFiles( BoxedCommand $command ) {
140 if ( $this->areUrlFilesAllowed() ) {
141 return;
142 }
143 foreach ( $command->getInputFiles() as $boxedName => $file ) {
144 if ( $file instanceof InputFileFromUrl ) {
145 throw new ShellboxError(
146 "Can't download input file \"$boxedName\" when " .
147 "allowUrlFiles is not set in the Client options" );
148 }
149 }
150 foreach ( $command->getOutputFiles() as $boxedName => $outputFile ) {
151 if ( $outputFile instanceof OutputFileToUrl ) {
152 throw new ShellboxError(
153 "Can't upload output file \"$boxedName\" when " .
154 "allowUrlOutput is not set in the Client options" );
155 }
156 }
157 foreach ( $command->getOutputGlobs() as $id => $outputGlob ) {
158 if ( $outputGlob instanceof OutputGlobToUrl ) {
159 throw new ShellboxError(
160 "Can't upload output glob \"$id\" when " .
161 "allowUrlOutput is not set in the Client options" );
162 }
163 }
164 }
165 }