22
33import java .sql .SQLException ;
44import java .util .Properties ;
5+ import java .util .logging .Level ;
6+ import java .util .logging .Logger ;
57
68import tech .ydb .auth .TokenAuthProvider ;
79import tech .ydb .auth .iam .CloudAuthHelper ;
810import tech .ydb .core .auth .StaticCredentials ;
911import tech .ydb .core .grpc .BalancingSettings ;
1012import tech .ydb .core .grpc .GrpcTransportBuilder ;
13+ import tech .ydb .jdbc .YdbDriver ;
1114
1215
1316public class YdbConnectionProperties {
17+ private static final Logger LOGGER = Logger .getLogger (YdbDriver .class .getName ());
18+
1419 static final YdbProperty <String > TOKEN = YdbProperty .content (YdbConfig .TOKEN_KEY , "Authentication token" );
1520
21+ static final YdbProperty <String > TOKEN_FILE = YdbProperty .file ("tokenFile" ,
22+ "Path to token file for the token-based authentication" );
23+
1624 static final YdbProperty <String > LOCAL_DATACENTER = YdbProperty .string ("localDatacenter" ,
1725 "Local Datacenter" );
1826
1927 static final YdbProperty <Boolean > USE_SECURE_CONNECTION = YdbProperty .bool ("secureConnection" ,
2028 "Use TLS connection" );
2129
22- static final YdbProperty <byte []> SECURE_CONNECTION_CERTIFICATE = YdbProperty .bytes ("secureConnectionCertificate" ,
23- "Use TLS connection with certificate from provided path" );
30+ static final YdbProperty <byte []> SECURE_CONNECTION_CERTIFICATE = YdbProperty .fileBytes (
31+ "secureConnectionCertificate" , "Use TLS connection with certificate from provided path"
32+ );
2433
34+ @ Deprecated
2535 static final YdbProperty <String > SERVICE_ACCOUNT_FILE = YdbProperty .content ("saFile" ,
2636 "Service account file based authentication" );
2737
38+ static final YdbProperty <String > SA_KEY_FILE = YdbProperty .file ("saKeyFile" ,
39+ "Path to key file for the service account authentication" );
40+
2841 static final YdbProperty <Boolean > USE_METADATA = YdbProperty .bool ("useMetadata" ,
2942 "Use metadata service for authentication" );
3043
@@ -41,7 +54,9 @@ public class YdbConnectionProperties {
4154 private final YdbValue <Boolean > useSecureConnection ;
4255 private final YdbValue <byte []> secureConnectionCertificate ;
4356 private final YdbValue <String > token ;
57+ private final YdbValue <String > tokenFile ;
4458 private final YdbValue <String > serviceAccountFile ;
59+ private final YdbValue <String > saKeyFile ;
4560 private final YdbValue <Boolean > useMetadata ;
4661 private final YdbValue <String > iamEndpoint ;
4762 private final YdbValue <String > metadataUrl ;
@@ -56,7 +71,9 @@ public YdbConnectionProperties(YdbConfig config) throws SQLException {
5671 this .useSecureConnection = USE_SECURE_CONNECTION .readValue (props );
5772 this .secureConnectionCertificate = SECURE_CONNECTION_CERTIFICATE .readValue (props );
5873 this .token = TOKEN .readValue (props );
74+ this .tokenFile = TOKEN_FILE .readValue (props );
5975 this .serviceAccountFile = SERVICE_ACCOUNT_FILE .readValue (props );
76+ this .saKeyFile = SA_KEY_FILE .readValue (props );
6077 this .useMetadata = USE_METADATA .readValue (props );
6178 this .iamEndpoint = IAM_ENDPOINT .readValue (props );
6279 this .metadataUrl = METADATA_URL .readValue (props );
@@ -87,33 +104,78 @@ public GrpcTransportBuilder applyToGrpcTransport(GrpcTransportBuilder builder) {
87104 builder = builder .withSecureConnection (secureConnectionCertificate .getValue ());
88105 }
89106
107+ String usedProvider = null ;
108+
109+ if (username != null && !username .isEmpty ()) {
110+ builder = builder .withAuthProvider (new StaticCredentials (username , password ));
111+ usedProvider = "username & password credentials" ;
112+ }
113+
114+ if (useMetadata .hasValue ()) {
115+ if (usedProvider != null ) {
116+ LOGGER .log (Level .WARNING , "Dublicate authentication config! Metadata credentials replaces {0}" ,
117+ usedProvider );
118+ }
119+
120+ if (metadataUrl .hasValue ()) {
121+ String url = metadataUrl .getValue ();
122+ builder = builder .withAuthProvider (CloudAuthHelper .getMetadataAuthProvider (url ));
123+ } else {
124+ builder = builder .withAuthProvider (CloudAuthHelper .getMetadataAuthProvider ());
125+ }
126+ usedProvider = "metadata credentials" ;
127+ }
128+
129+ if (tokenFile .hasValue ()) {
130+ if (usedProvider != null ) {
131+ LOGGER .log (Level .WARNING , "Dublicate authentication config! Token credentials replaces {0}" ,
132+ usedProvider );
133+ }
134+ builder = builder .withAuthProvider (new TokenAuthProvider (tokenFile .getValue ()));
135+ usedProvider = "token file credentitals" ;
136+ }
137+
90138 if (token .hasValue ()) {
139+ if (usedProvider != null ) {
140+ LOGGER .log (Level .WARNING , "Dublicate authentication config! Token credentials replaces {0}" ,
141+ usedProvider );
142+ }
91143 builder = builder .withAuthProvider (new TokenAuthProvider (token .getValue ()));
144+ usedProvider = "token value credentitals" ;
92145 }
93146
94- if (serviceAccountFile .hasValue ()) {
95- String json = serviceAccountFile .getValue ();
147+ if (saKeyFile .hasValue ()) {
148+ if (usedProvider != null ) {
149+ LOGGER .log (Level .WARNING , "Dublicate authentication config! Token credentials replaces {0}" ,
150+ usedProvider );
151+ }
152+ String json = saKeyFile .getValue ();
96153 if (iamEndpoint .hasValue ()) {
97154 String endpoint = iamEndpoint .getValue ();
98155 builder = builder .withAuthProvider (CloudAuthHelper .getServiceAccountJsonAuthProvider (json , endpoint ));
99156 } else {
100157 builder = builder .withAuthProvider (CloudAuthHelper .getServiceAccountJsonAuthProvider (json ));
101158 }
159+ builder = builder .withAuthProvider (new TokenAuthProvider (token .getValue ()));
160+ usedProvider = "service account credentitals" ;
102161 }
103162
104- if (useMetadata .hasValue ()) {
105- if (metadataUrl .hasValue ()) {
106- String url = metadataUrl .getValue ();
107- builder = builder .withAuthProvider (CloudAuthHelper .getMetadataAuthProvider (url ));
163+ if (serviceAccountFile .hasValue ()) {
164+ LOGGER .warning ("Option 'saFile' is deprecated and will be removed in next versions. "
165+ + "Use options 'saKeyFile' instead" );
166+ if (usedProvider != null ) {
167+ LOGGER .log (Level .WARNING , "Dublicate authentication config! Token credentials replaces {0}" ,
168+ usedProvider );
169+ }
170+ String json = serviceAccountFile .getValue ();
171+ if (iamEndpoint .hasValue ()) {
172+ String endpoint = iamEndpoint .getValue ();
173+ builder = builder .withAuthProvider (CloudAuthHelper .getServiceAccountJsonAuthProvider (json , endpoint ));
108174 } else {
109- builder = builder .withAuthProvider (CloudAuthHelper .getMetadataAuthProvider ( ));
175+ builder = builder .withAuthProvider (CloudAuthHelper .getServiceAccountJsonAuthProvider ( json ));
110176 }
111177 }
112178
113- if (username != null && !username .isEmpty ()) {
114- builder = builder .withAuthProvider (new StaticCredentials (username , password ));
115- }
116-
117179 return builder ;
118180 }
119181}
0 commit comments