-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add granular service accounts (#724)
* add granular service accounts * add test for granular roles for the SA * add Viewer role to CI account because the account is not owner of the projects * add roles/compute.xpnAdmin role at org level for 'compute.globalOperations.get' permission * add roles for hub and spoke transitivity * grant sensitive roles to networks service account only in hub and spoke projects * add note regarding project creator role for the service accounts * add sleep after bootstrap for API activation in seed project * add parent iam member module * update granular service account description * update terraform service account email in documentation * add output for service account id * fix terraform lint error * remove granular service account module Co-authored-by: Andrew Peabody <andrewpeabody@google.com>
- Loading branch information
1 parent
b2e8bfc
commit 4c84d80
Showing
21 changed files
with
425 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
locals { | ||
org_id = var.parent_type == "organization" ? var.parent_id : "" | ||
folder_id = var.parent_type == "folder" ? var.parent_id : "" | ||
} | ||
|
||
resource "google_organization_iam_member" "org_parent_iam" { | ||
for_each = toset(local.org_id != "" ? var.roles : []) | ||
|
||
org_id = local.org_id | ||
role = each.key | ||
member = var.member | ||
} | ||
|
||
resource "google_folder_iam_member" "folder_parent_iam" { | ||
for_each = toset(local.folder_id != "" ? var.roles : []) | ||
|
||
folder = local.folder_id | ||
role = each.key | ||
member = var.member | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
variable "parent_type" { | ||
description = "Type of the parent resource. valid values are `organization` and `folder`." | ||
type = string | ||
|
||
validation { | ||
condition = var.parent_type == "organization" || var.parent_type == "folder" | ||
error_message = "For parent_type only `organization` and `folder` are valid." | ||
} | ||
} | ||
|
||
variable "parent_id" { | ||
description = "ID of the parent resource." | ||
type = string | ||
} | ||
|
||
variable "member" { | ||
description = "Member to have the given roles in the parent resource. Prefix of `group:`, `user:` or `serviceAccount:` is required." | ||
type = string | ||
} | ||
|
||
variable "roles" { | ||
description = "Roles to grant to the member in the parent resource." | ||
type = list(string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
terraform { | ||
required_version = ">= 0.13" | ||
required_providers { | ||
google = { | ||
source = "hashicorp/google" | ||
version = ">= 3.77" | ||
} | ||
google-beta = { | ||
source = "hashicorp/google-beta" | ||
version = ">= 3.77" | ||
} | ||
random = { | ||
source = "hashicorp/random" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/** | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
locals { | ||
|
||
parent_type = var.parent_folder == "" ? "organization" : "folder" | ||
parent_id = var.parent_folder == "" ? var.org_id : var.parent_folder | ||
|
||
granular_sa = { | ||
"org" = "Foundation Organization SA. Managed by Terraform.", | ||
"env" = "Foundation Environment SA. Managed by Terraform.", | ||
"net" = "Foundation Network SA. Managed by Terraform.", | ||
"proj" = "Foundation Projects SA. Managed by Terraform.", | ||
} | ||
|
||
granular_sa_org_level_roles = { | ||
"org" = [ | ||
"roles/orgpolicy.policyAdmin", | ||
"roles/logging.configWriter", | ||
"roles/resourcemanager.organizationAdmin", | ||
"roles/securitycenter.notificationConfigEditor", | ||
"roles/resourcemanager.organizationViewer", | ||
"roles/accesscontextmanager.policyAdmin", | ||
], | ||
"net" = [ | ||
"roles/accesscontextmanager.policyAdmin", | ||
"roles/compute.xpnAdmin", | ||
], | ||
"proj" = [ | ||
"roles/accesscontextmanager.policyAdmin", | ||
"roles/serviceusage.serviceUsageConsumer" | ||
], | ||
} | ||
|
||
granular_sa_parent_level_roles = { | ||
"org" = [ | ||
"roles/resourcemanager.folderAdmin", | ||
], | ||
"env" = [ | ||
"roles/resourcemanager.folderAdmin" | ||
], | ||
"net" = [ | ||
"roles/resourcemanager.folderViewer", | ||
"roles/compute.networkAdmin", | ||
"roles/compute.securityAdmin", | ||
"roles/compute.orgSecurityPolicyAdmin", | ||
"roles/compute.orgSecurityResourceAdmin", | ||
"roles/dns.admin", | ||
], | ||
"proj" = [ | ||
"roles/resourcemanager.folderViewer", | ||
"roles/resourcemanager.folderIamAdmin", | ||
"roles/compute.networkAdmin", | ||
"roles/compute.xpnAdmin", | ||
], | ||
} | ||
} | ||
|
||
resource "google_service_account" "terraform-env-sa" { | ||
for_each = local.granular_sa | ||
|
||
project = module.seed_bootstrap.seed_project_id | ||
account_id = "terraform-${each.key}-sa" | ||
display_name = each.value | ||
} | ||
|
||
module "org_iam_member" { | ||
source = "./modules/parent-iam-member" | ||
for_each = local.granular_sa_org_level_roles | ||
|
||
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}" | ||
parent_type = "organization" | ||
parent_id = var.org_id | ||
roles = each.value | ||
} | ||
|
||
module "parent_iam_member" { | ||
source = "./modules/parent-iam-member" | ||
for_each = local.granular_sa_parent_level_roles | ||
|
||
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}" | ||
parent_type = local.parent_type | ||
parent_id = local.parent_id | ||
roles = each.value | ||
} | ||
|
||
resource "google_billing_account_iam_member" "tf_billing_user" { | ||
for_each = local.granular_sa | ||
|
||
billing_account_id = var.billing_account | ||
role = "roles/billing.user" | ||
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}" | ||
|
||
depends_on = [ | ||
google_service_account.terraform-env-sa | ||
] | ||
} | ||
|
||
resource "google_billing_account_iam_member" "billing_admin_user" { | ||
for_each = local.granular_sa | ||
|
||
billing_account_id = var.billing_account | ||
role = "roles/billing.admin" | ||
member = "serviceAccount:${google_service_account.terraform-env-sa[each.key].email}" | ||
|
||
depends_on = [ | ||
google_billing_account_iam_member.tf_billing_user | ||
] | ||
} | ||
|
||
resource "google_service_account_iam_member" "cloudbuild_terraform_sa_impersonate_permissions" { | ||
for_each = local.granular_sa | ||
|
||
service_account_id = google_service_account.terraform-env-sa[each.key].id | ||
role = "roles/iam.serviceAccountTokenCreator" | ||
member = "serviceAccount:${data.google_project.cloudbuild.number}@cloudbuild.gserviceaccount.com" | ||
|
||
depends_on = [ | ||
google_service_account.terraform-env-sa | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.