Manage Database Access Control with Terraform
This tutorial is part of the Manage Bytebase with Terraform series:
- Part 1: Manage Databases with Terraform - Set up instances and environments
- Part 2: Manage Projects with Terraform - Organize databases into projects
- Part 3: Manage Bytebase Settings with Terraform - Configure workspace settings, environment policies, approval flows, and risk management
- Part 4: Manage SQL Review Rules with Terraform - Set up SQL review policies
- Part 5: Manage Database Access Control with Terraform (This one) - Set up access controls and permissions
- Part 6: Manage Data Masking with Terraform - Configure data masking policies
๐ Complete tutorial terraform files on GitHub
File Structure: This tutorial series uses separate Terraform files for better organization. Files are numbered by tutorial part (e.g., 1-instances.tf
for Part 1, 2-projects.tf
for Part 2, etc.). Terraform automatically handles dependencies between files.
Learn how to manage users, groups, and access control policies using Terraform and the Terraform Bytebase Provider.
Bytebase Terraform Provider handles control plane configuration such as settings, policies, access controls. It does not handle data plane operations such as database creation, schema migrations, DML execution, query.
What Youโll Learn
- Create users and service accounts for team members
- Organize users into groups for easier management
- Configure workspace-level IAM policies for role assignments
- Set up project-level permissions with granular database access
Prerequisites
Before starting this tutorial, ensure you have:
- Completed Part 4: Manage SQL Review Rules with Terraform
- Bytebase running with ngrok and service account configured
- Your Terraform files from the previous tutorials
Setup
From the previous tutorials, you should have:
- Bytebase instances and projects configured
- Workspace settings and approval flows set up
- Service account with Workspace Admin role
Configure Access Control
Step 1 - Create Users
Create 5-user-iam.tf
with user resources:
Apply and Verify Users
Verify in Bytebase:
- Go to IAM & Admin > Users & Groups
- You should see all 9 users created
Step 2 - Create Groups
Add the following groups to your 5-user-iam.tf
file:
Apply and Verify Groups
Verify in Bytebase:
- Go to IAM & Admin > Users & Groups > Groups tab
- Click on Developer Team to see 3 members (dev1 as owner, dev2 and dev3 as members)
- Click on QA Team to see 2 members (qa1 as owner, qa2 as member)
Step 3 - Configure Workspace IAM Policy
Add the following workspace IAM policy to your 5-user-iam.tf
file. Note that project roles assigned at workspace level apply to ALL projects:
Apply and Verify Workspace IAM
Verify in Bytebase:
- Go to IAM & Admin > Members
- Check user roles:
- admin@example.com: Workspace Admin
- tf@service.bytebase.com: Workspace Admin
- dba@example.com, dba2@example.com: Workspace DBA
- dev1-3@example.com, qa1-2@example.com: Workspace Member
- Note that QA Team group has Project Viewer role for ALL projects
Step 4 - Configure Project IAM Policy
Add the following project IAM policy to your 5-user-iam.tf
file. These roles only apply to Project Two
, however, the project roles defined in workspace level will show in both Project One
and Project Two
:
Apply and Verify Project IAM
Verify in Bytebase:
-
Go to Project One > Manage > Members:
- QA Team: Project Viewer (inherited from workspace)
- Terraform Service Account: Project Owner (as creator)
-
Go to Project Two > Manage > Members:
- QA Team: Project Viewer (inherited from workspace)
- DBA users: Project Owner
- All users + Developer Team: Project Developer
- Developer Team: SQL Editor User (click edit to see table restrictions)
Step 5 - Test Access Control
Test the access control configuration:
-
Set a password for Developer 1:
- Go to IAM & Admin > Users & Groups
- Find and click on Developer 1
- Click Edit and set a password
-
Log in as Developer 1 in a new browser/incognito window:
- You should only see Project Two (not Project One)
- Navigate to SQL Editor
-
Test SQL Editor restrictions:
- Connect to
Prod > Prod Sample Instance > hr_prod
- Try querying
employee
table - โ Should work - Try querying
department
table - โ Should work - Try querying
salary
table - โ Should fail with permission error
- Connect to
Access Control Patterns
The example demonstrates several key patterns:
- User Types: Regular users (
USER
) and service accounts (SERVICE_ACCOUNT
) - Group Management: Groups with owners and members for team organization
- Workspace Roles: Admin, DBA, and Member roles at workspace level
- Project Roles: Owner and Developer roles at project level
- Conditional Access: Time-limited, table-specific SQL Editor permissions
Key Points
- IAM Hierarchy: Workspace policies apply globally, project policies are scoped to specific projects
- Project Roles at Workspace Level: When assigned in workspace IAM, project roles (like
projectViewer
) apply to ALL projects - Group Benefits: Manage permissions for teams rather than individuals
- Conditional Bindings: Fine-grained access control down to table level with expiration
- Role Inheritance: Higher roles include permissions of lower roles
Next Steps
Continue the series: Manage Data Masking with Terraform - configure data masking policies to protect sensitive data.
Resources: