2222import java .io .EOFException ;
2323import java .io .File ;
2424import java .io .FileInputStream ;
25+ import java .io .FileNotFoundException ;
26+ import java .io .FileOutputStream ;
2527import java .io .IOException ;
2628import java .math .BigInteger ;
2729import java .net .NetworkInterface ;
3032import java .security .NoSuchAlgorithmException ;
3133import java .sql .PreparedStatement ;
3234import java .sql .SQLException ;
35+ import java .util .ArrayList ;
3336import java .util .HashMap ;
3437import java .util .List ;
3538import java .util .Properties ;
@@ -393,29 +396,28 @@ protected void updateKeyPairs() {
393396 s_logger .info ("Processing updateKeyPairs" );
394397 }
395398 String already = _configDao .getValue ("ssh.privatekey" );
399+ String homeDir = Script .runSimpleBashScript ("echo ~" );
400+ String userid = System .getProperty ("user.name" );
401+ if (homeDir == "~" ) {
402+ s_logger .error ("No home directory was detected. Set the HOME environment variable to point to your user profile or home directory." );
403+ throw new CloudRuntimeException ("No home directory was detected. Set the HOME environment variable to point to your user profile or home directory." );
404+ }
405+ File privkeyfile = new File (homeDir + "/.ssh/id_rsa" );
406+ File pubkeyfile = new File (homeDir + "/.ssh/id_rsa.pub" );
396407
397408 if (already == null || already .isEmpty ()) {
398409 if (s_logger .isInfoEnabled ()) {
399410 s_logger .info ("Need to store in the database" );
400411 }
412+ Script .runSimpleBashScript ("if [ -f ~/.ssh/id_rsa ] ; then true ; else yes '' | ssh-keygen -t rsa -q ; fi" );
401413
402- String homeDir = Script .runSimpleBashScript ("echo ~" );
403- if (homeDir == "~" ) {
404- s_logger .error ("No home directory was detected. Set the HOME environment variable to point to your user profile or home directory." );
405- throw new RuntimeException ("No home directory was detected. Set the HOME environment variable to point to your user profile or home directory." );
406- }
407-
408- String keygenOutput = Script .runSimpleBashScript ("if [ -f ~/.ssh/id_rsa ] ; then true ; else yes '' | ssh-keygen -t rsa -q ; fi" );
409-
410- File privkeyfile = new File (homeDir + "/.ssh/id_rsa" );
411- File pubkeyfile = new File (homeDir + "/.ssh/id_rsa.pub" );
412414 byte [] arr1 = new byte [4094 ]; // configuration table column value size
413415 try {
414416 new DataInputStream (new FileInputStream (privkeyfile )).readFully (arr1 );
415417 } catch (EOFException e ) {
416418 } catch (Exception e ) {
417419 s_logger .error ("Cannot read the private key file" ,e );
418- throw new RuntimeException ("Cannot read the private key file" );
420+ throw new CloudRuntimeException ("Cannot read the private key file" );
419421 }
420422 String privateKey = new String (arr1 ).trim ();
421423 byte [] arr2 = new byte [4094 ]; // configuration table column value size
@@ -424,7 +426,7 @@ protected void updateKeyPairs() {
424426 } catch (EOFException e ) {
425427 } catch (Exception e ) {
426428 s_logger .warn ("Cannot read the public key file" ,e );
427- throw new RuntimeException ("Cannot read the public key file" );
429+ throw new CloudRuntimeException ("Cannot read the public key file" );
428430 }
429431 String publicKey = new String (arr2 ).trim ();
430432
@@ -442,7 +444,7 @@ protected void updateKeyPairs() {
442444 }
443445 } catch (SQLException ex ) {
444446 s_logger .error ("SQL of the private key failed" ,ex );
445- throw new RuntimeException ("SQL of the private key failed" );
447+ throw new CloudRuntimeException ("SQL of the private key failed" );
446448 }
447449
448450 try {
@@ -453,26 +455,70 @@ protected void updateKeyPairs() {
453455 }
454456 } catch (SQLException ex ) {
455457 s_logger .error ("SQL of the public key failed" ,ex );
456- throw new RuntimeException ("SQL of the public key failed" );
458+ throw new CloudRuntimeException ("SQL of the public key failed" );
457459 }
458- injectSshKeyIntoSystemVmIsoPatch ( pubkeyfile . getAbsolutePath ());
460+
459461 if (s_logger .isDebugEnabled ()) {
460462 s_logger .debug ("Public key inserted into systemvm iso" );
461463 }
462464 } else {
463465 s_logger .info ("Keypairs already in database" );
466+ if (userid .startsWith ("cloud" )) {
467+ s_logger .info ("Keypairs already in database, updating local copy" );
468+ updateKeyPairsOnDisk (homeDir );
469+ }
470+ }
471+ if (userid .startsWith ("cloud" )){
472+ s_logger .info ("Going to update systemvm iso with generated keypairs if needed" );
473+ injectSshKeysIntoSystemVmIsoPatch (pubkeyfile .getAbsolutePath (), privkeyfile .getAbsolutePath ());
474+ }
475+ }
476+
477+ private void writeKeyToDisk (String key , String keyPath ) {
478+
479+ File keyfile = new File ( keyPath );
480+ if (!keyfile .exists ()) {
481+ try {
482+ keyfile .createNewFile ();
483+ } catch (IOException e ) {
484+ s_logger .warn ("Failed to create file: " + e .toString ());
485+ throw new CloudRuntimeException ("Failed to update keypairs on disk: cannot create key file " + keyPath );
486+ }
487+ }
488+
489+ if (keyfile .exists ()) {
490+ try {
491+ FileOutputStream kStream = new FileOutputStream (keyfile );
492+ kStream .write (key .getBytes ());
493+ kStream .close ();
494+ } catch (FileNotFoundException e ) {
495+ s_logger .warn ("Failed to write key to " + keyfile .getAbsolutePath ());
496+ throw new CloudRuntimeException ("Failed to update keypairs on disk: cannot find key file " + keyPath );
497+ } catch (IOException e ) {
498+ s_logger .warn ("Failed to write key to " + keyfile .getAbsolutePath ());
499+ throw new CloudRuntimeException ("Failed to update keypairs on disk: cannot write to key file " + keyPath );
500+ }
464501 }
502+
465503 }
466504
505+ private void updateKeyPairsOnDisk (String homeDir ) {
506+
507+ String pubKey = _configDao .getValue ("ssh.publickey" );
508+ String prvKey = _configDao .getValue ("ssh.privatekey" );
509+ writeKeyToDisk (homeDir + "/.ssh/id_rsa" , prvKey );
510+ writeKeyToDisk (homeDir + "/.ssh/id_rsa.pub" , pubKey );
511+ }
467512
468- protected void injectSshKeyIntoSystemVmIsoPatch (String publicKeyPath ) {
513+ protected void injectSshKeysIntoSystemVmIsoPatch (String publicKeyPath , String privKeyPath ) {
469514 String injectScript = "scripts/vm/systemvm/injectkeys.sh" ;
470515 String scriptPath = Script .findScript ("" , injectScript );
471516 if ( scriptPath == null ) {
472517 throw new CloudRuntimeException ("Unable to find key inject script " + injectScript );
473518 }
474519 final Script command = new Script (scriptPath , s_logger );
475520 command .add (publicKeyPath );
521+ command .add (privKeyPath );
476522
477523 final String result = command .execute ();
478524 if (result != null ) {
0 commit comments