My VM deletes itself after a given time using this code:
ACCESS_TOKEN=\`curl "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" -H "Metadata-Flavor: Google" | python -c "import sys; import json; sys.stdout.write(json.loads(sys.stdin.read())['access_token'])"\`
curl -X "DELETE" https://compute.googleapis.com/compute/v1/projects/${gcp.project_id}/zones/${zone}/instances/${name} \\
-H "Authorization: Bearer $ACCESS_TOKEN"
The VM runs user supplied code; I want to lock down the VM so it can only delete itself.
I have created a role (https://console.cloud.google.com/iam-admin/roles) for compute.instances.delete, assigned it to a new service account, and assigned the service account to the VM.
The issue is that it only seems to work when created with these scopes which provide too much access to the Google APIs:
serviceAccounts: [
{
email: "service account email with just delete role",
scopes: [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/compute"
]
}
]
Without the above scopes I get "No service account scopes specified." in the ACCESS_TOKEN response.
Can this be done?