In dieser Anleitung wird beschrieben, wie Sie überprüfen können, ob Ihre Infrastructure-as-Code-Konfigurationen (IaC) nicht gegen Ihre Organisationsrichtlinien oder Security Health Analytics-Erkennungstypen verstoßen.
Lernziele
- Sicherheitsstatus erstellen
- Stellen Sie die Haltung in einem Projekt bereit.
- Eine Terraform-Beispieldatei auf Verstöße prüfen
- Beheben Sie die Verstöße in der Terraform-Datei und prüfen Sie die Datei noch einmal, um die Korrektur zu bestätigen.
Hinweise
Berechtigungen einrichten
-
Make sure that you have the following role or roles on the organization: Project Creator and Security Posture Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the organization.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
Zu IAM - Wählen Sie die Organisation aus.
- Klicken Sie auf Zugriff erlauben.
-
Geben Sie im Feld Neue Hauptkonten Ihre Nutzer-ID ein. Dies ist in der Regel die E-Mail-Adresse eines Google-Kontos.
- Wählen Sie in der Liste Rolle auswählen eine Rolle aus.
- Wenn Sie weitere Rollen hinzufügen möchten, klicken Sie auf Weitere Rolle hinzufügen und fügen Sie weitere Rollen hinzu.
- Klicken Sie auf Speichern.
Cloud Shell einrichten
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
- So ermitteln Sie Ihre Organisations-ID:
gcloud organizations list
-
Umgebung vorbereiten
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Security posture service and Security Command Center management APIs:
gcloud services enable securityposture.googleapis.com
securitycentermanagement.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Security posture service and Security Command Center management APIs:
gcloud services enable securityposture.googleapis.com
securitycentermanagement.googleapis.com - Kopieren Sie die Projektnummer. Sie benötigen die Projektnummer, um die Zielressource beim Bereitstellen der Konfiguration festzulegen.
gcloud projects describe PROJECT_ID
- Initialisieren Sie Terraform:
terraform init
Starten Sie den Cloud Shell-Editor in Cloud Shell. Klicken Sie zum Starten des Editors in der Symbolleiste des Cloud Shell-Fensters auf
Editor öffnen.
Erstellen Sie eine YAML-Datei mit dem Namen
example-standard.yaml
.Fügen Sie den folgenden Code in Ihre Datei ein:
Erstellen Sie in Cloud Shell die Haltung:
gcloud scc postures create organizations/ORGANIZATION_ID/locations/global/postures/example-standard --posture-from-file=example-standard.yaml
Kopieren Sie die ID der Haltungsüberarbeitung, die mit dem Befehl generiert wird.
Stellen Sie die Haltung in Ihrem Projekt bereit:
gcloud scc posture-deployments create organizations/ORGANIZATION_ID/locations/global/postureDeployments/example-standard \ --posture-name=organizations/ORGANIZATION_ID/locations/global/postures/example-standard \ --posture-revision-id="POSTURE_REVISION_ID" \ --target-resource=projects/PROJECT_NUMBER
Ersetzen Sie Folgendes:
ORGANIZATION_ID
: Ihre Organisations-ID.POSTURE REVISION_ID
: Die ID der Überarbeitung Ihres Beitrags, die Sie kopiert haben.PROJECT_NUMBER
: Ihre Projektnummer.
Starten Sie den Cloud Shell-Editor in Cloud Shell.
Erstellen Sie eine Terraform-Datei mit dem Namen
main.tf
.Fügen Sie den folgenden Code in Ihre Datei ein:
terraform { required_providers { google = { source = "hashicorp/google" } } } provider "google" { region = "us-central1" zone = "us-central1-c" } resource "google_compute_network" "example_network"{ name = "example-network-1" delete_default_routes_on_create = false auto_create_subnetworks = false routing_mode = "REGIONAL" mtu = 100 project = "PROJECT_ID" } resource "google_container_node_pool" "example_node_pool" { name = "example-node-pool-1" cluster = "example-cluster-1" project = "PROJECT_ID" initial_node_count = 2 node_config { preemptible = true machine_type = "e2-medium" } } resource "google_storage_bucket" "example_bucket" { name = "example-bucket-1" location = "EU" force_destroy = true project = "PROJECT_ID" uniform_bucket_level_access = false }
Ersetzen Sie
PROJECT_ID
durch die Projekt-ID des von Ihnen erstellten Projekts.Erstellen Sie in Cloud Shell die Terraform-Plandatei und konvertieren Sie sie in das JSON-Format:
terraform plan -out main.plan terraform show -json main.plan > mainplan.json
Erstellen Sie den IaC-Validierungsbericht für
mainplan.json
:gcloud scc iac-validation-reports create organizations/ORGANIZATION_ID/locations/global --tf-plan-file=mainplan.json
Dieser Befehl gibt einen IaC-Validierungsbericht zurück, in dem die folgenden Verstöße beschrieben werden:
- Die
mtu
fürexample_network
ist nicht 1.000. - Der
initial_node_count
fürexample_node_pool
ist nicht 3. - Für
example_bucket
ist kein einheitlicher Zugriff auf Bucket-Ebene aktiviert. - Für
example_bucket
ist kein Logging aktiviert.
- Die
Starten Sie den Cloud Shell-Editor in Cloud Shell.
Nehmen Sie die folgenden Änderungen an der Datei
main.tf
vor:terraform { required_providers { google = { source = "hashicorp/google" } } } provider "google" { region = "us-central1" zone = "us-central1-c" } resource "google_compute_network" "example_network"{ name = "example-network-1" delete_default_routes_on_create = false auto_create_subnetworks = false routing_mode = "REGIONAL" mtu = 1000 project = "PROJECT_ID" } resource "google_container_node_pool" "example_node_pool" { name = "example-node-pool-1" cluster = "example-cluster-1" project = "PROJECT_ID" initial_node_count = 3 node_config { preemptible = true machine_type = "e2-medium" } } resource "google_storage_bucket" "example_bucket" { name = "example-bucket-1" location = "EU" force_destroy = true project = "PROJECT_ID" uniform_bucket_level_access = true logging { log_bucket = "my-unique-logging-bucket" // Create a separate bucket for logs log_object_prefix = "tf-logs/" // Optional prefix for better structure } }
Ersetzen Sie
PROJECT_ID
durch die Projekt-ID des von Ihnen erstellten Projekts.Erstellen Sie in Cloud Shell die Terraform-Plandatei und konvertieren Sie sie in das JSON-Format:
terraform plan -out main.plan terraform show -json main.plan > mainplan.json
Erstellen Sie den IaC-Validierungsbericht für
mainplan.json
neu:gcloud scc iac-validation-reports create organizations/ORGANIZATION_ID/locations/global --tf-plan-file=mainplan.json
Status erstellen und bereitstellen
name: organizations/ORGANIZATION_ID/locations/global/postures/example-standard state: ACTIVE policySets: - policies: - constraint: orgPolicyConstraintCustom: customConstraint: actionType: ALLOW condition: "resource.initialNodeCount == 3" description: Set initial node count to be exactly 3. displayName: fixedNodeCount methodTypes: - CREATE name: organizations/ORGANIZATION_ID/customConstraints/custom.fixedNodeCount resourceTypes: - container.googleapis.com/NodePool policyRules: - enforce: true policyId: fixedNodeCount - constraint: securityHealthAnalyticsCustomModule: config: customOutput: {} description: Set MTU for a network to be exactly 1000. predicate: expression: "!(resource.mtu == 1000)" recommendation: Only create networks whose MTU is 1000. resourceSelector: resourceTypes: - compute.googleapis.com/Network severity: HIGH displayName: fixedMTU moduleEnablementState: ENABLED policyId: fixedMTU - constraint: securityHealthAnalyticsModule: moduleEnablementState: ENABLED moduleName: BUCKET_POLICY_ONLY_DISABLED policyId: bucket_policy_only_disabled - constraint: securityHealthAnalyticsModule: moduleEnablementState: ENABLED moduleName: BUCKET_LOGGING_DISABLED policyId: bucket_logging_disabled policySetId: policySet1
Ersetzen Sie ORGANIZATION_ID
durch Ihre Organisations-ID.
Terraform-Datei erstellen und validieren
Verstöße beheben
Bereinigen
Damit Ihrem Google Cloud-Konto die in dieser Anleitung verwendeten Ressourcen nicht in Rechnung gestellt werden, löschen Sie entweder das Projekt, das die Ressourcen enthält, oder Sie behalten das Projekt und löschen die einzelnen Ressourcen.
Projekt löschen
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Nächste Schritte
- Überprüfen Sie Ihre IaC-Konfiguration anhand der Richtlinien Ihrer Organisation.
- Referenzarchitekturen, Diagramme und Best Practices zu Google Cloud kennenlernen. Weitere Informationen zu Cloud Architecture Center