Encrypting OpenTofu state with the OpenBao key provider

Use OpenBao Transit as the key provider for OpenTofu state and plan encryption, with one transit key per state, BAO_ADDR and BAO_TOKEN handling and the documented Vault 1.14 compatibility limit.

On this page

How OpenBao Transit encrypts OpenTofu state with the openbao key provider

A platform team that stores OpenTofu state in infrastructure it controls can keep the encryption keys outside the state backend by pointing OpenTofu's openbao key provider at an OpenBao Transit key. The key provider calls the OpenBao Transit secrets engine to generate and wrap a data key, so the wrapping key never leaves OpenBao. OpenTofu generates a fresh data encryption key for each state or plan file, then asks OpenBao to wrap that data key with the key named in key_name. The encrypted state stores only the wrapped key, so anyone who can read the state bucket or file still cannot decrypt it without access to the Transit key.

The split of work matters for planning. OpenTofu owns the per-file data key and the encryption of the state or plan itself. OpenBao owns the long-lived wrapping key, its versions, its access policies and its audit trail. That means the platform team decides who can call the Transit key, which token OpenTofu runs with, and how the OpenBao cluster is backed up, because losing the Transit key or its backing storage makes the state unrecoverable. Set one Transit key per state file, as OpenTofu recommends, so that access to one stack's key does not expose another stack's state, and confirm the Transit engine path and key naming scheme before the first tofu init.

Version compatibility is the first check to run. OpenTofu documents that the OpenBao key provider is compatible with the last MPL-licensed HashiCorp Vault version, 1.14, but not with later BUSL-licensed Vault versions. Confirm the OpenBao release you plan to run against OpenTofu's current encryption documentation, which is linked below, and record that check in your change record, since a mismatch will surface as a failed state operation rather than a clear configuration warning.

What you need before configuring the key provider

The OpenBao key provider depends on four things working together: a cluster with Transit enabled, an OpenTofu build that ships the provider, a narrowly scoped token, and a network path that every runner can trust. Confirm each one before you touch the state configuration, because a failure in any of them usually shows up as a failed state read or write rather than a clear error.

An OpenBao cluster with Transit

The key provider calls the Transit secrets engine to generate data keys and wrap them, so the Transit engine must be mounted and reachable at the path you plan to use. The default path is transit, which the provider reads unless you set transit_engine_path. Confirm the cluster is unsealed and healthy, and decide whether it is dedicated to state encryption or shared with other workloads, since that choice affects who can audit and rotate the key.

An OpenTofu version with the key provider

The openbao key provider is part of OpenTofu's state and plan encryption feature, so the binary on every runner must include it. Pin the exact OpenTofu version in your own environment and test it against your intended OpenBao release. OpenTofu documents the OpenBao provider as compatible with the last MPL-licensed HashiCorp Vault version (1.14) and not with later BUSL-licensed versions, so confirm that compatibility line against the version you plan to run.

State encryption docs

A token with narrow Transit permissions

The provider authenticates with a token read from BAO_TOKEN or the token argument. Create a policy that allows only the operations needed on the specific Transit key, typically reading and using that key to generate data keys, and nothing else. Do not reuse a root or administrator token. Confirm with your security team how the token is issued, how long it lives, and how it is rotated on every runner that executes OpenTofu.

Network reachability from every runner

Each machine that runs tofu plan or tofu apply must reach the OpenBao address over the network, including CI runners, developer workstations and any automation hosts. Routing, firewall rules, proxies and DNS all sit in this path, and a runner that cannot reach OpenBao will fail during state operations. Ask your network team to confirm reachability from each runner pool to the OpenBao address before rollout, and document which runners are allowed to reach it.

TLS trust on each runner

The provider sends the token over HTTPS to the address you set in BAO_ADDR or address, so every runner must trust the certificate chain presented by OpenBao. Private certificate authorities are common in regulated environments, and a runner image missing the internal root will fail with a certificate error that can look like a state problem. Distribute the CA bundle through your standard image or runner configuration, and test the connection from a clean runner before you enable encryption.

Configuring the openbao key provider step by step

This sequence takes an OpenBao cluster from a Transit engine with no keys to an OpenTofu backend whose state and plan files are encrypted with a dedicated key. Each step ends with a check so the team can confirm the encryption works before it relies on it.

  1. Enable the Transit secrets engine

    An OpenBao administrator enables the Transit secrets engine at the default path, transit/, or at a dedicated path that matches the transit_engine_path value the team will set later. Transit is the engine that generates the data keys OpenTofu uses, so nothing else in the chain works without it. Run bao secrets list and confirm that a transit mount appears at the path you chose. If you choose a non-default path, record it now, because the OpenTofu configuration must match it exactly.

  2. Create one named transit key per state

    Create a separate named key in Transit for each OpenTofu state or plan file that will be encrypted, using a name that identifies the workspace, such as state-network-prod. OpenTofu recommends a separate key per state file, which limits what a compromised key exposes and keeps rotation and access decisions scoped to one state. Confirm each key with bao read transit/keys/<name> and check that the key type and version are what you expect before moving on.

  3. Write a scoped policy for the key

    The security or platform team writes a policy that allows the OpenTofu runner to call the Transit data key endpoint for the specific keys it needs and nothing else. Avoid granting access to the whole Transit mount or to key administration paths, since the runner only needs to generate and unwrap data keys. Test the policy with bao policy write and confirm that a token using it can request a data key for its own key name and is denied for other key names.

  4. Create a token and export the environment variables

    Create a token attached to the scoped policy, with a time-to-live and renewal settings that match how long the runner will need it, and store it in your secrets store or CI variable store, not in the repository. Export BAO_ADDR with the cluster address and BAO_TOKEN with the token in the shell or runner that will execute OpenTofu, since the openbao key provider reads both values from the environment when they are not set in the file. Verify the setup by running bao token lookup and confirming the policy and expiry shown are the ones you intended.

  5. Add the encryption block to the configuration

    Add a terraform or tofu block with an encryption configuration that defines an openbao key provider. Set key_name to the Transit key created for this state, and set address and token only if you do not rely on the environment variables. Set transit_engine_path if the mount is not at the default /transit. Then reference the key provider from the state and plan method blocks. Run tofu init and confirm it completes without errors about the key provider or unreachable address.

  6. Run a plan and apply with encryption enabled

    Run tofu plan and then tofu apply in a non-production workspace first, so the team can confirm behavior before changing a production state. Both commands should succeed and the key provider should be reached without reconfiguring the backend. If the plan fails with an authentication or permission error, check the token's policy against the key name in the configuration. A successful apply confirms that the runner can both encrypt and decrypt state with the chosen key.

  7. Confirm the state is no longer readable

    Inspect the state object in the backend, such as the S3 bucket or other storage location, and confirm that it is no longer plain JSON. An encrypted state file should contain envelope metadata and ciphertext rather than resource attributes or outputs. Also confirm that a read of the Transit key from a runner without the policy is denied. Record the results, because auditors and reviewers will ask for this evidence when they review how state is protected.

Why one transit key per state file reduces blast radius

OpenTofu's documented recommendation is to use a separate key for each state file, and the reason follows from how the key provider works. When a key management system is the key provider, OpenTofu generates a fresh data encryption key for each state or plan file and wraps that data key with the key referenced by key_name in the openbao key provider block. The OpenBao Transit engine performs the wrapping, so the state file's plaintext data key is only as exposed as the Transit key that protects it. If every state file in an organization shares one Transit key, a single leaked token or misconfigured policy that can use that key exposes every state file wrapped by it. With one key per state, the same compromise is limited to the state files that key protects.

The trade-off is key sprawl. Each state file then needs its own Transit key, its own name, and a policy that grants encrypt and decrypt on that key only to the workload or pipeline that manages that state. Teams should settle a naming convention before the first state is migrated, for example one that encodes the environment, the team and the state name, so that operators can tell which key belongs to which state during an incident. The payoff is operational: rotating or revoking one workload's access, or retiring a stack, touches one key and one policy instead of forcing a shared key to be rotated for every consumer. Confirm the Transit key rotation and deletion behavior in the OpenBao documentation for your version before you write the lifecycle runbook.

Ask the OpenTofu documentation whether the associated_data option binds the wrapped data key to extra context, and which values it accepts, before you rely on it as a second control. If it does, it may help tie a key to a specific state, but the exact behavior and any interaction with existing keys must be verified against the OpenTofu state encryption docs and tested in a non-production workspace first. Whatever you decide, record the key-to-state mapping in your infrastructure inventory so the mapping survives staff changes.

Mapping OpenTofu settings to OpenBao objects

This table maps each OpenTofu openbao key provider setting to the OpenBao object it points at, the state or plan file it protects, and the owner who should approve changes. Read it row by row before creating any Transit key or token, because one key per state keeps a compromised credential from exposing every state file.

SettingOpenBao objectProtectsApproval owner
key_nameOne Transit key per state or plan file, named for the workspace it protectsThe data encryption key wrapping one state or plan filePlatform team lead, with the workspace owner signing off
token or BAO_TOKENA scoped OpenBao token limited to encrypt and decrypt on the named Transit key onlyAccess to the wrapping key, so the token must not carry broader policiesSecurity team, with the platform team reviewing the policy text
address or BAO_ADDRThe cluster endpoint for the OpenBao service that hosts the Transit mountWhich cluster can unwrap the state key, and therefore where state can be readInfrastructure owner for the cluster
transit_engine_pathThe mount path of the Transit secrets engine (default /transit)Separation between teams or environments that share one clusterPlatform team lead
associated_dataBinding context passed with each encryption call, such as workspace name and environmentTies ciphertext to its intended state so it cannot be reused for another workspaceWorkspace owner, agreed with the security team before first use
Compatibility versionThe OpenBao release line in use, checked against the Vault version OpenTofu documentsEnsures the key provider keeps working through upgradesPlatform team, reviewed at each OpenBao upgrade

Hardening the token, key and state path

The OpenBao key provider gives anyone holding a token the ability to unwrap every state key that token can reach, so the token, the Transit key and the state file need to be controlled together. The cards below cover the policy, token, secret handling, key administration and audit controls that connect them.

Scope Transit policy to encrypt and decrypt

Give the OpenTofu token a policy that allows only update on the encrypt and decrypt paths for the named key, such as transit/encrypt/<key-name> and transit/decrypt/<key-name>. The token should not be able to list keys, read key configuration, rotate keys or touch other Transit paths. Check the policy with a test token before it reaches CI. Confirm the exact capabilities against the OpenBao policy documentation for your version.

OpenBao auth docs

Replace root tokens with short-lived tokens

Do not use a root token or a long-lived token in the OpenTofu configuration. Issue a token with a short time to live and a limited number of uses for each run, using an auth method that fits the runner, such as AppRole for servers or a JWT method for CI jobs. Where the pipeline can hold a wrapped secret instead of a raw token, prefer response wrapping so the token is only revealed to the process that needs it. Confirm the wrapping and TTL options available in your release.

Keep tokens out of code and logs

The token argument of the openbao key provider can be written into a .tf file, which then ends up in version control and plan output. Set BAO_TOKEN and BAO_ADDR in the runner environment instead, and mask them in CI variables. Review plan and apply logs and any debug output, because a token in a log has the same reach as the token in a file. Rotate any token that has appeared in a repository or log.

Limit who can read or rotate keys

Separate the operators who create and rotate Transit keys from the pipelines that only use them. Administrative capabilities such as update on keys paths, key rotation and key deletion should sit in a separate policy held by a small set of people. Each state file should have its own key, so access can be granted or revoked per state. Confirm the deletion and rotation controls on each key type before relying on them.

Audit Transit encrypt and decrypt operations

Enable an audit device so every Transit encrypt and decrypt request is recorded with the token identity, path and time. Use these records to confirm which runner unwrapped a state key and to spot unexpected access. Make sure the audit log is written to storage that the pipeline cannot modify. Confirm how sensitive fields are HMAC-hashed in your audit configuration so that the log itself does not expose key material.

Confirm root token and rekey hardening separately

Root token handling, rekey endpoints and unseal key custody protect the OpenBao cluster itself, which holds the Transit keys, so a weakness there defeats the state controls above. These controls are covered on the OpenBao security hardening page and should be checked in your own environment against your operating procedure. Ask who holds unseal material and confirm that no root token is stored for OpenTofu use.

Which Vault and OpenBao versions OpenTofu documents as compatible

The OpenBao key provider has a narrow compatibility statement, and it decides which Transit server you can point OpenTofu at. OpenTofu documents compatibility with the last MPL-licensed HashiCorp Vault version, Vault 1.14, and does not document compatibility with later Vault releases, which moved to the BUSL license. Treat that as the boundary for the key provider. OpenBao is a fork of Vault under MPL-2.0, and the OpenTofu documentation is the only place to confirm how it maps onto OpenBao releases, so check it before you upgrade the OpenBao server or the OpenTofu binary.

For a team that runs OpenBao as the Transit server, the practical risk is a version drift that nobody tested. OpenBao v2.7.0 and v2.7.1 are current releases on the project's line, and the OpenTofu documentation does not, in the material this page relies on, name them explicitly. Confirm the exact OpenBao release and OpenTofu release combination against the OpenTofu state and plan encryption documentation before you change either side. Record the pair you approve in your change log, because a later OpenBao patch can land while the OpenTofu guidance stays the same.

Before any production state is encrypted or re-keyed, run a test with the target pair. Point a non-production configuration at the test OpenBao address through BAO_ADDR and a scoped BAO_TOKEN, use a throwaway key_name, and confirm that OpenTofu can encrypt, read back and decrypt a state and a plan. If the test fails or the documentation does not cover your chosen OpenBao release, hold the upgrade and keep the existing pair. Ask your OpenTofu and OpenBao support contacts to confirm the result in writing for the versions you plan to run.

Questions teams ask before encrypting OpenTofu state with OpenBao

What does it cost to encrypt OpenTofu state with OpenBao?

No fixed price applies, because the cost is mostly the time of the people doing the work. Three components matter: internal engineering time to design key layout and update pipelines, the cost of running the OpenBao cluster that hosts Transit (which you likely already run or are planning to run), and any outside help you buy. OpenTofu itself is open source and carries no license fee for state encryption. Ask any partner for a written quote that lists each item, who is paid for it, and which parts are one-time (design, pipeline changes, first key creation) and which recur (cluster operation, upgrades, rotation reviews).

How long does the work take with many states and existing backends?

Duration depends mainly on how many state files you have, how many pipelines read and write them, and whether those states already live in a backend you must migrate from. A single state with a working Transit engine is a small change. Dozens of states across several teams add key creation, per-state key naming, pipeline credential changes and a rollout order that avoids breaking running plans. Ask for a timeline in the written scope that is tied to a state inventory, and confirm whether the quote covers testing the decrypt path before any state is rewritten.

What must our team provide before the work starts?

Three groups of access and people are needed. First, a path to the OpenBao cluster with an address the OpenTofu runners can reach. Second, a Transit administrator who can enable the transit secrets engine, create keys and write policies that let each pipeline encrypt and decrypt only its own key. Third, named owners for each pipeline and state, who can confirm which workspaces exist and approve changes. Confirm in the scope whether test and production clusters are separate and who holds the root or unseal material.

Can an internal platform team do this alone?

Often yes, if the team already operates OpenBao or Vault and has someone comfortable with Transit policies and OpenTofu configuration. The new skills are a key-per-state model, a token strategy for pipelines and a tested recovery path. Teams without OpenBao operating experience usually spend more time on cluster basics than on the OpenTofu block itself. Whichever way you go, check the OpenTofu documentation on the key provider before committing, since it lists the exact configuration keys and the Vault version range the provider supports.

Which OpenBao and Vault versions work with the OpenBao key provider?

OpenTofu documents that its OpenBao key provider is compatible with the last MPL-licensed HashiCorp Vault version, 1.14, and not with later BUSL-licensed Vault releases. Confirm this against the current OpenTofu state encryption documentation before you choose a version, because compatibility statements change. Test the chosen OpenBao release in a non-production workspace first, and record the exact OpenTofu and OpenBao versions in your change record so later upgrades can be checked against them.

How do key rotation and recovery affect ongoing effort?

OpenTofu generates a fresh data encryption key for each state or plan file and wraps it with the referenced Transit key, so routine data key creation needs no manual step. Rotating the Transit key itself and keeping old key versions available are the tasks to plan. Losing access to the Transit key, its token or the cluster means losing the ability to decrypt state, so recovery drills, backup of the OpenBao storage and a documented token renewal process are recurring work. Ask any quote to include who runs rotation reviews and restore tests, and how often.

Does encrypting state replace access control on our backends?

No. State encryption protects the contents of state and plan files at rest in the backend, but anyone who can read the backend and reach the Transit key still has a path to decrypt. Keep backend access restricted, scope each pipeline token to its own Transit key with a narrow policy, and log Transit operations through your OpenBao audit devices. Treat the key provider as one control within your broader secrets and access design, and confirm that the partner's scope covers those policies, not only the OpenTofu block.

Common failures when OpenTofu cannot reach OpenBao Transit

This table lists the failures teams most often hit when OpenTofu cannot use OpenBao Transit as its key provider, with the likely cause, the fix and what to check in logs. Read each row against the failing run, starting with the address, token and transit path, since those account for most initialization errors. The main takeaway is that a working state encryption setup needs the right engine path, a token scoped to the named key, and a separate key for each state.

FailureLikely cause and fixWhat to check in logs
Wrong transit_engine_pathThe Transit engine is mounted under a path other than the default /transit, or inside a namespace. Set transit_engine_path to the actual mount path and confirm the mount with your OpenBao administrator.Look for a not found or no handler error on the transit path in the OpenBao audit log, and compare the request path with the mount list.
BAO_ADDR missing in CI runnersThe OpenTofu job runs on a runner that does not export BAO_ADDR, so the address falls back to nothing. Set BAO_ADDR as a masked variable in the pipeline or pass address explicitly in the key_provider block.Check the OpenTofu init output for an address or connection error, and confirm the variable is present in the job environment before tofu runs.
BAO_TOKEN missing or expired in CI runnersThe token is not exported to the job, or it has expired or been revoked. Issue a token with a defined TTL for the pipeline or use an auth method that issues short-lived tokens, and store it in the CI secret store.Look for permission denied or invalid token responses in the OpenBao audit log and check the token's lease and accessor status.
Token lacks Transit permissions on the named keyThe policy allows the token to log in but not to call the data key operation on the key named by key_name. Grant update capability on the Transit data key path for that key only, then retest.In the audit log, find the denied request for the key path and confirm the policy attached to the token's entity or role.
One transit key shared across several statesTeams reuse a single key_name for many state files to save setup time. OpenTofu recommends a separate key per state file, so one compromised or retired key affects every state that uses it. Create one key per state and name each key after its state.Compare key_name values across backend configurations, and check whether a single key appears in several state files' encryption blocks.
Key name typo or key not createdThe key_name in the configuration does not match an existing Transit key, or the key was created in another namespace or mount. Confirm the key exists at the expected path before the first apply.Look for key not found responses in the audit log and list keys on the Transit mount with an operator token.
Version mismatch with Vault after 1.14OpenTofu documents that the OpenBao key provider works with the last MPL-licensed HashiCorp Vault version, 1.14, but not with later BUSL-licensed versions. Confirm that the server you point to is OpenBao or a compatible version, and check the OpenTofu release notes before upgrading either side.Check the server version reported by the OpenBao endpoint and any API response errors that appear after an upgrade on the Vault or OpenBao side.

How OpenTofu, OpenBao Transit and the state backend exchange keys

The flow starts on the runner, where OpenTofu reads BAO_ADDR and BAO_TOKEN and asks the OpenBao Transit engine for a data key tied to the referenced key. OpenTofu encrypts the state or plan locally with that data key, then stores the ciphertext and the wrapped key on the state backend. On the next run it reads both back and asks Transit to unwrap the key. Check that the runner can reach the Transit path and that the token is scoped to encrypt and decrypt with that key only.

Keys move between the runner, OpenBao Transit and the state backend, which holds only ciphertext.

Keeping encrypted state recoverable over time

Treat the Transit key and the token as two separate operational duties once the first plan succeeds. The key protects the data encryption keys wrapping each state or plan file, so losing it or the ability to use it can leave state unreadable, while the token decides who can ask OpenBao to unwrap those keys. Keep a written register that maps each state file to its key_name, its transit_engine_path (default /transit), and the policy or token role that runners use. Confirm the current rotation behavior of the Transit engine in the OpenBao docs before you schedule rotations, and confirm with OpenTofu how existing state files are re-encrypted after a key change, because that step decides whether old keys must stay available.

Test recovery on a schedule rather than assuming it works. Each quarter, or after any token or policy change, run tofu plan from a runner that holds only the intended BAO_TOKEN against a non-production copy of a state file, and confirm that a token without the Transit policy fails cleanly. Record the BAO_ADDR value, token TTL and renewal process so a rotated token does not silently break scheduled pipelines. Write down who can restore a lost key and how that is approved, since a deleted Transit key may not be recoverable from the state itself.

Plan for OpenBao being unavailable, because every plan and apply on an encrypted backend depends on it. Ask which runners, teams or on-call staff can run a documented break-glass procedure, and whether that procedure involves a separately held token or a local, decrypted copy of state kept under strict control. Backup of OpenBao storage, high availability design and failover belong to the related OpenBao high availability and operations pages, so confirm those answers there before you rely on this path. Review the OpenTofu state encryption documentation again whenever you change OpenTofu or OpenBao versions.

How DeployOpen scopes and delivers OpenTofu state encryption with OpenBao

DeployOpen plans and implements self-hosted open-source platforms inside your own cloud accounts, data centers or air-gapped networks, and that includes the OpenBao Transit key that wraps OpenTofu state and plan files. We agree the scope in writing before work starts, then build, test, document and hand over the setup so your platform and security teams can run it.

How we scope

  • Inventory which OpenTofu state and plan files need encryption and which backends hold them today.
  • Confirm the OpenBao version line, its Transit engine path and the Vault version compatibility OpenTofu documents for the openbao key provider.
  • Agree one transit key per state file, token policy, address handling and who approves access in a written scope.

How we build

  • Create the Transit keys and least-privilege policies, then wire the openbao key_provider block into your OpenTofu configuration.
  • Supply BAO_ADDR and BAO_TOKEN through your runner or secret delivery method rather than committing them to code.
  • Test state and plan read and write, failed decryption, key rotation handling and the fallback path before rollout.

How we hand over

  • Deliver the configuration, key layout, policy files and runbook for creating keys for new states.
  • Document the recovery path if the OpenBao endpoint or token is unavailable and who can decrypt state.
  • Walk your platform and security teams through the setup and record decisions for your compliance file.

How we support

  • Engagements are fixed-scope, with changes to the written scope agreed before they are built.
  • Ongoing support, upgrades and continuous testing are available as a retainer, including OpenBao and OpenTofu version changes.
  • Ask any quote to list build, test, documentation and retainer items separately so recurring and one-time costs are clear.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.