88import java .io .InputStream ;
99import java .io .InputStreamReader ;
1010import java .nio .charset .StandardCharsets ;
11+ import java .nio .file .Files ;
12+ import java .nio .file .Paths ;
13+ import java .security .KeyFactory ;
14+ import java .security .NoSuchAlgorithmException ;
15+ import java .security .interfaces .RSAPrivateCrtKey ;
16+ import java .security .spec .InvalidKeySpecException ;
17+ import java .security .spec .PKCS8EncodedKeySpec ;
1118import java .sql .Connection ;
1219import java .sql .DriverManager ;
1320import java .sql .ResultSet ;
1421import java .sql .SQLException ;
1522import java .sql .Statement ;
1623import java .util .ArrayList ;
24+ import java .util .Base64 ;
1725import java .util .HashMap ;
1826import java .util .List ;
1927import java .util .Map ;
@@ -52,20 +60,21 @@ enum Scope {
5260 PUT_FETCH_GET
5361 }
5462
55- public static void main (String [] args ) throws IOException , SQLException {
56- disableLogging ();
63+ public static void main (String [] args ) throws Exception {
5764 Map <String , String > arguments = parseArguments (args );
5865
59- String url = "jdbc:snowflake://" + arguments .get ("account" ) + "." + arguments . get ( " host" );
66+ String url = "jdbc:snowflake://" + arguments .get ("host" );
6067 Properties props = new Properties ();
6168 for (Map .Entry <String , String > entry : arguments .entrySet ()) {
6269 props .setProperty (entry .getKey (), entry .getValue ());
6370 }
71+ setPrivateKey (props );
72+ setupLogging (props );
6473
6574 javaVersion = props .getProperty ("java_version" );
6675 driverVersion = props .getProperty ("driver_version" );
6776
68- if (Scope .LOGIN .name ().toLowerCase ().equals (props .getProperty ("scope" ))){
77+ if (Scope .LOGIN .name ().toLowerCase ().equals (props .getProperty ("scope" ))) {
6978 testLogin (url , props );
7079 }
7180 if (Scope .PUT_FETCH_GET .name ().toLowerCase ().equals (props .getProperty ("scope" ))) {
@@ -84,10 +93,10 @@ private static void testLogin(String url, Properties properties) {
8493 } catch (SQLException e ) {
8594 success = false ;
8695 System .err .println (e .getMessage ());
87- logMetric ("cloudprober_driver_jdbc_perform_login " , Status .FAILURE );
96+ logMetric ("cloudprober_driver_java_perform_login " , Status .FAILURE );
8897 System .exit (1 );
8998 }
90- logMetric ("cloudprober_driver_jdbc_perform_login " , success ? Status .SUCCESS : Status .FAILURE );
99+ logMetric ("cloudprober_driver_java_perform_login " , success ? Status .SUCCESS : Status .FAILURE );
91100 }
92101
93102 private static void testPutFetchGet (String url , Properties properties ) {
@@ -271,8 +280,9 @@ private static void createDataStage(Statement statement) throws SQLException {
271280 }
272281 }
273282
274- private static void disableLogging () throws IOException {
275- String loggingPropertiesString = ".level=OFF" ;
283+ private static void setupLogging (Properties properties ) throws IOException {
284+ String loggingPropertiesString = "handlers=java.util.logging.ConsoleHandler\n .level=" + properties .getProperty ("log_level" );
285+ properties .put ("JAVA_LOGGING_CONSOLE_STD_OUT" , "false" );
276286 try (InputStream propertiesStream = new ByteArrayInputStream (
277287 loggingPropertiesString .getBytes (StandardCharsets .UTF_8 )
278288 )) {
@@ -281,7 +291,7 @@ private static void disableLogging() throws IOException {
281291 }
282292
283293 private static void logMetric (String metricName , Status status ) {
284- System .out .println (metricName + "{{ java_version=" + javaVersion + ", driver_version=" + driverVersion + "} " + status .getCode () + "}" );
294+ System .out .println (metricName + "{java_version=\" " + javaVersion + "\" , driver_version=\" " + driverVersion + "\" } " + status .getCode ());
285295 }
286296
287297 private static Map <String , String > parseArguments (String [] args ) {
@@ -341,4 +351,17 @@ private static String generateRandomString(int length) {
341351 builder .setCharAt (0 , Character .toUpperCase (builder .charAt (0 )));
342352 return builder .toString ();
343353 }
354+
355+ private static void setPrivateKey (Properties props ) throws IOException , NoSuchAlgorithmException , InvalidKeySpecException {
356+ String keyStr = new String (Files .readAllBytes (Paths .get (props .getProperty ("private_key_file" ))), StandardCharsets .UTF_8 ).trim ();
357+ byte [] keyBytes = Base64 .getUrlDecoder ().decode (keyStr );
358+
359+ // Convert the DER bytes to a private key object
360+ PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec (keyBytes );
361+ KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
362+ RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey ) keyFactory .generatePrivate (keySpec );
363+ props .put ("privateKey" , privateKey );
364+ // Remove the path from properties so the driver does not try to read it
365+ props .remove ("private_key_file" );
366+ }
344367}
0 commit comments