@@ -15,13 +15,6 @@ class CredentialStore
15
15
const ARRAY_KEY_FOR_VAULT = 'vault ' ;
16
16
const ARRAY_KEY_FOR_FILE = 'file ' ;
17
17
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
-
25
18
/**
26
19
* Credential storage array
27
20
*
@@ -58,6 +51,12 @@ public static function getInstance()
58
51
*/
59
52
private function __construct ()
60
53
{
54
+ // Initialize file storage
55
+ try {
56
+ $ this ->credStorage [self ::ARRAY_KEY_FOR_FILE ] = new FileStorage ();
57
+ } catch (TestFrameworkException $ e ) {
58
+ }
59
+
61
60
// Initialize vault storage
62
61
$ csBaseUrl = getenv ('CREDENTIAL_VAULT_BASE_URL ' );
63
62
$ csToken = getenv ('CREDENTIAL_VAULT_TOKEN ' );
@@ -71,20 +70,11 @@ private function __construct()
71
70
}
72
71
}
73
72
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
+ );
78
77
}
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
- );
88
78
}
89
79
90
80
/**
@@ -96,14 +86,12 @@ private function __construct()
96
86
*/
97
87
public function getSecret ($ key )
98
88
{
99
- // Get secret data from storage according to defined precedence
89
+ // Get secret data from storage according to the order they are stored
100
90
// 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 ;
107
95
}
108
96
}
109
97
@@ -122,10 +110,8 @@ public function getSecret($key)
122
110
public function decryptSecretValue ($ value )
123
111
{
124
112
// 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 );
129
115
}
130
116
}
131
117
@@ -138,10 +124,8 @@ public function decryptSecretValue($value)
138
124
public function decryptAllSecretsInString ($ string )
139
125
{
140
126
// 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 );
145
129
}
146
130
}
147
131
}
0 commit comments