migrate_api_key
Add an API key to Drupal migration source URLs
This module was created to support Drupal-to-Drupal migrations, but theoretically should work when querying any remote content that requires an API key to be passed as a query parameter.
How it works
It checks for the API key in Pantheon secrets first, then falls back to environment variables.
If the API key is not found or is empty, it adds a warning message in Drupal's logs but proceeds with the migration.
If you want to set up API keys for individual Drupal users, you can use the Key Auth module..
Security
To stay out of trouble (probably):
- Never use the API keys of elevated users or Admins, lest they ever get intercepted.
- This plugin does not encrypt or secure your API key in any way, it simply appends it to the source URLs of your migrations before they are sent as GET requests.
- If migrating content from another Drupal site, it is advisable to create an 'API' Role with the bare minimum 'view' permissions of whatever entities your migration is fetching.
- Only use it if your migrations are hitting HTTPS URLs!
Usage
- Ensure that the
MIGRATE_API_KEYis set either as an environment variable or as a Pantheon secret. If developing locally, you may need to restart your dev environment after setting the environment variable. - To any URL-based migration that requires the API key to be appended to its source URLs:
- Add the migration tag
add_api_key, and - Change the source plugin from
urltomigrate_api_key_url_plugin.
- Add the migration tag
- Run your migrations as usual. The plugin will automatically append your provided API key to the source URLs.
Example
Setting environment variables for DDEV, in config.local.yml:
name: my-project
type: drupal10
web_environment:
- MIGRATE_API_KEY=12345
...
Configuring a migration
id: example_migration
label: "Example Migration"
migration_group: example_group
migration_tags:
- "add_api_key"
source:
plugin: migrate_api_key_url_plugin
urls:
- "https://example.com/api/data"
...
The above configuration will result in the source URL:
https://example.com/api/data
being transformed to
https://example.com/api/data?api-key=12345
Troubleshooting
My environment variable is not being detected!
This module checks for Pantheon secrets first via pantheon_get_secret(), then falls back to getenv() and $_ENV[VAR]. If you are running your migrations in an environment that does not support these methods, it will not be retrieved.
It was also built using DDEV and has not been tested on other local development stacks e.g. Lando.
Ensure your variable is set in a place discoverable to your environment. If developing locally, ensure you have restarted your local environment after changing environment variables.
Known issues
PHP Unit tests are present but have not been run and are therefore untested.