@@ -15,13 +15,6 @@ class CredentialStore
1515 const ARRAY_KEY_FOR_VAULT = 'vault ' ;
1616 const ARRAY_KEY_FOR_FILE = 'file ' ;
1717
18- /**
19- * Numeric indexed array that defines the access precedence of credential storage
20- *
21- * @var array
22- */
23- private static $ credStoragePrecedence = [self ::ARRAY_KEY_FOR_FILE , self ::ARRAY_KEY_FOR_VAULT ];
24-
2518 /**
2619 * Credential storage array
2720 *
@@ -58,6 +51,12 @@ public static function getInstance()
5851 */
5952 private function __construct ()
6053 {
54+ // Initialize file storage
55+ try {
56+ $ this ->credStorage [self ::ARRAY_KEY_FOR_FILE ] = new FileStorage ();
57+ } catch (TestFrameworkException $ e ) {
58+ }
59+
6160 // Initialize vault storage
6261 $ csBaseUrl = getenv ('CREDENTIAL_VAULT_BASE_URL ' );
6362 $ csToken = getenv ('CREDENTIAL_VAULT_TOKEN ' );
@@ -71,20 +70,11 @@ private function __construct()
7170 }
7271 }
7372
74- // Initialize file storage
75- try {
76- $ this -> credStorage [ self :: ARRAY_KEY_FOR_FILE ] = new FileStorage ();
77- } catch ( TestFrameworkException $ e ) {
73+ if ( empty ( $ this -> credStorage )) {
74+ throw new TestFrameworkException (
75+ " No credential storage is properly configured. Please configure vault or .credentials file. "
76+ );
7877 }
79-
80- foreach ($ this ->credStorage as $ cred ) {
81- if (null !== $ cred ) {
82- return ;
83- }
84- }
85- throw new TestFrameworkException (
86- "No credential storage is properly configured. Please configure vault or .credentials file. "
87- );
8878 }
8979
9080 /**
@@ -96,14 +86,12 @@ private function __construct()
9686 */
9787 public function getSecret ($ key )
9888 {
99- // Get secret data from storage according to defined precedence
89+ // Get secret data from storage according to the order they are stored
10090 // File storage is preferred over vault storage to allow local secret value overriding remote secret value
101- foreach (self ::$ credStoragePrecedence as $ credType ) {
102- if (null !== $ this ->credStorage [$ credType ]) {
103- $ value = $ this ->credStorage [$ credType ]->getEncryptedValue ($ key );
104- if (null !== $ value ) {
105- return $ value ;
106- }
91+ foreach ($ this ->credStorage as $ storage ) {
92+ $ value = $ storage ->getEncryptedValue ($ key );
93+ if (null !== $ value ) {
94+ return $ value ;
10795 }
10896 }
10997
@@ -122,10 +110,8 @@ public function getSecret($key)
122110 public function decryptSecretValue ($ value )
123111 {
124112 // Loop through storage to decrypt value
125- foreach (self ::$ credStoragePrecedence as $ credType ) {
126- if (null !== $ this ->credStorage [$ credType ]) {
127- return $ this ->credStorage [$ credType ]->getDecryptedValue ($ value );
128- }
113+ foreach ($ this ->credStorage as $ storage ) {
114+ return $ storage ->getDecryptedValue ($ value );
129115 }
130116 }
131117
@@ -138,10 +124,8 @@ public function decryptSecretValue($value)
138124 public function decryptAllSecretsInString ($ string )
139125 {
140126 // Loop through storage to decrypt all occurrences from input string
141- foreach (self ::$ credStoragePrecedence as $ credType ) {
142- if (null !== $ this ->credStorage [$ credType ]) {
143- return $ this ->credStorage [$ credType ]->getAllDecryptedValuesInString ($ string );
144- }
127+ foreach ($ this ->credStorage as $ storage ) {
128+ return $ storage ->getAllDecryptedValuesInString ($ string );
145129 }
146130 }
147131}
0 commit comments