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
- Melden Sie sich in Ihrem Google Cloud Konto an. Wenn Sie noch nicht mit Google Cloudvertraut sind, erstellen Sie ein Konto, um die Leistung 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.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Identity API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Create a service account:
-
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. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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. - Click Create and continue.
-
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.
- Click Continue.
-
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.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Identity API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Create a service account:
-
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. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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. - Click Create and continue.
-
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.
- Click Continue.
-
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.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
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. Lesen 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:
Wichtig: Die in Ihrem Anwendungscode angegebenen OAuth-Bereiche, die zum Erstellen delegierter Anmeldedaten verwendet werden, müssen in der Liste der autorisierten Bereiche für die domainweite Delegierung in der Admin-Konsole vorhanden sein. Ein umfassenderer oder permissiverer Bereich funktioniert nicht. Wenn ein Bereich, der von der Anwendung zum Erstellen delegierter Anmeldedaten angefordert wird, in der domainweiten Delegierung nicht autorisiert ist, erhält die Anwendung einen
unauthorized_clientFehler.
Python
AUTH_SCOPES = ['https://www.googleapis.com/auth/iam']
# The read and write scope of the API. Note that you must authorize the
# exact same scope for domain-wide delegation in the Google Admin Console.
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 sich bei Verwendung der Standardanmeldedaten für Anwendungen als Dienstkonto auszugeben.
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.