2022-10-20 17:23:01 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* S3.php
|
|
|
|
*
|
|
|
|
* an S3 compatible data backend for PrivateBin with CEPH/RadosGW in mind
|
|
|
|
* see https://docs.ceph.com/en/latest/radosgw/s3/php/
|
2023-09-11 13:36:45 -04:00
|
|
|
* based on lib/Data/GoogleCloudStorage.php from PrivateBin version 1.6.0
|
2022-10-20 17:23:01 -04:00
|
|
|
*
|
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
|
|
|
* @copyright 2022 Felix J. Ogris (https://ogris.de/)
|
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
|
|
|
* @version 1.4.1
|
|
|
|
*
|
|
|
|
* Installation:
|
|
|
|
* 1. Make sure you have composer.lock and composer.json in the document root of your PasteBin
|
|
|
|
* 2. If not, grab a copy from https://github.com/PrivateBin/PrivateBin
|
|
|
|
* 3. As non-root user, install the AWS SDK for PHP:
|
|
|
|
* composer require aws/aws-sdk-php
|
|
|
|
* (On FreeBSD, install devel/php-composer2 prior, e.g.: make -C /usr/ports/devel/php-composer2 install clean)
|
|
|
|
* 4. In cfg/conf.php, comment out all [model] and [model_options] settings
|
|
|
|
* 5. Still in cfg/conf.php, add a new [model] section:
|
|
|
|
* [model]
|
|
|
|
* class = S3Storage
|
|
|
|
* 6. Add a new [model_options] as well, e.g. for a Rados gateway as part of your CEPH cluster:
|
|
|
|
* [model_options]
|
|
|
|
* region = ""
|
|
|
|
* version = "2006-03-01"
|
|
|
|
* endpoint = "https://s3.my-ceph.invalid"
|
|
|
|
* use_path_style_endpoint = true
|
|
|
|
* bucket = "my-bucket"
|
|
|
|
* prefix = "privatebin" (place all PrivateBin data beneath this prefix)
|
|
|
|
* accesskey = "my-rados-user"
|
|
|
|
* secretkey = "my-rados-pass"
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PrivateBin\Data;
|
|
|
|
|
|
|
|
use Aws\S3\Exception\S3Exception;
|
|
|
|
use Aws\S3\S3Client;
|
|
|
|
use PrivateBin\Json;
|
|
|
|
|
|
|
|
class S3Storage extends AbstractData
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* S3 client
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var S3Client
|
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private $_client = null;
|
2022-10-20 17:23:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* S3 client options
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private $_options = array();
|
2022-10-20 17:23:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* S3 bucket
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private $_bucket = null;
|
2022-10-20 17:23:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* S3 prefix for all PrivateBin data in this bucket
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private $_prefix = '';
|
2022-10-20 17:23:01 -04:00
|
|
|
|
|
|
|
/**
|
2022-10-27 19:01:02 -04:00
|
|
|
* instantiates a new S3 data backend.
|
2022-10-20 17:23:01 -04:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $options
|
2022-11-01 11:38:06 -04:00
|
|
|
* @return
|
2022-10-20 17:23:01 -04:00
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
public function __construct(array $options)
|
2022-10-20 17:23:01 -04:00
|
|
|
{
|
2023-02-26 20:58:18 -05:00
|
|
|
if (is_array($options)) {
|
2023-02-26 21:26:45 -05:00
|
|
|
// AWS SDK will try to load credentials from environment if credentials are not passed via configuration
|
2023-02-26 20:58:18 -05:00
|
|
|
// ref: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html#default-credential-chain
|
|
|
|
if (isset($options['accesskey']) && isset($options['secretkey'])) {
|
|
|
|
$this->_options['credentials'] = array();
|
2023-02-26 21:26:45 -05:00
|
|
|
|
|
|
|
$this->_options['credentials']['key'] = $options['accesskey'];
|
2023-02-26 20:58:18 -05:00
|
|
|
$this->_options['credentials']['secret'] = $options['secretkey'];
|
|
|
|
}
|
|
|
|
if (array_key_exists('region', $options)) {
|
|
|
|
$this->_options['region'] = $options['region'];
|
|
|
|
}
|
|
|
|
if (array_key_exists('version', $options)) {
|
|
|
|
$this->_options['version'] = $options['version'];
|
|
|
|
}
|
|
|
|
if (array_key_exists('endpoint', $options)) {
|
|
|
|
$this->_options['endpoint'] = $options['endpoint'];
|
|
|
|
}
|
|
|
|
if (array_key_exists('use_path_style_endpoint', $options)) {
|
|
|
|
$this->_options['use_path_style_endpoint'] = filter_var($options['use_path_style_endpoint'], FILTER_VALIDATE_BOOLEAN);
|
|
|
|
}
|
|
|
|
if (array_key_exists('bucket', $options)) {
|
|
|
|
$this->_bucket = $options['bucket'];
|
|
|
|
}
|
|
|
|
if (array_key_exists('prefix', $options)) {
|
|
|
|
$this->_prefix = $options['prefix'];
|
|
|
|
}
|
2022-10-20 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_client = new S3Client($this->_options);
|
2022-10-20 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns all objects in the given prefix.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param $prefix string with prefix
|
|
|
|
* @return array all objects in the given prefix
|
|
|
|
*/
|
|
|
|
private function _listAllObjects($prefix)
|
|
|
|
{
|
|
|
|
$allObjects = array();
|
|
|
|
$options = array(
|
2022-10-27 19:01:02 -04:00
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Prefix' => $prefix,
|
|
|
|
);
|
|
|
|
|
|
|
|
do {
|
2022-10-27 19:01:02 -04:00
|
|
|
$objectsListResponse = $this->_client->listObjects($options);
|
2022-10-20 17:23:01 -04:00
|
|
|
$objects = $objectsListResponse['Contents'] ?? array();
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
$allObjects[] = $object;
|
|
|
|
$options['Marker'] = $object['Key'];
|
|
|
|
}
|
|
|
|
} while ($objectsListResponse['IsTruncated']);
|
|
|
|
|
|
|
|
return $allObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-27 19:01:02 -04:00
|
|
|
* returns the S3 storage object key for $pasteid in $this->_bucket.
|
2022-10-20 17:23:01 -04:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param $pasteid string to get the key for
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function _getKey($pasteid)
|
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
if ($this->_prefix != '') {
|
|
|
|
return $this->_prefix . '/' . $pasteid;
|
2022-10-20 17:23:01 -04:00
|
|
|
}
|
|
|
|
return $pasteid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-27 19:01:02 -04:00
|
|
|
* Uploads the payload in the $this->_bucket under the specified key.
|
2022-10-20 17:23:01 -04:00
|
|
|
* The entire payload is stored as a JSON document. The metadata is replicated
|
|
|
|
* as the S3 object's metadata except for the fields attachment, attachmentname
|
|
|
|
* and salt.
|
|
|
|
*
|
|
|
|
* @param $key string to store the payload under
|
|
|
|
* @param $payload array to store
|
|
|
|
* @return bool true if successful, otherwise false.
|
|
|
|
*/
|
|
|
|
private function _upload($key, $payload)
|
|
|
|
{
|
|
|
|
$metadata = array_key_exists('meta', $payload) ? $payload['meta'] : array();
|
|
|
|
unset($metadata['attachment'], $metadata['attachmentname'], $metadata['salt']);
|
|
|
|
foreach ($metadata as $k => $v) {
|
|
|
|
$metadata[$k] = strval($v);
|
|
|
|
}
|
|
|
|
try {
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_client->putObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $key,
|
|
|
|
'Body' => Json::encode($payload),
|
|
|
|
'ContentType' => 'application/json',
|
|
|
|
'Metadata' => $metadata,
|
|
|
|
));
|
|
|
|
} catch (S3Exception $e) {
|
2022-10-27 19:01:02 -04:00
|
|
|
error_log('failed to upload ' . $key . ' to ' . $this->_bucket . ', ' .
|
2022-10-20 17:23:01 -04:00
|
|
|
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function create($pasteid, array $paste)
|
|
|
|
{
|
|
|
|
if ($this->exists($pasteid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_upload($this->_getKey($pasteid), $paste);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function read($pasteid)
|
|
|
|
{
|
|
|
|
try {
|
2022-10-27 19:01:02 -04:00
|
|
|
$object = $this->_client->getObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $this->_getKey($pasteid),
|
|
|
|
));
|
|
|
|
$data = $object['Body']->getContents();
|
|
|
|
return Json::decode($data);
|
|
|
|
} catch (S3Exception $e) {
|
2022-10-27 19:01:02 -04:00
|
|
|
error_log('failed to read ' . $pasteid . ' from ' . $this->_bucket . ', ' .
|
2022-10-20 17:23:01 -04:00
|
|
|
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function delete($pasteid)
|
|
|
|
{
|
|
|
|
$name = $this->_getKey($pasteid);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$comments = $this->_listAllObjects($name . '/discussion/');
|
|
|
|
foreach ($comments as $comment) {
|
|
|
|
try {
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_client->deleteObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $comment['Key'],
|
|
|
|
));
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
// ignore if already deleted.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
// there are no discussions associated with the paste
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_client->deleteObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $name,
|
|
|
|
));
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
// ignore if already deleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function exists($pasteid)
|
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
return $this->_client->doesObjectExistV2($this->_bucket, $this->_getKey($pasteid));
|
2022-10-20 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function createComment($pasteid, $parentid, $commentid, array $comment)
|
|
|
|
{
|
|
|
|
if ($this->existsComment($pasteid, $parentid, $commentid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$key = $this->_getKey($pasteid) . '/discussion/' . $parentid . '/' . $commentid;
|
|
|
|
return $this->_upload($key, $comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function readComments($pasteid)
|
|
|
|
{
|
|
|
|
$comments = array();
|
|
|
|
$prefix = $this->_getKey($pasteid) . '/discussion/';
|
|
|
|
try {
|
|
|
|
$entries = $this->_listAllObjects($prefix);
|
|
|
|
foreach ($entries as $entry) {
|
2022-10-27 19:01:02 -04:00
|
|
|
$object = $this->_client->getObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $entry['Key'],
|
|
|
|
));
|
|
|
|
$body = JSON::decode($object['Body']->getContents());
|
|
|
|
$items = explode('/', $entry['Key']);
|
|
|
|
$body['id'] = $items[3];
|
|
|
|
$body['parentid'] = $items[2];
|
|
|
|
$slot = $this->getOpenSlot($comments, (int) $object['Metadata']['created']);
|
|
|
|
$comments[$slot] = $body;
|
|
|
|
}
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
// no comments found
|
|
|
|
}
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function existsComment($pasteid, $parentid, $commentid)
|
|
|
|
{
|
|
|
|
$name = $this->_getKey($pasteid) . '/discussion/' . $parentid . '/' . $commentid;
|
2022-10-27 19:01:02 -04:00
|
|
|
return $this->_client->doesObjectExistV2($this->_bucket, $name);
|
2022-10-20 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function purgeValues($namespace, $time)
|
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
$path = $this->_prefix;
|
2022-10-20 17:23:01 -04:00
|
|
|
if ($path != '') {
|
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
$path .= 'config/' . $namespace;
|
|
|
|
|
|
|
|
try {
|
|
|
|
foreach ($this->_listAllObjects($path) as $object) {
|
|
|
|
$name = $object['Key'];
|
|
|
|
if (strlen($name) > strlen($path) && substr($name, strlen($path), 1) !== '/') {
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-27 19:01:02 -04:00
|
|
|
$head = $this->_client->headObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $name,
|
|
|
|
));
|
2022-10-27 19:01:02 -04:00
|
|
|
if ($head->get('Metadata') != null && array_key_exists('value', $head->get('Metadata'))) {
|
|
|
|
$value = $head->get('Metadata')['value'];
|
2022-10-20 17:23:01 -04:00
|
|
|
if (is_numeric($value) && intval($value) < $time) {
|
|
|
|
try {
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_client->deleteObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $name,
|
|
|
|
));
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
// deleted by another instance.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
// no objects in the bucket yet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For S3, the value will also be stored in the metadata for the
|
|
|
|
* namespaces traffic_limiter and purge_limiter.
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function setValue($value, $namespace, $key = '')
|
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
$prefix = $this->_prefix;
|
2022-10-20 17:23:01 -04:00
|
|
|
if ($prefix != '') {
|
|
|
|
$prefix .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($key === '') {
|
|
|
|
$key = $prefix . 'config/' . $namespace;
|
|
|
|
} else {
|
|
|
|
$key = $prefix . 'config/' . $namespace . '/' . $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata = array('namespace' => $namespace);
|
|
|
|
if ($namespace != 'salt') {
|
|
|
|
$metadata['value'] = strval($value);
|
|
|
|
}
|
|
|
|
try {
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_client->putObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $key,
|
|
|
|
'Body' => $value,
|
|
|
|
'ContentType' => 'application/json',
|
|
|
|
'Metadata' => $metadata,
|
|
|
|
));
|
|
|
|
} catch (S3Exception $e) {
|
2022-10-27 19:01:02 -04:00
|
|
|
error_log('failed to set key ' . $key . ' to ' . $this->_bucket . ', ' .
|
2022-10-20 17:23:01 -04:00
|
|
|
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getValue($namespace, $key = '')
|
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
$prefix = $this->_prefix;
|
2022-10-20 17:23:01 -04:00
|
|
|
if ($prefix != '') {
|
|
|
|
$prefix .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($key === '') {
|
|
|
|
$key = $prefix . 'config/' . $namespace;
|
|
|
|
} else {
|
|
|
|
$key = $prefix . 'config/' . $namespace . '/' . $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-10-27 19:01:02 -04:00
|
|
|
$object = $this->_client->getObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $key,
|
|
|
|
));
|
|
|
|
return $object['Body']->getContents();
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function _getExpiredPastes($batchsize)
|
|
|
|
{
|
|
|
|
$expired = array();
|
|
|
|
$now = time();
|
2022-10-27 19:01:02 -04:00
|
|
|
$prefix = $this->_prefix;
|
2022-10-20 17:23:01 -04:00
|
|
|
if ($prefix != '') {
|
|
|
|
$prefix .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
foreach ($this->_listAllObjects($prefix) as $object) {
|
2022-10-27 19:01:02 -04:00
|
|
|
$head = $this->_client->headObject(array(
|
|
|
|
'Bucket' => $this->_bucket,
|
2022-10-20 17:23:01 -04:00
|
|
|
'Key' => $object['Key'],
|
|
|
|
));
|
2022-10-27 19:01:02 -04:00
|
|
|
if ($head->get('Metadata') != null && array_key_exists('expire_date', $head->get('Metadata'))) {
|
|
|
|
$expire_at = intval($head->get('Metadata')['expire_date']);
|
2022-10-20 17:23:01 -04:00
|
|
|
if ($expire_at != 0 && $expire_at < $now) {
|
|
|
|
array_push($expired, $object['Key']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($expired) > $batchsize) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
// no objects in the bucket yet
|
|
|
|
}
|
|
|
|
return $expired;
|
|
|
|
}
|
2022-10-27 19:01:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getAllPastes()
|
|
|
|
{
|
|
|
|
$pastes = array();
|
2022-11-01 11:38:06 -04:00
|
|
|
$prefix = $this->_prefix;
|
2022-10-27 19:01:02 -04:00
|
|
|
if ($prefix != '') {
|
|
|
|
$prefix .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
foreach ($this->_listAllObjects($prefix) as $object) {
|
2022-11-01 11:38:06 -04:00
|
|
|
$candidate = substr($object['Key'], strlen($prefix));
|
|
|
|
if (strpos($candidate, '/') === false) {
|
2022-10-27 19:01:02 -04:00
|
|
|
$pastes[] = $candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
// no objects in the bucket yet
|
|
|
|
}
|
|
|
|
return $pastes;
|
|
|
|
}
|
2022-10-20 17:23:01 -04:00
|
|
|
}
|