From 8a5a90de7ed9712ad823dd60f0eecabddfd096bb Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 29 Sep 2014 18:15:35 +0200 Subject: [Db*] Fix SELECT for DbImage, change Timestamp to long, load public key from DB for satellite --- .../java/org/openslx/imagemaster/db/DbImage.java | 17 +++-- .../org/openslx/imagemaster/db/DbSatellite.java | 73 +++++++++++++++++----- 2 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/openslx/imagemaster/db/DbImage.java b/src/main/java/org/openslx/imagemaster/db/DbImage.java index a7a2adb..18a1cc8 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbImage.java +++ b/src/main/java/org/openslx/imagemaster/db/DbImage.java @@ -1,6 +1,5 @@ package org.openslx.imagemaster.db; -import java.sql.Timestamp; import java.util.List; import org.openslx.imagemaster.Globals; @@ -22,8 +21,8 @@ public class DbImage * Relative path of image file (relative to Globals.getImageDir()) */ public final String imagePath; - public final Timestamp imageCreateTime; - public final Timestamp imageUpdateTime; + public final long imageCreateTime; + public final long imageUpdateTime; public final int imageOwnerId; public final String contentOperatingSystem; public final boolean isValid; @@ -39,8 +38,8 @@ public class DbImage this.imageVersion = 0; this.imageName = null; this.imagePath = null; - this.imageCreateTime = null; - this.imageUpdateTime = null; + this.imageCreateTime = 0; + this.imageUpdateTime = 0; this.imageOwnerId = 0; this.contentOperatingSystem = null; this.isValid = false; @@ -52,7 +51,7 @@ public class DbImage } public DbImage( String uuid, int imageVersion, String imageName, String imagePath, - Timestamp imageCreateTime, Timestamp imageUpdateTime, int imageOwnerId, String contentOperatingSystem, + long imageCreateTime, long imageUpdateTime, int imageOwnerId, String contentOperatingSystem, boolean isValid, boolean isDeleted, String shortDescription, String longDescription, long fileSize, String missingBlocksList ) { @@ -192,7 +191,7 @@ public class DbImage return MySQL .findUniqueOrNull( DbImage.class, - "SELECT images.UUID, images.image_version, images.image_name, images.image_path, images.image_createTime, images.image_updateTime, images.image_owner, images.content_operatingSystem, images.status_isValid, images.status_isDeleted, images.image_shortDescription, images.image_longDescription, images.timestamp, images.fileSize, images.token, images.missingBlocks, images.serverSessionId FROM images WHERE uuid = ?", + "SELECT images.UUID, images.image_version, images.image_name, images.image_path, images.image_createTime, images.image_updateTime, images.image_owner, images.content_operatingSystem, images.status_isValid, images.status_isDeleted, images.image_shortDescription, images.image_longDescription, images.fileSize, images.missingBlocks FROM images WHERE uuid = ?", uuid ); } @@ -207,8 +206,8 @@ public class DbImage DbUser user = DbUser.forLogin( this.imageOwnerId ); if (user != null) owner = user.getLogin(); - return new ImageData( this.uuid, this.imageVersion, this.imageName, this.imageCreateTime.getTime(), - this.imageUpdateTime.getTime(), owner, this.contentOperatingSystem, this.isValid, + return new ImageData( this.uuid, this.imageVersion, this.imageName, this.imageCreateTime, + this.imageUpdateTime, owner, this.contentOperatingSystem, this.isValid, this.isDeleted, this.shortDescription, this.longDescription, this.fileSize ); } diff --git a/src/main/java/org/openslx/imagemaster/db/DbSatellite.java b/src/main/java/org/openslx/imagemaster/db/DbSatellite.java index fb35fb5..65450ed 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbSatellite.java +++ b/src/main/java/org/openslx/imagemaster/db/DbSatellite.java @@ -1,6 +1,13 @@ package org.openslx.imagemaster.db; -import org.openslx.imagemaster.util.ByteArray; +import java.math.BigInteger; +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.RSAPublicKeySpec; + +import org.apache.log4j.Logger; /** * Represents a satellite in the database. @@ -8,16 +15,31 @@ import org.openslx.imagemaster.util.ByteArray; */ public class DbSatellite { + private static final Logger LOG = Logger.getLogger( DbSatellite.class ); - private String organization, address, name, prefix; + private String organization, address, name, prefix, publickeyString; + private PublicKey publickey = null; + private static final KeyFactory keyFact; + + static + { + KeyFactory kf; + try { + kf = KeyFactory.getInstance( "RSA" ); + } catch ( NoSuchAlgorithmException e ) { + kf = null; + } + keyFact = kf; + } // needs to be public in order to be found by MySQL - public DbSatellite(String organization, String address, String name, String prefix) + public DbSatellite( String organization, String address, String name, String prefix, String publickeyString ) { this.organization = organization; this.address = address; this.name = name; this.prefix = prefix; + this.publickeyString = publickeyString; } public static DbSatellite fromOrganization( String organization ) @@ -25,10 +47,19 @@ public class DbSatellite return MySQL .findUniqueOrNull( DbSatellite.class, - "SELECT satellite.organization, satellite.address, satellite.name, satellite.prefix FROM satellite WHERE satellite.organization = ? LIMIT 1", + "SELECT satellite.organization, satellite.address, satellite.name, satellite.prefix, satellite.publickey FROM satellite WHERE satellite.organization = ? LIMIT 1", organization ); } + public static DbSatellite fromPrefix( String prefix ) + { + return MySQL + .findUniqueOrNull( + DbSatellite.class, + "SELECT satellite.organization, satellite.address, satellite.name, satellite.prefix, satellite.publickey FROM satellite WHERE satellite.prefix = ? LIMIT 1", + prefix ); + } + public String getAddress() { return address; @@ -49,17 +80,31 @@ public class DbSatellite return this.prefix; } - public static DbSatellite fromPrefix( String prefix ) + /** + * Get the public key of this organization, if known and valid. + * + * @return Public key, null on error or not known + */ + public PublicKey getPubkey() { - return MySQL - .findUniqueOrNull( - DbSatellite.class, - "SELECT satellite.organization, satellite.address, satellite.name, satellite.prefix FROM satellite WHERE satellite.prefix = ? LIMIT 1", - prefix ); + if ( publickey == null && keyFact != null && publickeyString != null ) { + String parts[] = publickeyString.split( " " ); + if ( parts.length != 2 ) + return null; + try { + BigInteger mod = new BigInteger( parts[0] ); + BigInteger exp = new BigInteger( parts[1] ); + RSAPublicKeySpec keySpec = new RSAPublicKeySpec( mod, exp ); + synchronized ( keyFact ) { + publickey = keyFact.generatePublic( keySpec ); + } + } catch ( InvalidKeySpecException e ) { + LOG.info( "PubKey of " + this.organization + " is not valid.", e ); + } catch ( NumberFormatException e ) { + LOG.info( "PubKey of " + this.organization + " is corrupted in database!", e ); + } + } + return publickey; } - public static byte[] getKeyfromOrganization( String organization ) - { - return MySQL.findUniqueOrNull( ByteArray.class, "SELECT publickey FROM satellite WHERE organization = ?", organization ).array; - } } -- cgit v1.2.3-55-g7522