Skip to content

MQE-1671: document using credentials with vault in MFTF tests #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ It points to `MAGENTO_BASE_URL` + `dev/tests/acceptance/utils/command.php`

Modify the default value:

- for non-default Magento installation
- when use a subdirectory in the `MAGENTO_BASE_URL`
- for non-default Magento installation
- when use a subdirectory in the `MAGENTO_BASE_URL`

Example: `dev/tests/acceptance/utils/command.php`

Expand All @@ -251,9 +251,35 @@ Example:
BROWSER=firefox
```

### CREDENTIAL_VAULT_ADDRESS

The Api address for a vault server.

Default: http://127.0.0.1:8200

Example:

```conf
# Default api address for local vault dev server
CREDENTIAL_VAULT_ADDRESS=http://127.0.0.1:8200
```

### CREDENTIAL_VAULT_SECRET_BASE_PATH

Vault secret engine base path.

Default: secret

Example:

```conf
# Default base path for kv secret engine in local vault dev server
CREDENTIAL_VAULT_SECRET_BASE_PATH=secret
```

<!-- Link definitions -->

[`MAGENTO_CLI_COMMAND_PATH`]: #magento_cli_command_path
[generateDate]: test/actions.md#generatedate
[mftf]: commands/mftf.md
[timezones]: http://php.net/manual/en/timezones.php
[timezones]: http://php.net/manual/en/timezones.php
126 changes: 103 additions & 23 deletions docs/credentials.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Credentials

When you test functionality that involves external services such as UPS, FedEx, PayPal, or SignifyD, use the MFTF credentials feature to hide sensitive [data][] like integration tokens and API keys.
When you test functionality that involves external services such as UPS, FedEx, PayPal, or SignifyD,
use the MFTF credentials feature to hide sensitive [data][] like integration tokens and API keys.

## Define sensitive data in `.credentials`
Currently MFTF supports two types of credential storage: **.credentials file** and **HashiCorp vault**.

#3 Configure File Storage
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a typo?


The MFTF creates a sample file for credentials during [initial setup][]: `magento2/dev/tests/acceptance/.credentials.example`.
The file contains an example list of keys for fields that can require credentials.

### Create `.credentials`
## Create `.credentials`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The headings are not accurate. For example, "Configure File Storage" should be one level higher than "Create .credentials", etc


To make the MFTF process the file with credentials, change directories to `magento2/dev/tests/acceptance/` and copy `.credentials.example` to `.credentials`.

Expand All @@ -19,7 +22,7 @@ cd dev/tests/acceptance/
cp .credentials.example .credentials
```

### Add `.credentials` to `.gitignore`
## Add `.credentials` to `.gitignore`
Copy link
Contributor Author

@jilu1 jilu1 Sep 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, and the rest of the headings


Verify that the file is excluded from tracking by `.gitignore` (unless you need this behavior):

Expand All @@ -33,49 +36,119 @@ The command outputs the path if the file is excluded:
.credentials
```

### Define sensitive data
## Define sensitive data in `.credentials` file

Open the `.credentials` file, uncomment the fields you want to use, and add your values:
Open the `.credentials` file, for Magento core credentials, uncomment the fields you want to use, and add your values:

```config
```conf
...
# Credentials for the USPS service
carriers_usps_userid=test_user
carriers_usps_password=Lmgxvrq89uPwECeV
magento/carriers_usps_userid=usps_test_user
magento/carriers_usps_password=Lmgxvrq89uPwECeV

# Credentials for the DHL service
#carriers/dhl/id_us=
#carriers/dhl/password_us=
#magento/carriers_dhl_id_us=dhl_test_user
#magento/carriers_dhl_password_us=Mlgxv3dsagVeG
....
```

Or add new key & value pairs for your own credentials. The keys use the following format:

```conf
<vendor>/<key_name>=<key_value>
```

<div class="bs-callout bs-callout-info" markdown="1">
The `/` symbol is not supported in a key name.
The `/` symbol is not supported in a `key_name` other than the one after your vendor or extension name.
</div>

You are free to use any other keys you like, as they are merely the keys to reference from your tests.
Otherwise you are free to use any other `key_name` you like, as they are merely the keys to reference from your tests.

```conf
# Credentials for the MyAwesome service
my_awesome_service_token=rRVSVnh3cbDsVG39oTMz4A
vendor/my_awesome_service_token=rRVSVnh3cbDsVG39oTMz4A
```

# Credentials for the USPS service
carriers_usps_userid=test_user
carriers_usps_password=Lmgxvrq89uPwECeV
....
## Configure Vault Storage

