Trying to figure out how to authenticate with the storage API from within a GKE cluster.
Code:
Storage storage = StorageOptions.newBuilder()
.setCredentials(ServiceAccountCredentials.getApplicationDefault())
.setProjectId(gcpProjectId)
.build().getService();
getApplicationDefault()
is documented to use these means to authenticate with the API:
- Credentials file pointed to by the {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable
- Credentials provided by the Google Cloud SDK {@code gcloud auth application-default login} command
- Google App Engine built-in credentials
- Google Cloud Shell built-in credentials
- Google Compute Engine built-in credentials
The application is using the GCP workload identity feature, so the application (in-cluster) service account is annotated with:
serviceAccount.annotations.iam.gke.io/gcp-service-account: [email protected]
Now the call to the storage account fails with the following error:
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Primary: /namespaces/my-project.svc.id.goog with additional claims does not have storage.objects.create access to the Google Cloud Storage object.",
"reason" : "forbidden"
} ],
"message" : "Primary: /namespaces/my-project.svc.id.goog with additional claims does not have storage.objects.create access to the Google Cloud Storage object."
}
This makes me think that the workload identity is not working correctly. I am expecting to receive an error message for my annotated service account and not the default one.
Is there anything else I should have been doing?