-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
534 additions
and
16 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
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
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 |
---|---|---|
|
@@ -358,7 +358,7 @@ public void listDatabaseRolesErrorWithToken() { | |
|
||
@Test | ||
public void getDatabaseIAMPolicy() { | ||
when(rpc.getDatabaseAdminIAMPolicy(DB_NAME)) | ||
when(rpc.getDatabaseAdminIAMPolicy(DB_NAME, null)) | ||
.thenReturn( | ||
Policy.newBuilder() | ||
.addBindings( | ||
|
@@ -367,11 +367,11 @@ public void getDatabaseIAMPolicy() { | |
.setRole("roles/viewer") | ||
.build()) | ||
.build()); | ||
com.google.cloud.Policy policy = client.getDatabaseIAMPolicy(INSTANCE_ID, DB_ID); | ||
com.google.cloud.Policy policy = client.getDatabaseIAMPolicy(INSTANCE_ID, DB_ID, 1); | ||
assertThat(policy.getBindings()) | ||
.containsExactly(Role.viewer(), Sets.newHashSet(Identity.user("[email protected]"))); | ||
|
||
when(rpc.getDatabaseAdminIAMPolicy(DB_NAME)) | ||
when(rpc.getDatabaseAdminIAMPolicy(DB_NAME, null)) | ||
.thenReturn( | ||
Policy.newBuilder() | ||
.addBindings( | ||
|
@@ -380,7 +380,7 @@ public void getDatabaseIAMPolicy() { | |
.setRole("roles/viewer") | ||
.build()) | ||
.build()); | ||
policy = client.getDatabaseIAMPolicy(INSTANCE_ID, DB_ID); | ||
policy = client.getDatabaseIAMPolicy(INSTANCE_ID, DB_ID, 1); | ||
assertThat(policy.getBindings()) | ||
.containsExactly( | ||
Role.viewer(), | ||
|
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 |
---|---|---|
|
@@ -755,13 +755,13 @@ public void backupListBackupOperations() | |
|
||
@Test | ||
public void getAndSetIAMPolicy() { | ||
Policy policy = client.getDatabaseIAMPolicy(INSTANCE_ID, DB_ID); | ||
Policy policy = client.getDatabaseIAMPolicy(INSTANCE_ID, DB_ID, 1); | ||
assertThat(policy).isEqualTo(Policy.newBuilder().build()); | ||
Policy newPolicy = | ||
Policy.newBuilder().addIdentity(Role.editor(), Identity.user("[email protected]")).build(); | ||
Policy returnedPolicy = client.setDatabaseIAMPolicy(INSTANCE_ID, DB_ID, newPolicy); | ||
assertThat(returnedPolicy).isEqualTo(newPolicy); | ||
assertThat(client.getDatabaseIAMPolicy(INSTANCE_ID, DB_ID)).isEqualTo(newPolicy); | ||
assertThat(client.getDatabaseIAMPolicy(INSTANCE_ID, DB_ID, 1)).isEqualTo(newPolicy); | ||
} | ||
|
||
@Test | ||
|
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
76 changes: 76 additions & 0 deletions
76
samples/snippets/src/main/java/com/example/spanner/AddNewRoleSample.java
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,76 @@ | ||
/* | ||
* Copyright 2022 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_add_new_database_role] | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.spanner.DatabaseAdminClient; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class AddNewRoleSample { | ||
static void addNewRole() throws InterruptedException, ExecutionException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
addNewRole(projectId, instanceId, databaseId); | ||
} | ||
|
||
static void addNewRole(String projectId, String instanceId, String databaseId) | ||
throws InterruptedException, ExecutionException { | ||
try (Spanner spanner = | ||
SpannerOptions.newBuilder() | ||
.setProjectId(projectId) | ||
.build() | ||
.getService()) { | ||
final DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient(); | ||
OperationFuture<Void, UpdateDatabaseDdlMetadata> operation = | ||
adminClient.updateDatabaseDdl( | ||
instanceId, | ||
databaseId, | ||
ImmutableList.of( | ||
"CREATE ROLE parent", | ||
"GRANT SELECT ON TABLE Albums TO ROLE parent", | ||
"CREATE ROLE child", | ||
"GRANT ROLE parent TO ROLE child"), | ||
null); | ||
|
||
// Wait for the operation to finish. | ||
// This will throw an ExecutionException if the operation fails. | ||
operation.get(); | ||
System.out.println("Successfully added parent and child roles"); | ||
|
||
// Delete role and membership. | ||
operation = | ||
adminClient.updateDatabaseDdl( | ||
instanceId, | ||
databaseId, | ||
ImmutableList.of("REVOKE ROLE parent FROM ROLE child", "DROP ROLE child"), | ||
null); | ||
// Wait for the operation to finish. | ||
// This will throw an ExecutionException if the operation fails. | ||
operation.get(); | ||
System.out.println("Successfully deleted chile role"); | ||
} | ||
} | ||
} | ||
// [END spanner_add_new_database_role] |
101 changes: 101 additions & 0 deletions
101
samples/snippets/src/main/java/com/example/spanner/EnableFineGrainedAccess.java
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,101 @@ | ||
/* | ||
* Copyright 2022 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_enable_fine_grained_access] | ||
|
||
import com.google.cloud.Binding; | ||
import com.google.cloud.Condition; | ||
import com.google.cloud.Policy; | ||
import com.google.cloud.spanner.DatabaseAdminClient; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class EnableFineGrainedAccess { | ||
static void enableFineGrainedAccess() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
String iamMember = "my-member"; | ||
String databaseRole = "my-role"; | ||
String conditionTitle = "my-title"; | ||
enableFineGrainedAccess( | ||
projectId, instanceId, databaseId, iamMember, databaseRole, conditionTitle); | ||
} | ||
|
||
static void enableFineGrainedAccess( | ||
String projectId, | ||
String instanceId, | ||
String databaseId, | ||
String iamMember, | ||
String databaseRole, | ||
String title) { | ||
try (Spanner spanner = | ||
SpannerOptions.newBuilder() | ||
.setProjectId(projectId) | ||
.build() | ||
.getService()) { | ||
final DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient(); | ||
Policy policy = adminClient.getDatabaseIAMPolicy(instanceId, databaseId, 3); | ||
int policyVersion = policy.getVersion(); | ||
if (policy.getVersion() < 3) { | ||
policyVersion = 3; | ||
} | ||
List<String> members = new ArrayList<>(); | ||
members.add(iamMember); | ||
List<Binding> bindings = new ArrayList<>(); | ||
bindings.addAll(policy.getBindingsList()); | ||
|
||
bindings.add( | ||
Binding.newBuilder() | ||
.setRole("roles/spanner.fineGrainedAccessUser") | ||
.setMembers(members) | ||
.build()); | ||
|
||
bindings.add( | ||
Binding.newBuilder() | ||
.setRole("roles/spanner.databaseRoleUser") | ||
.setCondition( | ||
Condition.newBuilder() | ||
.setDescription(title) | ||
.setExpression( | ||
String.format( | ||
"resource.name.endsWith(\"/databaseRoles/%s\")", databaseRole)) | ||
.setTitle(title) | ||
.build()) | ||
.setMembers(members) | ||
.build()); | ||
|
||
Policy policyWithConditions = | ||
Policy.newBuilder() | ||
.setVersion(policyVersion) | ||
.setEtag(policy.getEtag()) | ||
.setBindings(bindings) | ||
.build(); | ||
Policy response = | ||
adminClient.setDatabaseIAMPolicy(instanceId, databaseId, policyWithConditions); | ||
System.out.println( | ||
String.format( | ||
"Successfully enabled fined grained access, created policy with version %d", | ||
response.getVersion())); | ||
} | ||
} | ||
} | ||
// [END spanner_enable_fine_grained_access] |
Oops, something went wrong.