שילוב של אימות IaC עם Cloud Build

אתם יכולים לכתוב תצורת build שמורה ל-Cloud Build לאמת את התשתית כקוד (IaC) שמהווה חלק מה-build. אימות של IaC מאפשר לכם לקבוע אם הגדרות המשאבים של Terraform מפרות את מדיניות הארגון הקיימת ואת גלאי Security Health Analytics שמוחלים על המשאבים שלכם ב- Google Cloud .

מידע נוסף על אימות IaC זמין במאמר בנושא אימות IaC בהתאם למדיניות של Google Cloud הארגון.

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

כדי להתחיל להשתמש באימות IaC באמצעות Cloud Build, צריך להשלים את המשימות הבאות.

הפעלת רמת Premium או רמת Enterprise של Security Command Center

מוודאים שמהדורת Premium או Enterprise של Security Command Center מופעלת ברמת הארגון.

הפעלת Security Command Center מפעילה את ממשקי ה-API‏ securityposture.googleapis.com ו-securitycentermanagement.googleapis.com.

הגדרת ההרשאות

  1. Make sure that you have the following role or roles on the organization:

    • Security Posture Shift-Left Validator
    • Log Writer
    • Storage Writer
    • Storage Reader

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the organization.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      כניסה לדף IAM
    2. בוחרים את הארגון.
    3. לוחצים על ‎ Grant access.
    4. בשדה New principals, מזינים את מזהה המשתמש. ‫ בדרך כלל מזהה המשתמש הוא כתובת האימייל של חשבון Google.

    5. לוחצים על בחירת תפקיד ומחפשים את התפקיד.
    6. כדי לתת עוד תפקידים, לוחצים על Add another role ומוסיפים את כולם.
    7. לוחצים על Save.

    מידע נוסף על הרשאות אימות של IaC זמין במאמר IAM להפעלות ברמת הארגון.

    הפעלת Cloud Build API

    1. Enable the Cloud Build 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

    הגדרת כללי המדיניות

    הגדרת מדיניות ארגונית וגלאים של Security Health Analytics. כדי להגדיר את המדיניות הזו באמצעות מצב אבטחה, צריך לבצע את המשימות שבמאמר יצירה ופריסה של מצב אבטחה.

    יצירת קוד Terraform

    הוראות מפורטות מופיעות במאמר יצירת קוד Terraform.

    אימות ה-IAC ב-Cloud Build

    מוסיפים את המשימות הבאות לקובץ cloudbuild.yaml:

    1. מאתחלים את Terraform:

      - name: hashicorp/terraform
        args:
          - '-c'
          - |
            terraform init \
              -backend-config="bucket=STATE_BUCKET" \
              -backend-config="prefix=REPOSITORY_NAME" \
        dir: FOLDER
        id: Terraform Init
        entrypoint: sh
      

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

      • STATE_BUCKET עם שם הקטגוריה של Cloud Storage שבה רוצים לאחסן את מצב Terraform
      • REPOSITORY_NAME בשם המאגר שמארח את קוד Terraform.
      • FOLDER בשם התיקייה שבה רוצים לשמור את הארטיפקטים של Terraform.
    2. יוצרים קובץ תוכנית:

      - name: hashicorp/terraform
        args:
          - '-c'
          - |
            terraform plan -out tf.plan
        dir: FOLDER
        id: Terraform Plan
        entrypoint: sh
      
    3. ממירים את קובץ התוכנית לפורמט JSON:

      - name: hashicorp/terraform
        args:
          - '-c'
          - |
            terraform show -json tf.plan > plan.json
        dir: FOLDER
        id: Terraform Show
        entrypoint: sh
      
    4. יוצרים את דוח האימות של IaC:

      - name: gcr.io/cloud-builders/gcloud
        args:
          - '-c'
          - |
            gcloud scc iac-validation-reports create \
            organizations/ORGANIZATION_ID/locations/global --tf-plan-file=plan.json \
            --format="json(response.iacValidationReport)" > IaCScanReport_$BUILD_ID.json
        dir: FOLDER
        id: Run IaC scan
        entrypoint: /bin/bash
      

      מחליפים את ORGANIZATION_ID במזהה הארגון.

    5. אם אתם משתמשים ב-Cloud Storage, אתם יכולים להעלות את קובץ התוצאות בפורמט JSON ל-Cloud Storage:

      - name: gcr.io/cloud-builders/gcloud
        args:
          - storage
          - cp
          - IaCScanReport_$BUILD_ID.json
          - SCAN_RESULT_FILE_BUCKET
        dir: FOLDER
        id: Upload report file
      

      מחליפים את הערך ב-SCAN_RESULT_FILE_BUCKET בקטגוריה של Cloud Storage שאליה רוצים להעלות את קובץ התוצאות.

    6. כדי לראות את התוצאות בפורמט SARIF, מבצעים את הפעולות הבאות:

      1. ממירים את הקובץ:

        - name: golang
          args:
            - '-c'
            - |
              go run github.com/google/gcp-scc-iac-validation-utils/SARIFConverter@latest \
                --inputFilePath=IaCScanReport_$BUILD_ID.json
                --outputFilePath=IaCScanReport_$BUILD_ID.sarif.json
          dir: FOLDER
          id: Convert to SARIF format
          entrypoint: /bin/bash
        
      2. אופציונלי: מעלים את הקובץ ל-Cloud Storage:

        - name: gcr.io/cloud-builders/gcloud
          args:
            - storage
            - cp
            - IaCScanReport_$BUILD_ID.sarif.json
            - SCAN_RESULT_FILE_BUCKET
          dir: FOLDER
          id: Upload report file
        
    7. מאמתים את התוצאות. משלימים את השלב הזה בקובץ ה-JSON של התוצאות שלא הומר לפורמט SARIF:

      - name: golang
        args:
          - '-c'
          - |
            go run github.com/google/gcp-scc-iac-validation-utils/ReportValidator@latest \
              --inputFilePath=IaCScanReport_$BUILD_ID.json --failure_expression=FAILURE_CRITERIA
        dir: FOLDER
        id: Validate results
        entrypoint: /bin/bash
      

      מחליפים את FAILURE_CRITERIA בקריטריון של סף הכשל שקובע מתי ה-build נכשל. קריטריון הסף מבוסס על מספר הבעיות ברמות החומרה הקריטית, הגבוהה, הבינונית והנמוכה שמתגלות בסריקת האימות של IaC. FAILURE_CRITERIA מציין כמה בעיות מכל רמת חומרה מותרות, וגם מציין איך הבעיות מצטברות (AND או OR). לדוגמה, אם רוצים שה-build ייכשל אם תזוהה בעיה קריטית אחת או בעיה אחת ברמת חומרה גבוהה, צריך להגדיר את FAILURE_CRITERIA לערך Critical:1,High:1,Operator:OR. ערך ברירת המחדל הוא Critical:1,High:1,Medium:1,Low:1,Operator:OR, כלומר אם הסריקה של IaC לאימות נתקלת בהפרה בכל רמת חומרה, ה-build חייב להיכשל.

    8. אם ה-build נכשל, פותרים את ההפרות בקוד Terraform.

    המאמרים הבאים