Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dermatthes committed Jan 30, 2024
1 parent f36ec5f commit 23118ed
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 20 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ composer requre phore/objectstore

## Basic usage

```php
$store = new ObjectStore(\Phore\ObjectStore\ObjectStoreDriverFactory::Build("gcs://<bucket-name>?keyfile=/run/secrets/google-key-1"));
```


```php
$store = new ObjectStore(new GoogleCloudStoreDriver(__DIR__ . "/file/to/identity.json", "bucketName"));

Expand Down
5 changes: 5 additions & 0 deletions src/Driver/AzureObjectStoreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use Phore\Core\Exception\NotFoundException;
use Phore\ObjectStore\Encryption\ObjectStoreEncryption;
use Psr\Http\Message\StreamInterface;

class AzureObjectStoreDriver implements ObjectStoreDriver
Expand All @@ -40,6 +41,10 @@ public function __construct(string $accountName, string $accountKey, string $con
$this->blobClient = BlobRestProxy::createBlobService($connectionString);
$this->containerName = $containerName;
}
public function setEncryption(ObjectStoreEncryption $encryption)
{
throw new \InvalidArgumentException("Encryption not supported in Azure implementation");
}

/**
* @param string $objectId
Expand Down
15 changes: 10 additions & 5 deletions src/Driver/FileSystemObjectStoreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FileSystemObjectStoreDriver implements ObjectStoreDriver
* @var ObjectStoreEncryption
*/
private $encryption;

public function __construct(string $rootDir, ObjectStoreEncryption $encryption = null)
{
if (!class_exists('\Phore\FileSystem\PhoreDirectory')) {
Expand Down Expand Up @@ -122,6 +122,11 @@ public function get(string $objectId, array &$meta = null): string
return $this->encryption->decrypt($file->get_contents());
}

public function setEncryption(ObjectStoreEncryption $encryption)
{
$this->encryption = $encryption;
}

/**
* @param string $objectId
* @param array|null $meta
Expand All @@ -132,7 +137,7 @@ public function getStream(string $objectId, array &$meta = null): StreamInterfac
{
if ( ! $this->encryption->supportsStreaming())
throw new InvalidArgumentException("Encryption does not support streaming.");

return $this->rootDir->withSubPath($objectId)->asFile()->fopen('r');
}

Expand Down Expand Up @@ -193,11 +198,11 @@ public function append(string $objectId, string $appendData)
if ($targetFile->exists()) {
$data = $this->encryption->decrypt($targetFile->get_contents());
}

$data .= $appendData;

$targetFile->set_contents($this->encryption->encrypt($appendData));

return true;
}

Expand Down
14 changes: 9 additions & 5 deletions src/Driver/GoogleObjectStoreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Google\Cloud\Storage\Bucket;
use Google\Cloud\Storage\StorageClient;
use InvalidArgumentException;
use Phore\ObjectStore\Encryption\ObjectStoreEncryption;
use Phore\ObjectStore\Type\ObjectStoreObject;
use Psr\Http\Message\StreamInterface;

Expand All @@ -34,12 +35,12 @@ class GoogleObjectStoreDriver implements ObjectStoreDriver


private $putOpts = [];

/**
* GoogleObjectStoreDriver constructor.
*
*
* To Put Public files set $putOpts to ["predefinedAcl"=>"publicRead"]
*
*
* @param string|array $keyFile
* @param string $bucketName
*/
Expand All @@ -60,7 +61,10 @@ public function __construct($keyFile, string $bucketName, array $putOpts = ["pre
$this->bucket = $storage->bucket($bucketName);
}


public function setEncryption(ObjectStoreEncryption $encryption)
{
throw new InvalidArgumentException("Encryption not supported in Google implementation");
}
/**
* @param string $objectId
* @return bool
Expand All @@ -79,7 +83,7 @@ private function _getPutOpts($objectId, array $metadata = null): array
{
$opts = $this->putOpts;
$opts["name"] = $objectId;

if ($metadata !== null) {
$opts['metadata'] = [
'metadata' => $metadata
Expand Down
5 changes: 4 additions & 1 deletion src/Driver/ObjectStoreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


use Phore\Core\Exception\NotFoundException;
use Phore\ObjectStore\Encryption\ObjectStoreEncryption;
use Psr\Http\Message\StreamInterface;

/**
Expand All @@ -31,7 +32,7 @@ public function has(string $objectId): bool;
* @return mixed
*/
public function put(string $objectId, $content, array $metadata = null);

/**
* @param string $objectId
* @param $resource
Expand Down Expand Up @@ -139,4 +140,6 @@ public function walk(callable $walkFunction): bool;
*/
public function list(string $prefix = null): array;

public function setEncryption(ObjectStoreEncryption $encryption);

}
13 changes: 9 additions & 4 deletions src/Driver/PhoreGoogleObjectStoreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Phore\FileSystem\PhoreFile;
use Phore\HttpClient\Ex\PhoreHttpRequestException;
use Phore\ObjectStore\Encryption\ObjectStoreEncryption;
use Phore\ObjectStore\Encryption\PassThruNoEncryption;
use Psr\Http\Message\StreamInterface;


Expand Down Expand Up @@ -56,7 +57,7 @@ class PhoreGoogleObjectStoreDriver implements ObjectStoreDriver
* @var ObjectStoreEncryption
*/
public $encryption;

/**
* PhoreGoogleObjectStoreDriver constructor.
* @param string $configFilePath
Expand All @@ -72,9 +73,9 @@ public function __construct(string $configFilePath, string $bucketName, bool $re
$this->bucketName = $bucketName;
$this->base_url .= '/b/' . $bucketName;
$this->retry = $retry;

$this->accessToken = $this->_getJwt()['access_token'];

$this->encryption = $encryption;
if ($this->encryption === null)
$this->encryption = new PassThruNoEncryption();
Expand Down Expand Up @@ -174,6 +175,10 @@ private function _getContentType($objectId): string
}
}

public function setEncryption(ObjectStoreEncryption $encryption)
{
$this->encryption = $encryption;
}
/**
* @param string $objectId
* @param $content
Expand Down Expand Up @@ -344,7 +349,7 @@ public function append(string $objectId, string $data)
{
if ( ! $this->encryption->supportsAppending())
throw new InvalidArgumentException("Streaming is unsupported on this enryption method.");

$meta = $this->getMeta($objectId);
if ($meta === []) {
$this->put($objectId, $data);
Expand Down
6 changes: 5 additions & 1 deletion src/Driver/S3ObjectStoreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use Phore\Core\Exception\NotFoundException;
use Phore\ObjectStore\Encryption\ObjectStoreEncryption;
use Psr\Http\Message\StreamInterface;

class S3ObjectStoreDriver implements ObjectStoreDriver
Expand Down Expand Up @@ -44,7 +45,10 @@ public function __construct(string $region, string $bucket, string $account=null
$this->bucket = $bucket;
}


public function setEncryption(ObjectStoreEncryption $encryption)
{
throw new \InvalidArgumentException("Encryption not supported in S3 implementation");
}
public function has(string $objectId): bool
{
try {
Expand Down
13 changes: 9 additions & 4 deletions src/ObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ class ObjectStore
* @var ObjectStoreDriver
*/
private $driver;


/**
* ObjectStore constructor.
*
* Provide a objectStoreDriver as paramter 1 or a string with the connection string (see ObjectStoreDriverFactory)
*
* @param ObjectStoreDriver $objectStoreDriver
*/
public function __construct(ObjectStoreDriver $objectStoreDriver)
public function __construct(ObjectStoreDriver|string $objectStoreDriver)
{
if (is_string($objectStoreDriver))
$objectStoreDriver = ObjectStoreDriverFactory::Build($objectStoreDriver);
$this->driver = $objectStoreDriver;

}

/**
Expand Down Expand Up @@ -132,7 +137,7 @@ public function getDriver(): ObjectStoreDriver
{
return $this->driver;
}


/**
* @param string $objectId
Expand Down
69 changes: 69 additions & 0 deletions src/ObjectStoreDriverFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Phore\ObjectStore;

use Phore\ObjectStore\Driver\AzureObjectStoreDriver;
use Phore\ObjectStore\Driver\FileSystemObjectStoreDriver;
use Phore\ObjectStore\Driver\GoogleObjectStoreDriver;
use Phore\ObjectStore\Driver\PhoreGoogleObjectStoreDriver;
use Phore\ObjectStore\Driver\S3ObjectStoreDriver;
use Phore\ObjectStore\Encryption\SodiumSyncEncryption;

class ObjectStoreDriverFactory
{

/**
* Choose the correct driver based on the connection string
*
* Examples
* file:///path/to/dir?encrypt=SodiumSnyc&encryptSecret=xyz => FileSystemObjectStoreDriver mit SodiumSyncEncryption
* file:///path/to/dir?encrypt=SodiumSnyc&encryptSecretFile=/path/to/secret => FileSystemObjectStoreDriver mit SodiumSyncEncryption (load secret from file)
* gcs+phore://bucket-name?keyFile=/path/to/keyfile.json => PhoreGoogleObjectStoreDriver
* gcs+phore://bucket-name?keyFile=/path/to/keyfile.json&encrypt=SodiumSnyc&encryptSecretFile=/path/to/secret => PhoreGoogleObjectStoreDriver
*
*
* @param string $connectionString
* @return void
*/
public static function Build(string $connectionString) {
$url = phore_parse_url($connectionString);

// Parse the query string into array
$query = [];
parse_str($url->query, $query);

if ($url->scheme === "file") {
$path = $url->path;
if ($path === null)
throw new \InvalidArgumentException("Missing path in connection string. Specify path as 'file:///path/to/dir' (3 slashes)");
$driver = new FileSystemObjectStoreDriver($path);
if ($query['encrypt'] !== null) {
$driver->setEncryption(new SodiumSyncEncryption($query['encryptSecret'] ?? phore_file($query['encryptSecretFile'] ?? throw new \InvalidArgumentException("encryptSecret or encryptSecretFile missing"))->get_contents()));
}
return $driver;
}
if ($url->scheme === "gcs+phore") {
$keyFile = $query['keyFile'] ?? null;
if ($keyFile === null)
throw new \InvalidArgumentException("Missing keyFile in connection string. Specify keyFile as 'gcs+phore://bucket-name?keyFile=/path/to/keyfile.json'");
$driver = new PhoreGoogleObjectStoreDriver($keyFile, $url->host);
if ($query['encrypt'] !== null) {
$driver->setEncryption(new SodiumSyncEncryption($query['encryptSecret'] ?? phore_file($query['encryptSecretFile'])->get_contents()));
}
return $driver;
}
if ($url->scheme === "gcs") {
$keyFile = $query['keyFile'] ?? null;
if ($keyFile === null)
throw new \InvalidArgumentException("Missing keyFile in connection string. Specify keyFile as 'gcs+phore://bucket-name?keyFile=/path/to/keyfile.json'");
$driver = new GoogleObjectStoreDriver($keyFile, $url->host);
if ($query['encrypt'] !== null) {
$driver->setEncryption(new SodiumSyncEncryption($query['encryptSecret'] ?? phore_file($query['encryptSecretFile'])->get_contents()));
}
return $driver;
}


}

}

0 comments on commit 23118ed

Please sign in to comment.