Skip to content

Commit beb1ce4

Browse files
authored
Merge pull request #4 from boite/master
Objectstorage v3; now with modern crypto
2 parents e363dd8 + 995b567 commit beb1ce4

32 files changed

+723
-296
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
.DS_Store
44
objectstorage.conf
5+
.php_cs.cache
6+
.phpunit.result.cache
57

68
### Eclipse ###
79
*.pydevproject

.php_cs.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__.'/src')
5+
;
6+
7+
return PhpCsFixer\Config::create()
8+
->setRules([
9+
'@PSR1' => true,
10+
'@PSR2' => true,
11+
'@Symfony' => true,
12+
'phpdoc_align' => false,
13+
'concat_space' => ['spacing' => 'one'],
14+
'array_syntax' => ['syntax' => 'short'],
15+
])
16+
->setFinder($finder)
17+
;

README.md

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
# ObjectStorage 2.0 library
1+
# ObjectStorage 3.0 library
22

33
ObjectStorage library for your cloud-based applications.
44

5-
*NOTE: version 1.0, previously only available as dev-master, is still available by updating your composer.json to require version ~1.0*
6-
75
## Object Storage vs a normal file system
86

97
Object-based storage solves large scale storage problems for cloud-based applications.
@@ -78,42 +76,38 @@ $service->delete('my-message');
7876

7977
### Encryption
8078

81-
The library includes an EncryptionAdapter that will allow you to transparently encrypt/decrypt
79+
The library includes adapters to allow you to transparently encrypt/decrypt
8280
your data before it's passed to the storage backend.
8381

84-
This is done by wrapping the original storage adapter (s3, file, pdo, gridfs, etc) into
85-
the EncryptionAdapter. Here's an example
82+
This is done by wrapping the original storage adapter (s3, file, pdo, gridfs,
83+
etc) into the one of the encryption adapters. Here's an example
8684

8785
```php
88-
$adapter = new ObjectStorage\Adapter\PdoAdapter($pdo);
89-
$adapter = new ObjectStorage\Adapter\EncryptionAdapter($adapter, $key, $iv);
90-
// You can use $adapter as before, but all data will be encrypted
86+
$adapter = new \ObjectStorage\Adapter\EncryptedStorageAdapter(
87+
new \ObjectStorage\Adapter\PdoAdapter($pdo),
88+
\ParagonIE\Halite\KeyFactory::loadEncryptionKey($pathToKeyfile)
89+
);
90+
// You can use $adapter as before and both the storage keys and objects will be
91+
// encrypted (use PlaintextKeyEncryptedStorageAdapter if you don't want the
92+
// storage keys to be encrypted).
9193
```
9294

93-
The key and iv are hex encoded strings. To generate these, use the following command:
94-
95-
./bin/objectstorage objectstorage:generatekey
95+
The encryption routines are provided by [ParagonIE/Halite][] and libsodium.
9696

97-
This will output something like the following:
98-
99-
KEY: C2FE680A5613469189621C9E46B52C15C9C80E50370E7950D6FD2D027C4FAEF0
100-
IV: E5F3E442F3CE0ECC931B7E866A5F3121
101-
102-
Save these 2 values somewhere safely.
97+
Use the following command to generate an encryption key and save it to a file :-
10398

104-
The encryption is similar to using the following commands:
105-
106-
openssl enc -aes-256-cbc -K C2FE680A5613469189621C9E46B52C15C9C80E50370E7950D6FD2D027C4FAEF0 -iv E5F3E442F3CE0ECC931B7E866A5F3121 < original.txt > encrypted.aes
99+
```sh
100+
./bin/objectstorage genkey /path/to/a/file
101+
```
107102

108-
openssl enc -d -aes-256-cbc -K C2FE680A5613469189621C9E46B52C15C9C80E50370E7950D6FD2D027C4FAEF0 -iv E5F3E442F3CE0ECC931B7E866A5F3121 < encrypted.aes
109-
110-
You can also use the included encrypt + decrypt commands:
103+
You can also use the included encrypt + decrypt commands. In the following
104+
example we encrypt `example.pdf` with the encryption key in `key.asc` and then
105+
decrypt it again, using the same key and writing it to a new `example-new.pdf`:
111106

112-
export OBJECTSTORAGE_ENCRYPTION_KEY=C2FE680A5613469189621C9E46B52C15C9C80E50370E7950D6FD2D027C4FAEF0
113-
export OBJECTSTORAGE_ENCRYPTION_IV=E5F3E442F3CE0ECC931B7E866A5F3121
114-
115-
bin/objectstorage objectstorage:encrypt example.pdf > example.pdf.encrypted
116-
bin/objectstorage objectstorage:decrypt example.pdf.encrypted > example_new.pdf
107+
```sh
108+
bin/objectstorage encrypt key.asc example.pdf example.pdf.encrypted
109+
bin/objectstorage decrypt key.asc example.pdf.encrypted example-new.pdf
110+
```
117111

118112
## Console tool
119113

@@ -172,11 +166,20 @@ Then, add `linkorb/objectstorage` to your project's `composer.json`:
172166
```json
173167
{
174168
"require": {
175-
"linkorb/objectstorage": "~2.0"
169+
"linkorb/objectstorage": "^3.0"
176170
}
177171
}
178172
```
179173

