You are viewing documentation for Cozystack v1, which is currently in beta. For the latest stable version, see the v0 documentation.
ServiceAccount Tokens for API Access
Prerequisites
Before you begin, make sure that:
- A tenant already exists in Cozystack. See Create a User Tenant if you haven’t created one yet.
- You have access to the tenant namespace — either via OIDC credentials or an administrative kubeconfig.
kubectlis installed and configured.- (Optional)
jqis installed.
Retrieving the ServiceAccount Token
Each tenant in Cozystack has a Secret that contains a ServiceAccount token. The Secret has the same name as the tenant and is located in the tenant’s namespace.
- Log in to the Dashboard as a user with access to the tenant.
- Switch context to the target tenant if needed.
- On the left sidebar, navigate to the Administration → Info page and open the Secrets tab.
- Find the secret named
tenant-<name>(e.g.tenant-team1), where the Key is token. - Click the eye icon to reveal the Value field, then click the revealed data. The text will be copied to the clipboard automatically.
Retrieve the token for a tenant named <name>:
kubectl -n tenant-<name> get tenantsecret tenant-<name> -o json | jq -r '.data.token | @base64d'
To store the token in a variable for subsequent commands:
export TOKEN=$(kubectl -n tenant-<name> get tenantsecret tenant-<name> -o json | jq -r '.data.token | @base64d')
Using the Token for API Access
Once you have the token, you can
generate a kubeconfig for kubectl access, or use it directly with curl as shown below.
Token Security
ServiceAccount tokens in Cozystack do not expire by default. Handle them with the same care as passwords.
Test the Connection
First, verify your kubectl context points to the correct Cozystack cluster:
kubectl config current-context
kubectl cluster-info
Next, get the API server address:
export API_SERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')
Then, extract the CA certificate from the tenant secret:
kubectl -n tenant-<name> get secret tenant-<name> -o jsonpath='{.data.ca\.crt}' | base64 -d > ca.crt
Now, test the connection:
curl --cacert ca.crt -H "Authorization: Bearer ${TOKEN}" ${API_SERVER}/api
You can remove
ca.crtafter testing.