This repository was archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
docs(samples): add deny samples and tests #209
Merged
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
edc4134
docs(samples): init add deny samples and tests
Sita04 a21d50e
docs(samples): added requirements.txt
Sita04 7170f3b
docs(samples): minor update and refactoring
Sita04 a40c6fe
added nox files
Sita04 ded132e
added comments and minor refactoring
Sita04 2ef557f
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 1f500b8
added region tags
Sita04 5f63938
Merge branch 'deny-samples' of https://github.com/googleapis/python-i…
Sita04 f3c058d
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 91fa3cb
added region tags
Sita04 2d61e82
modified comments acc to review
Sita04 fe7805a
modified comments acc to review
Sita04 4714da2
updated env var
Sita04 ac45f12
Merge branch 'main' into deny-samples
nicain 83743d8
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 0ac4dc9
Merge branch 'main' into deny-samples
parthea 0b1bdf6
modified acc to review comments
Sita04 a21ead3
Merge remote-tracking branch 'origin/deny-samples' into deny-samples
Sita04 b58b1b4
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] f707ae1
modified acc to review comments
Sita04 6be97fd
Merge remote-tracking branch 'origin/deny-samples' into deny-samples
Sita04 c51313a
Merge branch 'main' into deny-samples
Sita04 69a06be
added init.py
Sita04 de2f2df
Merge remote-tracking branch 'origin/deny-samples' into deny-samples
Sita04 79f4ac3
updated acc to review comments
Sita04 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
Empty file.
This file contains hidden or 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,41 @@ | ||
# 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. | ||
|
||
import os | ||
import re | ||
import uuid | ||
|
||
from _pytest.capture import CaptureFixture | ||
import pytest | ||
|
||
from create_deny_policy import create_deny_policy | ||
from delete_deny_policy import delete_deny_policy | ||
|
||
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
GOOGLE_APPLICATION_CREDENTIALS = os.environ["GOOGLE_APPLICATION_CREDENTIALS"] | ||
|
||
|
||
@pytest.fixture | ||
def deny_policy(capsys: CaptureFixture) -> None: | ||
policy_id = f"limit-project-deletion-{uuid.uuid4()}" | ||
|
||
# Create the Deny policy. | ||
create_deny_policy(PROJECT_ID, policy_id) | ||
|
||
yield policy_id | ||
|
||
# Delete the Deny policy and assert if deleted. | ||
delete_deny_policy(PROJECT_ID, policy_id) | ||
out, _ = capsys.readouterr() | ||
assert re.search(f"Deleted the deny policy: {policy_id}", out) |
This file contains hidden or 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,119 @@ | ||
# 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. | ||
|
||
# This file contains code samples that demonstrate how to work with IAM client library's Deny feature. | ||
|
||
# [START iam_create_deny_policy] | ||
|
||
|
||
def create_deny_policy(project_id: str, policy_id: str) -> None: | ||
from google.cloud import iam_v2beta | ||
from google.cloud.iam_v2beta import types | ||
from google.type import expr_pb2 | ||
|
||
""" | ||
Create a deny policy. | ||
You can add deny policies to organizations, folders, and projects. | ||
Each of these resources can have up to 5 deny policies. | ||
|
||
Deny policies contain deny rules, which specify the following: | ||
1. The permissions to deny and/or exempt. | ||
2. The principals that are denied, or exempted from denial. | ||
3. An optional condition on when to enforce the deny rules. | ||
|
||
Params: | ||
project_id: ID or number of the Google Cloud project you want to use. | ||
policy_id: Specify the id of the Deny policy you want to create. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
policies_client = iam_v2beta.PoliciesClient() | ||
|
||
# Each deny policy is attached to an organization, folder, or project. | ||
# To work with deny policies, specify the attachment point. | ||
# | ||
# Its format can be one of the following: | ||
# 1. cloudresourcemanager.googleapis.com/organizations/ORG_ID | ||
# 2. cloudresourcemanager.googleapis.com/folders/FOLDER_ID | ||
# 3. cloudresourcemanager.googleapis.com/projects/PROJECT_ID | ||
# | ||
# The attachment point is identified by its URL-encoded resource name. Hence, replace | ||
# the "/" with "%2F". | ||
attachment_point = f"cloudresourcemanager.googleapis.com%2Fprojects%2F{project_id}" | ||
|
||
deny_rule = types.DenyRule() | ||
# Add one or more principals who should be denied the permissions specified in this rule. | ||
# For more information on allowed values, see: https://cloud.google.com/iam/docs/principal-identifiers | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
deny_rule.denied_principals = ["principalSet://goog/public:all"] | ||
|
||
# Optionally, set the principals who should be exempted from the | ||
# list of denied principals. For example, if you want to deny certain permissions | ||
# to a group but exempt a few principals, then add those here. | ||
# deny_rule.exception_principals = ["principalSet://goog/group/[email protected]"] | ||
|
||
# Set the permissions to deny. | ||
# The permission value is of the format: service_fqdn/resource.action | ||
# For the list of supported permissions, see: https://cloud.google.com/iam/help/deny/supported-permissions | ||
deny_rule.denied_permissions = [ | ||
"cloudresourcemanager.googleapis.com/projects.delete" | ||
] | ||
|
||
# Optionally, add the permissions to be exempted from this rule. | ||
# Meaning, the deny rule will not be applicable to these permissions. | ||
# deny_rule.exception_permissions = ["cloudresourcemanager.googleapis.com/projects.create"] | ||
|
||
# Set the condition which will enforce the deny rule. | ||
# If this condition is true, the deny rule will be applicable. Else, the rule will not be enforced. | ||
# The expression uses Common Expression Language syntax (CEL). | ||
# Here we block access based on tags. | ||
# | ||
# A tag is a key-value pair that can be attached to an organization, folder, or project. You can use deny policies to deny permissions based on tags without adding an IAM Condition to every role grant. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
# For example, imagine that you tag all of your projects as dev, test, or prod. You want only members of [email protected] to be able to perform operations on projects that are tagged prod. | ||
# To solve this problem, you create a deny rule that denies the cloudresourcemanager.googleapis.com/projects.delete permission to everyone except [email protected] for resources that are tagged test. | ||
deny_rule.denial_condition = expr_pb2.Expr( | ||
expression="!resource.matchTag('12345678/env', 'test')" | ||
) | ||
|
||
# Add the deny rule and a description for it. | ||
policy_rule = types.PolicyRule() | ||
policy_rule.description = "block all principals from deleting projects, unless the principal is a member of [email protected] and the project being deleted has a tag with the value test" | ||
policy_rule.deny_rule = deny_rule | ||
|
||
policy = types.Policy() | ||
policy.display_name = "Restrict project deletion access" | ||
policy.rules = [policy_rule] | ||
|
||
# Set the policy resource path, policy rules and a unique ID for the policy. | ||
request = types.CreatePolicyRequest() | ||
# Construct the full path of the policy. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
# Its format is: "policies/{attachmentPoint}/denypolicies/{policyId}" | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
request.parent = f"policies/{attachment_point}/denypolicies" | ||
request.policy = policy | ||
request.policy_id = policy_id | ||
|
||
# Build the create policy request. | ||
policies_client.create_policy(request=request) | ||
print(f"Created the deny policy: {policy_id}") | ||
|
||
|
||
if __name__ == "__main__": | ||
import uuid | ||
|
||
# Your Google Cloud project id. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
project_id = "your-google-cloud-project-id" | ||
# Any unique id (0 to 63 chars) starting with a lowercase alphabet. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
policy_id = f"deny-{uuid.uuid4()}" | ||
|
||
# Test the policy lifecycle. | ||
create_deny_policy(project_id, policy_id) | ||
|
||
# [END iam_create_deny_policy] |
This file contains hidden or 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,62 @@ | ||
# 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. | ||
|
||
# This file contains code samples that demonstrate how to work with IAM client library's Deny feature. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# [START iam_delete_deny_policy] | ||
def delete_deny_policy(project_id: str, policy_id: str) -> None: | ||
from google.cloud import iam_v2beta | ||
from google.cloud.iam_v2beta import types | ||
|
||
""" | ||
Delete the policy if you no longer want to enforce the rules in a deny policy. | ||
|
||
project_id: ID or number of the Google Cloud project you want to use. | ||
policy_id: Specify the ID of the deny policy you want to retrieve. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
policies_client = iam_v2beta.PoliciesClient() | ||
|
||
# Each deny policy is attached to an organization, folder, or project. | ||
# To work with deny policies, specify the attachment point. | ||
# | ||
# Its format can be one of the following: | ||
# 1. cloudresourcemanager.googleapis.com/organizations/ORG_ID | ||
# 2. cloudresourcemanager.googleapis.com/folders/FOLDER_ID | ||
# 3. cloudresourcemanager.googleapis.com/projects/PROJECT_ID | ||
# | ||
# The attachment point is identified by its URL-encoded resource name. Hence, replace | ||
# the "/" with "%2F". | ||
attachment_point = f"cloudresourcemanager.googleapis.com%2Fprojects%2F{project_id}" | ||
|
||
request = types.DeletePolicyRequest() | ||
# Construct the full path of the policy. | ||
# Its format is: "policies/{attachmentPoint}/denypolicies/{policyId}" | ||
request.name = f"policies/{attachment_point}/denypolicies/{policy_id}" | ||
|
||
# Create the DeletePolicy request. | ||
policies_client.delete_policy(request=request) | ||
print(f"Deleted the deny policy: {policy_id}") | ||
|
||
|
||
if __name__ == "__main__": | ||
import uuid | ||
|
||
# Your Google Cloud project id. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
project_id = "your-google-cloud-project-id" | ||
# Any unique id (0 to 63 chars) starting with a lowercase alphabet. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
policy_id = f"deny-{uuid.uuid4()}" | ||
|
||
delete_deny_policy(project_id, policy_id) | ||
|
||
# [END iam_delete_deny_policy] |
This file contains hidden or 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,63 @@ | ||
# 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. | ||
|
||
# This file contains code samples that demonstrate how to work with IAM client library's Deny feature. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# [START iam_get_deny_policy] | ||
def get_deny_policy(project_id: str, policy_id: str): | ||
from google.cloud import iam_v2beta | ||
from google.cloud.iam_v2beta import Policy, types | ||
|
||
""" | ||
Retrieve the deny policy given the project ID and policy ID. | ||
|
||
project_id: ID or number of the Google Cloud project you want to use. | ||
policy_id: Specify the ID of the deny policy you want to retrieve. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
policies_client = iam_v2beta.PoliciesClient() | ||
|
||
# Each deny policy is attached to an organization, folder, or project. | ||
# To work with deny policies, specify the attachment point. | ||
# | ||
# Its format can be one of the following: | ||
# 1. cloudresourcemanager.googleapis.com/organizations/ORG_ID | ||
# 2. cloudresourcemanager.googleapis.com/folders/FOLDER_ID | ||
# 3. cloudresourcemanager.googleapis.com/projects/PROJECT_ID | ||
# | ||
# The attachment point is identified by its URL-encoded resource name. Hence, replace | ||
# the "/" with "%2F". | ||
attachment_point = f"cloudresourcemanager.googleapis.com%2Fprojects%2F{project_id}" | ||
|
||
request = types.GetPolicyRequest() | ||
# Construct the full path of the policy. | ||
# Its format is: "policies/{attachmentPoint}/denypolicies/{policyId}" | ||
request.name = f"policies/{attachment_point}/denypolicies/{policy_id}" | ||
|
||
# Execute the GetPolicy request. | ||
policy = policies_client.get_policy(request=request) | ||
print(f"Retrieved the deny policy: {policy_id} : {policy}") | ||
return policy | ||
|
||
|
||
if __name__ == "__main__": | ||
import uuid | ||
|
||
# Your Google Cloud project id. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
project_id = "your-google-cloud-project-id" | ||
# Any unique id (0 to 63 chars) starting with a lowercase alphabet. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
policy_id = f"deny-{uuid.uuid4()}" | ||
|
||
policy = get_deny_policy(project_id, policy_id) | ||
|
||
# [END iam_get_deny_policy] |
This file contains hidden or 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,65 @@ | ||
# 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. | ||
|
||
# This file contains code samples that demonstrate how to work with IAM client library's Deny feature. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# [START iam_list_deny_policy] | ||
def list_deny_policy(project_id: str) -> None: | ||
from google.cloud import iam_v2beta | ||
from google.cloud.iam_v2beta import types | ||
|
||
""" | ||
List all the deny policies that are attached to a resource. | ||
A resource can have up to 5 deny policies. | ||
|
||
project_id: ID or number of the Google Cloud project you want to use. | ||
""" | ||
policies_client = iam_v2beta.PoliciesClient() | ||
|
||
# Each deny policy is attached to an organization, folder, or project. | ||
# To work with deny policies, specify the attachment point. | ||
# | ||
# Its format can be one of the following: | ||
# 1. cloudresourcemanager.googleapis.com/organizations/ORG_ID | ||
# 2. cloudresourcemanager.googleapis.com/folders/FOLDER_ID | ||
# 3. cloudresourcemanager.googleapis.com/projects/PROJECT_ID | ||
# | ||
# The attachment point is identified by its URL-encoded resource name. Hence, replace | ||
# the "/" with "%2F". | ||
attachment_point = f"cloudresourcemanager.googleapis.com%2Fprojects%2F{project_id}" | ||
|
||
request = types.ListPoliciesRequest() | ||
# Construct the full path of the resource to which the policy is attached. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
# Its format is: "policies/{attachmentPoint}/denypolicies" | ||
request.parent = f"policies/{attachment_point}/denypolicies" | ||
|
||
# Create a list request and iterate over the returned policies. | ||
policies = policies_client.list_policies(request=request) | ||
|
||
for policy in policies: | ||
print(policy.name) | ||
print("Listed all deny policies") | ||
|
||
|
||
if __name__ == "__main__": | ||
import uuid | ||
|
||
# Your Google Cloud project id. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
project_id = "your-google-cloud-project-id" | ||
# Any unique id (0 to 63 chars) starting with a lowercase alphabet. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
policy_id = f"deny-{uuid.uuid4()}" | ||
|
||
list_deny_policy(project_id) | ||
|
||
# [END iam_list_deny_policy] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.