From 85547f845caad8715a4c3418af11c96c352c643a Mon Sep 17 00:00:00 2001 From: Björn Hagemeister Date: Thu, 16 Oct 2014 15:36:11 +0200 Subject: Implemented --submitkey command line option. --- src/main/java/org/openslx/satellitedaemon/App.java | 7 ++- .../java/org/openslx/satellitedaemon/Identity.java | 55 +++++++++++----------- .../filetransfer/ThriftConnection.java | 13 +++++ 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java index 24a9f2b..3a81f56 100644 --- a/src/main/java/org/openslx/satellitedaemon/App.java +++ b/src/main/java/org/openslx/satellitedaemon/App.java @@ -49,12 +49,12 @@ public class App else System.exit( 2 ); } else { - log.error( "--genid requires an organization name" ); + log.error( "--genid requires " ); System.exit( 2 ); } } else if ( arg.equals( "--import" ) ) { if ( ( i + 4 ) < args.length ) { - log.error( "Illelgal option: '--import' requires 4 arguments, " ); + log.error( "Illegal option: '--import' requires 4 arguments, " ); System.exit( 2 ); } else { organizationName = args[i++]; @@ -137,8 +137,7 @@ public class App private static boolean submitKey( String ipAddress ) { - // TODO. - return false; + return Identity.submitKey( ipAddress ); } private static boolean updateAddress( String ipAddress ) diff --git a/src/main/java/org/openslx/satellitedaemon/Identity.java b/src/main/java/org/openslx/satellitedaemon/Identity.java index ce6b753..e95cf99 100644 --- a/src/main/java/org/openslx/satellitedaemon/Identity.java +++ b/src/main/java/org/openslx/satellitedaemon/Identity.java @@ -11,12 +11,15 @@ import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; import java.util.Properties; import java.util.Random; import org.apache.log4j.Logger; import org.openslx.encryption.AsymKeyHolder; +import org.openslx.satellitedaemon.filetransfer.ThriftConnection; import org.openslx.satellitedaemon.util.Util; public class Identity @@ -24,31 +27,26 @@ public class Identity private static Logger log = Logger.getLogger( Identity.class ); private static final Properties properties = new Properties(); - private static String organizationName = null; - private static BigInteger privExponent = null; - private static BigInteger pubExponent = null; - private static BigInteger modulus = null; - private static AsymKeyHolder akh = null; public static String getOrganizationName() { - return organizationName = properties.getProperty( "ORGANIZATION_NAME" ); + return properties.getProperty( "ORGANIZATION_NAME" ); } private static BigInteger getModulus() { - return modulus = toBigInt( properties.getProperty( "MODULUS" ) ); + return toBigInt( properties.getProperty( "MODULUS" ) ); } private static BigInteger getPublicExponent() { - return pubExponent = toBigInt( properties.getProperty( "PUBLIC_EXPONENT" ) ); + return toBigInt( properties.getProperty( "PUBLIC_EXPONENT" ) ); } private static BigInteger getPrivateExponent() { - return privExponent = toBigInt( properties.getProperty( "PRIVATE_EXPONENT" ) ); + return toBigInt( properties.getProperty( "PRIVATE_EXPONENT" ) ); } /** @@ -156,18 +154,9 @@ public class Identity */ public static boolean generateIdentity( String organizationName ) { - Identity.organizationName = organizationName; // generate new key pair. Identity.akh = new AsymKeyHolder(); - Identity.modulus = akh.getModulus(); - Identity.privExponent = akh.getPrivateExponent(); - Identity.pubExponent = akh.getPublicExponent(); - - return writeIdToFile( - Identity.organizationName, - Identity.modulus, - Identity.privExponent, - Identity.pubExponent ); + return writeIdToFile( organizationName, akh.getModulus(), akh.getPrivateExponent(), akh.getPublicExponent() ); } /** @@ -182,15 +171,25 @@ public class Identity */ public static boolean importIdentity( String organizationName, BigInteger modulus, BigInteger privateExp, BigInteger publicExp ) { - Identity.organizationName = organizationName; - Identity.modulus = modulus; - Identity.privExponent = privateExp; - Identity.pubExponent = publicExp; - return writeIdToFile( - Identity.organizationName, - Identity.modulus, - Identity.privExponent, - Identity.pubExponent ); + return writeIdToFile( organizationName, modulus, privateExp, publicExp ); + } + + public static boolean submitKey( String ipAddress ) + { + RSAPublicKey pubKey = (RSAPublicKey)getPublicKey(); + RSAPrivateKey privKey = (RSAPrivateKey)getPrivateKey(); + assert ( pubKey.getModulus() == privKey.getModulus() ); + + if ( !Identity.isValidKeyPair( + privKey.getModulus(), + privKey.getPrivateExponent(), + pubKey.getPublicExponent() ) ) + return false; + return ThriftConnection.registerSatellite( + getOrganizationName(), + ipAddress, + pubKey.getModulus().toString(), + pubKey.getPublicExponent().toString() ); } /** diff --git a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java index f716077..723da9b 100644 --- a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java +++ b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java @@ -272,4 +272,17 @@ public class ThriftConnection } return false; } + + public static boolean registerSatellite( String organizationId, String ipAddress, String modulus, String exponent ) + { + ImageServer.Client theClient = null; + theClient = getConnection(); + // No check for valid connection. --> not needed, because this satellite is not known yet by master. + try { + return theClient.registerSatellite( organizationId, ipAddress, modulus, exponent ); + } catch ( TException e ) { + log.error( "TException", e ); + return false; + } + } } -- cgit v1.2.3-55-g7522