OpenClaw - Part 04: Kubernetes dependencies and Telegram
- it-claw: an agent for monitoring the Kubernetes cluster that can be queried via Telegram.
- Read-only Kubernetes ServiceAccount and dedicated kubeconfig.
- Creating the Telegram bot using BotFather.
- Secrets centralized in Vault, retrieved by a Vault Agent container.
Introduction
After:
- introducing the concept of agents and presenting Openclaw
- installing the environment necessary for agent containerization
- deploying a solution for securing secrets
For reference, this agent, which we will call "itclaw", will serve as my assistant for monitoring and tracking my Kubernetes cluster.
The goal is to be able to query it so that it can connect to my K8S cluster to provide me with information about the status of pods, nodes, and other components, even sending me regular reports about my platform.
To do this, we first need to prepare two things.
- A kubectl configuration file adapted to the agent's needs and associated with a role on the cluster. I won't go into detail here; feel free to refer to my article on the concept of RBAC around Kubernetes. The goal is to give the agent read-only access to the objects in the cluster while preventing it from accessing secrets. When you are starting with agents, as I am, it's important to be cautious and take small steps. Security remains a priority, and I don't want to allow the agent to do whatever it wants on the cluster initially.
- A Telegram bot linked to the agent. We need a channel to talk with our agent, locally and remotely. OpenClaw supports several messaging solutions for this. However, Telegram, thanks to its popularity and ecosystem, often comes up for this purpose. I know the Telegram galaxy is probably not suited to enterprise use; for that, OpenClaw can also use Signal. However, as explained in my first article, that requires more prerequisites, starting with a dedicated phone number. So I will stick with Telegram for my use.
Kubernetes Dependencies
Let's start with the Kubernetes part.
Creating the service account and role
To isolate all potential objects needed for the agent to access the cluster, we will dedicate a namespace according to my usual naming convention: prd-openclaw-lan.
This namespace will allow us to declare the ServiceAccount object, which we will name svc-openclaw-it. It will represent the agent's identity on the cluster, and all interactions between the agent and Kubernetes will be done using this internal service account within the cluster.
Since access must be cluster-wide, if we want the agent to be able to observe and query all the objects in the cluster (except the content of secrets), we need a ClusterRole, which applies across the whole cluster. (Once again, feel free to look here if you are not comfortable with this.)
This ClusterRole will be assigned read access rights to all objects in the cluster (excluding secrets).
Finally, following standard K8S practice, we need a final ClusterRoleBinding object to link the service account to the role.
To simplify things, I have grouped the creation of all these objects into a single file all-object-openclaw.yml, and here is its content:
#Création du namespace
---
apiVersion: v1
kind: Namespace
metadata:
name: prd-openclaw-lan
#Création du service account pour l'agent it-claw
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: svc-openclaw-it
namespace: prd-openclaw-lan
annotations:
description: "Accès en lecture seule pour l'agent it-claw"
#Création du role spécifique pour l'agent it-claw avec accès en lecture seule à tous les objets sauf les secrets
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cr-openclaw-global-reader
rules:
#Groupe principal (Core) : on liste tout explicitement SAUF "secrets"
- apiGroups: [""]
resources:
- nodes
- namespaces
- pods
- pods/log
- configmaps
- endpoints
- events
- persistentvolumes
- persistentvolumeclaims
- serviceaccounts
- services
verbs: ["get", "list", "watch"]
#Autres groupes d'API (Deployments, Ingress, CronJobs, etc.) : accès total en lecture
- apiGroups:
- apps
- batch
- autoscaling
- networking.k8s.io
- storage.k8s.io
- policy
- rbac.authorization.k8s.io
- apiextensions.k8s.io
- admissionregistration.k8s.io
- scheduling.k8s.io
- certificates.k8s.io
- coordination.k8s.io
- discovery.k8s.io
resources: ["*"]
verbs: ["get", "list", "watch"]
#Association du role au compte de service
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: crb-openclaw-it-claw
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cr-openclaw-global-reader
subjects:
- kind: ServiceAccount
name: svc-openclaw-it
namespace: prd-openclaw-lan
Then, I simply apply the command kubectl apply -f to create the objects on my cluster.
kubectl apply -f all-object-openclaw.yml
Click the image to enlarge.
Now that the objects have been created, we need to generate an authentication token specific to the service account and then use this token to build a kubeconfig file.
Creating the kubeconfig file
On the suggestion of the AI and with its help (no shame in that), we will try to be clever and generate the file by using variables.
To do this, we need to access one of the control plane nodes of the cluster.
We generate the token with an appropriate lifespan. In my case, I've set it to be quite long, aiming for 3 years (26280 hours). Ideally, in a business environment, do not exceed six months. We place the token directly into a variable SA_TOKEN.
SA_TOKEN=$(kubectl create token svc-openclaw-it -n prd-openclaw-lan --duration=26280h)
Then, we retrieve the public CA of the K8S cluster that we want to query via the specific content of the configmap kube-root-ca.crt, and store it in a variable SA_CA.
SA_CA=$(kubectl get cm kube-root-ca.crt -o jsonpath='{.data.ca\.crt}')
(The configmap object kube-root-ca.crt is present by default in all K8S clusters).
Since this CA fingerprint will be stored in our kubeconfig file, we need to convert it to base64, and the result will be stored in a variable SA_CA_B64.
SA_CA_B64=$(echo -n "$SA_CA" | base64 -w0)
Click the image to enlarge.
With all of that, there's nothing left to do but generate our configuration file.
cat > /tmp/it-claw.kubeconfig <<EOF
apiVersion: v1
kind: Config
clusters:
- cluster:
certificate-authority-data: ${SA_CA_B64}
server: ${API_SERVER}
name: kub.coolcorp.priv
contexts:
- context:
cluster: kub.coolcorp.priv
namespace: default
user: svc-openclaw-it
name: [email protected]
current-context: [email protected]
users:
- name: svc-openclaw-it
user:
token: ${SA_TOKEN}
EOF
Click the image to enlarge.
For now, we store it in /tmp on our k8s node.
We can directly test its usage by forcing the kubectl command to use it via the variable KUBECONFIG=/tmp/it-claw.kubeconfig.
For example, by listing the nodes.
KUBECONFIG=/tmp/it-claw.kubeconfig kubectl get nodes
Click the image to enlarge.
For security reasons, we test access to the cluster's secrets.
KUBECONFIG=/tmp/it-claw.kubeconfig kubectl get secrets -n prd-openclaw-lan 2>&1 | head -2
And there, we get an error message indicating that the secrets are not accessible to the service account.
Click the image to enlarge.
Thus, even if our agent were compromised, it would not be able to access the content of the secrets on the cluster.
Be careful, our file it-claw.kubeconfig is nevertheless sensitive; the goal will be to secure it later. For now, we need to move its contents to our dedicated agent server prdlinage501 (and not leave the file lying around on the K8S node).
We will continue with the file on the agent-dedicated server later.
Telegram Dependency
For now, we'll deal with Telegram.
The goal is to create a Telegram bot that can be controlled by the agent so that we can communicate with it.
Well, of course, you first need to have a Telegram account and the application on your phone.
Bot Creation
Next, you'll need to search for the contact "BotFather". This is a technical account on the platform.
Be careful not to be fooled; make sure you find the one with a small blue icon indicating that it is an official account.
Also, double-check the spelling.
Click the image to enlarge.
In the conversation, type /newbot.
This will trigger the bot creation process.
Click the image to enlarge.
You need to follow the procedure, which means giving your bot a name and then a username (note that this must adhere to a certain format).
In return, Telegram will give you an API key associated with your newly created bot.
Click the image to enlarge.
Warning: This key is critical! It is confidential and must be secured, which is what we will do later. In the meantime, store it in a safe place and always remember to clear your conversation with BotFather so that you don't keep the API key value in your history.
Retrieving the user ID
This key is not sufficient on its own, as Telegram includes an additional layer of protection for communicating with your bot.
By default, it will only respond to you.
However, our itclaw agent will need to use it to communicate with you. Therefore, in addition to the API key, you will also need to provide it with your user ID. This can be obtained through the system's user interface.
Click the image to enlarge.
Once again, pay attention to the spelling of the account.
Once in the conversation, type the keyword /start.
Click the image to enlarge.
This will return your user ID to you.
Again, be careful with its value; it is less confidential than the API key, but keep it private nonetheless.
Now we have all the necessary elements.
Whether it's the Telegram API key or the Kubernetes configuration file, all of this constitutes sensitive information.
Using Vault
This is where our Vault instance, installed in the previous article, comes into action. All of this essential sensitive information for our agent will be stored in Vault.
Registering Dependencies
So, on our dedicated server for the agent prdlinage501, using our standard user account, just like last time, we will interact with Vault. I won't repeat the overall logic; feel free to review or refer to the dedicated article if needed.
We export our Vault instance into a variable to indicate to our Vault CLI where to connect, which is our local Vault instance.
export VAULT_ADDR="https://prdlinage501.coolcorp.priv:8200"
For security reasons, when interacting with Vault, we disable its session history.
set +o history
We log in as root to the instance.
vault login
Click the image to enlarge.
From there, we will store our various sensitive data in the secure key/value store, in the tree created in the previous article.
We start with the Telegram API key (or token) associated with our bot.
vault kv put secret/openclaw/it-claw/telegram_token value="<TOKEN_TELEGRAM>"
Click the image to enlarge.
Then, our Kubernetes configuration retrieved from our control plane (which we temporarily placed in /tmp on our prdlinage501 server).
Click the image to enlarge.
vault kv put secret/openclaw/it-claw/kubeconfig value=@/tmp/it-claw.kubeconfig
Click the image to enlarge.
Finally, while we don't store the Telegram user ID, since it is less sensitive, we need a third secret: the private key of our future agent.
Indeed, each OpenClaw agent has its own communication gateway, and this is protected by a unique token generated randomly.
This is the token that we will create (using the openssl command) and then store in Vault.
GATEWAY_TOKEN=$(openssl rand -hex 32)
vault kv put secret/openclaw/it-claw/gateway_token value="$GATEWAY_TOKEN"
unset GATEWAY_TOKEN
Click the image to enlarge.
Once everything is finished, we can ensure that all data is correctly stored in Vault.
vault kv list secret/openclaw/it-claw/
Click the image to enlarge.
Don't forget to delete any potential traces of temporary files, and then we can proceed.
Click the image to enlarge.
Retrieving Credentials
Still using Vault and our current user, we now need to retrieve the credentials necessary to authenticate our future agent using the AppRole concept presented in the dedicated article about Vault.
To do this, we will first ask Vault to generate a new dynamic password for the vault role openclaw-it-claw.
This secret will be stored in the SECRET_ID variable (this is only temporary).
SECRET_ID=$(vault write -field=secret_id -f auth/approle/role/openclaw-it-claw/secret-id)
Next, we put the role ID into the ROLE_ID variable. Normally, we already did this earlier and stored it in our agent's directory (/var/lib/openclaw/vault/it-claw-role-id).
But if necessary, we can request it again (for reference, this information is not confidential).
ROLE_ID=$(cat /var/lib/openclaw/vault/it-claw-role-id 2>/dev/null || \
vault read -field=role_id auth/approle/role/openclaw-it-claw/role-id)
After saving the data in Vault, we can verify that it is actually accessible using the authentication information recently obtained.
To do this, we generate a connection token TEST_TOKEN to access Vault using the SECRET_ID and the ROLE_ID.
TEST_TOKEN=$(vault write -field=token auth/approle/login \
role_id="$ROLE_ID" \
secret_id="$SECRET_ID")
We then use this token to try to obtain access to a piece of data in Vault that is related to our agent.
VAULT_TOKEN="$TEST_TOKEN" vault kv get secret/openclaw/it-claw/telegram_token > /dev/null && \
echo "AppRole OK" || echo "AppRole KO"
Click the image to enlarge.
All good.
It's time to store these authentication elements at the level of our agent directory.
The ROLE_ID should already be there, but not the SECRET_ID.
# role-id (non-sensible, équivalent d'un username)
echo "$ROLE_ID" | sudo tee /var/lib/openclaw/vault/it-claw-role-id > /dev/null
echo "$SECRET_ID" | sudo tee /var/lib/openclaw/vault/it-claw-secret-id > /dev/null
Click the image to enlarge.
We are touching on a sensitive point mentioned previously: the fact that we store the secret_id in the bot's directory.
Let's first limit access to this data by confining it to our openclaw user, which was created specifically for our needs and presented in the specific article about prerequisites.
sudo chown -R openclaw:openclaw /var/lib/openclaw/vault/
sudo chmod 644 /var/lib/openclaw/vault/it-claw-role-id
sudo chmod 644 /var/lib/openclaw/vault/it-claw-secret-id
Click the image to enlarge.
Not forgetting SELinux.
sudo restorecon -Rv /var/lib/openclaw/vault/
Click the image to enlarge.
We could go further by adding options such as secret_id_bound_cidrs="127.0.0.1/32" to our Vault role. This would ensure that the secret_id can only be used locally, making it unusable from any machine other than my server prdlinage501. This would limit the risks if this secret were stolen.
But that is not the subject here.
However, we should still remember to remove the variables.
unset SECRET_ID ROLE_ID TEST_TOKEN
Click the image to enlarge.
Conclusion
At this stage, the article is already relatively long, and we will stop here for now. We have retrieved and secured all the confidential data necessary for the use of the future itclaw agent.
In summary, it will be able to interact with me through a Telegram bot (itbot) and then access my Kubernetes cluster using a configuration file that will allow it to connect to the cluster as the service account svc-openclaw-it. Since this account only has read access to the cluster, the agent will not be able to do anything other than report information, and without being able to access the cluster's secret objects.
The agent will be able to obtain all the necessary confidential information from the Vault container that is assigned to it. The latter will be responsible for retrieving the required elements from our Vault instance.
In the next article, we can finally implement our openclaw agent within a dedicated container, as well as its Vault container.