Skip to content

Commit

Permalink
Fix filter for lastSerial in KeyRepository and tidyup
Browse files Browse the repository at this point in the history
The filter can be simplified removing the first condition in the and
('&') since it always true for all the records.

Additionally, some tidyup fixing log format, array designators and other
minor improvements.
  • Loading branch information
fmarco76 committed May 29, 2024
1 parent 1516d24 commit 58a2dc4
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions base/kra/src/main/java/com/netscape/cmscore/dbs/KeyRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import java.math.BigInteger;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

import org.mozilla.jss.netscape.security.x509.X500Name;

Expand All @@ -41,7 +42,7 @@
*/
public class KeyRepository extends Repository {

public static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(KeyRepository.class);
public static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(KeyRepository.class);

public static final String PROP_KEY_ID_GENERATOR = "key.id.generator";
public static final String DEFAULT_KEY_ID_GENERATOR = "legacy";
Expand Down Expand Up @@ -74,24 +75,24 @@ public void init() throws Exception {
DatabaseConfig dbConfig = dbSubsystem.getDBConfigStore();

mBaseDN = dbConfig.getSerialDN() + "," + dbSubsystem.getBaseDN();
logger.info("KeyRepository: - base DN: " + mBaseDN);
logger.info("KeyRepository: - base DN: {}", mBaseDN);

String value = dbConfig.getString(PROP_KEY_ID_GENERATOR, DEFAULT_KEY_ID_GENERATOR);
logger.info("KeyRepository: - key ID generator: " + value);
logger.info("KeyRepository: - key ID generator: {}", value);
setIDGenerator(value);

if (idGenerator == IDGenerator.RANDOM) {

idLength = dbConfig.getInteger(PROP_KEY_ID_LENGTH, DEFAULT_KEY_ID_LENGTH);
logger.info("KeyRepository: - key ID length: " + idLength);
logger.info("KeyRepository: - key ID length: {}", idLength);

} else {
initLegacyGenerator();
}

// register key record schema
DBRegistry reg = dbSubsystem.getRegistry();
String keyRecordOC[] = new String[2];
String[] keyRecordOC = new String[2];

keyRecordOC[0] = KeyDBSchema.LDAP_OC_TOP;
keyRecordOC[1] = KeyDBSchema.LDAP_OC_KEYRECORD;
Expand Down Expand Up @@ -172,21 +173,21 @@ public void initLegacyGenerator() throws Exception {
DatabaseConfig dbConfig = dbSubsystem.getDBConfigStore();

rangeDN = dbConfig.getSerialRangeDN() + "," + dbSubsystem.getBaseDN();
logger.info("KeyRepository: - range DN: " + rangeDN);
logger.info("KeyRepository: - range DN: {}", rangeDN);

minSerialName = DatabaseConfig.MIN_SERIAL_NUMBER;
String minSerial = dbConfig.getBeginSerialNumber();
if (minSerial != null) {
mMinSerialNo = new BigInteger(minSerial, mRadix);
}
logger.info("KeyRepository: - min serial: " + mMinSerialNo);
logger.info("KeyRepository: - min serial: {}", mMinSerialNo);

maxSerialName = DatabaseConfig.MAX_SERIAL_NUMBER;
String maxSerial = dbConfig.getEndSerialNumber();
if (maxSerial != null) {
mMaxSerialNo = new BigInteger(maxSerial, mRadix);
}
logger.info("KeyRepository: - max serial: " + mMaxSerialNo);
logger.info("KeyRepository: - max serial: {}", mMaxSerialNo);

nextMinSerialName = DatabaseConfig.NEXT_MIN_SERIAL_NUMBER;
String nextMinSerial = dbConfig.getNextBeginSerialNumber();
Expand All @@ -195,7 +196,7 @@ public void initLegacyGenerator() throws Exception {
} else {
mNextMinSerialNo = new BigInteger(nextMinSerial, mRadix);
}
logger.info("KeyRepository: - next min serial: " + mNextMinSerialNo);
logger.info("KeyRepository: - next min serial: {}", mNextMinSerialNo);

nextMaxSerialName = DatabaseConfig.NEXT_MAX_SERIAL_NUMBER;
String nextMaxSerial = dbConfig.getNextEndSerialNumber();
Expand All @@ -204,7 +205,7 @@ public void initLegacyGenerator() throws Exception {
} else {
mNextMaxSerialNo = new BigInteger(nextMaxSerial, mRadix);
}
logger.info("KeyRepository: - next max serial: " + mNextMaxSerialNo);
logger.info("KeyRepository: - next max serial: {}", mNextMaxSerialNo);

String lowWaterMark = dbConfig.getSerialLowWaterMark();
if (lowWaterMark != null) {
Expand Down Expand Up @@ -245,14 +246,14 @@ public void removeAllObjects() throws EBaseException {
* @param record key record
* @exception EBaseException failed to archive key
*/
public void addKeyRecord(KeyRecord record) throws EBaseException {
public void addKeyRecord(KeyRecord kRecord) throws EBaseException {

try (DBSSession s = dbSubsystem.createSession()) {
String name = "cn" + "=" +
record.getSerialNumber().toString() + "," + getDN();
kRecord.getSerialNumber().toString() + "," + getDN();

if (s != null)
s.add(name, record);
s.add(name, kRecord);
}
}

Expand Down Expand Up @@ -319,7 +320,7 @@ public KeyRecord readKeyRecord(X500Name ownerName)
public KeyRecord readKeyRecord(PublicKey publicKey)
throws EBaseException {
// XXX - setup binary search attributes
byte data[] = publicKey.getEncoded();
byte[] data = publicKey.getEncoded();

if (data == null)
throw new EBaseException("null data");
Expand Down Expand Up @@ -352,7 +353,7 @@ public KeyRecord readKeyRecord(String cert)

try (DBSSession s = dbSubsystem.createSession()) {
String filter = "(publicKey=x509cert#\"" + cert + "\")";
logger.debug("filter= " + filter);
logger.debug("KeyRepository: - filter= {}", filter);

if (s != null) {
DBSearchResults res = s.search(getDN(), filter);
Expand Down Expand Up @@ -405,8 +406,8 @@ public void deleteKeyRecord(BigInteger serialNo)
/**
* Read RFC-2254
*/
public static String escapeBinaryData(byte data[]) {
StringBuffer result = new StringBuffer();
public static String escapeBinaryData(byte[] data) {
StringBuilder result = new StringBuilder();

for (int i = 0; i < data.length; i++) {
result.append("\\" + Integer.toHexString(data[i]));
Expand All @@ -425,15 +426,15 @@ public static String escapeBinaryData(byte data[]) {
public Enumeration<KeyRecord> searchKeys(String filter, int maxSize)
throws EBaseException {

Vector<KeyRecord> v = new Vector<>();
ArrayList<KeyRecord> a = new ArrayList<>();

try (DBSSession s = dbSubsystem.createSession()) {
DBSearchResults sr = s.search(getDN(), filter, maxSize);
while (sr.hasMoreElements()) {
v.add((KeyRecord) sr.nextElement());
a.add((KeyRecord) sr.nextElement());
}
}
return v.elements();
return Collections.enumeration(a);
}

/**
Expand All @@ -448,15 +449,15 @@ public Enumeration<KeyRecord> searchKeys(String filter, int maxSize)
public Enumeration<KeyRecord> searchKeys(String filter, int maxSize, int timeLimit)
throws EBaseException {

Vector<KeyRecord> v = new Vector<>();
ArrayList<KeyRecord> a = new ArrayList<>();

try (DBSSession s = dbSubsystem.createSession()) {
DBSearchResults sr = s.search(getDN(), filter, maxSize, timeLimit);
while (sr.hasMoreElements()) {
v.add((KeyRecord) sr.nextElement());
a.add((KeyRecord) sr.nextElement());
}
}
return v.elements();
return Collections.enumeration(a);
}

/**
Expand Down Expand Up @@ -514,7 +515,7 @@ public RecordPagedList<KeyRecord> findPagedKeyRecords(String filter,
*/
@Deprecated(since = "11.6.0", forRemoval = true)
public KeyRecordList findKeyRecordsInList(String filter,
String attrs[], int pageSize) throws EBaseException {
String[] attrs, int pageSize) throws EBaseException {
return findKeyRecordsInList(filter, attrs, KeyRecord.ATTR_ID, pageSize);
}

Expand All @@ -531,7 +532,7 @@ public KeyRecordList findKeyRecordsInList(String filter,
*/
@Deprecated(since = "11.6.0", forRemoval = true)
public KeyRecordList findKeyRecordsInList(String filter,
String attrs[], String sortKey, int pageSize)
String[] attrs, String sortKey, int pageSize)
throws EBaseException {

KeyRecordList list = null;
Expand Down Expand Up @@ -560,7 +561,7 @@ public KeyRecordList findKeyRecordsInList(String filter,
*/
@Deprecated(since = "11.6.0", forRemoval = true)
public KeyRecordList findKeyRecordsInList(String filter,
String attrs[], String jumpTo, String sortKey, int pageSize)
String[] attrs, String jumpTo, String sortKey, int pageSize)
throws EBaseException {

KeyRecordList list = null;
Expand Down Expand Up @@ -598,7 +599,7 @@ public BigInteger getLastSerialNumberInRange(BigInteger serial_low_bound, BigInt
return null;
}

String ldapfilter = "(&(" + "serialno" + "=*" + ")(" + KeyRecord.ATTR_ID + "<="+serial_upper_bound+"))";
String ldapfilter = "(" + KeyRecord.ATTR_ID + "<="+serial_upper_bound+")";
String[] attrs = null;

RecordPagedList<KeyRecord> keyRecords = findPagedKeyRecords(ldapfilter, attrs, "-serialno");
Expand All @@ -616,7 +617,7 @@ public BigInteger getLastSerialNumberInRange(BigInteger serial_low_bound, BigInt

BigInteger ret = new BigInteger(serial_low_bound.toString(10));

ret = ret.add(new BigInteger("-1"));
ret = ret.add(BigInteger.valueOf(-1));

logger.debug("KeyRepository: getLastSerialNumberInRange returning: {}", ret);
return ret;
Expand Down

0 comments on commit 58a2dc4

Please sign in to comment.