23 private static $INSTANCE =
null;
37 private $encodedKey =
null;
44 private $credentials = [];
53 if (self::$INSTANCE ==
null) {
57 return self::$INSTANCE;
63 private function __construct()
65 $this->encodedKey = base64_encode(openssl_random_pseudo_bytes(16));
66 $this->iv = substr(hash(
'sha256', $this->encodedKey), 0, 16);
67 $creds = $this->readInCredentialsFile();
68 $this->credentials = $this->encryptCredFileContents($creds);
80 if (!array_key_exists($key, $this->credentials)) {
82 "{$key} not defined in .credentials, please provide a value in order to use this secret in a test." 89 "retrieving secret for key name {$key}" 93 return $this->credentials[$key] ??
null;
102 private function readInCredentialsFile()
104 $credsFilePath = str_replace(
105 '.credentials.example',
110 if (!file_exists($credsFilePath)) {
112 "Cannot find .credentials file, please create in " 113 . TESTS_BP .
" in order to reference sensitive information" 117 return file($credsFilePath, FILE_IGNORE_NEW_LINES);
126 private function encryptCredFileContents($credContents)
128 $encryptedCreds = [];
129 foreach ($credContents as $credValue) {
130 if (substr($credValue, 0, 1) ===
'#' || empty($credValue)) {
134 list($key,
$value) = explode(
"=", $credValue);
136 $encryptedCreds[$key] = openssl_encrypt(
138 self::ENCRYPTION_ALGO,
146 return $encryptedCreds;
157 return openssl_decrypt(
$value, self::ENCRYPTION_ALGO, $this->encodedKey, 0, $this->iv);
const CREDENTIALS_FILE_PATH
decryptSecretValue($value)