Skip to content

Commit ffec377

Browse files
committed
MQE-1918: MFTF AWS Secrets Manager - Local Use
1 parent 9bc59f8 commit ffec377

File tree

9 files changed

+483
-12
lines changed

9 files changed

+483
-12
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
"require": {
1212
"php": "7.0.2||7.0.4||~7.0.6||~7.1.0||~7.2.0||~7.3.0",
1313
"ext-curl": "*",
14+
"ext-json": "*",
15+
"ext-openssl": "*",
1416
"allure-framework/allure-codeception": "~1.3.0",
17+
"aws/aws-sdk-php": "^3.132",
1518
"codeception/codeception": "~2.4.5",
1619
"composer/composer": "^1.4",
1720
"consolidation/robo": "^1.0.0",

composer.lock

Lines changed: 146 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace tests\unit\Magento\FunctionalTestFramework\DataGenerator\Handlers\SecretStorage;
8+
9+
use Aws\SecretsManager\SecretsManagerClient;
10+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\SecretStorage\AwsSecretManagerStorage;
11+
use Aws\Result;
12+
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
13+
use ReflectionClass;
14+
15+
class AwsSecretManagerStorageTest extends MagentoTestCase
16+
{
17+
/**
18+
* Test encryption/decryption functionality in AwsSecretManagerStorage class.
19+
*/
20+
public function testEncryptAndDecrypt()
21+
{
22+
// Setup test data
23+
$testProfile = 'profile';
24+
$testRegion = 'region';
25+
$testLongKey = 'magento/myKey';
26+
$testShortKey = 'myKey';
27+
$testValue = 'myValue';
28+
$data = [
29+
'Name' => 'mftf/magento/' . $testShortKey,
30+
'SecretString' => json_encode([$testShortKey => $testValue])
31+
];
32+
/** @var Result */
33+
$result = new Result($data);
34+
35+
$mockClient = $this->getMockBuilder(SecretsManagerClient::class)
36+
->disableOriginalConstructor()
37+
->setMethods(['__call'])
38+
->getMock();
39+
40+
$mockClient->expects($this->once())
41+
->method('__call')
42+
->willReturnCallback(function($name, $args) use ($result) {
43+
return $result;
44+
});
45+
46+
/** @var SecretsManagerClient */
47+
$credentialStorage = new AwsSecretManagerStorage($testRegion, $testProfile);
48+
$reflection = new ReflectionClass($credentialStorage);
49+
$reflection_property = $reflection->getProperty('client');
50+
$reflection_property->setAccessible(true);
51+
$reflection_property->setValue($credentialStorage, $mockClient);
52+
53+
// Test getEncryptedValue()
54+
$encryptedCred = $credentialStorage->getEncryptedValue($testLongKey);
55+
56+
// Assert the value we've gotten is in fact not identical to our test value
57+
$this->assertNotEquals($testValue, $encryptedCred);
58+
59+
// Test getDecryptedValue()
60+
$actualValue = $credentialStorage->getDecryptedValue($encryptedCred);
61+
62+
// Assert that we are able to successfully decrypt our secret value
63+
$this->assertEquals($testValue, $actualValue);
64+
}
65+
}

docs/configuration.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,28 @@ Example:
277277
CREDENTIAL_VAULT_SECRET_BASE_PATH=secret
278278
```
279279

280+
### CREDENTIAL_AWS_SECRET_MANAGER_REGION
281+
282+
The region that Aws Secret Manager is located.
283+
284+
Example:
285+
286+
```conf
287+
# Region of Aws Secret Manager
288+
CREDENTIAL_AWS_SECRET_MANAGER_REGION=us-east-1
289+
```
290+
291+
### CREDENTIAL_AWS_SECRET_MANAGER_PROFILE
292+
293+
The profile used to connect to Aws Secret Manager.
294+
295+
Example:
296+
297+
```conf
298+
# Profile used to connect to Aws Secret Manager.
299+
CREDENTIAL_AWS_SECRET_MANAGER_PROFILE=default
300+
```
301+
280302
### ENABLE_BROWSER_LOG
281303

282304
Enables addition of browser logs to Allure steps

docs/credentials.md

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
When you test functionality that involves external services such as UPS, FedEx, PayPal, or SignifyD,
44
use the MFTF credentials feature to hide sensitive [data][] like integration tokens and API keys.
55

6-
Currently the MFTF supports two types of credential storage:
6+
Currently the MFTF supports three types of credential storage:
77

88
- **.credentials file**
9-
- **HashiCorp vault**
9+
- **HashiCorp Vault**
10+
- **Aws Secret Manager**
1011

1112
## Configure File Storage
1213

@@ -135,11 +136,64 @@ CREDENTIAL_VAULT_ADDRESS=http://127.0.0.1:8200
135136
CREDENTIAL_VAULT_SECRET_BASE_PATH=secret
136137
```
137138

138-
## Configure both File Storage and Vault Storage
139+
## Configure Aws Secret Manager
139140

140-
It is possible and sometimes useful to setup and use both `.credentials` file and vault for secret storage at the same time.
141-
In this case, the MFTF tests are able to read secret data at runtime from both storage options, but the local `.credentials` file will take precedence.
141+
Aws Secrets Manager offers secret management that supports:
142+
- Secret rotation with built-in integration for Amazon RDS, Amazon Redshift, and Amazon DocumentDB
143+
- Fine-grained policies and permissions
144+
- Audit secret rotation centrally for resources in the AWS Cloud, third-party services, and on-premises
142145

146+
### Prerequisites
147+
- AWS account
148+
- AWS Secret Manger is created and configured
149+
- IAM User or Role is created
150+
151+
### Store secrets in Aws Secret Manager
152+
153+
#### Secrets format
154+
`Secret Name`, `Secret Key`, `Secret Value` are three key pieces of information to construct an Aws Secret.
155+
`Secret Key` and `Secret Value` can be any content you want to secure, `Secret Name` must follow the format:
156+
157+
```conf
158+
mftf/<VENDOR>/<SECRET_KEY>
159+
```
160+
161+
```conf
162+
# Secret name for carriers_usps_userid
163+
mftf/magento/carriers_usps_userid
164+
165+
# Secret key for carriers_usps_userid
166+
carriers_usps_userid
167+
168+
# Secret name for carriers_usps_password
169+
mftf/magento/carriers_usps_password
170+
171+
# Secret key for carriers_usps_password
172+
carriers_usps_password
173+
```
174+
175+
### Setup MFTF to use Aws Secret Manager
176+
177+
To use Aws Secret Manager, the Aws region to connect to is required. You can set it through environment variable [`CREDENTIAL_AWS_SECRET_MANAGER_REGION`][] in `.env`.
178+
179+
MFTF uses the recommended [Default Credential Provider Chain][credential chain] to establish connection to Aws Secret Manager service.
180+
You can setup credentials according to [Default Credential Provider Chain][credential chain] and there is no MFTF specific setup required.
181+
Optionally, however, you can explicitly set Aws profile through environment variable [`CREDENTIAL_AWS_SECRET_MANAGER_PROFILE`][] in `.env`.
182+
183+
```conf
184+
# Sample Aws Secret Manager configuration
185+
CREDENTIAL_AWS_SECRET_MANAGER_REGION=us-east-1
186+
CREDENTIAL_AWS_SECRET_MANAGER_PROFILE=default
187+
```
188+
189+
## Configure multiple credential storage
190+
191+
It is possible and sometimes useful to setup and use multiple credential storage at the same time.
192+
In this case, the MFTF tests are able to read secret data at runtime from all storage options, in this case MFTF use the following precedence:
193+
194+
```
195+
.credentials File > HashiCorp Vault > Aws Secret Manager
196+
```
143197
<!-- {% raw %} -->
144198

145199
## Use credentials in a test
@@ -183,3 +237,6 @@ The MFTF tests delivered with Magento application do not use credentials and do
183237
[Vault KV2]: https://www.vaultproject.io/docs/secrets/kv/kv-v2.html
184238
[`CREDENTIAL_VAULT_ADDRESS`]: configuration.md#credential_vault_address
185239
[`CREDENTIAL_VAULT_SECRET_BASE_PATH`]: configuration.md#credential_vault_secret_base_path
240+
[credential chain]: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
241+
[`CREDENTIAL_AWS_SECRET_MANAGER_PROFILE`]: configuration.md#credential_aws_secret_manager_profile
242+
[`CREDENTIAL_AWS_SECRET_MANAGER_REGION`]: configuration.md#credential_aws_secret_manager_region

etc/config/.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ BROWSER=chrome
3030
#MAGENTO_RESTAPI_SERVER_PORT=8080
3131
#MAGENTO_RESTAPI_SERVER_PROTOCOL=https
3232

33-
#*** Uncomment and set vault address and secret base path if you want to use vault to manage _CREDS secrets ***#
33+
#*** To use HashiCorp Vault to manage _CREDS secrets, uncomment and set vault address and secret base path ***#
3434
#CREDENTIAL_VAULT_ADDRESS=http://127.0.0.1:8200
3535
#CREDENTIAL_VAULT_SECRET_BASE_PATH=secret
3636

37+
#*** To use AWS Secret Manager to manage _CREDS secrets, uncomment and set region, profile is optional, when omitted, AWS default credential provider chain will be used ***#
38+
#CREDENTIAL_AWS_SECRET_MANAGER_PROFILE=default
39+
#CREDENTIAL_AWS_SECRET_MANAGER_REGION=us-east-1
40+
3741
#*** Uncomment these properties to set up a dev environment with symlinked projects ***#
3842
#TESTS_BP=
3943
#FW_BP=

0 commit comments

Comments
 (0)