Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stewartshea/07ad335637e5f71c5fdf37d88c0ae92d to your computer and use it in GitHub Desktop.
Save stewartshea/07ad335637e5f71c5fdf37d88c0ae92d to your computer and use it in GitHub Desktop.
Test Service Account Access to Kubernetes API Server
apiserver=https://kubernetes.default.svc; namespace=${NAMESPACE}; context=${CONTEXT}; resource=""; serviceaccount=default; kubectl run curl-pod --image=curlimages/curl:latest --restart=Never --overrides="{ \"spec\": { \"serviceAccountName\": \"$serviceaccount\" } }" -n $namespace --context=$context --command -- sleep infinity && echo "Waiting for the curl-pod to be running..." && kubectl wait --for=condition=Ready pod/curl-pod --timeout=20s -n $namespace --context=$context && TOKEN=$(kubectl exec curl-pod -n $namespace --context=$context -- cat /var/run/secrets/kubernetes.io/serviceaccount/token) && echo "Performing a curl request to the Kubernetes API..." && kubectl exec curl-pod -n $namespace --context=$context -- curl -s -k -H "Authorization: Bearer $TOKEN" $apiserver$resource && echo "Cleaning up..." && kubectl delete pod curl-pod -n $namespace --context=$context && echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment