Skip to content

Commit

Permalink
Convert ca-cert-find server command to paged search
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarco76 committed Jan 9, 2024
1 parent 2e5ee9c commit d829749
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.MetaInfo;
import com.netscape.certsrv.base.SessionContext;
import com.netscape.certsrv.dbs.DBPagedSearch;
import com.netscape.certsrv.dbs.DBVirtualList;
import com.netscape.certsrv.dbs.EDBRecordNotFoundException;
import com.netscape.certsrv.dbs.Modification;
Expand Down Expand Up @@ -1218,6 +1219,33 @@ public Enumeration<CertRecord> findCertRecords(String filter)
return e;
}

/**
* Finds a list of certificate records that satisifies
* the filter.
*
* @param filter search filter
* @param attrs selected attribute
* @param sortKey key to use for sorting the returned elements
* @return a list of certificates
* @exception EBaseException failed to search
*/
public CertRecordPagedList findPagedCertRecords(String filter,
String[] attrs, String sortKey)
throws EBaseException {

logger.debug("CertificateRepository.findCertRecordsInList()");

try (DBSSession session = dbSubsystem.createSession()) {
DBPagedSearch<CertRecord> page = session.<CertRecord>createPagedSearch(
mBaseDN,
filter,
attrs,
sortKey);

return new CertRecordPagedList(page);
}
}

