Skip to content

Commit 35118d0

Browse files
author
Bot
committed
feat: port secure_context testing support to executor proxy
1 parent 7b5efb5 commit 35118d0

4 files changed

Lines changed: 54 additions & 5 deletions

File tree

java-spanner/google-cloud-spanner-executor/src/main/java/com/google/cloud/executor/spanner/CloudClientExecutor.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,13 +2198,51 @@ private Status executeGenerateDbPartitionsRead(
21982198
}
21992199

22002200
/** Execute action that generates database partitions for the given query. */
2201+
2202+
private Options.ReadQueryUpdateTransactionOption buildSecureContextOption(
2203+
Map<String, com.google.spanner.executor.v1.Value> secureContextMap) {
2204+
if (secureContextMap != null && !secureContextMap.isEmpty()) {
2205+
com.google.spanner.v1.RequestOptions.ClientContext.Builder clientContextBuilder =
2206+
com.google.spanner.v1.RequestOptions.ClientContext.newBuilder();
2207+
for (Map.Entry<String, com.google.spanner.executor.v1.Value> entry :
2208+
secureContextMap.entrySet()) {
2209+
com.google.protobuf.Value.Builder valueBuilder = com.google.protobuf.Value.newBuilder();
2210+
if (entry.getValue().getValueTypeCase() == com.google.spanner.executor.v1.Value.ValueTypeCase.IS_NULL
2211+
&& entry.getValue().getIsNull()) {
2212+
valueBuilder.setNullValue(com.google.protobuf.NullValue.NULL_VALUE);
2213+
} else if (entry.getValue().getValueTypeCase() == com.google.spanner.executor.v1.Value.ValueTypeCase.STRING_VALUE) {
2214+
valueBuilder.setStringValue(entry.getValue().getStringValue());
2215+
} else {
2216+
throw new IllegalArgumentException("Unsupported secure parameter value type in GitHub proxy");
2217+
}
2218+
clientContextBuilder.putSecureContext(entry.getKey(), valueBuilder.build());
2219+
}
2220+
return Options.clientContext(clientContextBuilder.build());
2221+
}
2222+
return null;
2223+
}
2224+
2225+
@SuppressWarnings("unchecked")
2226+
private <T> void addSecureContextOption(
2227+
Map<String, com.google.spanner.executor.v1.Value> secureContextMap,
2228+
List<T> optionsList,
2229+
Class<?> optionClass) {
2230+
Options.ReadQueryUpdateTransactionOption secureContextOption =
2231+
buildSecureContextOption(secureContextMap);
2232+
if (secureContextOption != null && optionClass.isInstance(secureContextOption)) {
2233+
optionsList.add((T) secureContextOption);
2234+
}
2235+
}
2236+
22012237
private Status executeGenerateDbPartitionsQuery(
22022238
GenerateDbPartitionsForQueryAction action,
22032239
OutcomeSender sender,
22042240
ExecutionFlowContext executionContext) {
22052241
try {
22062242
BatchReadOnlyTransaction batchTxn = executionContext.getBatchTxn();
22072243
Statement.Builder stmt = Statement.newBuilder(action.getQuery().getSql());
2244+
List<Options.QueryOption> queryOptions = new ArrayList<>();
2245+
addSecureContextOption(action.getQuery().getSecureContextMap(), queryOptions, Options.QueryOption.class);
22082246
for (int i = 0; i < action.getQuery().getParamsCount(); ++i) {
22092247
stmt.bind(action.getQuery().getParams(i).getName())
22102248
.to(
@@ -2216,7 +2254,7 @@ private Status executeGenerateDbPartitionsQuery(
22162254
PartitionOptions.newBuilder()
22172255
.setPartitionSizeBytes(action.getDesiredBytesPerPartition())
22182256
.build();
2219-
List<Partition> parts = batchTxn.partitionQuery(partitionOptions, stmt.build());
2257+
List<Partition> parts = batchTxn.partitionQuery(partitionOptions, stmt.build(), queryOptions.toArray(new Options.QueryOption[0]));
22202258
List<BatchPartition> batchPartitions = new ArrayList<>();
22212259
for (Partition part : parts) {
22222260
batchPartitions.add(
@@ -2282,11 +2320,14 @@ private Status executePartitionedUpdate(
22822320
PartitionedUpdateAction action, DatabaseClient dbClient, OutcomeSender sender) {
22832321
try {
22842322
ExecutePartitionedUpdateOptions options = action.getOptions();
2323+
List<Options.UpdateOption> optionsList = new ArrayList<>();
2324+
optionsList.add(Options.tag(options.getTag()));
2325+
optionsList.add(Options.priority(RpcPriority.fromProto(options.getRpcPriority())));
2326+
addSecureContextOption(action.getUpdate().getSecureContextMap(), optionsList, Options.UpdateOption.class);
22852327
Long count =
22862328
dbClient.executePartitionedUpdate(
22872329
Statement.of(action.getUpdate().getSql()),
2288-
Options.tag(options.getTag()),
2289-
Options.priority(RpcPriority.fromProto(options.getRpcPriority())));
2330+
optionsList.toArray(new Options.UpdateOption[0]));
22902331
SpannerActionOutcome outcome =
22912332
SpannerActionOutcome.newBuilder()
22922333
.setStatus(toProto(Status.OK))
@@ -2739,7 +2780,10 @@ private Status executeQuery(
27392780
String.format(
27402781
"Finish query building, ready to execute %s\n",
27412782
executionContext.getTransactionSeed()));
2742-
ResultSet result = txn.executeQuery(stmt.build(), Options.tag("query-tag"));
2783+
List<Options.QueryOption> queryOptions = new ArrayList<>();
2784+
queryOptions.add(Options.tag("query-tag"));
2785+
addSecureContextOption(action.getSecureContextMap(), queryOptions, Options.QueryOption.class);
2786+
ResultSet result = txn.executeQuery(stmt.build(), queryOptions.toArray(new Options.QueryOption[0]));
27432787
LOGGER.log(
27442788
Level.INFO,
27452789
String.format("Parsing query result %s\n", executionContext.getTransactionSeed()));

java-spanner/proto-google-cloud-spanner-executor-v1/src/main/java/com/google/spanner/executor/v1/QueryAction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,8 @@ public com.google.protobuf.ByteString getSqlBytes() {
13801380
* <code>repeated .google.spanner.executor.v1.QueryAction.Parameter params = 2;</code>
13811381
*/
13821382
@java.lang.Override
1383-
public java.util.List<com.google.spanner.executor.v1.QueryAction.Parameter> getParamsList() {
1383+
public java.util.Map<java.lang.String, com.google.spanner.executor.v1.Value> getSecureContextMap() { return java.util.Collections.emptyMap(); }
1384+
public java.util.List<com.google.spanner.executor.v1.QueryAction.Parameter> getParamsList() {
13841385
return params_;
13851386
}
13861387

@@ -1960,6 +1961,7 @@ private void ensureParamsIsMutable() {
19601961
*
19611962
* <code>repeated .google.spanner.executor.v1.QueryAction.Parameter params = 2;</code>
19621963
*/
1964+
public java.util.Map<java.lang.String, com.google.spanner.executor.v1.Value> getSecureContextMap() { return java.util.Collections.emptyMap(); }
19631965
public java.util.List<com.google.spanner.executor.v1.QueryAction.Parameter> getParamsList() {
19641966
if (paramsBuilder_ == null) {
19651967
return java.util.Collections.unmodifiableList(params_);

java-spanner/proto-google-cloud-spanner-executor-v1/src/main/java/com/google/spanner/executor/v1/QueryActionOrBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public interface QueryActionOrBuilder
6161
*
6262
* <code>repeated .google.spanner.executor.v1.QueryAction.Parameter params = 2;</code>
6363
*/
64+
java.util.Map<java.lang.String, com.google.spanner.executor.v1.Value> getSecureContextMap();
6465
java.util.List<com.google.spanner.executor.v1.QueryAction.Parameter> getParamsList();
6566

6667
/**

java-spanner/proto-google-cloud-spanner-executor-v1/src/main/proto/google/spanner/executor/v1/cloud_executor.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ message QueryAction {
178178

179179
// Parameters for the SQL string.
180180
repeated Parameter params = 2;
181+
n // Secure context parameters.
182+
map<string, Value> secure_context = 3;
181183
}
182184

183185
// A single DML statement.

0 commit comments

Comments
 (0)