Skip to content

Commit c2363e3

Browse files
rahul2393gcf-owl-bot[bot]rajatbhatta
authored
chore: add getter for database role in DatabaseClient and BatchClient (#2029)
* chore: add getter for database role in DatabaseClient and BatchClient for beam unit tests * fix clirr * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * incorporate requested changes * incorporate requested changes * incorporate requested changes * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java * Update BatchClientImplTest.java * Add doc for getDatabaseRole in DatabaseClient * Update DatabaseClient.java Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Rajat Bhatta <[email protected]>
1 parent a26a14a commit c2363e3

File tree

10 files changed

+82
-0
lines changed

10 files changed

+82
-0
lines changed

google-cloud-spanner/clirr-ignored-differences.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,14 @@
152152
<className>com/google/cloud/spanner/StructReader</className>
153153
<method>java.util.List getPgJsonbList(java.lang.String)</method>
154154
</difference>
155+
<difference>
156+
<differenceType>7012</differenceType>
157+
<className>com/google/cloud/spanner/BatchClient</className>
158+
<method>java.lang.String getDatabaseRole()</method>
159+
</difference>
160+
<difference>
161+
<differenceType>7012</differenceType>
162+
<className>com/google/cloud/spanner/DatabaseClient</className>
163+
<method>java.lang.String getDatabaseRole()</method>
164+
</difference>
155165
</differences>

google-cloud-spanner/src/main/java/com/google/cloud/spanner/BatchClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,13 @@ public interface BatchClient {
5959
* successive read/query.
6060
*/
6161
BatchReadOnlyTransaction batchReadOnlyTransaction(BatchTransactionId batchTransactionId);
62+
63+
/**
64+
* Returns the {@link DatabaseRole} used by the client connection. The database role that is used
65+
* determines the access permissions that a connection has. This can for example be used to create
66+
* connections that are only permitted to access certain tables.
67+
*/
68+
default String getDatabaseRole() {
69+
throw new UnsupportedOperationException("method should be overwritten");
70+
};
6271
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/BatchClientImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.opencensus.trace.Tracing;
3434
import java.util.List;
3535
import java.util.Map;
36+
import javax.annotation.Nullable;
3637

3738
/** Default implementation for Batch Client interface. */
3839
public class BatchClientImpl implements BatchClient {
@@ -42,6 +43,12 @@ public class BatchClientImpl implements BatchClient {
4243
this.sessionClient = checkNotNull(sessionClient);
4344
}
4445

46+
@Override
47+
@Nullable
48+
public String getDatabaseRole() {
49+
return this.sessionClient.getSpanner().getOptions().getDatabaseRole();
50+
}
51+
4552
@Override
4653
public BatchReadOnlyTransaction batchReadOnlyTransaction(TimestampBound bound) {
4754
SessionImpl session = sessionClient.createSession();

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ default Dialect getDialect() {
3636
throw new UnsupportedOperationException("method should be overwritten");
3737
}
3838

39+
/**
40+
* Returns the {@link DatabaseRole} used by the client connection. The database role that is used
41+
* determines the access permissions that a connection has. This can for example be used to create
42+
* connections that are only permitted to access certain tables.
43+
*
44+
* @return the {@link DatabaseRole} used by the client connection.
45+
*/
46+
default String getDatabaseRole() {
47+
throw new UnsupportedOperationException("method should be overwritten");
48+
}
49+
3950
/**
4051
* Writes the given mutations atomically to the database.
4152
*

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClientImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.opencensus.trace.Span;
2929
import io.opencensus.trace.Tracer;
3030
import io.opencensus.trace.Tracing;
31+
import javax.annotation.Nullable;
3132

3233
class DatabaseClientImpl implements DatabaseClient {
3334
private static final String READ_WRITE_TRANSACTION = "CloudSpanner.ReadWriteTransaction";
@@ -58,6 +59,12 @@ public Dialect getDialect() {
5859
return pool.getDialect();
5960
}
6061

62+
@Override
63+
@Nullable
64+
public String getDatabaseRole() {
65+
return pool.getDatabaseRole();
66+
}
67+
6168
@Override
6269
public Timestamp write(final Iterable<Mutation> mutations) throws SpannerException {
6370
return writeWithOptions(mutations).getCommitTimestamp();

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,7 @@ private enum Position {
17841784

17851785
private final SessionPoolOptions options;
17861786
private final SettableFuture<Dialect> dialect = SettableFuture.create();
1787+
private final String databaseRole;
17871788
private final SessionClient sessionClient;
17881789
private final ScheduledExecutorService executor;
17891790
private final ExecutorFactory<ScheduledExecutorService> executorFactory;
@@ -1858,6 +1859,7 @@ static SessionPool createPool(
18581859
SpannerOptions spannerOptions, SessionClient sessionClient, List<LabelValue> labelValues) {
18591860
return createPool(
18601861
spannerOptions.getSessionPoolOptions(),
1862+
spannerOptions.getDatabaseRole(),
18611863
((GrpcTransportOptions) spannerOptions.getTransportOptions()).getExecutorFactory(),
18621864
sessionClient,
18631865
new Clock(),
@@ -1879,6 +1881,7 @@ static SessionPool createPool(
18791881
Clock clock) {
18801882
return createPool(
18811883
poolOptions,
1884+
null,
18821885
executorFactory,
18831886
sessionClient,
18841887
clock,
@@ -1888,6 +1891,7 @@ static SessionPool createPool(
18881891

18891892
static SessionPool createPool(
18901893
SessionPoolOptions poolOptions,
1894+
String databaseRole,
18911895
ExecutorFactory<ScheduledExecutorService> executorFactory,
18921896
SessionClient sessionClient,
18931897
Clock clock,
@@ -1896,6 +1900,7 @@ static SessionPool createPool(
18961900
SessionPool pool =
18971901
new SessionPool(
18981902
poolOptions,
1903+
databaseRole,
18991904
executorFactory,
19001905
executorFactory.get(),
19011906
sessionClient,
@@ -1908,13 +1913,15 @@ static SessionPool createPool(
19081913

19091914
private SessionPool(
19101915
SessionPoolOptions options,
1916+
String databaseRole,
19111917
ExecutorFactory<ScheduledExecutorService> executorFactory,
19121918
ScheduledExecutorService executor,
19131919
SessionClient sessionClient,
19141920
Clock clock,
19151921
MetricRegistry metricRegistry,
19161922
List<LabelValue> labelValues) {
19171923
this.options = options;
1924+
this.databaseRole = databaseRole;
19181925
this.executorFactory = executorFactory;
19191926
this.executor = executor;
19201927
this.sessionClient = sessionClient;
@@ -1956,6 +1963,11 @@ Dialect getDialect() {
19561963
}
19571964
}
19581965

1966+
@Nullable
1967+
public String getDatabaseRole() {
1968+
return databaseRole;
1969+
}
1970+
19591971
@VisibleForTesting
19601972
int getNumberOfSessionsInUse() {
19611973
synchronized (lock) {

google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.spanner;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertEquals;
2021
import static org.mockito.ArgumentMatchers.anyString;
2122
import static org.mockito.Mockito.anyMap;
2223
import static org.mockito.Mockito.eq;
@@ -115,4 +116,9 @@ public void testBatchReadOnlyTxnWithTxnId() {
115116
assertThat(batchTxn.getReadTimestamp())
116117
.isEqualTo(batchTxn.getBatchTransactionId().getTimestamp());
117118
}
119+
120+
@Test
121+
public void testGetDatabaseRole() {
122+
assertEquals(client.getDatabaseRole(), "role");
123+
}
118124
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class DatabaseClientImplTest {
9494
private static final String TEST_PROJECT = "my-project";
9595
private static final String TEST_INSTANCE = "my-instance";
9696
private static final String TEST_DATABASE = "my-database";
97+
private static final String TEST_DATABASE_ROLE = "my-role";
9798
private static final String INSTANCE_NAME =
9899
String.format("projects/%s/instances/%s", TEST_PROJECT, TEST_INSTANCE);
99100
private static final String DATABASE_NAME =
@@ -149,6 +150,7 @@ public void setUp() {
149150
spanner =
150151
SpannerOptions.newBuilder()
151152
.setProjectId(TEST_PROJECT)
153+
.setDatabaseRole(TEST_DATABASE_ROLE)
152154
.setChannelProvider(channelProvider)
153155
.setCredentials(NoCredentials.getInstance())
154156
.setSessionPoolOption(SessionPoolOptions.newBuilder().setFailOnSessionLeak().build())
@@ -2318,4 +2320,11 @@ public void testUntypedNullParameters() {
23182320
assertNotNull(updateCount);
23192321
assertEquals(1L, updateCount.longValue());
23202322
}
2323+
2324+
@Test
2325+
public void testGetDatabaseRole() {
2326+
DatabaseClient client =
2327+
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
2328+
assertEquals(TEST_DATABASE_ROLE, client.getDatabaseRole());
2329+
}
23212330
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.cloud.spanner.MetricRegistryConstants.NUM_READ_SESSIONS;
2121
import static com.google.cloud.spanner.MetricRegistryConstants.NUM_SESSIONS_BEING_PREPARED;
2222
import static com.google.cloud.spanner.MetricRegistryConstants.NUM_WRITE_SESSIONS;
23+
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_DEFAULT_LABEL_VALUES;
2324
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS;
2425
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_TYPE;
2526
import static com.google.common.truth.Truth.assertThat;
@@ -108,6 +109,7 @@ public class SessionPoolTest extends BaseSessionPoolTest {
108109
SessionPool pool;
109110
SessionPoolOptions options;
110111
private String sessionName = String.format("%s/sessions/s", db.getName());
112+
private String TEST_DATABASE_ROLE = "my-role";
111113

112114
@Parameters(name = "min sessions = {0}")
113115
public static Collection<Object[]> data() {
@@ -127,6 +129,7 @@ private SessionPool createPool(
127129
Clock clock, MetricRegistry metricRegistry, List<LabelValue> labelValues) {
128130
return SessionPool.createPool(
129131
options,
132+
TEST_DATABASE_ROLE,
130133
new TestExecutorFactory(),
131134
client.getSessionClient(db),
132135
clock,
@@ -1178,6 +1181,13 @@ public void testSessionMetrics() throws Exception {
11781181
assertThat(writePreparedSessions.value()).isEqualTo(0L);
11791182
}
11801183

1184+
@Test
1185+
public void testGetDatabaseRole() throws Exception {
1186+
setupMockSessionCreation();
1187+
pool = createPool(new FakeClock(), new FakeMetricRegistry(), SPANNER_DEFAULT_LABEL_VALUES);
1188+
assertEquals(TEST_DATABASE_ROLE, pool.getDatabaseRole());
1189+
}
1190+
11811191
private void mockKeepAlive(Session session) {
11821192
ReadContext context = mock(ReadContext.class);
11831193
ResultSet resultSet = mock(ResultSet.class);

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITDatabaseRolePermissionTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public void grantAndRevokeDatabaseRolePermissions() throws Exception {
110110
try (ResultSet rs =
111111
dbClient.singleUse().executeQuery(Statement.of("SELECT COUNT(*) as cnt FROM T"))) {
112112
assertTrue(rs.next());
113+
assertEquals(dbClient.getDatabaseRole(), dbRoleParent);
113114
} catch (PermissionDeniedException e) {
114115
// This is not expected
115116
fail("Got PermissionDeniedException when it should not have occurred.");

0 commit comments

Comments
 (0)