Token Management
This guide covers the creation, usage, and lifecycle management of Kubernetes tokens for secure InSpec scanning.
Token Types
Short-Lived Tokens (Recommended)
Short-lived tokens are created on-demand and expire automatically:
| # Create a token with default expiration (1 hour)
kubectl create token inspec-scanner -n inspec-test
# Create a token with custom expiration
kubectl create token inspec-scanner -n inspec-test --duration=30m
|
Benefits:
- Automatic expiration
- No token storage/cleanup required
- Reduced risk if token is exposed
Bound Service Account Tokens
For automated pipelines, you can create bound service account tokens:
| apiVersion: v1
kind: Secret
metadata:
name: scanner-token
namespace: inspec-test
annotations:
kubernetes.io/service-account.name: inspec-scanner
kubernetes.io/service-account.expiration: "86400" # 24 hours in seconds
type: kubernetes.io/service-account-token
|
Benefits:
- Configurable expiration
- Can be rotated with Kubernetes secrets rotation
- Compatible with older Kubernetes tooling
Token Generation in CI/CD Pipelines
GitLab CI Example
| stages:
- scan
variables:
KUBE_NAMESPACE: inspec-test
create_token:
stage: scan
script:
- TOKEN=$(kubectl create token inspec-scanner -n $KUBE_NAMESPACE)
- echo "SCAN_TOKEN=$TOKEN" >> scan_credentials.env
artifacts:
reports:
dotenv: scan_credentials.env
run_scan:
stage: scan
needs: [create_token]
script:
- echo "$SCAN_TOKEN" > token.txt
- ./run_scan.sh token.txt
artifacts:
reports:
scan: scan-results.json
|
GitHub Actions Example
| jobs:
scan:
runs-on: ubuntu-latest
steps:
- name: Generate Token
id: generate-token
run: |
TOKEN=$(kubectl create token inspec-scanner -n inspec-test)
echo "::set-output name=token::$TOKEN"
- name: Run Scan
run: |
echo "${{ steps.generate-token.outputs.token }}" > token.txt
./run_scan.sh token.txt
|
Token Security Best Practices
- Short Expiration: Use the shortest practical token expiration
- Just-in-Time Creation: Generate tokens when needed, not in advance
- Secure Storage: Store tokens in secure CI/CD variables or secrets
- Mask in Logs: Ensure tokens are masked in CI/CD logs
- Single-Use: Use each token only once, then discard
- Audience Restriction: If possible, restrict token audience
Token Expiration Testing
Test token expiration to ensure your system handles it gracefully:
| # Create a token with short expiration
TOKEN=$(kubectl create token inspec-scanner -n inspec-test --duration=30s)
# Save token to a file
echo "$TOKEN" > test-token.txt
# Use token immediately (should work)
KUBECONFIG=<your-config> K8S_AUTH_TOKEN=$(cat test-token.txt) inspec exec ...
# Wait for expiration
sleep 35
# Try again (should fail)
KUBECONFIG=<your-config> K8S_AUTH_TOKEN=$(cat test-token.txt) inspec exec ...
|
Token Audit and Troubleshooting
Decoding Tokens
| # Get the token
TOKEN=$(kubectl create token inspec-scanner -n inspec-test)
# Decode token payload (middle section)
echo $TOKEN | cut -d. -f2 | base64 -d 2>/dev/null | jq .
|
This shows token metadata including:
- Expiration time
- Subject (service account)
- Audience
- Issuer
Verifying Token Privileges
| # Use auth can-i to check permissions with a token
kubectl auth can-i get pods --namespace=inspec-test --token=$TOKEN
# Check specific resource access
kubectl auth can-i create pods/exec --namespace=inspec-test --token=$TOKEN --resource-name=target-pod
|
References