atldays / laravel-secrets
Load secrets from external providers and apply them to Laravel from a cached payload.
Requires
- php: ^8.1
- aws/aws-sdk-php: ^3.337
- illuminate/console: ^10.0|^11.0|^12.0|^13.0
- illuminate/contracts: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- spatie/laravel-data: ^4.20
- spatie/laravel-package-tools: ^1.92
Requires (Dev)
- laravel/pint: ^1.13|^1.24
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.5|^11.5|^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-01 13:26:17 UTC
README
atldays/laravel-secrets lets Laravel applications load sensitive configuration from external secret providers without turning every request into a network call.
It gives you the same developer experience as regular environment variables, but the values come from a provider, are cached inside Laravel, and are applied during boot only from that cached payload.
That means:
- no provider API calls during normal requests
- no custom runtime lookup layer in your application code
- no need to replace your config with package-specific accessors
- one explicit refresh step when secrets change
This package is designed for teams that want a safer and more operationally friendly alternative to keeping production secrets directly in .env files.
Why Use It
Most Laravel applications still depend on environment variables for things like:
APP_KEY- database credentials
- queue and mail credentials
- API tokens
- service-to-service authentication
That works well in local development, but in production it often means:
- secrets are copied into files on disk
- rotation becomes manual and error-prone
- infrastructure and application config drift apart
- secret storage is handled outside a dedicated secret manager
Laravel Secrets loads values from a provider and makes them available through Laravel's configuration.
How It Works
php artisan secrets:config-cache --no-interactionfetches and validates secrets at deployment time and publishes Laravel's configuration cache. Normal application startup reads mapped values throughconfig()without contacting the provider or the package cache.php artisan secrets:cachestores the provider payload in the configured cache store. At boot, the package can read that payload and apply values to Laravel's env repository and the paths inconfig_variables.
Supported Drivers
Currently supported drivers:
AWS Secrets Manager
More drivers are welcome. See CONTRIBUTING.md for the driver contribution workflow.
Installation
composer require atldays/laravel-secrets
Compatibility
Current support matrix:
- PHP
8.1+ - Laravel
10,11,12, and13 - AWS Secrets Manager as the first built-in driver
The package is tested in CI across the supported Laravel versions, and the AWS driver also includes live integration coverage.
Publish the config when you want to customize the package:
php artisan vendor:publish --tag=secrets-config
Quick Start
Minimal setup:
<?php use Atldays\Secrets\Drivers\AwsSecretManager; return [ 'validation' => [ 'required' => ['APP_KEY', 'DB_PASSWORD'], 'optional' => [], ], 'config_variables' => [ 'app.key' => 'APP_KEY', 'database.connections.pgsql.host' => 'DB_HOST', 'database.connections.pgsql.database' => 'DB_DATABASE', 'database.connections.pgsql.username' => 'DB_USERNAME', 'database.connections.pgsql.password' => 'DB_PASSWORD', ], 'drivers' => [ AwsSecretManager::class => [ 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'secret_id' => env('AWS_SECRETS_MANAGER_SECRET_ID'), ], ], ];
Set AWS_SECRETS_MANAGER_SECRET_ID to the exact name or ARN of the AWS secret containing those keys, then prepare the configuration cache.
php artisan secrets:config-cache --no-interaction
The application can now read the mapped values through config().
Configuration
apply_secrets
Controls whether cached secrets should be applied during boot.
true: the package applies cached secretsfalse: the package leaves the application config untouched
failure_mode
Controls what happens when applying cached secrets during boot fails.
Supported values:
throwwarnignore
This affects boot-time application of cached secrets, not the secrets:cache command itself. The cache command always reports its own execution errors directly.
cache
Controls where the secrets payload is stored after a successful refresh.
store: any Laravel cache storekey: the cache key that holds the payloadttl: cache lifetime in minutes
The default ttl is 43200, which equals 30 days.
config_variables
This is the explicit bridge between your secret payload and Laravel config.
Laravel resolves many env() calls before package boot, so the package cannot rely on env mutation alone. That is why config_variables exists.
Example:
'config_variables' => [ 'app.key' => 'APP_KEY', 'database.connections.pgsql.password' => 'DB_PASSWORD', ]
validation
The secrets:config-cache command checks the merged provider payload before writing the cache. Configure both lists with provider key names in config/secrets.php. The lists default to empty.
'validation' => [ 'required' => ['APP_KEY', 'REDIS_PASSWORD'], 'optional' => ['MAIL_PASSWORD'], ], 'config_variables' => [ 'app.key' => 'APP_KEY', 'database.redis.default.password' => 'REDIS_PASSWORD', 'mail.mailers.smtp.password' => 'MAIL_PASSWORD', ],
A missing key, empty string, or whitespace-only string fails the build for a required secret. An optional secret in the same state produces a warning and keeps its configured fallback. Mapped strings are copied verbatim, including '0' and leading or trailing spaces.
AWS Secret Manager
Driver Configuration
The AWS driver supports:
regionversionsecret_idkey_strategylist_max_resultsfilterfilter_modefilter_options
Example:
use Atldays\Secrets\Drivers\AwsSecretManager; use Atldays\Secrets\Filters\AwsSecretManagerFilter; 'drivers' => [ AwsSecretManager::class => [ 'region' => env('AWS_DEFAULT_REGION', 'eu-central-1'), 'version' => env('AWS_SECRETS_MANAGER_VERSION', '2017-10-17'), 'key_strategy' => env('AWS_SECRETS_MANAGER_KEY_STRATEGY', 'basename'), 'list_max_results' => env('AWS_SECRETS_MANAGER_LIST_MAX_RESULTS'), 'filter' => AwsSecretManagerFilter::class, 'filter_mode' => env('AWS_SECRETS_MANAGER_FILTER_MODE', 'or'), 'filter_options' => [ 'tags' => env('AWS_SECRETS_MANAGER_TAGS'), 'prefixes' => env('AWS_SECRETS_MANAGER_PREFIXES'), 'names' => env('AWS_SECRETS_MANAGER_NAMES'), ], ], ],
Read a secret by exact name or ARN
Set AWS_SECRETS_MANAGER_SECRET_ID or set secret_id in the AWS driver configuration. The driver calls GetSecretValue directly for that secret, bypassing listing and filters. With an empty secret_id, the driver uses the configured filters. For a plain-text or binary value, key_strategy uses the secret name returned by AWS.
Grant the runtime role secretsmanager:GetSecretValue on the exact secret ARN. If the secret uses a customer-managed KMS key, also grant kms:Decrypt on that key and allow the role to use it in the key policy. See the AWS GetSecretValue permissions.
AWS Authentication Best Practices
If your application runs on AWS infrastructure, prefer IAM-based runtime credentials instead of hardcoding access keys.
Recommended:
- EC2: attach an IAM role to the instance profile
- ECS: use a task role
- EKS: use IAM Roles for Service Accounts
- Lambda: use the function execution role
This lets the AWS SDK resolve credentials automatically without storing long-lived secrets in your application environment.
If your application runs outside AWS, use standard AWS SDK credentials such as:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKEN- shared credentials profiles
That works fine, but should usually be treated as the less preferred option compared to IAM-based runtime identity on AWS-managed infrastructure.
Pagination
The AWS driver supports provider pagination through NextToken.
If you set list_max_results, the package passes it to AWS as MaxResults and keeps requesting pages until the full result set is collected.
This is useful when you want explicit control over page size or when you want to verify pagination behavior in integration tests.
Secret Shapes
The AWS driver supports multiple secret shapes.
JSON object
If the secret value is a JSON object, every key becomes a secret entry.
Example:
{
"DB_PASSWORD": "secret",
"DB_PORT": 5432
}
Result:
[
'DB_PASSWORD' => 'secret',
'DB_PORT' => '5432',
]
name / value wrapper
If the secret value looks like this:
{
"name": "APP_KEY",
"value": "base64:..."
}
the package produces exactly one secret entry:
[
'APP_KEY' => 'base64:...',
]
Plain text
If the secret is plain text, the final key is derived from the secret name using key_strategy.
Given:
Name: /project/production/APP_KEY
Value: base64:...
With basename:
[
'APP_KEY' => 'base64:...',
]
With name:
[
'/project/production/APP_KEY' => 'base64:...',
]
SecretBinary
SecretBinary is supported. When AWS returns binary data in base64 form, the driver decodes it before building the final payload.
Filtering
Filtering is built around a driver-agnostic contract, so the same model can be reused by future drivers.
Built-in AWS Filter
The package ships with Atldays\Secrets\Filters\AwsSecretManagerFilter.
Its filter_options support:
tagsprefixesnames
Example:
'filter' => \Atldays\Secrets\Filters\AwsSecretManagerFilter::class, 'filter_mode' => 'or', 'filter_options' => [ 'tags' => 'application:api|admin,environment:production', 'prefixes' => '/project/prod/,/project/shared/', 'names' => '/project/exact/APP_KEY,/project/exact/DB_PASSWORD', ],
The built-in AWS filter treats tags, prefixes, and names as OR conditions inside the filter itself.
If all filter_options are empty, all available AWS secrets match.
Custom Filters
You can replace the built-in filter with your own class, or pass multiple filter classes.
Each filter must implement Atldays\Secrets\Contracts\SecretFilter.
Example:
namespace App\Secrets\Filters; use Atldays\Secrets\Contracts\SecretFilter; use Atldays\Secrets\Contracts\SecretReferenceContract; class ProductionProjectFilter implements SecretFilter { public function matches(SecretReferenceContract $secret): bool { return $secret->hasTag('environment', 'production') && $secret->nameStartsWith('/project/prod/'); } }
Use it like this:
'filter' => App\Secrets\Filters\ProductionProjectFilter::class,
Or combine multiple filters:
'filter' => [ App\Secrets\Filters\EnvironmentFilter::class, App\Secrets\Filters\PrefixFilter::class, ], 'filter_mode' => 'and',
Supported filter_mode values:
orand
Meaning:
or: at least one filter class must matchand: every filter class must match
Secret References
Filters receive a SecretReferenceContract, not the final secret value.
That is intentional.
It allows filters to decide whether a secret should be fetched without exposing the secret payload itself during the listing stage.
The current reference object includes:
- driver name
- secret name
- provider identifier
- tags
- metadata returned by the provider's listing API
For AWS, metadata contains the raw ListSecrets entry, such as:
NameARNTagsKmsKeyId- creation and rotation metadata
It does not contain:
SecretStringSecretBinary- resolved secret values
Useful helper methods on the reference include:
tag()hasTag()hasTagIn()hasName()hasNameIn()nameStartsWith()nameEndsWith()nameContains()hasIdentifier()hasMetadata()meta()
Public API
Facade
The package exposes the Secrets facade as the main public entry point.
use Atldays\Secrets\Facades\Secrets; $freshSecrets = Secrets::fetch(); // Read fresh secrets directly from the provider. $freshAwsSecrets = Secrets::fetch(\Atldays\Secrets\Drivers\AwsSecretManager::class); // Read fresh secrets from one driver. $payload = Secrets::cache(); // Fetch fresh secrets and store the payload in the configured cache. $awsPayload = Secrets::cache(\Atldays\Secrets\Drivers\AwsSecretManager::class); // Refresh the cache for one driver only. $cachedValues = Secrets::values(); // Read only the resolved KEY => VALUE pairs from the cached payload. $storedPayload = Secrets::stored(); // Read the full cached payload DTO, including drivers and secrets. $appliedCount = Secrets::apply(); // Apply cached secrets to Laravel env/config and get the number of applied secrets. $cleared = Secrets::clear(); // Remove the cached payload from the configured cache store.
Commands
secrets:config-cache
php artisan secrets:config-cache --no-interaction
php artisan secrets:config-cache --no-interaction --driver='App\Secrets\CustomDriver'
The command reads current Laravel configuration and environment settings, fetches the secrets, and applies the validation rules. It saves the full Laravel config array with only the provider values selected by config_variables. The temporary file and the published cache both have mode 0600. A failed build preserves the previous cache and exits with a nonzero status.
Once the command starts, it uses controlled messages for errors while reloading the environment and configuration, fetching secrets, validating values, or writing the cache. On a cold start, Laravel parses .env before Artisan invokes the command. A syntax error at that stage can include part of the malformed line in stderr. Supply sensitive settings through the provider or the container environment so they stay out of .env and .env.* files used during deployment.
secrets:cache
Fetches fresh secrets from the provider and stores them in the configured cache.
Use it when:
- you deploy a new release
- secrets were rotated in the provider
- you want to refresh the cached payload before the application starts serving traffic
Optional flags:
--driver=...Refresh secrets from one specific driver only
Example:
php artisan secrets:cache
php artisan secrets:cache --driver='App\Secrets\CustomDriver'
secrets:clear
Removes the cached payload from the configured cache store.
Use it when:
- you want to force a completely fresh refresh cycle
- you are debugging stale secret payloads
- you want to invalidate the current cache before a deployment step
Example:
php artisan secrets:clear
secrets:list
Reads secrets from the configured cache and lists the resolved secret names.
By default, values are masked completely.
Use it when:
- you want to confirm that the cache was successfully warmed
- you want to inspect which keys are currently available
- you want a safe operator-friendly validation step during deployment
Optional flags:
--freshRead directly from the provider instead of the cached payload--driver=...Limit fresh reads to one driver--revealShow full values instead of masked values--forceRequired in production when revealing secret values
Examples:
php artisan secrets:list php artisan secrets:list --fresh php artisan secrets:list --reveal
secrets:get
Reads one named secret from the configured cache.
Use it when:
- you want to verify one specific secret
- you want to compare cached values with provider values
- a trusted operator needs to inspect a value in a private terminal
Optional flags:
--freshRead directly from the provider instead of the cached payload--driver=...Limit fresh reads to one driver--revealShow the full value--rawPrint only the value, without extra console formatting--forceRequired in production when revealing values
Examples:
php artisan secrets:get APP_KEY php artisan secrets:get APP_KEY --fresh php artisan secrets:get APP_KEY --reveal php artisan secrets:get APP_KEY --raw
Use --reveal and --raw only for manual inspection in a trusted terminal. In production, both require --force.
Docker deployment
Install the entrypoint in the image and configure Docker to run it as the same unprivileged user as Laravel. Run the command after the image is built, inside the running container, with runtime credentials for the provider and a writable cache directory owned by the application user.
#!/bin/sh set -eu cd /app php artisan secrets:config-cache --no-interaction exec "$@"
For example, use USER app and ENTRYPOINT ["/usr/local/bin/app-entrypoint"] in the Dockerfile, with the application process as CMD. A failed cache build stops the entrypoint before it starts the application. Keep connections made by application service providers lazy during Artisan bootstrap so that the command can run before Redis and database credentials have been populated.
Laravel's default cache path is bootstrap/cache/config.php. Set APP_CONFIG_CACHE in the container environment if you use a different location. Keep the cache and its temporary files outside the public document root, and exclude them from Git, the Docker build context, and CI artifacts. Use secrets:config-cache for configuration and run any route, event, and view cache commands individually.
For CI, use a fake driver and the command's exit status. This container startup check also uses an exit status to verify the mapped Redis password.
php -r ' require "vendor/autoload.php"; $app = require "bootstrap/app.php"; $app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap(); $value = config("database.redis.default.password"); exit(is_string($value) && trim($value) !== "" ? 0 : 1); '
After rotating a secret, rerun the command in each deployment that uses it and restart long-lived processes such as PHP-FPM, queue workers, Horizon, or Octane so they reload the configuration.
Security Notes
- The package does not fetch provider secrets during normal request handling.
- In filter mode, filters select secret references before their values are fetched.
- Cached secrets should be stored in a cache backend appropriate for your environment.
config_variablesshould be treated as an explicit allowlist of Laravel config values that may be overwritten by secrets.- Laravel's config cache contains plaintext. Mode
0600restricts access to the file owner at the filesystem level. The application user and the container administrator can still read it.
Testing Status
The package currently includes:
- local adapter-level tests
- manager and command tests
- DTO and helper-method tests
- live AWS integration tests for:
- prefixes
- names
- tags
- custom filters
SecretBinary- KMS-encrypted secrets
- forced pagination