174+
## Older versions of this library
175+
176+
Version 1.0, previously only available as dev-master, is still available by
177+
updating your composer.json to require version "~1.0".
178+
179+
The `php5` branch will still work with PHP <= 5.6, but it will not have the
180+
latest features and, particularly, should not be used if you need encrypted
181+
storage.
182+
180183
## Contributing
181184

182185
Ready to build and improve on this repo? Excellent!
@@ -195,3 +198,5 @@ Btw, we're hiring!
195198
## License
196199

197200
Please check LICENSE.md for full license information
201+
202+
[ParagonIE/Halite]: <https://paragonie.com/project/halite> "Halite - Simple PHP Cryptography Library"

bin/objectstorage

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
#!/usr/bin/env php
22
<?php
33

4-
use Symfony\Component\Console\Application;
5-
64
require_once(__DIR__ . "/../vendor/autoload.php");
75

8-
$application = new Application('ObjectStorage CLI utility', '1.0.0');
6+
use ObjectStorage\Command\DecryptCommand;
7+
use ObjectStorage\Command\DeleteCommand;
8+
use ObjectStorage\Command\DownloadCommand;
9+
use ObjectStorage\Command\EncryptCommand;
10+
use ObjectStorage\Command\GenerateKeyCommand;
11+
use ObjectStorage\Command\ListCommand;
12+
use ObjectStorage\Command\UploadCommand;
13+
use Symfony\Component\Console\Application;
14+
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
15+
16+
$application = new Application('ObjectStorage CLI utility', 'v3');
917
$application->setCatchExceptions(true);
10-
$application->add(new \ObjectStorage\Command\UploadCommand());
11-
$application->add(new \ObjectStorage\Command\DownloadCommand());
12-
$application->add(new \ObjectStorage\Command\ListCommand());
13-
$application->add(new \ObjectStorage\Command\DeleteCommand());
14-
$application->add(new \ObjectStorage\Command\GenerateKeyCommand());
15-
$application->add(new \ObjectStorage\Command\EncryptCommand());
16-
$application->add(new \ObjectStorage\Command\DecryptCommand());
18+
$application->setCommandLoader(new FactoryCommandLoader([
19+
'objectstorage:upload' => function () { return new UploadCommand(); },
20+
'objectstorage:download' => function () { return new DownloadCommand(); },
21+
'objectstorage:list' => function () { return new ListCommand(); },
22+
'objectstorage:delete' => function () { return new DeleteCommand(); },
23+
'genkey' => function () { return new GenerateKeyCommand(); },
24+
'encrypt' => function () { return new EncryptCommand(); },
25+
'decrypt' => function () { return new DecryptCommand(); },
26+
]));
1727
$application->run();

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.3.0"
15+
"php": "^7.2",
16+
"paragonie/halite": "^4"
1617
},
1718
"require-dev": {
18-
"symfony/console": "~2.4",
19-
"aws/aws-sdk-php": "~2.7"
19+
"symfony/console": "^4",
20+
"aws/aws-sdk-php": "^3",
21+
"friendsofphp/php-cs-fixer": "^2.15",
22+
"phpunit/phpunit": "^8.1"
2023
},
2124
"suggest": {
2225
"linkorb/bergen-client-php": "To use the Bergen Adapter."

phpunit.xml.dist

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
bootstrap="tests/bootstrap.php"
9+
>
10+
<php>
11+
<ini name="error_reporting" value="-1" />
12+
<server name="APP_ENV" value="test" force="true" />
13+
<server name="SHELL_VERBOSITY" value="-1" />
14+
</php>
15+
16+
<testsuites>
17+
<testsuite name="Project Test Suite">
18+
<directory>tests</directory>
19+
</testsuite>
20+
</testsuites>
21+
22+
<filter>
23+
<whitelist>
24+
<directory>src</directory>
25+
</whitelist>
26+
</filter>
27+
</phpunit>

src/Adapter/BergenAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Bergen\Client\V1\V1StorageClient;
99
use GuzzleHttp\RequestOptions;
1010

11-
class BergenAdapter implements StorageAdapterInterface
11+
class BergenAdapter implements BuildableAdapterInterface, StorageAdapterInterface
1212
{
1313
/**
1414
* @var \Bergen\Client\V1\V1StorageClient
@@ -60,6 +60,7 @@ public function getData($key)
6060
} catch (UnexpectedResponseError $e) {
6161
throw new AdapterException('Unable to get data.', null, $e);
6262
}
63+
6364
return (string) $response->getBody();
6465
}
6566

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ObjectStorage\Adapter;
4+
5+
interface BuildableAdapterInterface
6+
{
7+
/**
8+
* Build an instance of the adapter.
9+
*
10+
* @param array $config
11+
*
12+
* @return \ObjectStorage\Adapter\StorageAdapterInterface
13+
*/
14+
public static function build(array $config);
15+
}

src/Adapter/Bzip2Adapter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace ObjectStorage\Adapter;
44

5-
use RuntimeException;
6-
use InvalidArgumentException;
7-
85
class Bzip2Adapter implements StorageAdapterInterface
96
{
107
private $child;
@@ -19,13 +16,15 @@ public function __construct(StorageAdapterInterface $child, $level)
1916
public function setData($key, $data)
2017
{
2118
$data = bzcompress($data, $this->level);
19+
2220
return $this->child->setData($key, $data);
2321
}
2422

2523
public function getData($key)
2624
{
2725
$data = $this->child->getData($key);
2826
$data = bzdecompress($data);
27+
2928
return $data;
3029
}
3130

0 commit comments

Comments
 (0)