RESTBagOStuff.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. use Psr\Log\LoggerInterface;
  3. /**
  4. * Interface to key-value storage behind an HTTP server.
  5. *
  6. * Uses URL of the form "baseURL/{KEY}" to store, fetch, and delete values.
  7. *
  8. * E.g., when base URL is `/sessions/v1`, then the store would do:
  9. *
  10. * `PUT /sessions/v1/12345758`
  11. *
  12. * and fetch would do:
  13. *
  14. * `GET /sessions/v1/12345758`
  15. *
  16. * delete would do:
  17. *
  18. * `DELETE /sessions/v1/12345758`
  19. *
  20. * Minimal generic configuration:
  21. *
  22. * @code
  23. * $wgObjectCaches['sessions'] = array(
  24. * 'class' => 'RESTBagOStuff',
  25. * 'url' => 'http://localhost:7231/wikimedia.org/somepath/'
  26. * );
  27. * @endcode
  28. *
  29. * Configuration for Kask (session storage):
  30. * @code
  31. * $wgObjectCaches['sessions'] = array(
  32. * 'class' => 'RESTBagOStuff',
  33. * 'url' => 'https://kaskhost:1234/sessions/v1/',
  34. * 'httpParams' => [
  35. * 'readHeaders' => [],
  36. * 'writeHeaders' => [ 'content-type' => 'application/octet-stream' ],
  37. * 'deleteHeaders' => [],
  38. * 'writeMethod' => 'POST',
  39. * ],
  40. * 'serialization_type' => 'JSON',
  41. * 'extendedErrorBodyFields' => [ 'type', 'title', 'detail', 'instance' ]
  42. * );
  43. * $wgSessionCacheType = 'sessions';
  44. * @endcode
  45. */
  46. class RESTBagOStuff extends MediumSpecificBagOStuff {
  47. /**
  48. * Default connection timeout in seconds. The kernel retransmits the SYN
  49. * packet after 1 second, so 1.2 seconds allows for 1 retransmit without
  50. * permanent failure.
  51. */
  52. const DEFAULT_CONN_TIMEOUT = 1.2;
  53. /**
  54. * Default request timeout
  55. */
  56. const DEFAULT_REQ_TIMEOUT = 3.0;
  57. /**
  58. * @var MultiHttpClient
  59. */
  60. private $client;
  61. /**
  62. * REST URL to use for storage.
  63. * @var string
  64. */
  65. private $url;
  66. /**
  67. * @var array http parameters: readHeaders, writeHeaders, deleteHeaders, writeMethod
  68. */
  69. private $httpParams;
  70. /**
  71. * Optional serialization type to use. Allowed values: "PHP", "JSON", or "legacy".
  72. * "legacy" is PHP serialization with no serialization type tagging or hmac protection.
  73. * @var string
  74. * @deprecated since 1.34, the "legacy" value will be removed in 1.35.
  75. * Use either "PHP" or "JSON".
  76. */
  77. private $serializationType;
  78. /**
  79. * Optional HMAC Key for protecting the serialized blob. If omitted, or if serializationType
  80. * is "legacy", then no protection is done
  81. * @var string
  82. */
  83. private $hmacKey;
  84. /**
  85. * @var array additional body fields to log on error, if possible
  86. */
  87. private $extendedErrorBodyFields;
  88. public function __construct( $params ) {
  89. $params['segmentationSize'] = $params['segmentationSize'] ?? INF;
  90. if ( empty( $params['url'] ) ) {
  91. throw new InvalidArgumentException( 'URL parameter is required' );
  92. }
  93. if ( empty( $params['client'] ) ) {
  94. // Pass through some params to the HTTP client.
  95. $clientParams = [
  96. 'connTimeout' => $params['connTimeout'] ?? self::DEFAULT_CONN_TIMEOUT,
  97. 'reqTimeout' => $params['reqTimeout'] ?? self::DEFAULT_REQ_TIMEOUT,
  98. ];
  99. foreach ( [ 'caBundlePath', 'proxy' ] as $key ) {
  100. if ( isset( $params[$key] ) ) {
  101. $clientParams[$key] = $params[$key];
  102. }
  103. }
  104. $this->client = new MultiHttpClient( $clientParams );
  105. } else {
  106. $this->client = $params['client'];
  107. }
  108. $this->httpParams['writeMethod'] = $params['httpParams']['writeMethod'] ?? 'PUT';
  109. $this->httpParams['readHeaders'] = $params['httpParams']['readHeaders'] ?? [];
  110. $this->httpParams['writeHeaders'] = $params['httpParams']['writeHeaders'] ?? [];
  111. $this->httpParams['deleteHeaders'] = $params['httpParams']['deleteHeaders'] ?? [];
  112. $this->extendedErrorBodyFields = $params['extendedErrorBodyFields'] ?? [];
  113. $this->serializationType = $params['serialization_type'] ?? 'legacy';
  114. $this->hmacKey = $params['hmac_key'] ?? '';
  115. // The parent constructor calls setLogger() which sets the logger in $this->client
  116. parent::__construct( $params );
  117. // Make sure URL ends with /
  118. $this->url = rtrim( $params['url'], '/' ) . '/';
  119. // Default config, R+W > N; no locks on reads though; writes go straight to state-machine
  120. $this->attrMap[self::ATTR_SYNCWRITES] = self::QOS_SYNCWRITES_QC;
  121. }
  122. public function setLogger( LoggerInterface $logger ) {
  123. parent::setLogger( $logger );
  124. $this->client->setLogger( $logger );
  125. }
  126. protected function doGet( $key, $flags = 0, &$casToken = null ) {
  127. $casToken = null;
  128. $req = [
  129. 'method' => 'GET',
  130. 'url' => $this->url . rawurlencode( $key ),
  131. 'headers' => $this->httpParams['readHeaders'],
  132. ];
  133. list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
  134. if ( $rcode === 200 ) {
  135. if ( is_string( $rbody ) ) {
  136. $value = $this->decodeBody( $rbody );
  137. /// @FIXME: use some kind of hash or UUID header as CAS token
  138. $casToken = ( $value !== false ) ? $rbody : null;
  139. return $value;
  140. }
  141. return false;
  142. }
  143. if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
  144. return $this->handleError( "Failed to fetch $key", $rcode, $rerr, $rhdrs, $rbody );
  145. }
  146. return false;
  147. }
  148. protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
  149. // @TODO: respect WRITE_SYNC (e.g. EACH_QUORUM)
  150. // @TODO: respect $exptime
  151. $req = [
  152. 'method' => $this->httpParams['writeMethod'],
  153. 'url' => $this->url . rawurlencode( $key ),
  154. 'body' => $this->encodeBody( $value ),
  155. 'headers' => $this->httpParams['writeHeaders'],
  156. ];
  157. list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
  158. if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
  159. return true;
  160. }
  161. return $this->handleError( "Failed to store $key", $rcode, $rerr, $rhdrs, $rbody );
  162. }
  163. protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
  164. // @TODO: make this atomic
  165. if ( $this->get( $key ) === false ) {
  166. return $this->set( $key, $value, $exptime, $flags );
  167. }
  168. return false; // key already set
  169. }
  170. protected function doDelete( $key, $flags = 0 ) {
  171. // @TODO: respect WRITE_SYNC (e.g. EACH_QUORUM)
  172. $req = [
  173. 'method' => 'DELETE',
  174. 'url' => $this->url . rawurlencode( $key ),
  175. 'headers' => $this->httpParams['deleteHeaders'],
  176. ];
  177. list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
  178. if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
  179. return true;
  180. }
  181. return $this->handleError( "Failed to delete $key", $rcode, $rerr, $rhdrs, $rbody );
  182. }
  183. public function incr( $key, $value = 1, $flags = 0 ) {
  184. // @TODO: make this atomic
  185. $n = $this->get( $key, self::READ_LATEST );
  186. if ( $this->isInteger( $n ) ) { // key exists?
  187. $n = max( $n + (int)$value, 0 );
  188. // @TODO: respect $exptime
  189. return $this->set( $key, $n ) ? $n : false;
  190. }
  191. return false;
  192. }
  193. public function decr( $key, $value = 1, $flags = 0 ) {
  194. return $this->incr( $key, -$value, $flags );
  195. }
  196. /**
  197. * Processes the response body.
  198. *
  199. * @param string $body request body to process
  200. * @return mixed|bool the processed body, or false on error
  201. */
  202. private function decodeBody( $body ) {
  203. if ( $this->serializationType === 'legacy' ) {
  204. $serialized = $body;
  205. } else {
  206. $pieces = explode( '.', $body, 3 );
  207. if ( count( $pieces ) !== 3 || $pieces[0] !== $this->serializationType ) {
  208. return false;
  209. }
  210. list( , $hmac, $serialized ) = $pieces;
  211. if ( $this->hmacKey !== '' ) {
  212. $checkHmac = hash_hmac( 'sha256', $serialized, $this->hmacKey, true );
  213. if ( !hash_equals( $checkHmac, base64_decode( $hmac ) ) ) {
  214. return false;
  215. }
  216. }
  217. }
  218. switch ( $this->serializationType ) {
  219. case 'JSON':
  220. $value = json_decode( $serialized, true );
  221. return ( json_last_error() === JSON_ERROR_NONE ) ? $value : false;
  222. case 'PHP':
  223. case 'legacy':
  224. return unserialize( $serialized );
  225. default:
  226. throw new \DomainException(
  227. "Unknown serialization type: $this->serializationType"
  228. );
  229. }
  230. }
  231. /**
  232. * Prepares the request body (the "value" portion of our key/value store) for transmission.
  233. *
  234. * @param string $body request body to prepare
  235. * @return string the prepared body
  236. * @throws LogicException
  237. */
  238. private function encodeBody( $body ) {
  239. switch ( $this->serializationType ) {
  240. case 'JSON':
  241. $value = json_encode( $body );
  242. if ( $value === false ) {
  243. throw new InvalidArgumentException( __METHOD__ . ": body could not be encoded." );
  244. }
  245. break;
  246. case 'PHP':
  247. case "legacy":
  248. $value = serialize( $body );
  249. break;
  250. default:
  251. throw new \DomainException(
  252. "Unknown serialization type: $this->serializationType"
  253. );
  254. }
  255. if ( $this->serializationType !== 'legacy' ) {
  256. if ( $this->hmacKey !== '' ) {
  257. $hmac = base64_encode(
  258. hash_hmac( 'sha256', $value, $this->hmacKey, true )
  259. );
  260. } else {
  261. $hmac = '';
  262. }
  263. $value = $this->serializationType . '.' . $hmac . '.' . $value;
  264. }
  265. return $value;
  266. }
  267. /**
  268. * Handle storage error
  269. * @param string $msg Error message
  270. * @param int $rcode Error code from client
  271. * @param string $rerr Error message from client
  272. * @param array $rhdrs Response headers
  273. * @param string $rbody Error body from client (if any)
  274. * @return false
  275. */
  276. protected function handleError( $msg, $rcode, $rerr, $rhdrs, $rbody ) {
  277. $message = "$msg : ({code}) {error}";
  278. $context = [
  279. 'code' => $rcode,
  280. 'error' => $rerr
  281. ];
  282. if ( $this->extendedErrorBodyFields !== [] ) {
  283. $body = $this->decodeBody( $rbody );
  284. if ( $body ) {
  285. $extraFields = '';
  286. foreach ( $this->extendedErrorBodyFields as $field ) {
  287. if ( isset( $body[$field] ) ) {
  288. $extraFields .= " : ({$field}) {$body[$field]}";
  289. }
  290. }
  291. if ( $extraFields !== '' ) {
  292. $message .= " {extra_fields}";
  293. $context['extra_fields'] = $extraFields;
  294. }
  295. }
  296. }
  297. $this->logger->error( $message, $context );
  298. $this->setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
  299. return false;
  300. }
  301. }