Skip to content

Commit

Permalink
cadc-inventory-db: rename KeyPair to PreauthKeyPair
Browse files Browse the repository at this point in the history
  • Loading branch information
pdowler committed Dec 13, 2023
1 parent fed6c9b commit 0f194e8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 78 deletions.
2 changes: 1 addition & 1 deletion cadc-inventory-db/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String,Object> config = new TreeMap<String,Object>();
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);
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -179,20 +178,20 @@ public void testPutGetUpdateDelete() {
// TODO: udpate

// list
Set<KeyPair> keys = dao.list();
Set<PreauthKeyPair> keys = dao.list();
Assert.assertNotNull(keys);
Assert.assertEquals(1, keys.size());
Iterator<KeyPair> iter = keys.iterator();
Iterator<PreauthKeyPair> 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"));
Assert.assertEquals("round trip metachecksum", mcs, mcs3);

// delete
dao.delete(expected.getID());
KeyPair deleted = dao.get(expected.getID());
PreauthKeyPair deleted = dao.get(expected.getID());
Assert.assertNull(deleted);

} catch (Exception unexpected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,29 @@
*
* @author pdowler
*/
public class KeyPair extends Entity implements Comparable<KeyPair> {
private static final Logger log = Logger.getLogger(KeyPair.class);
public class PreauthKeyPair extends Entity implements Comparable<PreauthKeyPair> {
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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -80,22 +80,22 @@
*
* @author pdowler
*/
public class KeyPairDAO extends AbstractDAO<KeyPair> {
private static final Logger log = Logger.getLogger(KeyPairDAO.class);
public class PreauthKeyPairDAO extends AbstractDAO<PreauthKeyPair> {
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");
}
Expand All @@ -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);
Expand All @@ -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<KeyPair> list() {
public Set<PreauthKeyPair> list() {
checkInit();
log.debug("LIST");
long t = System.currentTimeMillis();

try {
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
EntityList get = gen.getEntityList(KeyPair.class);
Set<KeyPair> result = get.query(jdbc);
EntityList get = gen.getEntityList(PreauthKeyPair.class);
Set<PreauthKeyPair> result = get.query(jdbc);
return result;
} catch (BadSqlGrammarException ex) {
handleInternalFail(ex);
Expand Down
Loading

0 comments on commit 0f194e8

Please sign in to comment.