<p>Trying to figure out how to authenticate with the storage API from within a GKE cluster.</p>
<p>Code:</p>
<pre><code>Storage storage = StorageOptions.newBuilder()
  .setCredentials(ServiceAccountCredentials.getApplicationDefault())
  .setProjectId(gcpProjectId)
  .build().getService();
</code></pre>
<p><code>getApplicationDefault()</code> is documented to use these means to authenticate with the API:</p>
<ol>
<li>Credentials file pointed to by the {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable</li>
<li>Credentials provided by the Google Cloud SDK {@code gcloud auth application-default login} command</li>
<li>Google App Engine built-in credentials</li>
<li>Google Cloud Shell built-in credentials</li>
<li>Google Compute Engine built-in credentials</li>
</ol>
<p>The application is using the GCP workload identity feature, so the application (in-cluster) service account is annotated with:</p>
<pre><code>serviceAccount.annotations.iam.gke.io/gcp-service-account: my-service-account@my-project.iam.gserviceaccount.com
</code></pre>
<p>Now the call to the storage account fails with the following error:</p>
<pre><code>{
  &quot;code&quot; : 403,
  &quot;errors&quot; : [ {
    &quot;domain&quot; : &quot;global&quot;,
    &quot;message&quot; : &quot;Primary: /namespaces/my-project.svc.id.goog with additional claims does not have storage.objects.create access to the Google Cloud Storage object.&quot;,
    &quot;reason&quot; : &quot;forbidden&quot;
  } ],
  &quot;message&quot; : &quot;Primary: /namespaces/my-project.svc.id.goog with additional claims does not have storage.objects.create access to the Google Cloud Storage object.&quot;
}
</code></pre>
<p>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.</p>
<p>Is there anything else I should have been doing?</p>

<p>The answer, in part, aside from the annotation syntax, is that, just like me, you probably didn't look closely enough at this part in the documentation:</p>
<pre><code>    gcloud iam service-accounts add-iam-policy-binding \
  --role roles/iam.workloadIdentityUser \
  --member &quot;serviceAccount:PROJECT_ID.svc.id.goog[K8S_NAMESPACE/KSA_NAME]&quot; \
  GSA_NAME@PROJECT_ID.iam.gserviceaccount.com
</code></pre>
<p>Notice the <code>PROJECT_ID.svc.id.goog[K8S_NAMESPACE/KSA_NAME]</code> piece. It's something they give no examples on as far as syntax but it looks like this in my terraform.</p>
<pre><code>resource &quot;google_project_iam_member&quot; &quot;app-binding-2&quot; {
  role   = &quot;roles/iam.workloadIdentityUser&quot;
  member = &quot;serviceAccount:${local.ws_vars[&quot;project-id&quot;]}.svc.id.goog[mynamespace/myk8ssaname]&quot;
}
</code></pre>
<p>Weirdly, I didn't know you could bind an IAM policy to a k8s service account, even more weirdly you can bind this in the terraform even if the namespace doesn't exist, much less the service account. So you can run this first before deployments.</p>
<p>I truly wish Google would provide better documentation and support, this took me several hours to figure out.</p>
