TraceMiddleware.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace Aws;
  3. use Aws\Exception\AwsException;
  4. use GuzzleHttp\Promise\RejectedPromise;
  5. use Psr\Http\Message\RequestInterface;
  6. use Psr\Http\Message\ResponseInterface;
  7. use Psr\Http\Message\StreamInterface;
  8. /**
  9. * Traces state changes between middlewares.
  10. */
  11. class TraceMiddleware
  12. {
  13. private $prevOutput;
  14. private $prevInput;
  15. private $config;
  16. private static $authHeaders = [
  17. 'X-Amz-Security-Token' => '[TOKEN]',
  18. ];
  19. private static $authStrings = [
  20. // S3Signature
  21. '/AWSAccessKeyId=[A-Z0-9]{20}&/i' => 'AWSAccessKeyId=[KEY]&',
  22. // SignatureV4 Signature and S3Signature
  23. '/Signature=.+/i' => 'Signature=[SIGNATURE]',
  24. // SignatureV4 access key ID
  25. '/Credential=[A-Z0-9]{20}\//i' => 'Credential=[KEY]/',
  26. // S3 signatures
  27. '/AWS [A-Z0-9]{20}:.+/' => 'AWS AKI[KEY]:[SIGNATURE]',
  28. // STS Presigned URLs
  29. '/X-Amz-Security-Token=[^&]+/i' => 'X-Amz-Security-Token=[TOKEN]',
  30. // Crypto *Stream Keys
  31. '/\["key.{27,36}Stream.{9}\]=>\s+.{7}\d{2}\) "\X{16,64}"/U' => '["key":[CONTENT KEY]]',
  32. ];
  33. /**
  34. * Configuration array can contain the following key value pairs.
  35. *
  36. * - logfn: (callable) Function that is invoked with log messages. By
  37. * default, PHP's "echo" function will be utilized.
  38. * - stream_size: (int) When the size of a stream is greater than this
  39. * number, the stream data will not be logged. Set to "0" to not log any
  40. * stream data.
  41. * - scrub_auth: (bool) Set to false to disable the scrubbing of auth data
  42. * from the logged messages.
  43. * - http: (bool) Set to false to disable the "debug" feature of lower
  44. * level HTTP adapters (e.g., verbose curl output).
  45. * - auth_strings: (array) A mapping of authentication string regular
  46. * expressions to scrubbed strings. These mappings are passed directly to
  47. * preg_replace (e.g., preg_replace($key, $value, $debugOutput) if
  48. * "scrub_auth" is set to true.
  49. * - auth_headers: (array) A mapping of header names known to contain
  50. * sensitive data to what the scrubbed value should be. The value of any
  51. * headers contained in this array will be replaced with the if
  52. * "scrub_auth" is set to true.
  53. */
  54. public function __construct(array $config = [])
  55. {
  56. $this->config = $config + [
  57. 'logfn' => function ($value) { echo $value; },
  58. 'stream_size' => 524288,
  59. 'scrub_auth' => true,
  60. 'http' => true,
  61. 'auth_strings' => [],
  62. 'auth_headers' => [],
  63. ];
  64. $this->config['auth_strings'] += self::$authStrings;
  65. $this->config['auth_headers'] += self::$authHeaders;
  66. }
  67. public function __invoke($step, $name)
  68. {
  69. $this->prevOutput = $this->prevInput = [];
  70. return function (callable $next) use ($step, $name) {
  71. return function (
  72. CommandInterface $command,
  73. RequestInterface $request = null
  74. ) use ($next, $step, $name) {
  75. $this->createHttpDebug($command);
  76. $start = microtime(true);
  77. $this->stepInput([
  78. 'step' => $step,
  79. 'name' => $name,
  80. 'request' => $this->requestArray($request),
  81. 'command' => $this->commandArray($command)
  82. ]);
  83. return $next($command, $request)->then(
  84. function ($value) use ($step, $name, $command, $start) {
  85. $this->flushHttpDebug($command);
  86. $this->stepOutput($start, [
  87. 'step' => $step,
  88. 'name' => $name,
  89. 'result' => $this->resultArray($value),
  90. 'error' => null
  91. ]);
  92. return $value;
  93. },
  94. function ($reason) use ($step, $name, $start, $command) {
  95. $this->flushHttpDebug($command);
  96. $this->stepOutput($start, [
  97. 'step' => $step,
  98. 'name' => $name,
  99. 'result' => null,
  100. 'error' => $this->exceptionArray($reason)
  101. ]);
  102. return new RejectedPromise($reason);
  103. }
  104. );
  105. };
  106. };
  107. }
  108. private function stepInput($entry)
  109. {
  110. static $keys = ['command', 'request'];
  111. $this->compareStep($this->prevInput, $entry, '-> Entering', $keys);
  112. $this->write("\n");
  113. $this->prevInput = $entry;
  114. }
  115. private function stepOutput($start, $entry)
  116. {
  117. static $keys = ['result', 'error'];
  118. $this->compareStep($this->prevOutput, $entry, '<- Leaving', $keys);
  119. $totalTime = microtime(true) - $start;
  120. $this->write(" Inclusive step time: " . $totalTime . "\n\n");
  121. $this->prevOutput = $entry;
  122. }
  123. private function compareStep(array $a, array $b, $title, array $keys)
  124. {
  125. $changes = [];
  126. foreach ($keys as $key) {
  127. $av = isset($a[$key]) ? $a[$key] : null;
  128. $bv = isset($b[$key]) ? $b[$key] : null;
  129. $this->compareArray($av, $bv, $key, $changes);
  130. }
  131. $str = "\n{$title} step {$b['step']}, name '{$b['name']}'";
  132. $str .= "\n" . str_repeat('-', strlen($str) - 1) . "\n\n ";
  133. $str .= $changes
  134. ? implode("\n ", str_replace("\n", "\n ", $changes))
  135. : 'no changes';
  136. $this->write($str . "\n");
  137. }
  138. private function commandArray(CommandInterface $cmd)
  139. {
  140. return [
  141. 'instance' => spl_object_hash($cmd),
  142. 'name' => $cmd->getName(),
  143. 'params' => $cmd->toArray()
  144. ];
  145. }
  146. private function requestArray(RequestInterface $request = null)
  147. {
  148. return !$request ? [] : array_filter([
  149. 'instance' => spl_object_hash($request),
  150. 'method' => $request->getMethod(),
  151. 'headers' => $this->redactHeaders($request->getHeaders()),
  152. 'body' => $this->streamStr($request->getBody()),
  153. 'scheme' => $request->getUri()->getScheme(),
  154. 'port' => $request->getUri()->getPort(),
  155. 'path' => $request->getUri()->getPath(),
  156. 'query' => $request->getUri()->getQuery(),
  157. ]);
  158. }
  159. private function responseArray(ResponseInterface $response = null)
  160. {
  161. return !$response ? [] : [
  162. 'instance' => spl_object_hash($response),
  163. 'statusCode' => $response->getStatusCode(),
  164. 'headers' => $this->redactHeaders($response->getHeaders()),
  165. 'body' => $this->streamStr($response->getBody())
  166. ];
  167. }
  168. private function resultArray($value)
  169. {
  170. return $value instanceof ResultInterface
  171. ? [
  172. 'instance' => spl_object_hash($value),
  173. 'data' => $value->toArray()
  174. ] : $value;
  175. }
  176. private function exceptionArray($e)
  177. {
  178. if (!($e instanceof \Exception)) {
  179. return $e;
  180. }
  181. $result = [
  182. 'instance' => spl_object_hash($e),
  183. 'class' => get_class($e),
  184. 'message' => $e->getMessage(),
  185. 'file' => $e->getFile(),
  186. 'line' => $e->getLine(),
  187. 'trace' => $e->getTraceAsString(),
  188. ];
  189. if ($e instanceof AwsException) {
  190. $result += [
  191. 'type' => $e->getAwsErrorType(),
  192. 'code' => $e->getAwsErrorCode(),
  193. 'requestId' => $e->getAwsRequestId(),
  194. 'statusCode' => $e->getStatusCode(),
  195. 'result' => $this->resultArray($e->getResult()),
  196. 'request' => $this->requestArray($e->getRequest()),
  197. 'response' => $this->responseArray($e->getResponse()),
  198. ];
  199. }
  200. return $result;
  201. }
  202. private function compareArray($a, $b, $path, array &$diff)
  203. {
  204. if ($a === $b) {
  205. return;
  206. }
  207. if (is_array($a)) {
  208. $b = (array) $b;
  209. $keys = array_unique(array_merge(array_keys($a), array_keys($b)));
  210. foreach ($keys as $k) {
  211. if (!array_key_exists($k, $a)) {
  212. $this->compareArray(null, $b[$k], "{$path}.{$k}", $diff);
  213. } elseif (!array_key_exists($k, $b)) {
  214. $this->compareArray($a[$k], null, "{$path}.{$k}", $diff);
  215. } else {
  216. $this->compareArray($a[$k], $b[$k], "{$path}.{$k}", $diff);
  217. }
  218. }
  219. } elseif ($a !== null && $b === null) {
  220. $diff[] = "{$path} was unset";
  221. } elseif ($a === null && $b !== null) {
  222. $diff[] = sprintf("%s was set to %s", $path, $this->str($b));
  223. } else {
  224. $diff[] = sprintf("%s changed from %s to %s", $path, $this->str($a), $this->str($b));
  225. }
  226. }
  227. private function str($value)
  228. {
  229. if (is_scalar($value)) {
  230. return (string) $value;
  231. }
  232. if ($value instanceof \Exception) {
  233. $value = $this->exceptionArray($value);
  234. }
  235. ob_start();
  236. var_dump($value);
  237. return ob_get_clean();
  238. }
  239. private function streamStr(StreamInterface $body)
  240. {
  241. return $body->getSize() < $this->config['stream_size']
  242. ? (string) $body
  243. : 'stream(size=' . $body->getSize() . ')';
  244. }
  245. private function createHttpDebug(CommandInterface $command)
  246. {
  247. if ($this->config['http'] && !isset($command['@http']['debug'])) {
  248. $command['@http']['debug'] = fopen('php://temp', 'w+');
  249. }
  250. }
  251. private function flushHttpDebug(CommandInterface $command)
  252. {
  253. if ($res = $command['@http']['debug']) {
  254. rewind($res);
  255. $this->write(stream_get_contents($res));
  256. fclose($res);
  257. $command['@http']['debug'] = null;
  258. }
  259. }
  260. private function write($value)
  261. {
  262. if ($this->config['scrub_auth']) {
  263. foreach ($this->config['auth_strings'] as $pattern => $replacement) {
  264. $value = preg_replace_callback(
  265. $pattern,
  266. function ($matches) use ($replacement) {
  267. return $replacement;
  268. },
  269. $value
  270. );
  271. }
  272. }
  273. call_user_func($this->config['logfn'], $value);
  274. }
  275. private function redactHeaders(array $headers)
  276. {
  277. if ($this->config['scrub_auth']) {
  278. $headers = $this->config['auth_headers'] + $headers;
  279. }
  280. return $headers;
  281. }
  282. }