הגדרת API של דומיינים ברשימת ההיתרים

בדף הזה מוסבר איך להגדיר את Allowlisted Domains API לפני הוספה, הצגה וקבלת או מחיקה של דומיינים מרשימת ההיתרים.

לפני שמתחילים

לפני שמשתמשים בממשקי Cloud Identity API, צריך להגדיר את Cloud Identity. הוראות מפורטות זמינות במאמר הגדרת Cloud Identity.

התקנה של ספריית הלקוח של Python

כדי להתקין את ספריית הלקוח של Python, מריצים את הפקודה הבאה:

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

מידע נוסף על הגדרת סביבת הפיתוח בשפת Python מופיע במדריך להגדרת סביבת הפיתוח בשפת Python.

הפעלת ה-API והגדרת פרטי כניסה

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  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 serviceusage.services.enable permission. 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.

    Enable the API

  5. 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

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

  7. Enable the Cloud Identity API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

  8. יוצרים חשבון שירות:
    • מוודאים שיש לכם את תפקיד ה-IAM‏ Create Service Accounts (roles/iam.serviceAccountCreator) ואת התפקיד Project IAM Admin (roles/resourcemanager.projectIamAdmin). איך מקצים תפקידים
    • במסוף Google Cloud , נכנסים לדף יצירת חשבון שירות.
    • בוחרים את הפרויקט הרצוי.
    • כותבים שם בשדה Service account name. השדה מזהה חשבון שירות במסוף Google Cloud יאוכלס בהתאם לשם הזה.
    • כותבים תיאור בשדה Service account description. לדוגמה, Service account for quickstart.
    • לוחצים על Create and continue.
    • מקצים לחשבון השירות את התפקיד פרויקט > בעלים. כדי להקצות את התפקיד, בוחרים באפשרות Project > Owner (פרויקט > בעלים) מהרשימה Select a role (בחירת תפקיד).
    • לוחצים על Continue.
    • לוחצים על Done כדי לסיים ליצור את חשבון השירות. לא לסגור את חלון הדפדפן. תשתמשו בו בשלב הבא.
  9. יוצרים מפתח של חשבון שירות:
    • במסוף Google Cloud Cloud, לוחצים על כתובת האימייל של חשבון השירות שיצרתם.
    • לוחצים על Keys.
    • לוחצים על Add key ואז על Create new key.
    • לוחצים על Create. למחשב שלכם תתבצע הורדה של קובץ JSON עם המפתח.
    • לוחצים על Close.

יצירה של מפתח API

  1. נכנסים לדף Credentials במסוף Google Cloud .
  2. לוחצים על Create Credentials (יצירת אמצעי אימות) ובוחרים באפשרות API key (מפתח API).
  3. בתיבת הדו-שיח API key created מוצג מפתח ה-API החדש שיצרתם. מעתיקים את המפתח הזה לשימוש בסקריפט כקבוע API_KEY.

אימות כחשבון שירות עם הקצאת הרשאות ברמת הדומיין

אם אתם אדמינים שמנהלים דומיינים ברשימת ההיתרים, או אם אתם רוצים להעניק לחשבון הרשאות ברמת הדומיין כדי שהוא יוכל לנהל דומיינים ברשימת ההיתרים בשם האדמינים, אתם צריכים לבצע אימות כחשבון שירות ואז להעניק לחשבון השירות הרשאות ברמת הדומיין.

פרטים על הגדרת הענקת גישה ברמת הדומיין זמינים במאמר בנושא שליטה בהרשאות הגישה ל-API באמצעות הענקת גישה ברמת הדומיין. מומלץ לעיין בשיטות המומלצות כדי לצמצם את סיכוני האבטחה שקשורים לשימוש במתן הרשאות גישה ברמת הדומיין.

צריך לספק את היקפי ההרשאות הבאים כדי לתת הרשאה לחשבון השירות:

  • לפעולות קריאה וכתיבה (create, ‏ delete, ‏ list, ‏ get): https://www.googleapis.com/auth/cloud-identity.allowlisteddomains

  • לפעולות לקריאה בלבד (list, ‏ get): https://www.googleapis.com/auth/cloud-identity.allowlisteddomains.readonly

אתחול פרטי הכניסה ויצירת מופע של לקוח

כשמאתחלים את פרטי הכניסה בקוד, מציינים את כתובת האימייל שחשבון השירות פועל בשמה על ידי קריאה ל-with_subject() בפרטי הכניסה.

בדוגמה הבאה מוצג אופן יצירת מופע של לקוח באמצעות פרטי כניסה של חשבון שירות כדי לקיים אינטראקציה עם Allowlisted Domains API:

Python

from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = [
    'https://www.googleapis.com/auth/cloud-identity.allowlisteddomains.readonly',
    'https://www.googleapis.com/auth/cloud-identity.allowlisteddomains',
]
SERVICE_ACCOUNT_FILE = 'SERVICE_ACCOUNT_CREDENTIAL_FILE'
PROD_DISCOVERY_URL_BASE = (
    'https://cloudidentity.googleapis.com/$discovery/rest'
)
API_KEY = 'YOUR_API_KEY'

def create_service(version, delegated_email):
  """Instantiates a client using service account credentials."""
  credentials = service_account.Credentials.from_service_account_file(
      SERVICE_ACCOUNT_FILE, scopes=SCOPES
  )
  delegated_credentials = credentials.with_subject(delegated_email)
  url = f'{PROD_DISCOVERY_URL_BASE}?version={version}&key={API_KEY}'

  service = googleapiclient.discovery.build(
      serviceName=None,
      version=None,
      credentials=delegated_credentials,
      discoveryServiceUrl=url,
      static_discovery=False,
  )
  return service

מחליפים את מה שכתוב בשדות הבאים:

  • SERVICE_ACCOUNT_CREDENTIAL_FILE: קובץ המפתח של חשבון השירות שיצרתם קודם במסמך הזה
  • YOUR_API_KEY: מפתח ה-API שהועתק מדף פרטי הכניסה במסוף Google Cloud

דוגמאות מפורטות לקוד לדוגמה לקריאה לפעולות של Allowlisted Domains API זמינות במאמר הצגה וקבלת של דומיינים שכלולים ברשימת ההיתרים.