-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explicitly address org policy requirements (Closes #158) #171
Open
bbhuston
wants to merge
4
commits into
main
Choose a base branch
from
issue/GH-158
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
24df96f
Add support for overriding default GCP org policies
bbhuston 38bd3c3
Add checks to guarantee resource creation order
bbhuston 717d551
Merge branch 'main' of github.com:googleforgames/global-multiplayer-d…
bbhuston c728f45
Add troubleshooting README
bbhuston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Troubleshooting Org Policy Issues | ||
|
||
Have you encountered any error messages related to `compute.requireShieldedVm`, `iam.disableServiceAccountKeyCreation`, `compute.vmCanIpForward`, or `compute.vmExternalIpAccess` when following the instructions in this repo's [quickstart guide](https://github.com/googleforgames/global-multiplayer-demo/blob/main/README.md)? If so, please try the steps outlined in this README to update your GCP Org Policies in a manner that meets this demo's minimum requirements. | ||
|
||
**BELOW:** Example error arising from a conflict with default Org Policy settings. | ||
```shell | ||
│ Error: Error creating service account key: googleapi: Error 400: Key creation is not allowed on this service account. | ||
│ Details: | ||
│ [ | ||
│ { | ||
│ "@type": "type.googleapis.com/google.rpc.PreconditionFailure", | ||
│ "violations": [ | ||
│ { | ||
│ "description": "Key creation is not allowed on this service account.", | ||
│ "subject": "projects/gameops-001/serviceAccounts/[email protected]?configvalue=allocation-endpoint-esp-sa%40gameops-001.iam.gserviceaccount.com", | ||
│ "type": "constraints/iam.disableServiceAccountKeyCreation" | ||
│ } | ||
│ ] | ||
│ } | ||
│ ] | ||
│ , failedPrecondition | ||
│ | ||
│ with google_service_account_key.ae_sa_key, | ||
│ on allocation-endpoint.tf line 54, in resource "google_service_account_key" "ae_sa_key": | ||
│ 54: resource "google_service_account_key" "ae_sa_key" { | ||
│ omitted... | ||
``` | ||
### Double-check the prerequisites | ||
|
||
Please read this repo's [primary README](https://github.com/googleforgames/global-multiplayer-demo/blob/main/README.md) and ensure that all its main prerequisites have been met, such as creating a GCP Project | ||
|
||
### Set up your environment | ||
|
||
Navigate to this repo's `infrastructure/org-policies` subdirectory and then please run the following commands. | ||
|
||
```shell | ||
# Set your GCP Project ID | ||
export PROJECT_ID=<PROJECT_ID> | ||
gcloud config set project ${PROJECT_ID} | ||
|
||
# Add this value to your default tfvars file | ||
sed -i 's/PROJECT_ID/${PROJECT_ID}/g' terraform.tfvars.sample | ||
cp terraform.tfvars.sample terraform.tfvar | ||
|
||
# Set your GCP Organization ID | ||
export ORGANIZATION_ID=<ORGANIZATION_ID> | ||
|
||
# Enter the email address that you use to authenticate with GCP | ||
export GCP_EMAIL_ADRESS=<GCP_EMAIL_ADRESS> | ||
``` | ||
|
||
### Authenticate your user identity and generate Application Default Credentials (ADC) | ||
|
||
Run the following command to simultanously authenticate you as an end user with Google Cloud and to obtain a set of Application Default Credentials. Later on in this README, Terraform will perform a `user_project_override` and leverage your user identity to provision GCP org policies. This is neccessary because org policies are a protected resource that (usually) cannot be edited with standard ADC credentials. | ||
|
||
```shell | ||
gcloud auth login --no-launch-browser --brief --update-adc --quiet | ||
``` | ||
|
||
### Update any missing org-level GCP IAM permissions | ||
|
||
If you are already a super user in your GCP organization, run the following commands to ensure that you have the IAM permissions neccessary to edit GCP Org Policy resources. | ||
|
||
```shell | ||
gcloud organizations add-iam-policy-binding ${ORGANIZATION_ID} --condition=None --member="user:${GCP_EMAIL_ADRESS}" --role="roles/orgpolicy.PolicyAdmin" | ||
``` | ||
|
||
If you do not have such elevated permissions, please share this README with your platform administrator and request that they either (1) grant you this IAM permission, or (2) run the provisioning steps in this guide on your behalf. | ||
|
||
### Apply project-scoped overrides to your default GCP org policies | ||
|
||
Now that you are authenticated and have the neccessary IAM permissions in place, run the following commands to create a few project-scoped overrides to your default org policies. | ||
|
||
```shell | ||
terraform init | ||
terraform apply | ||
``` | ||
More specifically, this step will edit the four [organization policy constraints](https://cloud.google.com/resource-manager/docs/organization-policy/org-policy-constraints) listed below. | ||
|
||
- `compute.requireShieldedVm` | ||
- `iam.disableServiceAccountKeyCreation` | ||
- `compute.vmCanIpForward` | ||
- `compute.vmExternalIpAccess` | ||
|
||
### Final Step | ||
|
||
Once you have completed this README, return to this repo's [quickstart guide](https://github.com/googleforgames/global-multiplayer-demo/blob/main/README.md) and try running through its instructions once again. With any luck, your org policy issues will be resolved and you will be able to successfully finish provisioning the demo. | ||
|
||
**BELOW:** Example output shown when Terraform successfully updates the organization policy constraints. | ||
```shell | ||
...omitted | ||
google_project_service.project["cloudresourcemanager.googleapis.com"]: Creation complete after 7s [id=planet-scale-demo-044/cloudresourcemanager.googleapis.com] | ||
module.gcp_org_policy_v2_vmCanIpForward.google_org_policy_policy.project_policy[0]: Creating... | ||
module.gcp_org_policy_v2_disableServiceAccountKeyCreation.google_org_policy_policy.project_policy_boolean[0]: Creating... | ||
module.gcp_org_policy_v2_vmExternalIpAccess.google_org_policy_policy.project_policy[0]: Creating... | ||
module.gcp_org_policy_v2_requireShieldedVm.google_org_policy_policy.project_policy_boolean[0]: Creating... | ||
module.gcp_org_policy_v2_vmCanIpForward.google_org_policy_policy.project_policy[0]: Creation complete after 3s [id=projects/planet-scale-demo-044/policies/compute.vmCanIpForward] | ||
module.gcp_org_policy_v2_vmExternalIpAccess.google_org_policy_policy.project_policy[0]: Creation complete after 3s [id=projects/planet-scale-demo-044/policies/compute.vmExternalIpAccess] | ||
module.gcp_org_policy_v2_requireShieldedVm.google_org_policy_policy.project_policy_boolean[0]: Creation complete after 4s [id=projects/planet-scale-demo-044/policies/compute.requireShieldedVm] | ||
module.gcp_org_policy_v2_disableServiceAccountKeyCreation.google_org_policy_policy.project_policy_boolean[0]: Creation complete after 4s [id=projects/planet-scale-demo-044/policies/iam.disableServiceAccountKeyCreation] | ||
Apply complete! Resources: 6 added, 0 changed, 0 destroyed. | ||
``` | ||
|
||
## Licence | ||
|
||
Apache 2.0 | ||
|
||
This is not an officially supported Google product |
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,24 @@ | ||
# Copyright 2023 Google LLC All Rights Reserved. | ||
# | ||
# 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_providers { | ||
google = { | ||
source = "hashicorp/google" | ||
version = "4.56.0" | ||
} | ||
} | ||
|
||
required_version = ">= 1.3" | ||
} |
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,86 @@ | ||
// Copyright 2023 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 | ||
// | ||
// https://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. | ||
|
||
|
||
module "gcp_org_policy_v2_requireShieldedVm" { | ||
source = "terraform-google-modules/org-policy/google//modules/org_policy_v2" | ||
version = "~> 5.2.0" | ||
|
||
policy_root = "project" | ||
policy_root_id = var.project | ||
rules = [{ | ||
enforcement = false | ||
allow = [] | ||
deny = [] | ||
conditions = [] | ||
}] | ||
constraint = "compute.requireShieldedVm" | ||
policy_type = "boolean" | ||
|
||
depends_on = [google_project_service.project] | ||
} | ||
|
||
module "gcp_org_policy_v2_disableServiceAccountKeyCreation" { | ||
source = "terraform-google-modules/org-policy/google//modules/org_policy_v2" | ||
version = "~> 5.2.0" | ||
|
||
policy_root = "project" | ||
policy_root_id = var.project | ||
rules = [{ | ||
enforcement = false | ||
allow = [] | ||
deny = [] | ||
conditions = [] | ||
}] | ||
constraint = "iam.disableServiceAccountKeyCreation" | ||
policy_type = "boolean" | ||
|
||
depends_on = [google_project_service.project] | ||
} | ||
|
||
module "gcp_org_policy_v2_vmCanIpForward" { | ||
source = "terraform-google-modules/org-policy/google//modules/org_policy_v2" | ||
version = "~> 5.2.0" | ||
|
||
policy_root = "project" | ||
policy_root_id = var.project | ||
rules = [{ | ||
enforcement = false | ||
allow = [] | ||
deny = [] | ||
conditions = [] | ||
}] | ||
constraint = "compute.vmCanIpForward" | ||
policy_type = "list" | ||
|
||
depends_on = [google_project_service.project] | ||
} | ||
|
||
module "gcp_org_policy_v2_vmExternalIpAccess" { | ||
source = "terraform-google-modules/org-policy/google//modules/org_policy_v2" | ||
version = "~> 5.2.0" | ||
|
||
policy_root = "project" | ||
policy_root_id = var.project | ||
rules = [{ | ||
enforcement = false | ||
allow = [] | ||
deny = [] | ||
conditions = [] | ||
}] | ||
constraint = "compute.vmExternalIpAccess" | ||
policy_type = "list" | ||
|
||
depends_on = [google_project_service.project] | ||
} |
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,37 @@ | ||
# Copyright 2023 Google LLC All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
resource "google_project_service" "project" { | ||
project = var.project | ||
for_each = toset(var.gcp_project_services) | ||
service = each.value | ||
|
||
timeouts { | ||
create = "30m" | ||
update = "40m" | ||
} | ||
|
||
# Ensure service is truly active before continuing onward | ||
provisioner "local-exec" { | ||
markmandel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
command = <<EOF | ||
while [ ! $(gcloud services list --project=${var.project} | grep ${each.value} | wc -l ) ]; | ||
do | ||
sleep 1s | ||
done | ||
EOF | ||
} | ||
|
||
disable_dependent_services = false | ||
disable_on_destroy = false | ||
} |
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,23 @@ | ||
# Copyright 2023 Google LLC All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
provider "google" { | ||
project = var.project | ||
user_project_override = true | ||
} | ||
|
||
provider "google-beta" { | ||
project = var.project | ||
user_project_override = true | ||
} |
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,24 @@ | ||
# Copyright 2023 Google LLC All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
|
||
# Replace PROJECT_ID value shown below with your specific GCP Project ID. | ||
# This the Project ID that Org Policy changes will be applied to. | ||
project = "PROJECT_ID" | ||
|
||
# GCP APIs to enable | ||
gcp_project_services = [ | ||
"cloudresourcemanager.googleapis.com", | ||
"orgpolicy.googleapis.com" | ||
] |
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,26 @@ | ||
# Copyright 2023 Google LLC All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
# Project Variables | ||
|
||
variable "project" { | ||
type = string | ||
description = "GCP Project ID" | ||
} | ||
|
||
variable "gcp_project_services" { | ||
type = list(any) | ||
description = "GCP Service APIs (<api>.googleapis.com) to enable for this project" | ||
default = [] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how far down the rabbit hole we want to go to make this super versatile and generalized. But do we want to take in scenarios that people would use this at a
organization
,folder
orproject
.Today is the infrastructure all in one project?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
today, I think it would be good to have this as a variable and default it to
project
. We would probably have to make all sorts of considerations though with the other TF files if we start allowing different policy roots