op environment secret edit. Any application that reads the Environment picks up the new value the next time it starts.
This guide uses an Environment named Production that contains a secret named API_KEY. The script refers to the Environment by its ID, which keeps working if the Environment is renamed.
Requirements
Before you can rotate a secret with 1Password CLI, you’ll need to:- Sign up for 1Password
- Install 1Password CLI version
2.43.0or later - An Environment that contains the secret you want to rotate
Step 1: Create a service account with write access to the Environment
Service accounts are a token-based authentication method that you can scope to specific Environments. Create one that has write access to theProduction Environment, so the script can edit the secret.
- Use the service account creation wizard on 1Password.com.
- Give your service account a name. For example,
Secret Rotation Service Account. - Choose the
ProductionEnvironment and give the service account write access. This can’t be changed later. - Select Create Account, then select Save in 1Password to save the service account token in your 1Password account.
Step 2: Export your service account token
Export the service account token you saved in the previous step to theOP_SERVICE_ACCOUNT_TOKEN environment variable.
- Bash, Zsh, sh
- fish
- PowerShell
Step 3: Write the rotation script
The script does three things in order: generate a new value, update the credential at your provider, and store the new value in the Environment. Update the provider before the Environment, so that if the provider call fails, the Environment still holds the value that works.Command arguments can be visible to other processes on your machine. The script pipes the new value to
op environment secret edit as JSON instead of passing it with the --value flag, and never prints it.rotate.sh and make it executable, then run it once to confirm it works. To check the result without revealing the value:
Step 4: Run the script on a schedule
Run the script from any scheduler that can set theOP_SERVICE_ACCOUNT_TOKEN environment variable. For example, to run it every day at 02:00 with cron, add this line to your crontab: