26 KiB
OpenBao
Outline 📋
🧐 What is it?
OpenBao is a secret management tool embedded into your application stack. It can handle the creation, storage, and management of secrets, ensuring they remain secure. For seamless integration across multiple Kubernetes applications, OpenBao works with the Kubernetes External Secrets Operator, which converts OpenBao’s secrets into Kubernetes secrets.
🤔 Why do I want it?
Secure Secrets Storage
OpenBao organizes your secrets in one encrypted location, reducing the risk of loss or accidental exposure, such as through a public code push. To dive deeper take a look here 🤿
Controlled Access
OpenBao allows you to define rules about who can access which secrets and under what conditions. To dive deeper take a look here 🤿
Leasing and Revocation
Secrets with a lease are automatically revoked at the end of their lifespan. Revocation can also be done manually. To dive deeper take a look here 🤿
Dynamic Secrets
OpenBao generates short-lived secrets on demand and revokes them automatically when they’re no longer needed. To dive deeper take a look here 🤿
Encrypction as a Service
OpenBao encrypts and decrypts data without storing it, offering cryptographic functions as a service. To dive deeper take a look here 🤿
Audit Devices and logs
Detailed logs track who accessed which secrets and when, providing complete traceability in case of a breach. To dive deeper, take a look here 🤿
Secret Engines
OpenBao offers diverse ways to manage secrets through various secret engines. To dive deeper take a look here 🤿.
🤿 Diving deeper
Secure Secrets Storage
OpenBao validates and authorizes clients (users, machines, applications) using tokens before granting access to secrets or sensitive data.
How does gaining access to OpenBao secrets work?
-
Authentication: Before a human or machine can gain any access, an administrator must configure OpenBao with an auth method. When a client tries to log in to OpenBao, the system checks client’s data against an internal or external (see Validation below) system. Once authenticated, OpenBao generates an access token for the client. This token is then used for every action performed in OpenBao.
Note: the administrator can also provide you with an already generated access token.
-
Validation: If trusted third-party credential repositories (e.g., GitHub, LDAP, AppRole) are specified, OpenBao forwards authentication to them.
-
Authorization: OpenBao applies policies based on the authentication method and rules set by the admin to the token that was generated during authentication. Policies provide a declarative way to grant or forbid access to certain paths and operations in OpenBao.
-
Access: The client uses the token for access to secrets, keys, or encryption capabilities, as per the associated policies.
Here's a tutorial how to use it 🔨
If you want to read more about authentication, visit https://openbao.org/docs/concepts/auth/ 🔗
Controlled Access
OpenBao uses policies to manage access to secrets. Policies are basically a combination of paths and capabilities (operations) that they allow on these path. Let’s consider this example:
path "database/postgres" {
capabilities = ["create", " read ", "list", "delete"]
}
path "database/myslq/* " {
capabilities = ["read", "list"]
}
This policy permits creating, reading, listing, and deleting the secrets at database/postgres/
, but only allows reading and listing all resources in database/mysql/
and its subpaths.
Here's a tutorial how to use it 🔨
For even deeper understanding, go to https://openbao.org/docs/concepts/policies/ 🔗
Dynamic Secrets
Dynamic secrets do not exist until read. These secrets are revoked automatically after use. Most of the OpenBao's secrets engines are dynamic secret engines (more on that later).
For example: An application requests Kubernetes credentials from OpenBao, which generates a token with specific permissions. The token is automatically revoked after use or its lease end.
Here's a tutorial how to use it 🔨
Leasing and Revocation
To simplify key rolling processes, consumers using OpenBao should regularly verify their subscription status to either renew leases where permitted or request replacements for secrets that need updating. In addition to that, a lease can be revoked manually. When this happens, it invalidates that secret immediately and prevents any further renewals. OpenBao can revoke not only single secrets, but a tree of secrets, for example all secrets read by a specific user, or all secrets of a particular type.
For example: In the Kubernetes secrets engine, revoking a lease deletes associated Kubernetes service accounts, immeditally rendering their access keys invalid.
Here's a tutorial how to use it 🔨
Encryption as a Service
The transit secrets engine handles cryptographic operations on data in transit without storing it. It can encrypt, decrypt, sign, verify, and generate hashes or random bytes.
For example: Your application sends data to OpenBao for encryption before saving it to a database. OpenBao can then decrypt the data on demand, ensuring security even if the database is compromised.
Here's a tutorial how to use it 🔨
Here you can read more about the transit secrets engine https://openbao.org/docs/secrets/transit/ 🔗
Audit devices and logs
Audit devices are the tool for collecting detailed logs of all requests to OpenBao, and their responses. Because every operation with OpenBao is an API request/response, when using a single audit device, the audit log contains every interaction with the OpenBao API, including errors - except for a few paths which do not go via the audit system. Audit log telemetry on the other hand provides information on the health of your configured audit devices.
Types of Audit Devices:
- File: Writes audit logs to a local file.
- Syslog: Sends audit logs to local syslog agent for centralized logging and analysis.
- Socket: Sends audit events to a custom HTTP endpoint for integration with other systems.
Here's a tutorial how to use it 🔨
For more information, plese read https://openbao.org/docs/audit/ 🔗 and https://openbao.org/docs/internals/telemetry/metrics/audit/ 🔗
Secret Engines
One of the core concepts of OpenBao are secret engines. Think of them as a set of path-based APIs, that can be provided with some set of data, after which they take some action on that data, and they return a result. Let’s shortly take a look at a few examples of OpenBao’s secret engines to better understand what they are.
- Key-Value secret engines – for storing key value pairs (with our without secret versioning: kv1/kv2)
- PKI (Public Key Infrastructure) secret engine - for certificate management
- SSH secret engine - for managing SSH credentials
- Transit secrets engine - for encrypting data without storing it
- Time-based One-Time Passwords (TOTP) secret engine - for two-factor authentication
- Kubernetes secrets engine - for seamless integration with containerized applications
- And many others...
Here you can find out more about secret engines https://openbao.org/docs/secrets/ 🔗
How to set it up?
Note: This paragraph will be absolete in the future as the functionality will be automated.
Hint: To be able to use OpenBao it has to be unsealed first. This happens automatically.
The External Secrets Operator needs a kubernetes secret containing the OpenBao's initial token (see above) to access its secrets. You can create it with:
kubectl create secret generic vault-token --from-literal=token=<root_token_from_getpassword.sh> -n openbao
To perform any actions in OpenBao you need to authenticate using the following command:
kubectl exec -ti openbao-0 -n openbao -- vault login <root_token_from_getpassword.sh>
For demontrational purposes you can enable a Key-Value secret engine on the path /data with:
kubectl exec -ti openbao-0 -n openbao -- vault secrets enable -path=data kv
And to add your first secret just run:
kubectl exec -ti openbao-0 -n openbao -- vault kv put data/postgres POSTGRES_USER=admin POSTGRES_PASSWORD=123456
To fetch it as a kubernetes secret you'll need to create an external-secrets.yaml file and apply it to the cluster with kubectl apply -f external-secrets.yaml
# external-secret.yaml
apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
name: external-secret
namespace: openbao
spec:
refreshInterval: "15s" #This specifies the time interval at which the ExternalSecret controller will refresh the secrets.
secretStoreRef: # This references the first file.
name: bao-backend
kind: SecretStore
target: #This specifies the target Kubernetes secret that the ExternalSecret will create.
name: postgres-secret
creationPolicy: Owner
data: # This is an array of secret key-value pairs that the ExternalSecret will retrieve from the Vault secret store and store in the Kubernetes secret.
- secretKey: POSTGRES_USER #Name of the k8 secret that is being created
remoteRef: #This is an object that contains the reference to the secret in the Vault secret store.
key: data/postgres # This specifies the path to the secret in the Vault secret store
property: POSTGRES_USER #This specifies the name of the secret property to retrieve from the Vault secret.
- secretKey: POSTGRES_PASSWORD
remoteRef:
key: data/postgres
property: POSTGRES_PASSWORD
After that just run kubectl get externalsecrets -A
to check that there are no problems with synchronization. And to access the secret on your cluster run: kubectl get secrets -n openbao
🔨 How to use it?
Uneasling
When an OpenBao server starts, it begins in a sealed state. In this state, OpenBao is configured to know where and how to access the physical storage, but it cannot decrypt any of the stored data because it's lacking the encryption key. Unsealing is the process of solving this issue.
Note: In the sealed state no operations are possible exept of unsealing the OpenBao and checking the status of the seal.
This encryption key is stored together with the OpenBao's secrets, but encrypted with another encryption key, known as the root key. The root key however is also encrypted, this time by the unseal key. The unseal key can have the form of an algorith called Shamir Secret's Sharing (multiple Key Shares) or might be stored in a trusted device or service.
- Shamir's Secret Sharing: The unseal key is split into multiple parts (e.g. five parts), and only a subset (e.g. three parts) is needed to reconstruct it.
- Trusted Device or Service: The unseal key can be stored securely in a trusted service, such as Cloud KMS or AWS KMS.
Graphical recap:
To learn more, visit: https://openbao.org/docs/concepts/seal/ 🔗
In our stack unsealing happens automatically. To access the root token just run: ./getpassword.sh
Secret Engines
Note: OpenBao is a fork of Hashicorp Vault. The "bao" keyword is the equivalent of the "vault" keyword and they can be used interchangeably for OpenBao.
To use OpenBao, you first need to log in using your root token.
kubectl exec -ti openbao-0 -n openbao -- bao login <root_token_from_getpassword.sh>
Let's enable the first secret engine — the key-value (KV) secrets engine without secret versioning - on the path /database/.
kubectl exec -ti openbao-0 -n openbao -- bao secrets enable -path=database kv
Now, let's add your first secret to the postgres and mysql folders in that KV engine path.
kubectl exec -ti openbao-0 -n openbao -- bao kv put database/postgres user=postgres password=123456
kubectl exec -ti openbao-0 -n openbao -- bao kv put database/mysql user=admin password=654321
This way, we've saved two secrets with each two key value pairs in the KV secret engine activated at path /database/.
To retrieve this secret, use the get command with the specific path.
kubectl exec -ti openbao-0 -n openbao -- bao kv get database/postgres
or
kubectl exec -ti openbao-0 -n openbao -- bao kv get database/mysql
Controlled Access
OpenBao allows you to write with entities, authentication method, or tokens. Let's only consider the entities in detail for the understanding puprose and only take a brief look at the the auth methods and tokens.
Entities
First, enable the userpass auth method.
kubectl exec -ti openbao-0 -n openbao -- bao auth enable userpass
Now let's create the two users, user1 and user2, with two different policies user1-policy and user2-policy.
kubectl exec -ti openbao-0 -n openbao -- bao write auth/userpass/users/user1 password="password1" policies="user1-policy"
kubectl exec -ti openbao-0 -n openbao -- bao write auth/userpass/users/user2 password="password2" policies="user2-policy"
User 1 Policy
Create a file called user1-policy.hcl
with the following content:
path "database/postgres" {
capabilities = ["create", "read" , "list", "delete"]
}
Now let's copy it to the pod running OpenBao with:
kubectl cp user1-policy.hcl -n openbao openbao-0:/tmp/user1-policy.hcl
And now apply the hcl file to the user1-policy which is already assigned to the user 1:
kubectl exec -ti openbao-0 -n openbao -- bao policy write user1-policy /tmp/user1-policy.hcl
User 2 Policy
Now create another filed called user2-policy.hcl
.
path "database/myslq/* " {
capabilities = ["read", "list"]
}
Let's copy it:
kubectl cp user2-policy.hcl -n openbao openbao-0:/tmp/user2-policy.hcl
And apply the policy with:
kubectl exec -ti openbao-0 -n openbao -- bao policy write user2-policy /tmp/user2-policy.hcl
Login
Now let's login as the user 1, with:
kubectl exec -ti openbao-0 -n openbao -- bao login -method=userpass username=user1 password=password1
And let's access the database/postgres
secrets with:
kubectl exec -ti openbao-0 -n openbao -- bao kv get database/postgres
Let's delete them and then check if they're really gone:
kubectl exec -ti openbao-0 -n openbao -- bao kv delete database/postgres
kubectl exec -ti openbao-0 -n openbao -- bao kv list database/postgres
Trying to access the secrets at database/mysql
should not be possible, since our policy doesn't allow it. Let's confirm that with:
kubectl exec -ti openbao-0 -n openbao -- bao kv get database/mysql
You can also try performing these steps accordingly for the user 2.
To login back to the root user run: kubectl exec -ti openbao-0 -n openbao -- bao login <root_token>
Authentication methods
For example:
kubectl exec -ti openbao-0 -n openbao -- bao write auth/userpass/users/<username> policies=<policy1>,<policy2>
or
kubectl exec -ti openbao-0 -n openbao -- bao write auth/aws/role/<role-name> policies=<policy1>,<policy2>
Tokens
For example:
kubectl exec -ti openbao-0 -n openbao -- bao token create -policy="<policy-name>"
or for all tokens created in your OpenBao instance:
kubectl exec -ti openbao-0 -n openbao -- bao write sys/policies/acl/default policy=my-policy
Dynamic Secrets
Until now, we’ve been using the static Key-Value (KV) Secret Engine to store credentials. Let’s switch to a dynamic one—the Database Secret Engine.
To delete the KV Secrets Engine at path /database
run:
kubectl exec -ti openbao-0 -n openbao -- bao secrets disable database
To set up the Database Secret Engine, we’ll need the following:
- A running database instance
- The dynamic secret engine itself
- A database role
Database
First, create a new namespace:
kubectl create namespace postgres-demo
Next, define the following postgres-deployment.yaml file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
namespace: postgres-demo
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:15
ports:
- containerPort: 5432
env:
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: password
- name: POSTGRES_DB
value: postgres
volumeMounts:
- name: postgres-data
mountPath: /var/lib/postgresql/data
volumes:
- name: postgres-data
emptyDir: {}
Then, define the corresponding postgres-service.yaml file:
apiVersion: v1
kind: Service
metadata:
name: postgres
namespace: postgres-namespace
spec:
type: ClusterIP
selector:
app: postgres
ports:
- port: 5432
targetPort: 5432
Apply both files with:
kubectl apply -f postgres-deployment.yaml
kubectl apply -f postgres-service.yaml
Secret Engine
Enable the Database Secret Engine on the default path by running:
kubectl exec -ti openbao-0 -n openbao -- bao enable database
Hint: You can explicitly specify a custom path using the -path="/custom/path"
argument.
Now, configure the secret engine to connect to the PostgreSQL instance and define which roles it will support:
kubectl exec -ti openbao-0 -n openbao -- bao write database/config/my-postgres \
plugin_name=postgresql-database-plugin \
connection_url="postgresql://postgres:password@postgres.postgres-demo.svc.cluster.local:5432/postgres?sslmode=disable" \
allowed_roles="readonly, my-postgres-role"
In this example:
- The
readonly
role is a default one. - The
my-postgres-role
is a custom role we’ll define in the next step.
Database Role
A OpenBao role is a policy-like configuration that:
- Tells OpenBao how to create database users.
- Defines what permissions these users will have in the database.
- Controls the lifetime (TTL) of the credentials Vault generates.
- Specifies which Vault clients (e.g., applications or users) are allowed to request credentials.
Let's consider this example:
kubectl exec -ti openbao-0 -n openbao -- bao write database/roles/my-postgres-role \
db_name=postgres \
creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}'; GRANT ALL PRIVILEGES ON DATABASE mydatabase TO \"{{name}}\";" \
default_ttl="1h" \
max_ttl="24h"
The my-postgres-role
:
- Tells OpenBao to create a user with a
{{name}}
and{{password}}
from theconnection_url
of the secret engine specification - in our example postgres and password. - Defines to
GRANT ALL PRIVILEGES ON DATABASE
to that user. - Sets the
default_ttl
to 1 hour andmax_ttl
to 24 hours. - As already mentioned - dynamic secrets do not exist until read. Only users with access to the path
database/roles/my-postgres-role
are allowed to get the above specified credentials.
Let's now generate a postgres database credentials with:
kubectl exec -ti openbao-0 -n openbao -- bao read database/creds/readonly
or
kubectl exec -ti openbao-0 -n openbao -- bao read database/creds/my-postgres-role
Leasing and Revocation
Leasing
To understand how leasing and revocation work we will need the Dynamic Secrets Engine we've set up before.
As we've seen the bao read
command returns us the secrets lease ID.
To set the Lease Durations for a whole Secrets Engine use:
kubectl exec -ti openbao-0 -n openbao -- bao write database/config max_ttl=1h default_ttl=30m
You can also override the lease duration of a single secret with:
kubectl exec -ti openbao-0 -n openbao -- bao read -ttl=15m database/mysql
Revocation
You can revoke a secret manually using:
vault lease revoke <lease_id>
for a single secret, or
vault lease revoke -prefix <database>
for all secrets issued under a specific path.
Enrypction as a Service
Enable the Database Secret Engine on the default path by running:
kubectl exec -ti openbao-0 -n openbao -- bao secrets enable transit
Hint: You can explicitly specify a custom path using the -path="/custom/path"
argument.
Encryption
Create your first encryption key, with:
kubectl exec -ti openbao-0 -n openbao -- bao write -f transit/keys/my-key
This creates a AES-256-GCM (by default) encryption key named my-key and stores it securely.
To encrypt some data, use:
kubectl exec -ti openbao-0 -n openbao -- bao write transit/encrypt/my-key plaintext=$(echo -n "hello-world" | base64)
Explanation:
bao write transit/encrypt/my-key
- points to the key we want to useplaintext
- for encryption there are two main types of data:plaintext
(unencrypted) andciphertext
(encrypted)$(echo -n "hello-world" | base64)
- OpenBao expects all data for encryption to be in the Base64 format.
Decryption
To decrypt the data, use:
kubectl exec -ti openbao-0 -n openbao -- bao write transit/decrypt/my-key ciphertext="vault:v1:..."
Explanation:
bao write transit/decrypt/my-key
- notice that this time we're using thedecrypt
part of the pathciphertext
- this time the data we're providing is already encryptedvault:
- indicates that the text has been encrypted by OpenBaov1
- tells OpenBao which version of the key has been used to encrypt the data. This way OpenBao knows how to handle the ciphertext even after key rotation.
You should see aGVsbG8td29ybGQ=
as the result. To convert it to a readable form, run:
echo "aGVsbG8td29ybGQ=" | base64 --decode
For more information visit https://openbao.org/docs/secrets/transit/#setup 🔗
Audit Devices and logs
For a File Audit Device use:
bao audit enable file file_path=/var/log/openbao_audit.log
For a Syslog Audit Device use:
bao audit enable syslog
And for a Socket Audit device run:
bao audit enable socket address=<address> socket_type=<tcp/udp>
📝 Cheatsheet
All important commands discussed in the tutorials
# Login to OpenBao
kubectl exec -ti openbao-0 -n openbao -- bao login <root_token_from_getpassword.sh>
or
kubectl exec -ti openbao-0 -n openbao -- bao login -method=userpass username=user1 password=password1
# Enable Key-Value secrets engine
kubectl exec -ti openbao-0 -n openbao -- bao secrets enable -path=database kv
# Add a secret to the KV engine
kubectl exec -ti openbao-0 -n openbao -- bao kv put database/postgres user=postgres password=123456
# Retrieve a secret from the KV engine
kubectl exec -ti openbao-0 -n openbao -- bao kv get database/postgres
# Enable userpass authentication method
kubectl exec -ti openbao-0 -n openbao -- bao auth enable userpass
# Create a user with specific policies
kubectl exec -ti openbao-0 -n openbao -- bao write auth/userpass/users/user1 password="password1" policies="user1-policy"
# Apply policy for a user
kubectl exec -ti openbao-0 -n openbao -- bao policy write user1-policy /tmp/user1-policy.hcl
# Write a database secret engine configuration
kubectl exec -ti openbao-0 -n openbao -- bao write database/config/my-postgres \
plugin_name=postgresql-database-plugin \
connection_url="postgresql://postgres:password@postgres.postgres-demo.svc.cluster.local:5432/postgres?sslmode=disable" \
allowed_roles="readonly, my-postgres-role"
# Write a database role
kubectl exec -ti openbao-0 -n openbao -- bao write database/roles/my-postgres-role \
db_name=postgres \
creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}'; GRANT ALL PRIVILEGES ON DATABASE mydatabase TO \"{{name}}\";" \
default_ttl="1h" \
max_ttl="24h"
# Generate dynamic credentials
kubectl exec -ti openbao-0 -n openbao -- bao read database/creds/my-postgres-role
# Change lease duration of a secret
kubectl exec -ti openbao-0 -n openbao -- bao read -ttl=15m database/mysql
# Revoke a secret or multiple secrets
vault lease revoke <lease_id>
vault lease revoke -prefix <database>
# Create encryption key
kubectl exec -ti openbao-0 -n openbao -- bao write -f transit/keys/my-key
# Encrypt data
kubectl exec -ti openbao-0 -n openbao -- bao write transit/encrypt/my-key plaintext=$(echo -n "hello-world" | base64)
# Decrypt data
kubectl exec -ti openbao-0 -n openbao -- bao write transit/decrypt/my-key ciphertext="vault:v1:..."
# File audit device
kubectl exec -ti openbao-0 -n openbao -- bao audit enable file file_path=/var/log/openbao_audit.log
# Syslog audit device
kubectl exec -ti openbao-0 -n openbao -- bao audit enable syslog
# Socket audit device
bao audit enable socket address=<address> socket_type=<tcp/udp>
🔗 References
- image 1: https://openbao.org/docs/concepts/policies/
- image 2: https://openbao.org/docs/concepts/seal/
- https://openbao.org/docs/what-is-openbao/
- https://openbao.org/docs/secrets/transit/
- https://www.vaultproject.io/
- https://developer.hashicorp.com/vault
- https://developer.hashicorp.com/vault/tutorials/get-started/understand-static-dynamic-secrets#dynamic-secrets