This repository was archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
docs(samples): add iam deny samples and test #371
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5bb15bb
docs(samples): init add iam deny samples and test
Sita04 621d1bc
docs(samples): added comments
Sita04 fa49e81
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] f839a97
docs(samples): add pom.xml and removed jupiter dependency
Sita04 3e5f528
minor lro update and refactoring
Sita04 f1c6fac
added comments and minor refactoring
Sita04 baef573
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] bb57add
added region tags
Sita04 2d98a05
Merge remote-tracking branch 'origin/deny-samples' into deny-samples
Sita04 48943de
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] edc9e85
updated acc to review comments
Sita04 d24fdb4
Merge remote-tracking branch 'origin/deny-samples' into deny-samples
Sita04 405da44
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 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
Next
Next commit
docs(samples): init add iam deny samples and test
- Loading branch information
commit 5bb15bbfd6ef4695ee9b048d662ae1b1e0df0cf6
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
samples/cloud-client/snippets/src/main/java/CreateDenyPolicy.java
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,84 @@ | ||
/* | ||
* 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 com.google.iam.v2beta.CreatePolicyRequest; | ||
import com.google.iam.v2beta.DenyRule; | ||
import com.google.iam.v2beta.PoliciesClient; | ||
import com.google.iam.v2beta.Policy; | ||
import com.google.iam.v2beta.PolicyRule; | ||
import com.google.type.Expr; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class CreateDenyPolicy { | ||
|
||
public static void main(String[] args) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
// ID or number of the Google Cloud project you want to use. | ||
String projectId = "your-google-cloud-project-id"; | ||
|
||
// Specify the name of the Deny policy you want to retrieve. | ||
String policyName = "deny-policy-name"; | ||
|
||
createDenyPolicy(projectId, policyName); | ||
} | ||
|
||
public static void createDenyPolicy(String projectId, String policyName) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
|
||
try (PoliciesClient policiesClient = PoliciesClient.create()) { | ||
String attachmentPoint = | ||
String.format("cloudresourcemanager.googleapis.com/projects/%s", projectId) | ||
.replaceAll("/", "%2F"); | ||
|
||
String policyParent = String.format( | ||
"policies/%s/denypolicies", attachmentPoint); | ||
|
||
Policy policy = Policy.newBuilder() | ||
.setName(policyName) | ||
.setDisplayName("something") | ||
.addRules(PolicyRule.newBuilder() | ||
.setDescription( | ||
"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") | ||
.setDenyRule(DenyRule.newBuilder() | ||
.addDeniedPrincipals("principalSet://goog/public:all") | ||
.addExceptionPrincipals("principalSet://goog/group/[email protected]") | ||
.addDeniedPermissions("cloudresourcemanager.googleapis.com/projects.delete") | ||
.addExceptionPermissions("iam.googleapis.com/roles.list") | ||
.setDenialCondition(Expr.newBuilder() | ||
.setExpression("!resource.matchTag('12345678/env', 'test')") | ||
.setTitle("Only for non-test projects") | ||
.build()) | ||
.build()) | ||
.build()) | ||
.build(); | ||
|
||
CreatePolicyRequest createPolicyRequest = CreatePolicyRequest.newBuilder() | ||
.setParent(policyParent) | ||
.setPolicy(policy) | ||
.setPolicyId("deny-" + UUID.randomUUID()) | ||
.build(); | ||
|
||
Policy response = policiesClient.createPolicyAsync(createPolicyRequest) | ||
.get(3, TimeUnit.MINUTES); | ||
System.out.println(response.getName()); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
samples/cloud-client/snippets/src/main/java/DeleteDenyPolicy.java
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,60 @@ | ||
/* | ||
* 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 com.google.iam.v2beta.DeletePolicyRequest; | ||
import com.google.iam.v2beta.PoliciesClient; | ||
import com.google.iam.v2beta.Policy; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class DeleteDenyPolicy { | ||
|
||
public static void main(String[] args) | ||
throws IOException, InterruptedException, ExecutionException, TimeoutException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
|
||
// ID or number of the Google Cloud project you want to use. | ||
String projectId = "your-google-cloud-project-id"; | ||
|
||
// Specify the name of the Deny policy you want to delete. | ||
String policyName = "deny-policy-name"; | ||
|
||
deleteDenyPolicy(projectId, policyName); | ||
} | ||
|
||
public static void deleteDenyPolicy(String projectId, String policyName) | ||
throws IOException, InterruptedException, ExecutionException, TimeoutException { | ||
try (PoliciesClient policiesClient = PoliciesClient.create()) { | ||
|
||
String attachmentPoint = | ||
String.format("cloudresourcemanager.googleapis.com/projects/%s", projectId) | ||
.replaceAll("/", "%2F"); | ||
|
||
String policyParent = String.format( | ||
"policies/%s/denypolicies/%s", attachmentPoint, policyName); | ||
|
||
DeletePolicyRequest deletePolicyRequest = DeletePolicyRequest.newBuilder() | ||
.setName(policyParent) | ||
.build(); | ||
|
||
Policy policy = policiesClient.deletePolicyAsync(deletePolicyRequest) | ||
.get(3, TimeUnit.MINUTES); | ||
System.out.println(policy.getName()); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
samples/cloud-client/snippets/src/main/java/GetDenyPolicy.java
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,59 @@ | ||
/* | ||
* 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 com.google.iam.v2beta.GetPolicyRequest; | ||
import com.google.iam.v2beta.PoliciesClient; | ||
import com.google.iam.v2beta.Policy; | ||
import java.io.IOException; | ||
|
||
public class GetDenyPolicy { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
|
||
// ID or number of the Google Cloud project you want to use. | ||
String projectId = "your-google-cloud-project-id"; | ||
|
||
// Specify the name of the Deny policy you want to retrieve. | ||
String policyName = "deny-policy-name"; | ||
|
||
getDenyPolicy(projectId, policyName); | ||
} | ||
|
||
|
||
// Retrieve the Deny policy given the project id and policy name. | ||
public static void getDenyPolicy(String projectId, String policyName) throws IOException { | ||
// Create the IAM Policies client. | ||
try (PoliciesClient policiesClient = PoliciesClient.create()) { | ||
|
||
String attachmentPoint = | ||
String.format("cloudresourcemanager.googleapis.com/projects/%s", projectId) | ||
.replaceAll("/", "%2F"); | ||
|
||
String policyParent = String.format( | ||
"policies/%s/denypolicies/%s", attachmentPoint, policyName); | ||
|
||
// Specify the policyParent and execute the Policy Get request. | ||
GetPolicyRequest getPolicyRequest = GetPolicyRequest.newBuilder() | ||
.setName(policyParent) | ||
.build(); | ||
|
||
Policy policy = policiesClient.getPolicy(getPolicyRequest); | ||
System.out.println(policy.getName()); | ||
} | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
samples/cloud-client/snippets/src/main/java/ListDenyPolicies.java
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,46 @@ | ||
/* | ||
* 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 com.google.iam.v2beta.PoliciesClient; | ||
import com.google.iam.v2beta.Policy; | ||
import java.io.IOException; | ||
|
||
public class ListDenyPolicies { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
// ID or number of the Google Cloud project you want to use. | ||
String projectId = "your-google-cloud-project-id"; | ||
|
||
listDenyPolicies(projectId); | ||
} | ||
|
||
public static void listDenyPolicies(String projectId) throws IOException { | ||
// Initialize the IAM service. | ||
try (PoliciesClient policiesClient = PoliciesClient.create()) { | ||
String attachmentPoint = | ||
String.format("cloudresourcemanager.googleapis.com/projects/%s", projectId) | ||
.replaceAll("/", "%2F"); | ||
|
||
String policyParent = String.format( | ||
"policies/%s/denypolicies", attachmentPoint); | ||
|
||
for (Policy policy : policiesClient.listPolicies(policyParent).iterateAll()) { | ||
System.out.println(policy.getName()); | ||
} | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
samples/cloud-client/snippets/src/main/java/UpdateDenyPolicy.java
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,82 @@ | ||
/* | ||
* 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 com.google.iam.v2beta.DenyRule; | ||
import com.google.iam.v2beta.PoliciesClient; | ||
import com.google.iam.v2beta.Policy; | ||
import com.google.iam.v2beta.PolicyRule; | ||
import com.google.iam.v2beta.UpdatePolicyRequest; | ||
import com.google.type.Expr; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class UpdateDenyPolicy { | ||
|
||
public static void main(String[] args) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
|
||
// ID or number of the Google Cloud project you want to use. | ||
String projectId = "your-google-cloud-project-id"; | ||
|
||
// Specify the name of the Deny policy you want to retrieve. | ||
String policyName = "deny-policy-name"; | ||
|
||
updateDenyPolicy(projectId, policyName); | ||
} | ||
|
||
public static void updateDenyPolicy(String projectId, String policyName) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
|
||
try (PoliciesClient policiesClient = PoliciesClient.create()) { | ||
|
||
String attachmentPoint = | ||
String.format("cloudresourcemanager.googleapis.com/projects/%s", projectId) | ||
.replaceAll("/", "%2F"); | ||
|
||
String policyParent = String.format( | ||
"policies/%s/denypolicies/%s", attachmentPoint, policyName); | ||
|
||
Policy policy = Policy.newBuilder() | ||
.setName(policyParent) | ||
.addRules(PolicyRule.newBuilder() | ||
.setDescription( | ||
"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") | ||
.setDenyRule(DenyRule.newBuilder() | ||
.addDeniedPrincipals("principalSet://goog/public:all") | ||
.addExceptionPrincipals("principalSet://goog/group/[email protected]") | ||
.addDeniedPermissions("cloudresourcemanager.googleapis.com/projects.delete") | ||
.addExceptionPermissions("iam.googleapis.com/roles.list") | ||
.setDenialCondition(Expr.newBuilder() | ||
.setExpression("!resource.matchTag('12345678/env', 'prod')") | ||
.setTitle("Only for non-test projects") | ||
.build()) | ||
.build()) | ||
.build()) | ||
.build(); | ||
|
||
UpdatePolicyRequest updatePolicyRequest = UpdatePolicyRequest.newBuilder() | ||
.setPolicy(policy) | ||
.build(); | ||
|
||
Policy response = policiesClient.updatePolicyAsync(updatePolicyRequest) | ||
.get(3, TimeUnit.MINUTES); | ||
System.out.println(response.getName()); | ||
} | ||
} | ||
} |
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.