Skip to content

Commit

Permalink
Merge pull request #3 from mittwald/feature/error-handling
Browse files Browse the repository at this point in the history
Feature/error handling
  • Loading branch information
ins0 authored Jul 10, 2020
2 parents 63ab718 + d867eb0 commit c3c73ff
Show file tree
Hide file tree
Showing 23 changed files with 435 additions and 232 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function __construct(
`$apiHost` Hashicorp Vault REST Endpoint URL

## Bulk Requests
Bulk Requests **will not** throw `InvalidDataExceptions`. Using Bulk Requests requires to iterate through the Response
and calling `hasErrors` within the `BasicMetaResponse`.
Using Bulk Requests also requires to iterate through the Response
and calling `hasErrors` within the `MetaData` of each Bulk Item to ensure it was processed successfully.

## Exceptions
Calling library methods will throw exceptions, indicating where ever invalid data was provided
Expand Down Expand Up @@ -110,3 +110,9 @@ ___
`InvalidDataException`

Exception indicates a failed server payload validation.

___

`KeyNameNotFoundException`

Will be thrown when trying to request an API Endpoint where the Key Name - that is indicated within the url - will not exist.
5 changes: 2 additions & 3 deletions examples/BulkOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@

/** @var EncryptDataResponse $bulkResult */
foreach($encryptBulkResponse as $bulkResult) {
// BULK REQUEST WON'T THROW INVALID DATA EXCEPTIONS
// SO YOU ARE RESPONSABLE TO CHECK IF EVERY BULK WAS
// SUCCESSFULLY PROCESSED
// TO ENSURE EVERY BULK ENCRYPT REQUEST WAS SUCCESSFULLY PROCESSED
// YOU HAVE TO CHECK FOR ERRORS OF EVERY BULK ITEM
if (!$bulkResult->getMetaData()->hasErrors()) {
var_dump($bulkResult->getCiphertext());
}
Expand Down
11 changes: 11 additions & 0 deletions src/VaultPHP/Exceptions/KeyNameNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace VaultPHP\Exceptions;

/**
* Class KeyNameNotFoundException
* @package VaultPHP\Exceptions
*/
class KeyNameNotFoundException extends VaultResponseException
{
}
11 changes: 11 additions & 0 deletions src/VaultPHP/Response/ApiErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace VaultPHP\Response;

class ApiErrors
{
const ENCRYPTION_KEY_NOT_FOUND = [
'no existing key named .+ could be found',
'encryption key not found',
];
};
29 changes: 23 additions & 6 deletions src/VaultPHP/Response/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class MetaData implements MetaDataInterface
/** @var string|null */
private $wrap_info;

/** @var array|null */
private $warnings;
/** @var string[] */
private $warnings = [];

/** @var object|null */
private $auth;

/** @var array|null */
private $errors;
/** @var string[] */
private $errors = [];

/**
* MetaData constructor.
Expand Down Expand Up @@ -97,7 +97,7 @@ public function getWrapInfo()
}

/**
* @return array|null
* @return string[]
*/
public function getWarnings()
{
Expand All @@ -113,13 +113,30 @@ public function getAuth()
}

/**
* @return array|null
* @return string[]
*/
public function getErrors()
{
return $this->errors;
}

/**
* @param array $error
* @return boolean
*/
public function containsError($error) {
/** @var string $apiError */
foreach ($this->getErrors() as $apiError) {
/** @var string $errorMessage */
foreach ($error as $errorMessage) {
if (preg_match("#${errorMessage}#i", $apiError)) {
return true;
}
}
}
return false;
}

/**
* @return boolean
*/
Expand Down
6 changes: 6 additions & 0 deletions src/VaultPHP/Response/MetaDataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ public function getErrors();
* @return boolean
*/
public function hasErrors();

/**
* @param array $error
* @return bool
*/
public function containsError($error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public function __construct($name)

/**
* @param string $type
* @return void
* @return $this
*/
public function setType($type)
{
$this->type = $type;
return $this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ public function getCiphertext()

/**
* @param string $ciphertext
* @return void
* @return $this
*/
public function setCiphertext($ciphertext)
{
$this->ciphertext = $ciphertext;
return $this;
}

/**
Expand All @@ -62,11 +63,12 @@ public function getNonce()

/**
* @param string|null $nonce
* @return void
* @return $this
*/
public function setNonce($nonce)
{
$this->nonce = $nonce;
return $this;
}

/**
Expand All @@ -79,10 +81,11 @@ public function getContext()

/**
* @param string|null $context
* @return void
* @return $this
*/
public function setContext($context)
{
$this->context = $context;
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ public function getPlaintext()

/**
* @param string $plaintext
* @return void
* @return $this
*/
public function setPlaintext($plaintext)
{
$this->plaintext = base64_encode($plaintext);
return $this;
}

/**
Expand All @@ -62,11 +63,12 @@ public function getNonce()

/**
* @param string|null $nonce
* @return void
* @return $this
*/
public function setNonce($nonce)
{
$this->nonce = $nonce;
return $this;
}

/**
Expand All @@ -79,10 +81,11 @@ public function getContext()

/**
* @param string|null $context
* @return void
* @return $this
*/
public function setContext($context)
{
$this->context = $context;
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
namespace VaultPHP\SecretEngines\Engines\Transit\Request\EncryptData;

use VaultPHP\SecretEngines\Interfaces\BulkResourceRequestInterface;
use VaultPHP\SecretEngines\Interfaces\EncryptionTypeRequestInterface;
use VaultPHP\SecretEngines\Interfaces\NamedRequestInterface;
use VaultPHP\SecretEngines\Traits\ArrayExportTrait;
use VaultPHP\SecretEngines\Traits\BulkRequestTrait;
use VaultPHP\SecretEngines\Traits\EncryptionTypeRequestTrait;
use VaultPHP\SecretEngines\Traits\NamedRequestTrait;

final class EncryptDataBulkRequest implements BulkResourceRequestInterface, NamedRequestInterface
final class EncryptDataBulkRequest implements
BulkResourceRequestInterface,
NamedRequestInterface,
EncryptionTypeRequestInterface
{
use ArrayExportTrait;
use NamedRequestTrait;
use BulkRequestTrait;
use EncryptionTypeRequestTrait;

/**
* EncryptDataRequest constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

namespace VaultPHP\SecretEngines\Engines\Transit\Request\EncryptData;

use VaultPHP\SecretEngines\Interfaces\EncryptionTypeRequestInterface;
use VaultPHP\SecretEngines\Interfaces\NamedRequestInterface;
use VaultPHP\SecretEngines\Interfaces\ResourceRequestInterface;
use VaultPHP\SecretEngines\Traits\EncryptionTypeRequestTrait;
use VaultPHP\SecretEngines\Traits\NamedRequestTrait;

/**
* Class EncryptDataRequest
* @package VaultPHP\SecretEngines\Transit\Request\EncryptData
*/
final class EncryptDataRequest extends EncryptData implements ResourceRequestInterface, NamedRequestInterface
final class EncryptDataRequest extends EncryptData implements
ResourceRequestInterface,
NamedRequestInterface,
EncryptionTypeRequestInterface
{
use NamedRequestTrait;
use EncryptionTypeRequestTrait;

/**
* EncryptDataRequest constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,52 @@ public function __construct($name)

/**
* @param $allow boolean
* @return void
* @return $this
*/
public function setDeletionAllowed($allow)
{
$this->deletion_allowed = (boolean)$allow;
return $this;
}

/**
* @param int $min_decryption_version
* @return void
* @return $this
*/
public function setMinDecryptionVersion($min_decryption_version)
{
$this->min_decryption_version = (int)$min_decryption_version;
return $this;
}

/**
* @param int $min_encryption_version
* @return void
* @return $this
*/
public function setMinEncryptionVersion($min_encryption_version)
{
$this->min_encryption_version = (int)$min_encryption_version;
return $this;
}

/**
* @param bool $exportable
* @return void
* @return $this
*/
public function setExportable($exportable)
{
$this->exportable = (bool)$exportable;
return $this;
}

/**
* @param bool $allow_plaintext_backup
* @return void
* @return $this
*/
public function setAllowPlaintextBackup($allow_plaintext_backup)
{
$this->allow_plaintext_backup = (bool)$allow_plaintext_backup;
return $this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace VaultPHP\SecretEngines\Interfaces;

/**
* Interface EncryptionTypeRequestInterface
* @package VaultPHP\SecretEngines\Interfaces
*/
interface EncryptionTypeRequestInterface
{
/**
* @return string|null
*/
public function getType();

/**
* @param string $type
* @return $this
*/
public function setType($type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function getName();

/**
* @param string $name
* @return void
* @return $this
*/
public function setName($name);
}
3 changes: 2 additions & 1 deletion src/VaultPHP/SecretEngines/Traits/BulkRequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ trait BulkRequestTrait

/**
* @param mixed $request
* @return void
* @return $this
*/
public function addBulkRequest($request)
{
$this->batch_input[] = $request;
return $this;
}

/**
Expand Down
Loading

0 comments on commit c3c73ff

Please sign in to comment.