Skip to content

Commit

Permalink
Converted listCredentials() to return an iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Apr 10, 2016
1 parent 4a2c49d commit 872b480
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
43 changes: 25 additions & 18 deletions src/CredStash.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CredStash\Exception\AutoIncrementException;
use CredStash\Store\DynamoDbStore;
use CredStash\Store\StoreInterface;
use Iterator;
use Traversable;

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function listCredentials($pattern = '*')

$credentials = $this->filterCredentials($pattern, $credentials);

$credentials = array_map(function ($version) { return ltrim($version, '0'); }, $credentials);
$credentials = $this->unpadVersion($credentials);

return $credentials;
}
Expand All @@ -115,12 +116,9 @@ public function search($pattern = '*', $context = [], $version = null)

$credentials = $this->listCredentials($pattern);

$result = [];
foreach ($credentials as $name => $credentialVersion) {
$result[$name] = $this->get($name, $context, $version !== null ? $version : $credentialVersion);
yield $name => $this->get($name, $context, $version !== null ? $version : $credentialVersion);
}

return $result;
}

/**
Expand Down Expand Up @@ -266,27 +264,36 @@ private function paddedInt($int)
/**
* Filter list of credentials to those that match the pattern given.
*
* @param string $pattern
* @param array $credentials
* @param string $pattern
* @param Iterator $credentials
*
* @return array
* @return Iterator
*/
private function filterCredentials($pattern, array $credentials)
private function filterCredentials($pattern, $credentials)
{
if (!$pattern || $pattern === '*') {
return $credentials;
if ($pattern && $pattern !== '*') {
$pattern = str_replace('\\', '\\\\', $pattern);
} else {
$pattern = null;
}

$pattern = str_replace('\\', '\\\\', $pattern);

$result = [];

foreach ($credentials as $name => $version) {
if (fnmatch($pattern, $name)) {
$result[$name] = $version;
if ($pattern && !fnmatch($pattern, $name)) {
continue;
}
yield $name => $version;
}
}

return $result;
/**
* @param Iterator $credentials
*
* @return Iterator
*/
private function unpadVersion($credentials)
{
foreach ($credentials as $name => $version) {
yield $name => ltrim($version, '0');
}
}
}
7 changes: 4 additions & 3 deletions src/CredStashInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CredStash\Exception\DuplicateCredentialVersionException;
use CredStash\Exception\EncryptionException;
use CredStash\Exception\IntegrityException;
use Iterator;
use Traversable;

/**
Expand All @@ -28,7 +29,7 @@ interface CredStashInterface
*
* @param string $pattern The pattern to search for.
*
* @return array [name => version]
* @return Iterator [name => version]
*/
public function listCredentials($pattern = '*');

Expand All @@ -42,7 +43,7 @@ public function listCredentials($pattern = '*');
* @throws IntegrityException If the HMAC does not match.
* @throws DecryptionException If decryption fails.
*
* @return array [name => secret]
* @return Iterator [name => secret]
*/
public function getAll($context = [], $version = null);

Expand All @@ -57,7 +58,7 @@ public function getAll($context = [], $version = null);
* @throws IntegrityException If the HMAC does not match.
* @throws DecryptionException If decryption fails.
*
* @return array [name => secret]
* @return Iterator [name => secret]
*/
public function search($pattern = '*', $context = [], $version = null);

Expand Down
15 changes: 5 additions & 10 deletions src/Store/DynamoDbStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,19 @@ public function __construct(DynamoDbClient $db, $tableName = self::DEFAULT_TABLE
*/
public function listCredentials()
{
$response = $this->db->scan([
$pages = $this->db->getPaginator('Scan', [
'TableName' => $this->tableName,
'ProjectionExpression' => '#N, version',
'ExpressionAttributeNames' => [
'#N' => 'name',
],
]);

if ($response['Count'] === 0) {
return [];
}

$result = [];
foreach ($response['Items'] as $item) {
$result[$item['name']['S']] = $item['version']['S'];
foreach ($pages as $page) {
foreach ($page['Items'] as $item) {
yield $item['name']['S'] => $item['version']['S'];
}
}

return $result;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Store/StoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use CredStash\Credential;
use CredStash\Exception\DuplicateCredentialVersionException;
use CredStash\Exception\CredentialNotFoundException;
use Iterator;

/**
* A credential storage.
Expand All @@ -16,7 +17,7 @@ interface StoreInterface
/**
* Fetches the names and version of every credential in the store.
*
* @return array [name => normalized (padded) numeric version string]
* @return Iterator [name => normalized (padded) numeric version string]
*/
public function listCredentials();

Expand Down

0 comments on commit 872b480

Please sign in to comment.