Skip to content

Commit fd9e721

Browse files
committed
MQE-1919: MFTF AWS Secrets Manager - CI Use
1 parent a863890 commit fd9e721

File tree

3 files changed

+86
-48
lines changed

3 files changed

+86
-48
lines changed

etc/config/.env.example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ BROWSER=chrome
3737
#*** To use AWS Secrets Manager to manage _CREDS secrets, uncomment and set region, profile is optional, when omitted, AWS default credential provider chain will be used ***#
3838
#CREDENTIAL_AWS_SECRETS_MANAGER_PROFILE=default
3939
#CREDENTIAL_AWS_SECRETS_MANAGER_REGION=us-east-1
40-
#*** If using non-default AWS account ***#
41-
#CREDENTIAL_AWS_ACCOUNT_ID=
4240

4341
#*** Uncomment these properties to set up a dev environment with symlinked projects ***#
4442
#TESTS_BP=

src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/CredentialStore.php

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,45 +57,10 @@ public static function getInstance()
5757
*/
5858
private function __construct()
5959
{
60-
// Initialize file storage
61-
try {
62-
$this->credStorage[self::ARRAY_KEY_FOR_FILE] = new FileStorage();
63-
} catch (TestFrameworkException $e) {
64-
}
65-
66-
// Initialize vault storage
67-
$cvAddress = getenv('CREDENTIAL_VAULT_ADDRESS');
68-
$cvSecretPath = getenv('CREDENTIAL_VAULT_SECRET_BASE_PATH');
69-
if ($cvAddress !== false && $cvSecretPath !== false) {
70-
try {
71-
$this->credStorage[self::ARRAY_KEY_FOR_VAULT] = new VaultStorage(
72-
UrlFormatter::format($cvAddress, false),
73-
'/' . trim($cvSecretPath, '/')
74-
);
75-
} catch (TestFrameworkException $e) {
76-
}
77-
}
78-
79-
// Initialize AWS Secrets Manager storage
80-
$awsRegion = getenv('CREDENTIAL_AWS_SECRETS_MANAGER_REGION');
81-
$awsProfile = getenv('CREDENTIAL_AWS_SECRETS_MANAGER_PROFILE');
82-
$awsId = getenv('CREDENTIAL_AWS_ACCOUNT_ID');
83-
if ($awsRegion !== false) {
84-
if ($awsProfile === false) {
85-
$awsProfile = null;
86-
}
87-
if ($awsId === false) {
88-
$awsId = null;
89-
}
90-
try {
91-
$this->credStorage[self::ARRAY_KEY_FOR_AWS_SECRETS_MANAGER] = new AwsSecretsManagerStorage(
92-
$awsRegion,
93-
$awsProfile,
94-
$awsId
95-
);
96-
} catch (TestFrameworkException $e) {
97-
}
98-
}
60+
// Initialize credential storage by defined order of precedence as the following
61+
$this->initializeFileStorage();
62+
$this->initializeVaultStorage();
63+
$this->initializeAwsSecretsManagerStorage();
9964

10065
if (empty($this->credStorage)) {
10166
throw new TestFrameworkException(
@@ -155,4 +120,68 @@ public function decryptAllSecretsInString($string)
155120
return $storage->getAllDecryptedValuesInString($string);
156121
}
157122
}
123+
124+
/**
125+
* Initialize file storage
126+
*
127+
* @return void
128+
*/
129+
private function initializeFileStorage()
130+
{
131+
// Initialize file storage
132+
try {
133+
$this->credStorage[self::ARRAY_KEY_FOR_FILE] = new FileStorage();
134+
} catch (TestFrameworkException $e) {
135+
}
136+
}
137+
138+
/**
139+
* Initialize Vault storage
140+
*
141+
* @return void
142+
*/
143+
private function initializeVaultStorage()
144+
{
145+
// Initialize vault storage
146+
$cvAddress = getenv('CREDENTIAL_VAULT_ADDRESS');
147+
$cvSecretPath = getenv('CREDENTIAL_VAULT_SECRET_BASE_PATH');
148+
if ($cvAddress !== false && $cvSecretPath !== false) {
149+
try {
150+
$this->credStorage[self::ARRAY_KEY_FOR_VAULT] = new VaultStorage(
151+
UrlFormatter::format($cvAddress, false),
152+
'/' . trim($cvSecretPath, '/')
153+
);
154+
} catch (TestFrameworkException $e) {
155+
}
156+
}
157+
}
158+
159+
/**
160+
* Initialize AWS Secrets Manager storage
161+
*
162+
* @return void
163+
*/
164+
private function initializeAwsSecretsManagerStorage()
165+
{
166+
// Initialize AWS Secrets Manager storage
167+
$awsRegion = getenv('CREDENTIAL_AWS_SECRETS_MANAGER_REGION');
168+
$awsProfile = getenv('CREDENTIAL_AWS_SECRETS_MANAGER_PROFILE');
169+
$awsId = getenv('CREDENTIAL_AWS_ACCOUNT_ID');
170+
if (!empty($awsRegion)) {
171+
if (empty($awsProfile)) {
172+
$awsProfile = null;
173+
}
174+
if (empty($awsId)) {
175+
$awsId = null;
176+
}
177+
try {
178+
$this->credStorage[self::ARRAY_KEY_FOR_AWS_SECRETS_MANAGER] = new AwsSecretsManagerStorage(
179+
$awsRegion,
180+
$awsProfile,
181+
$awsId
182+
);
183+
} catch (TestFrameworkException $e) {
184+
}
185+
}
186+
}
158187
}

src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/SecretStorage/AwsSecretsManagerStorage.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,20 @@ private function parseAwsSecretResult($awsResult, $key)
145145
if (isset($awsResult['SecretString'])) {
146146
$rawSecret = $awsResult['SecretString'];
147147
} else {
148-
throw new TestFrameworkException("Error parsing result from AWS Secrets Manager");
148+
throw new TestFrameworkException(
149+
"'SecretString' field is not set in AWS Result. Error parsing result from AWS Secrets Manager"
150+
);
149151
}
152+
153+
// Secrets are saved as JSON structures of key/value pairs if using AWS Secrets Manager console, and
154+
// Secrets are saved as plain text if using AWS CLI. We need to handle both cases.
150155
$secret = json_decode($rawSecret, true);
151156
if (isset($secret[$key])) {
152157
return $secret[$key];
158+
} elseif (is_string($awsResult)) {
159+
return $awsResult;
153160
}
154-
throw new TestFrameworkException("Error parsing result from AWS Secrets Manager");
161+
throw new TestFrameworkException("$key not found in AWS Result. Error parsing result from AWS Secrets Manager");
155162
}
156163

157164
/**
@@ -169,13 +176,17 @@ private function createAwsSecretsManagerClient($region, $profile)
169176
return;
170177
}
171178

172-
// Create AWS Secrets Manager client
173-
$this->client = new SecretsManagerClient([
174-
'profile' => $profile,
179+
$options = [
175180
'region' => $region,
176-
'version' => self::LATEST_VERSION
177-
]);
181+
'version' => self::LATEST_VERSION,
182+
];
178183

184+
if (!empty($profile)) {
185+
$options['profile'] = $profile;
186+
}
187+
188+
// Create AWS Secrets Manager client
189+
$this->client = new SecretsManagerClient($options);
179190
if ($this->client === null) {
180191
throw new TestFrameworkException("Unable to create AWS Secrets Manager client");
181192
}

0 commit comments

Comments
 (0)