Policy API einrichten

Auf dieser Seite wird erläutert, wie Sie die Cloud Identity Policy API einrichten, bevor Sie Richtlinien auflisten und abrufen.

Python-Clientbibliothek installieren

Führen Sie den folgenden Befehl aus, um die Python-Clientbibliothek zu installieren:

  pip install --upgrade google-api-python-client google-auth \
    google-auth-oauthlib google-auth-httplib2 absly-py

Weitere Informationen zur Einrichtung der Python-Entwicklungsumgebung finden Sie im Einrichtungsleitfaden für die Python-Entwicklungsumgebung.

API aktivieren und Anmeldedaten für Dienstkonto einrichten

  1. Melden Sie sich in Ihrem Google Cloud Konto an. Wenn Sie noch kein Konto bei Google Cloudhaben, erstellen Sie ein Konto, um die Leistungsfähigkeit unserer Produkte in der Praxis sehen und bewerten zu können. Neukunden erhalten außerdem ein Guthaben von 300 $, um Arbeitslasten auszuführen, zu testen und bereitzustellen.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Identity API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the Service Account Token Creator role to the service account.

      To grant the role, find the Select a role list, then select Service Account Token Creator.

    7. Click Continue.
    8. In the Service account users role field, enter the identifier for the principal that will attach the service account to other resources, such as Compute Engine instances.

      This is typically the email address for a Google Account.

    9. Click Done to finish creating the service account.

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Cloud Identity API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  9. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the Service Account Token Creator role to the service account.

      To grant the role, find the Select a role list, then select Service Account Token Creator.

    7. Click Continue.
    8. In the Service account users role field, enter the identifier for the principal that will attach the service account to other resources, such as Compute Engine instances.

      This is typically the email address for a Google Account.

    9. Click Done to finish creating the service account.

Als Dienstkonto mit domainweiter Delegierung authentifizieren

Wenn Sie ein Administrator sind, der Identitätsrichtlinien verwaltet, oder ein Konto mit domainweiten Berechtigungen zur Verwaltung von Google-Richtlinien im Namen von Administratoren bereitstellen möchten, sollten Sie sich als Dienstkonto authentifizieren und diesem dann die domainweiten Berechtigungen gewähren.

Informationen zum Einrichten der domainweiten Delegierung finden Sie unter API-Zugriff mit domainweiter Delegierung steuern. Beachten Sie die Best Practices, um die Sicherheitsrisiken zu minimieren, die mit der Verwendung der domainweiten Delegierung verbunden sind.

Nachdem Sie die domainweite Delegierung eingerichtet haben, können Sie die Standardanmeldedaten für Anwendungen (Application Default Credentials, ADC) für die Authentifizierung verwenden. Wenn Sie ADC verwenden, kann Ihr Code in einer Entwicklungs- oder Produktionsumgebung ausgeführt werden, ohne dass sich die Authentifizierung Ihrer Anwendung bei Google Cloud-Diensten und APIs ändert. Geben Sie beim Initialisieren der Anmeldedaten in Ihrem Code die E-Mail-Adresse an, unter der das Dienstkonto agiert. Verwenden Sie dazu den Parameter subject() für die Anmeldedaten. Achten Sie darauf, dass der E-Mail-Adresse die Rolle Service Account User für das Dienstkonto zugewiesen ist (wie oben beschrieben). Beispiel:

Python

AUTH_SCOPES = ['https://www.googleapis.com/auth/iam']
# The read and write scope of the API. Note that you must provide the
# required scope to the service account while setting up domain-wide 
# delegation.
POLICY_SCOPES = ['https://www.googleapis.com/auth/cloud-identity.policies']
TOKEN_URI = "https://accounts.google.com/o/oauth2/token"

_ADMIN_EMAIL = flags.DEFINE_string(
    name='admin_email',
    default=None,
    help='Administrator email to call as',
    required=True,
)

# Fetch application default credentials (ADC)
credentials, _ = google.auth.default(scopes=AUTH_SCOPES)

# Populate account information
request = requests.Request()
credentials.refresh(request)

# Create an IAM signer
signer = iam.Signer(request, credentials,
                    credentials.service_account_email)

# Create domain-wide delegated (DWD) credentials
delegated_credentials = service_account.Credentials(
    signer=signer,
    service_account_email=credentials.service_account_email,
    token_uri=TOKEN_URI,
    scopes=POLICY_SCOPES,
    subject=_ADMIN_EMAIL.value
)

Verwenden Sie das Flag impersonate-service-account, um ein Dienstkonto zu imitieren, wenn Sie die Standardanmeldedaten für Anwendungen verwenden.

Shell

gcloud auth application-default login --impersonate-service-account=<service_account_email>
--scopes=https://www.googleapis.com/auth/iam,https://www.googleapis.com/auth/cloud-identity.policies

Detaillierter Beispielcode zum Aufrufen der Policy API, einschließlich des Codes für die Authentifizierung, finden Sie unter Richtlinien auflisten und abrufen.