Ich kann die gcloud API gcloud iam service-accounts sign-jwt erfolgreich verwenden, um ein JWT zu signieren. Ich habe das resultierende JWT mit API Gateway verwendet, um seine Gültigkeit beim Identitätswechsel eines Dienstkontos zu bestätigen.
In Python kann ich ein externes Anmeldeinformationsobjekt und bei Bedarf ein imitiertes Dienstkonto-Anmeldeinformationsobjekt erstellen, aber beides schlägt fehl, wenn ich die Aktualisierung vor dem Signieren durchführe.
Wenn ich versuche, die meiner Meinung nach äquivalenten Aufrufe von gcloud in den Python-Google-Clientbibliotheken zu verwenden (google.auth.jwt.encode(...)) Ich erhalte die Fehlermeldung:
Code: Select all
google.auth.exceptions.RefreshError: ('Unable to acquire impersonated credentials', '{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT"
}
}')
Code: Select all
import time
import google.auth
import google.auth.jwt
from google.auth import iam
from google.auth.transport.requests import Request
def test_signing():
credentials, _ = google.auth.default()
now = int(time.time())
payload = {
"iat": now,
"exp": now + 3600,
"iss": "[email protected]",
"sub": "[email protected]",
"aud": 'myproject',
}
signer = iam.Signer(
Request(),
credentials,
"[email protected]"
)
signature = google.auth.jwt.encode(signer, payload)
assert signature
Code: Select all
permissions:
id-token: write
contents: read
jobs:
tests:
env:
GITHUB_ORG: ${{ github.repository_owner }}
timeout-minutes: 5
runs-on: ${{ matrix.os }}
continue-on-error: false
strategy:
matrix:
python-version: ["3.12"]
os: [ubuntu-latest]
steps:
- name: Run action checkout v4
uses: actions/checkout@v4
- name: auth
id: auth
uses: google-github-actions/auth@v3
with:
workload_identity_provider: '../locations/global/workloadIdentityPools/github/providers/myoidc'
service_account: '[email protected]'
project_id: 'myproject'
create_credentials_file: true
export_environment_variables: true
- name: Setup gcloud
uses: google-github-actions/setup-gcloud@v3
- name: sign token
run: |
cat > payload.json
Mobile version