-
Notifications
You must be signed in to change notification settings - Fork 132
MQE-1600: MFTF Vault integration #382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
6ec227f
3889f9a
c3f67ea
4c10d1c
36c5865
3f7fe82
6313da3
80500e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
language: php | ||
php: | ||
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,31 +12,35 @@ | |
|
||
class CredentialStore | ||
{ | ||
const ARRAY_KEY_FOR_VAULT = 'vault'; | ||
const ARRAY_KEY_FOR_FILE = 'file'; | ||
|
||
/** | ||
* Singleton instance | ||
* Numeric indexed array that defines the access precedence of credential storage | ||
* | ||
* @var CredentialStore | ||
* @var array | ||
*/ | ||
private static $INSTANCE = null; | ||
private static $credStoragePrecedence = [self::ARRAY_KEY_FOR_FILE, self::ARRAY_KEY_FOR_VAULT]; | ||
|
||
/** | ||
* File storage for credentials | ||
* Credential storage array | ||
* | ||
* @var FileStorage | ||
* @var array | ||
*/ | ||
private $credFile = null; | ||
private $credStorage = []; | ||
|
||
/** | ||
* Vault storage for credentials | ||
* Singleton instance | ||
* | ||
* @var VaultStorage | ||
* @var CredentialStore | ||
*/ | ||
private $credVault = null; | ||
private static $INSTANCE = null; | ||
|
||
/** | ||
* Static singleton getter for CredentialStore Instance | ||
* | ||
* @return CredentialStore | ||
* @throws TestFrameworkException | ||
*/ | ||
public static function getInstance() | ||
{ | ||
|
@@ -48,7 +52,9 @@ public static function getInstance() | |
} | ||
|
||
/** | ||
* CredentialStore constructor. | ||
* CredentialStore constructor | ||
* | ||
* @throws TestFrameworkException | ||
*/ | ||
private function __construct() | ||
{ | ||
|
@@ -57,16 +63,28 @@ private function __construct() | |
$csToken = getenv('CREDENTIAL_VAULT_TOKEN'); | ||
if ($csBaseUrl !== false && $csToken !== false) { | ||
try { | ||
$this->credVault = new VaultStorage(rtrim($csBaseUrl, '/'), $csToken); | ||
$this->credStorage[self::ARRAY_KEY_FOR_VAULT] = new VaultStorage( | ||
rtrim($csBaseUrl, '/'), | ||
$csToken | ||
); | ||
} catch (TestFrameworkException $e) { | ||
jilu1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
// Initialize file storage | ||
try { | ||
$this->credFile = new FileStorage(); | ||
$this->credStorage[self::ARRAY_KEY_FOR_FILE] = new FileStorage(); | ||
} catch (TestFrameworkException $e) { | ||
jilu1 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log the exception so that we can see that That way, if it fails in a build or something we can refer to the logs to see what did and didn't initialize properly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the exceptions are already logged in FileStorage and VaultStorage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but human readable statements will make the log more useful. Instead of just seeing a failure message, you see that the credential store is not using a specific method. |
||
} | ||
|
||
foreach ($this->credStorage as $cred) { | ||
jilu1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (null !== $cred) { | ||
return; | ||
} | ||
} | ||
throw new TestFrameworkException( | ||
"No credential storage is properly configured. Please configure vault or .credentials file." | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -78,24 +96,20 @@ private function __construct() | |
*/ | ||
public function getSecret($key) | ||
{ | ||
// Get secret data from vault storage first | ||
if (null !== $this->credVault) { | ||
$value = $this->credVault->getEncryptedValue($key); | ||
if (!empty($value)) { | ||
return $value; | ||
} | ||
} | ||
|
||
// Get secret data from file when not found in vault | ||
if (null !== $this->credFile) { | ||
$value = $this->credFile->getEncryptedValue($key); | ||
if (!empty($value)) { | ||
return $value; | ||
// Get secret data from storage according to defined precedence | ||
// File storage is preferred over vault storage to allow local secret value overriding remote secret value | ||
foreach (self::$credStoragePrecedence as $credType) { | ||
if (null !== $this->credStorage[$credType]) { | ||
$value = $this->credStorage[$credType]->getEncryptedValue($key); | ||
if (null !== $value) { | ||
return $value; | ||
} | ||
} | ||
} | ||
|
||
throw new TestFrameworkException( | ||
"value for key \"$key\" not found in credential storage." | ||
"\"{$key}\" not defined in vault or .credentials file, " | ||
. "please provide a value in order to use this secret in a test." | ||
); | ||
} | ||
|
||
|
@@ -107,12 +121,11 @@ public function getSecret($key) | |
*/ | ||
public function decryptSecretValue($value) | ||
{ | ||
if (null !== $this->credVault) { | ||
return $this->credVault->getDecryptedValue($value); | ||
} | ||
|
||
if (null !== $this->credFile) { | ||
return $this->credFile->getDecryptedValue($value); | ||
// Loop through storage to decrypt value | ||
foreach (self::$credStoragePrecedence as $credType) { | ||
if (null !== $this->credStorage[$credType]) { | ||
return $this->credStorage[$credType]->getDecryptedValue($value); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -124,12 +137,11 @@ public function decryptSecretValue($value) | |
*/ | ||
public function decryptAllSecretsInString($string) | ||
{ | ||
if (null !== $this->credVault) { | ||
return $this->credVault->getAllDecryptedValues($string); | ||
} | ||
|
||
if (null !== $this->credFile) { | ||
return $this->credFile->getAllDecryptedValues($string); | ||
// Loop through storage to decrypt all occurrences from input string | ||
foreach (self::$credStoragePrecedence as $credType) { | ||
if (null !== $this->credStorage[$credType]) { | ||
return $this->credStorage[$credType]->getAllDecryptedValuesInString($string); | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.