Identity and Access Management (IAM)
# CHAPTER 3
Identity and Access Management (IAM)
1. Introduction
Security in the cloud does not start with firewalls; it starts with Identity. If a malicious actor gains access to an administrator's credentials, firewalls are useless because the attacker simply turns them off. Identity and Access Management (IAM) is the central nervous system of GCP security. It dictates *Who* can do *What* on *Which* resource. In this chapter, we will master the Principle of Least Privilege, explore the relationship between Members and Roles, and introduce Service Accounts.2. Learning Objectives
By the end of this chapter, you will be able to:- Define the three pillars of IAM: Principals, Roles, and Policies.
- Differentiate between Basic, Predefined, and Custom roles.
- Understand and enforce the Principle of Least Privilege.
- Define what a Service Account is and why it is critical for automation.
- Assign IAM permissions within the GCP Console.
3. Beginner-Friendly Explanation
Imagine a high-security office building.-
The Principal (Who): The employee holding an ID badge (e.g.,
alice@company.com).
- The Role (What): A title stamped on the badge, like "Janitor" or "CEO".
- The Policy (Which Resource): The building's rulebook. It says: "People with the Janitor role are allowed to open the Supply Closet. They are NOT allowed to open the Server Room."
In GCP, you never give permission directly to a person. You give a person a Role. The Role contains the permissions.
4. The Three Types of Roles
When assigning permissions, you have three categories of Roles:-
1.
Basic Roles (Avoid these): The original, legacy roles (
Viewer,Editor,Owner). They are dangerously broad. If you make someone anEditor, they can edit *everything* in the entire project.
-
2.
Predefined Roles (Use these): Created and maintained by Google. They are highly specific. Example:
Compute Instance Admin(Can create/delete Virtual Machines, but cannot touch databases).
- 3. Custom Roles: You create these yourself by hand-picking exact API permissions (e.g., "Can view VMs, but cannot delete them"). Used only when Predefined roles don't perfectly fit.
5. Service Accounts (Software Identity)
Humans use Gmail addresses (user@gmail.com) to log in. But what if a Python script running on a server needs to upload a file to Cloud Storage? A script cannot type in a password or complete 2-Factor Authentication!
A Service Account is a special email address (e.g., my-app@my-project.iam.gserviceaccount.com) created exclusively for software. You give the Service Account a Role (e.g., Storage Object Creator), attach the account to your Virtual Machine, and the Python script inherits those permissions automatically, with no passwords required!
6. The Principle of Least Privilege
The golden rule of Cloud Security: Give a user the exact minimum permissions they need to do their job, and absolutely nothing more. If a junior developer only needs to view logs to debug an app, give them theLogs Viewer role. Do not give them the Editor role just because it's faster to assign. If their laptop is hacked, Least Privilege prevents the hacker from destroying the database.
7. Mini Project: Assign Secure IAM Roles
Let's practice granting restricted access.Step-by-Step Tutorial:
- 1. Open the GCP Console and navigate to IAM & Admin > IAM.
- 2. Click the + GRANT ACCESS button at the top.
- 3. In the New principals box, type the email address of a friend or a secondary Google account you own.
- 4. In the Select a role dropdown, do NOT choose a Basic role. Instead, search for and select Compute Network Viewer.
- 5. Click Save.
- 6. *The Result:* That user can now log into your GCP project. They can look at your VPC networks and IP addresses, but if they try to look at your Virtual Machines or Databases, the console will present them with a massive "PERMISSION DENIED" error. Security in action!
8. Real-World Scenarios
A company hired a freelance web developer to upload HTML files to a Cloud Storage bucket. The lazy IT admin gave the freelancer theProject Editor role. The freelancer's email account was breached by a hacker. Because the hacker had Editor access, they bypassed the storage bucket entirely, spun up 50 massive Virtual Machines in the project, and started mining Bitcoin, costing the company $20,000 in 48 hours. Had the admin used Least Privilege and assigned the Storage Object Creator role, the hacker could only have uploaded files, preventing the Bitcoin mining disaster.
9. Best Practices
-
IAM Conditions: You can attach time-based conditions to Roles. If a contractor needs access to a database to perform a weekend migration, use IAM Conditions to grant them the
Database Adminrole, but explicitly set it to automatically expire on Monday morning at 8:00 AM.
10. Security Tips
- Never Download Service Account Keys: While GCP allows you to download Service Account credentials as a physical JSON file to your laptop, you should avoid this at all costs. If you accidentally commit that JSON file to GitHub, hackers will find it in seconds. Always use native IAM binding or Workload Identity Federation instead of downloading keys.
11. CLI Examples
To view the IAM policy of your current project (who has access to what):To create a new Service Account via CLI:
12. Exercises
-
1.
Explain why assigning the Basic
Editorrole violates the Principle of Least Privilege.
- 2. What is the fundamental difference between a User Account and a Service Account?
13. FAQs
Q: I gave a user permission, but they still can't see the resource. Why? A: IAM permissions can take 60 to 120 seconds to propagate globally across Google's servers. Tell the user to wait two minutes and hard-refresh their browser.14. Interview Questions
- Q: Define the Principle of Least Privilege. Provide a specific scenario demonstrating how violating this principle within GCP IAM could lead to a catastrophic data breach.
- Q: Explain the architectural security benefits of attaching a Service Account directly to a Compute Engine instance, rather than embedding long-lived Service Account JSON keys within application source code.