Configurer l'API Policy
Cette page explique comment configurer l'API Cloud Identity Policy avant de lister et d'obtenir des stratégies.
Installer la bibliothèque cliente Python
Pour installer la bibliothèque cliente Python, exécutez la commande suivante :
pip install --upgrade google-api-python-client google-auth \
google-auth-oauthlib google-auth-httplib2 absly-py
Pour savoir comment configurer votre environnement de développement Python, consultez le guide de configuration d'un environnement de développement Python.
Activer l'API et configurer les identifiants du compte de service
- Connectez-vous à votre Google Cloud compte. Si vous débutez sur Google Cloud, créez un compte pour évaluer les performances de nos produits en conditions réelles. Les nouveaux clients bénéficient également de 300 $ de crédits sans frais pour exécuter, tester et déployer des charges de travail.
-
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
(
S'authentifier en tant que compte de service avec délégation au niveau du domaine
Si vous êtes un administrateur qui gère des stratégies d'identité ou si vous souhaitez fournir à un compte des droits au niveau du domaine afin qu'il puisse gérer les stratégies Google pour le compte des administrateurs, vous devez vous authentifier en tant que compte de service, puis accorder des droits au niveau du domaine à ce compte.
Pour en savoir plus sur la configuration de la délégation au niveau du domaine, consultez la page Contrôler l'accès à l'API à l'aide de la délégation au niveau du domaine. Consultez les bonnes pratiques pour atténuer les risques de sécurité associés à l'utilisation de la délégation au niveau du domaine.
Une fois la délégation au niveau du domaine configurée, vous pouvez utiliser les identifiants par défaut de l'application (ADC) pour l'authentification. Lorsque vous utilisez le service ADC, votre code peut s'exécuter dans un environnement de développement ou bien de production, sans modifier la manière dont votre application s'authentifie auprès des API et services Google Cloud.
Lors de l'initialisation des identifiants dans votre code, spécifiez l'adresse e-mail sur laquelle le compte de service agit à l'aide du paramètre subject() sur l'identifiant. Assurez-vous que le rôle Service Account User est attribué à l'adresse e-mail sur le compte de service (comme décrit ci-dessus).
Exemple :
Important : Les champs d'application OAuth spécifiés dans le code de votre application et utilisés pour créer des identifiants délégués doivent être présents dans la liste des champs d'application autorisés pour la délégation au niveau du domaine dans la console d'administration Google. Un champ d'application plus large ou plus permissif ne fonctionnera pas. Si un champ d'application demandé par l'application pour créer des identifiants délégués n'est pas autorisé dans la délégation au niveau du domaine, l'application reçoit une
unauthorized_clienterreur.
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
)
Pour emprunter l'identité d'un compte de service lorsque vous utilisez les identifiants par défaut de l'application, utilisez l'option impersonate-service-account.
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
Vous trouverez un exemple de code détaillé pour appeler l'API Policy, y compris le code d' authentification, dans la section Lister et obtenir des stratégies.