Hashicorp vault secures, stores, and tightly controls access to data in modern computing.
It provides advanced data protection for your testing credentials.

MFTF works with both `vault enterprise` and `vault open source` that use `KV Version 2` secret engine.

## Install vault CLI

Download and install vault CLI tool if you want to run or develop MFTF tests locally. [Download Vault][Download Vault]

### Authenticate to vault via vault CLI

Authenticate to vault server via vault CLI tool. [Login Vault][Login Vault]

```terminal
vault login -method -path
```
**Do not** use `-no-store` command option, as MFTF will rely on the persisted token in the token helper
(usually the local filesystem) for future api requests.

### Store secrets in vault

MFTF uses `KV Version 2` secret engine for secret storage.
More information for working with `KV Version 2` can be found in [Vault KV2][Vault KV2].

### Secrets path and key convention

The path and key for secret data must follow:

```conf
<SECRETS_BASE_PATH>/mftf/<VENDOR>/<SECRET_KEY>
```

```conf
# Secret path and key for carriers_usps_userid
secret/mftf/magento/carriers_usps_userid

# Secret path and key for carriers_usps_password
secret/mftf/magento/carriers_usps_password
```

### Write secrets to vault

You can use vault CLI or Api to write secret data (credentials, etc) to vault. Here is a CLI example:

```terminal
vault kv put secret/mftf/magento/carriers_usps_userid carriers_usps_userid=usps_test_user
vault kv put secret/mftf/magento/carriers_usps_password carriers_usps_password=Lmgxvrq89uPwECeV
```

### Setup MFTF to use vault

Add vault configuration environment variables [`CREDENTIAL_VAULT_ADDRESS`][] and [`CREDENTIAL_VAULT_SECRET_BASE_PATH`][]
from `etc/config/.env.example` in `.env`.
Set values according to your vault server configuration.

```conf
# Default vault dev server
CREDENTIAL_VAULT_ADDRESS=http://127.0.0.1:8200
CREDENTIAL_VAULT_SECRET_BASE_PATH=secret
```

## Configure both File Storage and Vault Storage

It is possible and sometimes useful to setup and use both `.credentials` file and vault for secret storage at the same time.
In this case, MFTF tests are able to read secret data at runtime from both storage, and local `.credentials` file will take precedence.

<!-- {% raw %} -->

## Use credentials in a test

Access the data defined in the `.credentials` file using the [`fillField`][] action with the `userInput` attribute.
Define the value as a reference to the corresponding key in the credentials file such as `{{_CREDS.my_data_key}}`:
Credentials can be used in actions: [`fillField`][], [`magentoCLI`][], and [`createData`][].

Define the value as a reference to the corresponding key in the credentials file or vault such as `{{_CREDS.my_data_key}}`:

- `_CREDS` is an environment constant pointing to the `.credentials` file
- `my_data_key` is a key in the the `.credentials` file that contains the value to be used in a test step

For example:
For example, reference secret data in the [`fillField`][] action with the `userInput` attribute.

```xml
<fillField stepKey="FillApiToken" selector=".api-token" userInput="{{_CREDS.my_data_key}}" />
Expand All @@ -88,13 +161,20 @@ For example:
The generated tests do not contain credentials values.
The MFTF dynamically retrieves, encrypts, and decrypts the sensitive data during test execution.
Decrypted credentials do not appear in the console, error logs, or [test reports][].
The decrypted values are only available in the `.credentials` file.
The decrypted values are only available in the `.credentials` file or within vault.

<div class="bs-callout bs-callout-info">
The MFTF tests delivered with Magento application do not use credentials and do not cover external services, because of sensitivity of the data.</div>

<!-- Link definitions -->
[`fillField`]: test/actions.md#fillfield
[`magentoCLI`]: test/actions.md#magentocli
[`createData`]: test/actions.md#createdata
[data]: data.md
[initial setup]: getting-started.md
[test reports]: reporting.md
[Download Vault]: https://www.hashicorp.com/products/vault/
[Login Vault]: https://www.vaultproject.io/docs/commands/login.html
[Vault KV2]: https://www.vaultproject.io/docs/secrets/kv/kv-v2.html
[`CREDENTIAL_VAULT_ADDRESS`]: configuration.md#CREDENTIAL_VAULT_ADDRESS
[`CREDENTIAL_VAULT_SECRET_BASE_PATH`]: configuration.md#CREDENTIAL_VAULT_SECRET_BASE_PATH