diff --git a/cadc-inventory-db/build.gradle b/cadc-inventory-db/build.gradle index d96867ab..f6f235f8 100644 --- a/cadc-inventory-db/build.gradle +++ b/cadc-inventory-db/build.gradle @@ -25,7 +25,7 @@ def git_url = 'https://github.com/opencadc/storage-inventory' mainClassName = 'org.opencadc.inventory.db.version.Main' dependencies { - compile 'org.opencadc:cadc-util:[1.9.5,2.0)' + compile 'org.opencadc:cadc-util:[1.10.3,2.0)' compile 'org.opencadc:cadc-gms:[1.0.0,)' compile 'org.opencadc:cadc-inventory:[0.9.4,)' compile 'org.opencadc:cadc-vos:[2.0,3.0)' diff --git a/cadc-inventory-db/src/intTest/java/org/opencadc/inventory/db/KeyPairDAOTest.java b/cadc-inventory-db/src/intTest/java/org/opencadc/inventory/db/PreauthKeyPairDAOTest.java similarity index 86% rename from cadc-inventory-db/src/intTest/java/org/opencadc/inventory/db/KeyPairDAOTest.java rename to cadc-inventory-db/src/intTest/java/org/opencadc/inventory/db/PreauthKeyPairDAOTest.java index 5eb32c7b..b07aedcc 100644 --- a/cadc-inventory-db/src/intTest/java/org/opencadc/inventory/db/KeyPairDAOTest.java +++ b/cadc-inventory-db/src/intTest/java/org/opencadc/inventory/db/PreauthKeyPairDAOTest.java @@ -71,11 +71,12 @@ import ca.nrc.cadc.db.DBConfig; import ca.nrc.cadc.db.DBUtil; import ca.nrc.cadc.util.Log4jInit; +import ca.nrc.cadc.util.RsaSignatureGenerator; import java.net.URI; +import java.security.KeyPair; import java.security.MessageDigest; import java.util.Iterator; import java.util.Map; -import java.util.Random; import java.util.Set; import java.util.TreeMap; import javax.sql.DataSource; @@ -84,31 +85,31 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.opencadc.inventory.KeyPair; +import org.opencadc.inventory.PreauthKeyPair; import org.opencadc.inventory.db.version.InitDatabase; /** * * @author pdowler */ -public class KeyPairDAOTest { - private static final Logger log = Logger.getLogger(KeyPairDAOTest.class); +public class PreauthKeyPairDAOTest { + private static final Logger log = Logger.getLogger(PreauthKeyPairDAOTest.class); static { Log4jInit.setLevel("org.opencadc.inventory", Level.DEBUG); Log4jInit.setLevel("ca.nrc.cadc.db.version", Level.DEBUG); } - KeyPairDAO dao = new KeyPairDAO(); + PreauthKeyPairDAO dao = new PreauthKeyPairDAO(); - public KeyPairDAOTest()throws Exception { + public PreauthKeyPairDAOTest()throws Exception { DBConfig dbrc = new DBConfig(); ConnectionConfig cc = dbrc.getConnectionConfig(TestUtil.SERVER, TestUtil.DATABASE); - DBUtil.createJNDIDataSource("jdbc/KeyPairDAOTest", cc); + DBUtil.createJNDIDataSource("jdbc/PreauthKeyPairDAOTest", cc); Map config = new TreeMap(); config.put(SQLGenerator.class.getName(), SQLGenerator.class); - config.put("jndiDataSourceName", "jdbc/KeyPairDAOTest"); + config.put("jndiDataSourceName", "jdbc/PreauthKeyPairDAOTest"); config.put("database", TestUtil.DATABASE); config.put("schema", TestUtil.SCHEMA); dao.setConfig(config); @@ -126,7 +127,7 @@ public void setup() log.info("clearing old content..."); SQLGenerator gen = dao.getSQLGenerator(); DataSource ds = dao.getDataSource(); - String sql = "delete from " + gen.getTable(KeyPair.class); + String sql = "delete from " + gen.getTable(PreauthKeyPair.class); log.info("pre-test cleanup: " + sql); ds.getConnection().createStatement().execute(sql); log.info("clearing old content... OK"); @@ -135,17 +136,15 @@ public void setup() @Test public void testPutGetUpdateDelete() { String name = "testPutGetUpdateDelete"; - Random rnd = new Random(); - byte[] publicKey = new byte[128]; - rnd.nextBytes(publicKey); - byte[] privateKey = new byte[512]; - rnd.nextBytes(privateKey); - - + KeyPair kp = RsaSignatureGenerator.getKeyPair(4096); + byte[] publicKey = kp.getPublic().getEncoded(); + byte[] privateKey = kp.getPrivate().getEncoded(); + log.info("generated keys (4096): " + publicKey.length + "," + privateKey.length); try { - KeyPair expected = new KeyPair(name, publicKey, privateKey); + + PreauthKeyPair expected = new PreauthKeyPair(name, publicKey, privateKey); - KeyPair notFound = dao.get(expected.getID()); + PreauthKeyPair notFound = dao.get(expected.getID()); Assert.assertNull(notFound); dao.put(expected); @@ -159,7 +158,7 @@ public void testPutGetUpdateDelete() { Assert.assertEquals("put metachecksum", mcs, mcs0); // get by ID - KeyPair fid = dao.get(expected.getID()); + PreauthKeyPair fid = dao.get(expected.getID()); Assert.assertNotNull(fid); Assert.assertEquals(expected.getName(), fid.getName()); Assert.assertEquals(expected.getPublicKey().length, fid.getPublicKey().length); @@ -179,12 +178,12 @@ public void testPutGetUpdateDelete() { // TODO: udpate // list - Set keys = dao.list(); + Set keys = dao.list(); Assert.assertNotNull(keys); Assert.assertEquals(1, keys.size()); - Iterator iter = keys.iterator(); + Iterator iter = keys.iterator(); Assert.assertTrue(iter.hasNext()); - KeyPair actual = iter.next(); + PreauthKeyPair actual = iter.next(); Assert.assertEquals(expected.getPublicKey().length, fid.getPublicKey().length); Assert.assertEquals(expected.getPrivateKey().length, fid.getPrivateKey().length); URI mcs3 = fid.computeMetaChecksum(MessageDigest.getInstance("MD5")); @@ -192,7 +191,7 @@ public void testPutGetUpdateDelete() { // delete dao.delete(expected.getID()); - KeyPair deleted = dao.get(expected.getID()); + PreauthKeyPair deleted = dao.get(expected.getID()); Assert.assertNull(deleted); } catch (Exception unexpected) { diff --git a/cadc-inventory-db/src/main/java/org/opencadc/inventory/KeyPair.java b/cadc-inventory-db/src/main/java/org/opencadc/inventory/PreauthKeyPair.java similarity index 85% rename from cadc-inventory-db/src/main/java/org/opencadc/inventory/KeyPair.java rename to cadc-inventory-db/src/main/java/org/opencadc/inventory/PreauthKeyPair.java index 2559e4f6..3266918b 100644 --- a/cadc-inventory-db/src/main/java/org/opencadc/inventory/KeyPair.java +++ b/cadc-inventory-db/src/main/java/org/opencadc/inventory/PreauthKeyPair.java @@ -76,29 +76,29 @@ * * @author pdowler */ -public class KeyPair extends Entity implements Comparable { - private static final Logger log = Logger.getLogger(KeyPair.class); +public class PreauthKeyPair extends Entity implements Comparable { + private static final Logger log = Logger.getLogger(PreauthKeyPair.class); private final String name; private final byte[] publicKey; private final byte[] privateKey; - public KeyPair(String name, byte[] publicKey, byte[] privateKey) { + public PreauthKeyPair(String name, byte[] publicKey, byte[] privateKey) { super(); - InventoryUtil.assertNotNull(KeyPair.class, "name", name); - InventoryUtil.assertNotNull(KeyPair.class, "publicKey", publicKey); - InventoryUtil.assertNotNull(KeyPair.class, "privateKey", privateKey); + InventoryUtil.assertNotNull(PreauthKeyPair.class, "name", name); + InventoryUtil.assertNotNull(PreauthKeyPair.class, "publicKey", publicKey); + InventoryUtil.assertNotNull(PreauthKeyPair.class, "privateKey", privateKey); this.name = name; this.publicKey = publicKey; this.privateKey = privateKey; } // ctor for DAO class - public KeyPair(UUID id, String name, byte[] publicKey, byte[] privateKey) { + public PreauthKeyPair(UUID id, String name, byte[] publicKey, byte[] privateKey) { super(id); - InventoryUtil.assertNotNull(KeyPair.class, "name", name); - InventoryUtil.assertNotNull(KeyPair.class, "publicKey", publicKey); - InventoryUtil.assertNotNull(KeyPair.class, "privateKey", privateKey); + InventoryUtil.assertNotNull(PreauthKeyPair.class, "name", name); + InventoryUtil.assertNotNull(PreauthKeyPair.class, "publicKey", publicKey); + InventoryUtil.assertNotNull(PreauthKeyPair.class, "privateKey", privateKey); this.name = name; this.publicKey = publicKey; this.privateKey = privateKey; @@ -128,12 +128,12 @@ public boolean equals(Object o) { if (o == null) { return false; } - KeyPair f = (KeyPair) o; + PreauthKeyPair f = (PreauthKeyPair) o; return this.compareTo(f) == 0; } @Override - public int compareTo(KeyPair t) { + public int compareTo(PreauthKeyPair t) { return name.compareTo(t.name); } } diff --git a/cadc-inventory-db/src/main/java/org/opencadc/inventory/db/KeyPairDAO.java b/cadc-inventory-db/src/main/java/org/opencadc/inventory/db/PreauthKeyPairDAO.java similarity index 88% rename from cadc-inventory-db/src/main/java/org/opencadc/inventory/db/KeyPairDAO.java rename to cadc-inventory-db/src/main/java/org/opencadc/inventory/db/PreauthKeyPairDAO.java index 6c67df8d..aa808559 100644 --- a/cadc-inventory-db/src/main/java/org/opencadc/inventory/db/KeyPairDAO.java +++ b/cadc-inventory-db/src/main/java/org/opencadc/inventory/db/PreauthKeyPairDAO.java @@ -71,7 +71,7 @@ import java.util.Set; import java.util.UUID; import org.apache.log4j.Logger; -import org.opencadc.inventory.KeyPair; +import org.opencadc.inventory.PreauthKeyPair; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.core.JdbcTemplate; @@ -80,22 +80,22 @@ * * @author pdowler */ -public class KeyPairDAO extends AbstractDAO { - private static final Logger log = Logger.getLogger(KeyPairDAO.class); +public class PreauthKeyPairDAO extends AbstractDAO { + private static final Logger log = Logger.getLogger(PreauthKeyPairDAO.class); - public KeyPairDAO() { + public PreauthKeyPairDAO() { super(true); } - public KeyPairDAO(AbstractDAO src) { + public PreauthKeyPairDAO(AbstractDAO src) { super(src); } - public KeyPair get(UUID id) { - return super.get(KeyPair.class, id); + public PreauthKeyPair get(UUID id) { + return super.get(PreauthKeyPair.class, id); } - public KeyPair get(String name) { + public PreauthKeyPair get(String name) { if (name == null) { throw new IllegalArgumentException("name cannot be null"); } @@ -106,9 +106,9 @@ public KeyPair get(String name) { try { JdbcTemplate jdbc = new JdbcTemplate(dataSource); - SQLGenerator.KeyPairGet get = ( SQLGenerator.KeyPairGet) gen.getEntityGet(KeyPair.class); + SQLGenerator.KeyPairGet get = ( SQLGenerator.KeyPairGet) gen.getEntityGet(PreauthKeyPair.class); get.setName(name); - KeyPair ret = get.execute(jdbc); + PreauthKeyPair ret = get.execute(jdbc); return ret; } catch (BadSqlGrammarException ex) { handleInternalFail(ex); @@ -120,18 +120,18 @@ public KeyPair get(String name) { } public void delete(UUID id) { - super.delete(KeyPair.class, id); + super.delete(PreauthKeyPair.class, id); } - public Set list() { + public Set list() { checkInit(); log.debug("LIST"); long t = System.currentTimeMillis(); try { JdbcTemplate jdbc = new JdbcTemplate(dataSource); - EntityList get = gen.getEntityList(KeyPair.class); - Set result = get.query(jdbc); + EntityList get = gen.getEntityList(PreauthKeyPair.class); + Set result = get.query(jdbc); return result; } catch (BadSqlGrammarException ex) { handleInternalFail(ex); diff --git a/cadc-inventory-db/src/main/java/org/opencadc/inventory/db/SQLGenerator.java b/cadc-inventory-db/src/main/java/org/opencadc/inventory/db/SQLGenerator.java index 629571fa..4a347107 100644 --- a/cadc-inventory-db/src/main/java/org/opencadc/inventory/db/SQLGenerator.java +++ b/cadc-inventory-db/src/main/java/org/opencadc/inventory/db/SQLGenerator.java @@ -94,7 +94,7 @@ import org.opencadc.inventory.DeletedArtifactEvent; import org.opencadc.inventory.DeletedStorageLocationEvent; import org.opencadc.inventory.InventoryUtil; -import org.opencadc.inventory.KeyPair; +import org.opencadc.inventory.PreauthKeyPair; import org.opencadc.inventory.ObsoleteStorageLocation; import org.opencadc.inventory.SiteLocation; import org.opencadc.inventory.StorageLocation; @@ -162,7 +162,7 @@ protected void init() { // internal this.tableMap.put(ObsoleteStorageLocation.class, pref + ObsoleteStorageLocation.class.getSimpleName()); this.tableMap.put(HarvestState.class, pref + HarvestState.class.getSimpleName()); - this.tableMap.put(KeyPair.class, pref + KeyPair.class.getSimpleName()); + this.tableMap.put(PreauthKeyPair.class, pref + PreauthKeyPair.class.getSimpleName()); String[] cols = new String[] { "uri", // first column is logical key @@ -227,7 +227,7 @@ protected void init() { "metaChecksum", "id" // last column is always PK }; - this.columnMap.put(KeyPair.class, cols); + this.columnMap.put(PreauthKeyPair.class, cols); // optional vospace log.debug("vosSchema: " + vosSchema); @@ -315,7 +315,7 @@ public EntityGet getEntityGet(Class c, boolean forUpdate) { if (StorageSite.class.equals(c)) { return new StorageSiteGet(forUpdate); } - if (KeyPair.class.equals(c)) { + if (PreauthKeyPair.class.equals(c)) { return new KeyPairGet(forUpdate); } if (Node.class.equals(c)) { @@ -389,7 +389,7 @@ public EntityList getEntityList(Class c) { if (StorageSite.class.equals(c)) { return new StorageSiteList(); } - if (KeyPair.class.equals(c)) { + if (PreauthKeyPair.class.equals(c)) { return new KeyPairList(); } throw new UnsupportedOperationException("entity-list: " + c.getName()); @@ -422,7 +422,7 @@ public EntityPut getEntityPut(Class c, boolean update) { if (HarvestState.class.equals(c)) { return new HarvestStatePut(update); } - if (KeyPair.class.equals(c)) { + if (PreauthKeyPair.class.equals(c)) { return new KeyPairPut(update); } if (Node.class.isAssignableFrom(c)) { @@ -852,7 +852,7 @@ public PreparedStatement createPreparedStatement(Connection conn) throws SQLExce } } - class KeyPairGet implements EntityGet { + class KeyPairGet implements EntityGet { private UUID id; private String name; private final boolean forUpdate; @@ -871,16 +871,16 @@ public void setName(String name) { } @Override - public KeyPair execute(JdbcTemplate jdbc) { - return (KeyPair) jdbc.query(this, new KeyPairExtractor()); + public PreauthKeyPair execute(JdbcTemplate jdbc) { + return (PreauthKeyPair) jdbc.query(this, new KeyPairExtractor()); } @Override public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { - StringBuilder sb = getSelectFromSQL(KeyPair.class, false); + StringBuilder sb = getSelectFromSQL(PreauthKeyPair.class, false); sb.append(" WHERE "); if (id != null) { - String col = getKeyColumn(KeyPair.class, true); + String col = getKeyColumn(PreauthKeyPair.class, true); sb.append(col).append(" = ?"); } else if (name != null) { sb.append("name = ?"); @@ -902,19 +902,19 @@ public PreparedStatement createPreparedStatement(Connection conn) throws SQLExce } } - private class KeyPairList implements EntityList { + private class KeyPairList implements EntityList { @Override - public Set query(JdbcTemplate jdbc) { - List keys = (List) jdbc.query(this, new KeyPairRowMapper()); - Set ret = new TreeSet<>(); + public Set query(JdbcTemplate jdbc) { + List keys = (List) jdbc.query(this, new KeyPairRowMapper()); + Set ret = new TreeSet<>(); ret.addAll(keys); return ret; } @Override public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { - StringBuilder sb = getSelectFromSQL(KeyPair.class, false); + StringBuilder sb = getSelectFromSQL(PreauthKeyPair.class, false); String sql = sb.toString(); log.debug("KeyPairList: " + sql); PreparedStatement prep = conn.prepareStatement(sql); @@ -1357,17 +1357,17 @@ public PreparedStatement createPreparedStatement(Connection conn) throws SQLExce } - private class KeyPairPut implements EntityPut { + private class KeyPairPut implements EntityPut { private final Calendar utc = Calendar.getInstance(DateUtil.UTC); private final boolean update; - private KeyPair value; + private PreauthKeyPair value; KeyPairPut(boolean update) { this.update = update; } @Override - public void setValue(KeyPair value) { + public void setValue(PreauthKeyPair value) { this.value = value; } @@ -1380,10 +1380,10 @@ public void execute(JdbcTemplate jdbc) { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { String sql = null; if (update) { - sql = getUpdateSQL(KeyPair.class); + sql = getUpdateSQL(PreauthKeyPair.class); } else { - sql = getInsertSQL(KeyPair.class); + sql = getInsertSQL(PreauthKeyPair.class); } log.debug("KeyPairPut: " + sql); PreparedStatement prep = conn.prepareStatement(sql); @@ -2013,11 +2013,11 @@ public StorageSite extractData(ResultSet rs) throws SQLException, DataAccessExce } } - private class KeyPairRowMapper implements RowMapper { + private class KeyPairRowMapper implements RowMapper { Calendar utc = Calendar.getInstance(DateUtil.UTC); @Override - public KeyPair mapRow(ResultSet rs, int i) throws SQLException { + public PreauthKeyPair mapRow(ResultSet rs, int i) throws SQLException { int col = 1; final String name = rs.getString(col++); final byte[] pub = rs.getBytes(col++); @@ -2027,18 +2027,18 @@ public KeyPair mapRow(ResultSet rs, int i) throws SQLException { final URI metaChecksum = Util.getURI(rs, col++); final UUID id = Util.getUUID(rs, col++); - KeyPair s = new KeyPair(id, name, pub, priv); + PreauthKeyPair s = new PreauthKeyPair(id, name, pub, priv); InventoryUtil.assignLastModified(s, lastModified); InventoryUtil.assignMetaChecksum(s, metaChecksum); return s; } } - private class KeyPairExtractor implements ResultSetExtractor { + private class KeyPairExtractor implements ResultSetExtractor { final Calendar utc = Calendar.getInstance(DateUtil.UTC); @Override - public KeyPair extractData(ResultSet rs) throws SQLException, DataAccessException { + public PreauthKeyPair extractData(ResultSet rs) throws SQLException, DataAccessException { if (!rs.next()) { return null; } diff --git a/cadc-inventory-db/src/main/resources/inventory.KeyPair.sql b/cadc-inventory-db/src/main/resources/inventory.KeyPair.sql index 1902222a..50fc0dcc 100644 --- a/cadc-inventory-db/src/main/resources/inventory.KeyPair.sql +++ b/cadc-inventory-db/src/main/resources/inventory.KeyPair.sql @@ -1,5 +1,5 @@ -create table .KeyPair ( +create table .PreauthKeyPair ( name varchar(32) not null, publicKey bytea not null, privateKey bytea not null, @@ -9,4 +9,4 @@ create table .KeyPair ( metaChecksum varchar(136) not null ); -create unique index kp_name_index on .KeyPair(name); +create unique index kp_name_index on .PreauthKeyPair(name);