Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.66% covered (warning)
68.66%
92 / 134
20.00% covered (danger)
20.00%
2 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
RESTBagOStuff
69.17% covered (warning)
69.17%
92 / 133
20.00% covered (danger)
20.00%
2 / 10
119.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
60.87% covered (warning)
60.87%
14 / 23
0.00% covered (danger)
0.00%
0 / 1
6.50
 setLogger
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 doGet
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
8
 doSet
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 doAdd
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 doDelete
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
2.02
 doIncrWithInit
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 decodeBody
70.59% covered (warning)
70.59%
12 / 17
0.00% covered (danger)
0.00%
0 / 1
11.06
 encodeBody
77.78% covered (warning)
77.78%
14 / 18
0.00% covered (danger)
0.00%
0 / 1
6.40
 handleError
47.06% covered (danger)
47.06%
8 / 17
0.00% covered (danger)
0.00%
0 / 1
14.27
1<?php
2
3namespace Wikimedia\ObjectCache;
4
5use InvalidArgumentException;
6use Psr\Log\LoggerInterface;
7use Wikimedia\Http\MultiHttpClient;
8
9/**
10 * Store key-value data via an HTTP service.
11 *
12 * ### Important caveats
13 *
14 * This interface is currently an incomplete BagOStuff implementation,
15 * supported only for use with MediaWiki features that accept a dedicated
16 * cache type to use for a narrow set of cache keys that share the same
17 * key expiry and replication requirements, and where the key-value server
18 * in question is statically configured with domain knowledge of said
19 * key expiry and replication requirements.
20 *
21 * Specifically, RESTBagOStuff has the following limitations:
22 *
23 * - The expiry parameter is ignored in methods like `set()`.
24 *
25 *   There is not currently an agreed protocol for sending this to a
26 *   server. This class is written for use with MediaWiki\Session\SessionManager
27 *   and Kask/Cassandra at WMF, which does not expose a customizable expiry.
28 *
29 *   As such, it is not recommended to use RESTBagOStuff to back a general
30 *   purpose cache type (such as MediaWiki's main cache, or main stash).
31 *   Instead, it is only supported for MediaWiki features where a cache type
32 *   can be pointed to a narrow set of keys that naturally share the same TTL
33 *   anyway, or where the feature behaves correctly even if the logical expiry
34 *   is longer than specified (e.g. immutable keys, or value verification)
35 *
36 * - Most methods are non-atomic.
37 *
38 *   The class should only be used for get, set, and delete operations.
39 *   Advanced methods like `incr()`, `add()` and `lock()` do exist but
40 *   inherit a native and best-effort implementation based on get+set.
41 *   These should not be relied upon.
42 *
43 * ### Backend requirements
44 *
45 * The HTTP server will receive requests for URLs like `{baseURL}/{KEY}`. It
46 * must implement the GET, PUT and DELETE methods.
47 *
48 * E.g., when the base URL is `/sessions/v1`, then `set()` will:
49 *
50 * `PUT /sessions/v1/mykeyhere`
51 *
52 * and `get()` would do:
53 *
54 * `GET /sessions/v1/mykeyhere`
55 *
56 * and `delete()` would do:
57 *
58 * `DELETE /sessions/v1/mykeyhere`
59 *
60 * ### Example configuration
61 *
62 * Minimal generic configuration:
63 *
64 * @code
65 * $wgObjectCaches['sessions'] = array(
66 *    'class' => 'RESTBagOStuff',
67 *    'url' => 'http://localhost:7231/example/'
68 * );
69 * @endcode
70 *
71 *
72 * Configuration for [Kask](https://www.mediawiki.org/wiki/Kask) session store:
73 * @code
74 * $wgObjectCaches['sessions'] = array(
75 *    'class' => 'RESTBagOStuff',
76 *    'url' => 'https://kaskhost:1234/sessions/v1/',
77 *    'httpParams' => [
78 *        'readHeaders' => [],
79 *        'writeHeaders' => [ 'content-type' => 'application/octet-stream' ],
80 *        'deleteHeaders' => [],
81 *        'writeMethod' => 'POST',
82 *    ],
83 *    'serialization_type' => 'JSON',
84 *     'extendedErrorBodyFields' => [ 'type', 'title', 'detail', 'instance' ]
85 * );
86 * $wgSessionCacheType = 'sessions';
87 * @endcode
88 */
89class RESTBagOStuff extends MediumSpecificBagOStuff {
90    /**
91     * Default connection timeout in seconds. The kernel retransmits the SYN
92     * packet after 1 second, so 1.2 seconds allows for 1 retransmit without
93     * permanent failure.
94     */
95    private const DEFAULT_CONN_TIMEOUT = 1.2;
96
97    /**
98     * Default request timeout
99     */
100    private const DEFAULT_REQ_TIMEOUT = 3.0;
101
102    /**
103     * @var MultiHttpClient
104     */
105    private $client;
106
107    /**
108     * REST URL to use for storage.
109     *
110     * @var string
111     */
112    private $url;
113
114    /**
115     * HTTP parameters: readHeaders, writeHeaders, deleteHeaders, writeMethod.
116     *
117     * @var array
118     */
119    private $httpParams;
120
121    /**
122     * Optional serialization type to use. Allowed values: "PHP", "JSON".
123     *
124     * @var string
125     */
126    private $serializationType;
127
128    /**
129     * Optional HMAC Key for protecting the serialized blob. If omitted no protection is done
130     *
131     * @var string
132     */
133    private $hmacKey;
134
135    /**
136     * @var array additional body fields to log on error, if possible
137     */
138    private $extendedErrorBodyFields;
139
140    public function __construct( array $params ) {
141        $params['segmentationSize'] ??= INF;
142        if ( empty( $params['url'] ) ) {
143            throw new InvalidArgumentException( 'URL parameter is required' );
144        }
145
146        if ( empty( $params['client'] ) ) {
147            // Pass through some params to the HTTP client.
148            $clientParams = [
149                'connTimeout' => $params['connTimeout'] ?? self::DEFAULT_CONN_TIMEOUT,
150                'reqTimeout' => $params['reqTimeout'] ?? self::DEFAULT_REQ_TIMEOUT,
151            ];
152            foreach ( [ 'caBundlePath', 'proxy', 'telemetry' ] as $key ) {
153                if ( isset( $params[$key] ) ) {
154                    $clientParams[$key] = $params[$key];
155                }
156            }
157            $this->client = new MultiHttpClient( $clientParams );
158        } else {
159            $this->client = $params['client'];
160        }
161
162        $this->httpParams['writeMethod'] = $params['httpParams']['writeMethod'] ?? 'PUT';
163        $this->httpParams['readHeaders'] = $params['httpParams']['readHeaders'] ?? [];
164        $this->httpParams['writeHeaders'] = $params['httpParams']['writeHeaders'] ?? [];
165        $this->httpParams['deleteHeaders'] = $params['httpParams']['deleteHeaders'] ?? [];
166        $this->extendedErrorBodyFields = $params['extendedErrorBodyFields'] ?? [];
167        $this->serializationType = $params['serialization_type'] ?? 'PHP';
168        $this->hmacKey = $params['hmac_key'] ?? '';
169
170        // The parent constructor calls setLogger() which sets the logger in $this->client
171        parent::__construct( $params );
172
173        // Make sure URL ends with /
174        $this->url = rtrim( $params['url'], '/' ) . '/';
175
176        $this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_DISK;
177    }
178
179    public function setLogger( LoggerInterface $logger ): void {
180        parent::setLogger( $logger );
181        $this->client->setLogger( $logger );
182    }
183
184    /** @inheritDoc */
185    protected function doGet( $key, $flags = 0, &$casToken = null ) {
186        $getToken = ( $casToken === self::PASS_BY_REF );
187        $casToken = null;
188
189        $req = [
190            'method' => 'GET',
191            'url' => $this->url . rawurlencode( $key ),
192            'headers' => $this->httpParams['readHeaders'],
193        ];
194
195        $value = false;
196        $valueSize = false;
197        [ $rcode, , , $rbody, $rerr ] = $this->client->run( $req );
198        if ( $rcode === 200 && is_string( $rbody ) ) {
199            $value = $this->decodeBody( $rbody );
200            $valueSize = strlen( $rbody );
201            // @FIXME: use some kind of hash or UUID header as CAS token
202            if ( $getToken && $value !== false ) {
203                $casToken = $rbody;
204            }
205        } elseif ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
206            $this->handleError( 'Failed to fetch {cacheKey}', $rcode, $rerr, $rbody,
207                [ 'cacheKey' => $key ] );
208        }
209
210        $this->updateOpStats( self::METRIC_OP_GET, [ $key => [ 0, $valueSize ] ] );
211
212        return $value;
213    }
214
215    /** @inheritDoc */
216    protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
217        $req = [
218            'method' => $this->httpParams['writeMethod'],
219            'url' => $this->url . rawurlencode( $key ),
220            'body' => $this->encodeBody( $value ),
221            'headers' => $this->httpParams['writeHeaders'],
222        ];
223
224        [ $rcode, , , $rbody, $rerr ] = $this->client->run( $req );
225        $res = ( $rcode === 200 || $rcode === 201 || $rcode === 204 );
226        if ( !$res ) {
227            $this->handleError( 'Failed to store {cacheKey}', $rcode, $rerr, $rbody,
228                [ 'cacheKey' => $key ] );
229        }
230
231        $this->updateOpStats( self::METRIC_OP_SET, [ $key => [ strlen( $req['body'] ), 0 ] ] );
232
233        return $res;
234    }
235
236    /** @inheritDoc */
237    protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
238        // NOTE: This is non-atomic
239        if ( $this->get( $key ) === false ) {
240            return $this->set( $key, $value, $exptime, $flags );
241        }
242
243        // key already set
244        return false;
245    }
246
247    /** @inheritDoc */
248    protected function doDelete( $key, $flags = 0 ) {
249        $req = [
250            'method' => 'DELETE',
251            'url' => $this->url . rawurlencode( $key ),
252            'headers' => $this->httpParams['deleteHeaders'],
253        ];
254
255        [ $rcode, , , $rbody, $rerr ] = $this->client->run( $req );
256        $res = in_array( $rcode, [ 200, 204, 205, 404, 410 ] );
257        if ( !$res ) {
258            $this->handleError( 'Failed to delete {cacheKey}', $rcode, $rerr, $rbody,
259                [ 'cacheKey' => $key ] );
260        }
261
262        $this->updateOpStats( self::METRIC_OP_DELETE, [ $key ] );
263
264        return $res;
265    }
266
267    /** @inheritDoc */
268    protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
269        // NOTE: This is non-atomic
270        $curValue = $this->doGet( $key );
271        if ( $curValue === false ) {
272            $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false;
273        } elseif ( $this->isInteger( $curValue ) ) {
274            $sum = max( $curValue + $step, 0 );
275            $newValue = $this->doSet( $key, $sum, $exptime ) ? $sum : false;
276        } else {
277            $newValue = false;
278        }
279
280        return $newValue;
281    }
282
283    /**
284     * Processes the response body.
285     *
286     * @param string $body request body to process
287     *
288     * @return mixed|bool the processed body, or false on error
289     */
290    private function decodeBody( $body ) {
291        $pieces = explode( '.', $body, 3 );
292        if ( count( $pieces ) !== 3 || $pieces[0] !== $this->serializationType ) {
293            return false;
294        }
295        [ , $hmac, $serialized ] = $pieces;
296        if ( $this->hmacKey !== '' ) {
297            $checkHmac = hash_hmac( 'sha256', $serialized, $this->hmacKey, true );
298            if ( !hash_equals( $checkHmac, base64_decode( $hmac ) ) ) {
299                return false;
300            }
301        }
302
303        switch ( $this->serializationType ) {
304            case 'JSON':
305                $value = json_decode( $serialized, true );
306                return ( json_last_error() === JSON_ERROR_NONE ) ? $value : false;
307
308            case 'PHP':
309                return unserialize( $serialized );
310
311            default:
312                throw new \DomainException(
313                    "Unknown serialization type: $this->serializationType"
314                );
315        }
316    }
317
318    /**
319     * Prepares the request body (the "value" portion of our key/value store) for transmission.
320     *
321     * @param string $body request body to prepare
322     *
323     * @return string the prepared body
324     */
325    private function encodeBody( $body ) {
326        switch ( $this->serializationType ) {
327            case 'JSON':
328                $value = json_encode( $body );
329                if ( $value === false ) {
330                    throw new InvalidArgumentException( __METHOD__ . ": body could not be encoded." );
331                }
332                break;
333
334            case 'PHP':
335                $value = serialize( $body );
336                break;
337
338            default:
339                throw new \DomainException(
340                    "Unknown serialization type: $this->serializationType"
341                );
342        }
343
344        if ( $this->hmacKey !== '' ) {
345            $hmac = base64_encode(
346                hash_hmac( 'sha256', $value, $this->hmacKey, true )
347            );
348        } else {
349            $hmac = '';
350        }
351        return $this->serializationType . '.' . $hmac . '.' . $value;
352    }
353
354    /**
355     * Handle storage error
356     *
357     * @param string $msg Error message
358     * @param int $rcode Error code from client
359     * @param string $rerr Error message from client
360     * @param string $rbody Error body from client (if any)
361     * @param array $context Error context for PSR-3 logging
362     */
363    private function handleError( $msg, $rcode, $rerr, $rbody, $context = [] ): void {
364        $message = "$msg : ({code}) {error}";
365        $context = [
366            'code' => $rcode,
367            'error' => $rerr
368        ] + $context;
369
370        if ( $this->extendedErrorBodyFields !== [] ) {
371            $body = $this->decodeBody( $rbody );
372            if ( $body ) {
373                $extraFields = '';
374                foreach ( $this->extendedErrorBodyFields as $field ) {
375                    if ( isset( $body[$field] ) ) {
376                        $extraFields .= " : ({$field}{$body[$field]}";
377                    }
378                }
379                if ( $extraFields !== '' ) {
380                    $message .= " {extra_fields}";
381                    $context['extra_fields'] = $extraFields;
382                }
383            }
384        }
385
386        $this->logger->error( $message, $context );
387        $this->setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
388    }
389}
390
391/** @deprecated class alias since 1.43 */
392class_alias( RESTBagOStuff::class, 'RESTBagOStuff' );