/**
* Finds certificate records. Here is a list of filter
* attribute can be used:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import javax.servlet.http.HttpServletResponse;

/**
* @author Marco Fargetta <[email protected]>
* @author Marco Fargetta {@literal <[email protected]>}
*/
public class CAServlet extends HttpServlet {
public static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.dogtagpki.cli.CLI;
import org.dogtagpki.cli.CommandCLI;
import org.dogtagpki.jss.tomcat.TomcatJSS;
import org.dogtagpki.server.ca.CAConfig;
import org.dogtagpki.server.ca.CAEngineConfig;
import org.dogtagpki.util.logging.PKILogger;
import org.dogtagpki.util.logging.PKILogger.LogLevel;
Expand All @@ -30,7 +29,7 @@
import com.netscape.cmscore.base.ConfigStorage;
import com.netscape.cmscore.base.FileConfigStorage;
import com.netscape.cmscore.dbs.CertRecord;
import com.netscape.cmscore.dbs.CertRecordList;
import com.netscape.cmscore.dbs.CertRecordPagedList;
import com.netscape.cmscore.dbs.CertificateRepository;
import com.netscape.cmscore.dbs.DBSubsystem;
import com.netscape.cmscore.ldapconn.LDAPConfig;
Expand Down Expand Up @@ -82,8 +81,6 @@ public void execute(CommandLine cmd) throws Exception {
String filter = builder.buildFilter();
logger.info("- filter: " + filter);

int size = 20;

String instanceDir = CMS.getInstanceDir();

TomcatJSS tomcatjss = TomcatJSS.getInstance();
Expand Down Expand Up @@ -115,48 +112,40 @@ public void execute(CommandLine cmd) throws Exception {
dbSubsystem.setEngineConfig(cs);
dbSubsystem.init(dbConfig, ldapConfig, socketConfig, passwordStore);

CAConfig caConfig = cs.getCAConfig();

logger.info("Initializing cert repository");

int increment = caConfig.getInteger(CertificateRepository.PROP_INCREMENT, 5);
logger.info("- increment: " + increment);

try {
CertificateRepository certificateRepository = new CertificateRepository(secureRandom, dbSubsystem);
certificateRepository.init();

CertRecordList list = certificateRepository.findCertRecordsInList(filter, null, "serialno", size);
int total = list.getSize();

for (int i = 0; i < total; i++) {

if (i > 0) {
CertRecordPagedList certPages = certificateRepository.findPagedCertRecords(filter, null, "serialno");
boolean follow = false;
for (CertRecord cRec: certPages) {
CertId id = new CertId(cRec.getSerialNumber());
X509Certificate cert = cRec.getCertificate();
if(follow) {
System.out.println();
} else {
follow = true;
}

CertRecord record = list.getCertRecord(i);
CertId id = new CertId(record.getSerialNumber());
X509Certificate cert = record.getCertificate();

System.out.println(" Serial Number: " + id.toHexString());
System.out.println(" Subject DN: " + cert.getSubjectDN());
System.out.println(" Issuer DN: " + cert.getIssuerDN());

System.out.println(" Status: " + record.getStatus());
System.out.println(" Status: " + cRec.getStatus());

System.out.println(" Not Valid Before: " + cert.getNotBefore());
System.out.println(" Not Valid After: " + cert.getNotAfter());

System.out.println(" Issued On: " + record.getCreateTime());
System.out.println(" Issued By: " + record.getIssuedBy());
System.out.println(" Issued On: " + cRec.getCreateTime());
System.out.println(" Issued By: " + cRec.getIssuedBy());

Date revokedOn = record.getRevokedOn();
Date revokedOn = cRec.getRevokedOn();
if (revokedOn != null) {
System.out.println(" Revoked On: " + revokedOn);
}

String revokedBy = record.getRevokedBy();
String revokedBy = cRec.getRevokedBy();
if (revokedBy != null) {
System.out.println(" Revoked By: " + revokedBy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.slf4j.LoggerFactory;

/**
* @author Marco Fargetta <[email protected]>
* @author Marco Fargetta {@literal <[email protected]>}
*/
@WebServlet("/v2/info")
public class CAInfoServlet extends CAServlet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import com.netscape.cmscore.dbs.RevocationInfo;

/**
* @author Marco Fargetta <[email protected]>
* @author Marco Fargetta {@literal <[email protected]>}
*/
@WebServlet("/v2/certs/*")
public class CertServlet extends CAServlet {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2023 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.dbs;

import java.util.List;

import com.netscape.certsrv.base.EBaseException;
import com.netscape.cmscore.dbs.CertRecord;

/**
* A class represents a paged search.
*
* @author Marco Fargetta {@literal <[email protected]>}
*/
public abstract class DBPagedSearch<E extends IDBObj> {

public abstract List<CertRecord> getPage() throws EBaseException;

public abstract List<CertRecord> getPage(int size) throws EBaseException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2023 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmscore.dbs;

import java.util.Iterator;
import java.util.List;

import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.dbs.DBPagedSearch;

/**
* Contain all records in a page for a paged search.
*
* @author Marco Fargetta {@literal <[email protected]>}
*/
public class CertRecordPagedList implements Iterable<CertRecord> {
public static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CertRecordPagedList.class);

private DBPagedSearch<CertRecord> pages;
private Iterator<CertRecord> pageEntries;
/**
* Constructs a request paged.
*/
public CertRecordPagedList(DBPagedSearch<CertRecord> pages) {
this.pages = pages;
try {
pageEntries = pages.getPage().iterator();
} catch (EBaseException e) {
throw new RuntimeException("CertRecordPagedList: Error to get a new page", e);
}
}

@Override
public Iterator<CertRecord> iterator() {
return new CertRecordPageIterator();
}

class CertRecordPageIterator implements Iterator<CertRecord> {

@Override
public boolean hasNext() {
if (!pageEntries.hasNext()) {
try {
List<CertRecord> newPage = pages.getPage();
pageEntries = newPage.iterator();
} catch (EBaseException e) {
throw new RuntimeException("CertRecordPagedList: Error to get a new page", e);
}
}
return pageEntries.hasNext();
}

@Override
public CertRecord next() {
if (hasNext()) {
return pageEntries.next();
}
return null;
}

}
}
17 changes: 17 additions & 0 deletions base/server/src/main/java/com/netscape/cmscore/dbs/DBSSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.netscape.cmscore.dbs;

import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.dbs.DBPagedSearch;
import com.netscape.certsrv.dbs.DBVirtualList;
import com.netscape.certsrv.dbs.EDBException;
import com.netscape.certsrv.dbs.IDBObj;
Expand Down Expand Up @@ -353,6 +354,22 @@ public <T extends IDBObj> DBVirtualList<T> createVirtualList(
return null;
}

/**
* Retrieves a paged search of objects.
*
* @param base starting point of the search
* @param filter search filter
* @param attrs selected attributes
* @param startFrom starting point
* @param sortKey key used to sort the list
* @return search results in virtual list
* @exception EBaseException failed to search
*/
public <T extends IDBObj> DBPagedSearch<T> createPagedSearch(String base, String filter, String[] attrs,
String sortKey) throws EBaseException {
return null;
}

public void abandon(LDAPSearchResults results) throws EBaseException {
}
}
Loading

0 comments on commit d829749

Please sign in to comment.