Overview

Identity and Access Management (IAM) is a critical component of Google Cloud Platform (GCP) that allows you to manage access to your resources securely. IAM enables you to define who (identity) has what access (role) to which resource.

Key Concepts

  1. Identities

Identities are entities that can be authenticated and authorized to access GCP resources. They include:

  • Google Accounts: Individual user accounts.
  • Service Accounts: Special accounts used by applications and virtual machines (VMs) to make authorized API calls.
  • Google Groups: Collections of Google accounts and service accounts.
  • Cloud Identity or G Suite domains: All the users in a domain.

  1. Roles

Roles are collections of permissions. They determine what actions an identity can perform on a resource. There are three types of roles:

  • Primitive Roles: Basic roles that include Owner, Editor, and Viewer.
  • Predefined Roles: Granular roles that provide more specific permissions.
  • Custom Roles: User-defined roles that allow you to specify a custom set of permissions.

  1. Policies

Policies bind one or more members to a role. A policy is attached to a resource and defines who has what type of access to that resource.

  1. Permissions

Permissions determine what operations are allowed on a resource. Permissions are not assigned directly to users; they are assigned to roles, which are then granted to users.

Practical Example

Setting Up IAM Roles and Permissions

  1. Navigate to the IAM & Admin Console:

    • Go to the GCP Console.
    • Select the project you want to manage.
    • Navigate to "IAM & Admin" > "IAM".
  2. Add a Member:

    • Click on "Add".
    • Enter the email address of the member you want to add.
    • Select a role from the dropdown menu. For example, choose "Viewer" to grant read-only access.
  3. Assign a Predefined Role:

    • To assign a predefined role, select a role that matches the required permissions. For example, "Compute Engine Admin" for managing Compute Engine resources.
  4. Create a Custom Role:

    • Navigate to "IAM & Admin" > "Roles".
    • Click on "Create Role".
    • Provide a name, description, and specify the permissions you want to include in the role.
    • Save the role and assign it to a member as described above.

Example Code: Assigning a Role Using gcloud CLI

# Assign the 'Viewer' role to a user
gcloud projects add-iam-policy-binding [PROJECT_ID] \
  --member='user:[email protected]' \
  --role='roles/viewer'

Explanation:

  • [PROJECT_ID]: Replace with your GCP project ID.
  • user:[email protected]: Replace with the email of the user you want to grant access to.
  • roles/viewer: The role you are assigning to the user.

Practical Exercise

Exercise: Assigning Roles to a Service Account

  1. Create a Service Account:

    • Navigate to "IAM & Admin" > "Service Accounts".
    • Click on "Create Service Account".
    • Provide a name and description for the service account.
    • Click "Create".
  2. Assign a Role to the Service Account:

    • After creating the service account, click on the service account email.
    • Click on "Add Key" > "Create New Key".
    • Choose JSON and click "Create". Save the key file securely.
    • Navigate back to "IAM".
    • Click on "Add" and enter the service account email.
    • Assign a role, such as "Storage Admin".
  3. Verify the Permissions:

    • Use the service account key file to authenticate and verify that the service account has the correct permissions.

Solution

# Create a service account
gcloud iam service-accounts create my-service-account \
  --description="My service account" \
  --display-name="my-service-account"

# Assign the 'Storage Admin' role to the service account
gcloud projects add-iam-policy-binding [PROJECT_ID] \
  --member='serviceAccount:my-service-account@[PROJECT_ID].iam.gserviceaccount.com' \
  --role='roles/storage.admin'

Explanation:

  • my-service-account: The name of the service account.
  • [PROJECT_ID]: Replace with your GCP project ID.
  • roles/storage.admin: The role you are assigning to the service account.

Common Mistakes and Tips

  • Over-privileging: Avoid granting overly broad permissions. Use the principle of least privilege.
  • Not using groups: Use Google Groups to manage permissions for multiple users efficiently.
  • Neglecting service accounts: Ensure service accounts have only the necessary permissions and rotate keys regularly.

Conclusion

In this section, you learned about the fundamental concepts of IAM in GCP, including identities, roles, policies, and permissions. You also practiced assigning roles to users and service accounts using both the GCP Console and the gcloud CLI. Understanding and properly configuring IAM is crucial for maintaining the security and integrity of your GCP resources. In the next module, we will delve into networking and security, starting with VPC Networks.

© Copyright 2024. All rights reserved