|
| 1 | +/* |
| 2 | + * Copyright 2022 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.spanner; |
| 18 | + |
| 19 | +// [START spanner_enable_fine_grained_access] |
| 20 | + |
| 21 | +import com.google.cloud.Binding; |
| 22 | +import com.google.cloud.Condition; |
| 23 | +import com.google.cloud.Policy; |
| 24 | +import com.google.cloud.spanner.DatabaseAdminClient; |
| 25 | +import com.google.cloud.spanner.Spanner; |
| 26 | +import com.google.cloud.spanner.SpannerOptions; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +public class EnableFineGrainedAccess { |
| 31 | + static void enableFineGrainedAccess() { |
| 32 | + // TODO(developer): Replace these variables before running the sample. |
| 33 | + String projectId = "my-project"; |
| 34 | + String instanceId = "my-instance"; |
| 35 | + String databaseId = "my-database"; |
| 36 | + String iamMember = "my-member"; |
| 37 | + String databaseRole = "my-role"; |
| 38 | + String conditionTitle = "my-title"; |
| 39 | + enableFineGrainedAccess( |
| 40 | + projectId, instanceId, databaseId, iamMember, databaseRole, conditionTitle); |
| 41 | + } |
| 42 | + |
| 43 | + static void enableFineGrainedAccess( |
| 44 | + String projectId, |
| 45 | + String instanceId, |
| 46 | + String databaseId, |
| 47 | + String iamMember, |
| 48 | + String databaseRole, |
| 49 | + String title) { |
| 50 | + try (Spanner spanner = |
| 51 | + SpannerOptions.newBuilder() |
| 52 | + .setProjectId(projectId) |
| 53 | + .build() |
| 54 | + .getService()) { |
| 55 | + final DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient(); |
| 56 | + Policy policy = adminClient.getDatabaseIAMPolicy(instanceId, databaseId, 3); |
| 57 | + int policyVersion = policy.getVersion(); |
| 58 | + if (policy.getVersion() < 3) { |
| 59 | + policyVersion = 3; |
| 60 | + } |
| 61 | + List<String> members = new ArrayList<>(); |
| 62 | + members.add(iamMember); |
| 63 | + List<Binding> bindings = new ArrayList<>(); |
| 64 | + bindings.addAll(policy.getBindingsList()); |
| 65 | + |
| 66 | + bindings.add( |
| 67 | + Binding.newBuilder() |
| 68 | + .setRole("roles/spanner.fineGrainedAccessUser") |
| 69 | + .setMembers(members) |
| 70 | + .build()); |
| 71 | + |
| 72 | + bindings.add( |
| 73 | + Binding.newBuilder() |
| 74 | + .setRole("roles/spanner.databaseRoleUser") |
| 75 | + .setCondition( |
| 76 | + Condition.newBuilder() |
| 77 | + .setDescription(title) |
| 78 | + .setExpression( |
| 79 | + String.format( |
| 80 | + "resource.name.endsWith(\"/databaseRoles/%s\")", databaseRole)) |
| 81 | + .setTitle(title) |
| 82 | + .build()) |
| 83 | + .setMembers(members) |
| 84 | + .build()); |
| 85 | + |
| 86 | + Policy policyWithConditions = |
| 87 | + Policy.newBuilder() |
| 88 | + .setVersion(policyVersion) |
| 89 | + .setEtag(policy.getEtag()) |
| 90 | + .setBindings(bindings) |
| 91 | + .build(); |
| 92 | + Policy response = |
| 93 | + adminClient.setDatabaseIAMPolicy(instanceId, databaseId, policyWithConditions); |
| 94 | + System.out.println( |
| 95 | + String.format( |
| 96 | + "Successfully enabled fined grained access, created policy with version %d", |
| 97 | + response.getVersion())); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +// [END spanner_enable_fine_grained_access] |
0 commit comments