36
36
import com.google.cloud.spanner.SpannerOptions;
37
37
import com.google.common.annotations.VisibleForTesting;
38
38
import com.google.common.base.Preconditions;
39
+ import com.google.common.base.Strings;
39
40
import com.google.common.collect.Sets;
40
41
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
41
42
import java.io.IOException;
@@ -205,8 +206,14 @@ public String[] getValidValues() {
205
206
public static final String CREDENTIALS_PROPERTY_NAME = "credentials";
206
207
/** Name of the 'encodedCredentials' connection property. */
207
208
public static final String ENCODED_CREDENTIALS_PROPERTY_NAME = "encodedCredentials";
209
+
210
+ public static final String ENABLE_ENCODED_CREDENTIALS_SYSTEM_PROPERTY =
211
+ "ENABLE_ENCODED_CREDENTIALS";
208
212
/** Name of the 'credentialsProvider' connection property. */
209
213
public static final String CREDENTIALS_PROVIDER_PROPERTY_NAME = "credentialsProvider";
214
+
215
+ public static final String ENABLE_CREDENTIALS_PROVIDER_SYSTEM_PROPERTY =
216
+ "ENABLE_CREDENTIALS_PROVIDER";
210
217
/**
211
218
* OAuth token to use for authentication. Cannot be used in combination with a credentials file.
212
219
*/
@@ -219,6 +226,8 @@ public String[] getValidValues() {
219
226
public static final String NUM_CHANNELS_PROPERTY_NAME = "numChannels";
220
227
/** Name of the 'channelProvider' connection property. */
221
228
public static final String CHANNEL_PROVIDER_PROPERTY_NAME = "channelProvider";
229
+
230
+ public static final String ENABLE_CHANNEL_PROVIDER_SYSTEM_PROPERTY = "ENABLE_CHANNEL_PROVIDER";
222
231
/** Custom user agent string is only for other Google libraries. */
223
232
private static final String USER_AGENT_PROPERTY_NAME = "userAgent";
224
233
/** Query optimizer version to use for a connection. */
@@ -248,6 +257,19 @@ public String[] getValidValues() {
248
257
public static final String MAX_PARTITIONED_PARALLELISM_PROPERTY_NAME =
249
258
"maxPartitionedParallelism";
250
259
260
+ private static final String GUARDED_CONNECTION_PROPERTY_ERROR_MESSAGE =
261
+ "%s can only be used if the system property %s has been set to true. "
262
+ + "Start the application with the JVM command line option -D%s=true";
263
+
264
+ private static String generateGuardedConnectionPropertyError(
265
+ String systemPropertyName, String connectionPropertyName) {
266
+ return String.format(
267
+ GUARDED_CONNECTION_PROPERTY_ERROR_MESSAGE,
268
+ connectionPropertyName,
269
+ systemPropertyName,
270
+ systemPropertyName);
271
+ }
272
+
251
273
/** All valid connection properties. */
252
274
public static final Set<ConnectionProperty> VALID_PROPERTIES =
253
275
Collections.unmodifiableSet(
@@ -846,40 +868,64 @@ static boolean parseRetryAbortsInternally(String uri) {
846
868
847
869
@VisibleForTesting
848
870
static @Nullable String parseEncodedCredentials(String uri) {
849
- return parseUriProperty(uri, ENCODED_CREDENTIALS_PROPERTY_NAME);
871
+ String encodedCredentials = parseUriProperty(uri, ENCODED_CREDENTIALS_PROPERTY_NAME);
872
+ checkGuardedProperty(
873
+ encodedCredentials,
874
+ ENABLE_ENCODED_CREDENTIALS_SYSTEM_PROPERTY,
875
+ ENCODED_CREDENTIALS_PROPERTY_NAME);
876
+ return encodedCredentials;
850
877
}
851
878
852
879
@VisibleForTesting
853
880
static @Nullable CredentialsProvider parseCredentialsProvider(String uri) {
854
- String name = parseUriProperty(uri, CREDENTIALS_PROVIDER_PROPERTY_NAME);
855
- if (name != null) {
881
+ String credentialsProviderName = parseUriProperty(uri, CREDENTIALS_PROVIDER_PROPERTY_NAME);
882
+ checkGuardedProperty(
883
+ credentialsProviderName,
884
+ ENABLE_CREDENTIALS_PROVIDER_SYSTEM_PROPERTY,
885
+ CREDENTIALS_PROVIDER_PROPERTY_NAME);
886
+ if (!Strings.isNullOrEmpty(credentialsProviderName)) {
856
887
try {
857
888
Class<? extends CredentialsProvider> clazz =
858
- (Class<? extends CredentialsProvider>) Class.forName(name );
889
+ (Class<? extends CredentialsProvider>) Class.forName(credentialsProviderName );
859
890
Constructor<? extends CredentialsProvider> constructor = clazz.getDeclaredConstructor();
860
891
return constructor.newInstance();
861
892
} catch (ClassNotFoundException classNotFoundException) {
862
893
throw SpannerExceptionFactory.newSpannerException(
863
894
ErrorCode.INVALID_ARGUMENT,
864
- "Unknown or invalid CredentialsProvider class name: " + name ,
895
+ "Unknown or invalid CredentialsProvider class name: " + credentialsProviderName ,
865
896
classNotFoundException);
866
897
} catch (NoSuchMethodException noSuchMethodException) {
867
898
throw SpannerExceptionFactory.newSpannerException(
868
899
ErrorCode.INVALID_ARGUMENT,
869
- "Credentials provider " + name + " does not have a public no-arg constructor.",
900
+ "Credentials provider "
901
+ + credentialsProviderName
902
+ + " does not have a public no-arg constructor.",
870
903
noSuchMethodException);
871
904
} catch (InvocationTargetException
872
905
| InstantiationException
873
906
| IllegalAccessException exception) {
874
907
throw SpannerExceptionFactory.newSpannerException(
875
908
ErrorCode.INVALID_ARGUMENT,
876
- "Failed to create an instance of " + name + ": " + exception.getMessage(),
909
+ "Failed to create an instance of "
910
+ + credentialsProviderName
911
+ + ": "
912
+ + exception.getMessage(),
877
913
exception);
878
914
}
879
915
}
880
916
return null;
881
917
}
882
918
919
+ private static void checkGuardedProperty(
920
+ String value, String systemPropertyName, String connectionPropertyName) {
921
+ if (!Strings.isNullOrEmpty(value)
922
+ && !Boolean.parseBoolean(System.getProperty(systemPropertyName))) {
923
+ throw SpannerExceptionFactory.newSpannerException(
924
+ ErrorCode.FAILED_PRECONDITION,
925
+ generateGuardedConnectionPropertyError(systemPropertyName, connectionPropertyName));
926
+ }
927
+ }
928
+
883
929
@VisibleForTesting
884
930
static @Nullable String parseOAuthToken(String uri) {
885
931
String value = parseUriProperty(uri, OAUTH_TOKEN_PROPERTY_NAME);
@@ -907,6 +953,8 @@ static String parseNumChannels(String uri) {
907
953
@VisibleForTesting
908
954
static String parseChannelProvider(String uri) {
909
955
String value = parseUriProperty(uri, CHANNEL_PROVIDER_PROPERTY_NAME);
956
+ checkGuardedProperty(
957
+ value, ENABLE_CHANNEL_PROVIDER_SYSTEM_PROPERTY, CHANNEL_PROVIDER_PROPERTY_NAME);
910
958
return value != null ? value : DEFAULT_CHANNEL_PROVIDER;
911
959
}
912
960
0 commit comments