Google Cloud Java SDK mit Workload-Identität?

Oct 08 2020

Es wird versucht, herauszufinden, wie Sie sich mit der Speicher-API innerhalb eines GKE-Clusters authentifizieren können.

Code:

Storage storage = StorageOptions.newBuilder()
  .setCredentials(ServiceAccountCredentials.getApplicationDefault())
  .setProjectId(gcpProjectId)
  .build().getService();

getApplicationDefault() ist dokumentiert, um diese Mittel zur Authentifizierung bei der API zu verwenden:

  1. Anmeldeinformationsdatei, auf die die Umgebungsvariable {@code GOOGLE_APPLICATION_CREDENTIALS} verweist
  2. Anmeldeinformationen, die vom Google Cloud SDK-Befehl {@code gcloud auth application-default login} bereitgestellt werden
  3. In Google App Engine integrierte Anmeldeinformationen
  4. In Google Cloud Shell integrierte Anmeldeinformationen
  5. In Google Compute Engine integrierte Anmeldeinformationen

Die Anwendung verwendet die GCP-Workload-Identitätsfunktion, sodass das Anwendungsdienstkonto (im Cluster) mit folgenden Anmerkungen versehen ist:

serviceAccount.annotations.iam.gke.io/gcp-service-account: [email protected]

Jetzt schlägt der Aufruf des Speicherkontos mit folgendem Fehler fehl:

{
  "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."
}

Dies lässt mich denken, dass die Workload-Identität nicht richtig funktioniert. Ich erwarte eine Fehlermeldung für mein kommentiertes Dienstkonto und nicht das Standardkonto.

Gibt es noch etwas, was ich hätte tun sollen?

Antworten

MoritzSchmitzv.Hülst Oct 09 2020 at 15:47

Die Anmerkung ist falsch. Anstatt von:

serviceAccount.annotations.iam.gke.io/gcp-service-account: [email protected]

es muss sein

iam.gke.io/gcp-service-account: [email protected]

Jetzt zeigt die Fehlermeldung auch, dass die App eine Workload-Identität verwendet:

java.io.IOException: Unexpected Error code 403 trying to get security access token from Compute Engine metadata for the default service account: Unable to generate access token; IAM returned 403 Forbidden: The caller does not have permission
This error could be caused by a missing IAM policy binding on the target IAM service account.
For more information, refer to the Workload Identity documentation:
    https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity#creating_a_relationship_between_ksas_and_gsas