Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ca certs api v2 #4635

Merged
merged 4 commits into from
Dec 11, 2023
Merged

Ca certs api v2 #4635

merged 4 commits into from
Dec 11, 2023

Conversation

fmarco76
Copy link
Member

@fmarco76 fmarco76 commented Dec 7, 2023

Implement v2 REST end-point for /ca/v2/certs

Implement the REST operations:

  • GET of /ca/v2/certs/
  • GET of /ca/v2/certs
  • POST to /ca/v2/certs/search.

These implement the behaviour of the current REST operations (see [1, 2 and 3]). The new version is not based on RESTeasy framework for the REST operation and the DB searches have replaced VLV with paged queries.

The only differences with the previous implementation are:

  • the total value now show the number of returned item;
  • the number of returned entry is limited (this is hard-coded for now).

This replicate the current behaviour imeplemented in
/ca/rest/certs/<id>.
Implement the REST operations:
- GET of /ca/v2/certs
- POST to /ca/v2/certs/search.

These implement the behaviour of the current REST operations (see [1, 2]). The new version is not based on RESTeasy framework for the REST operation and the DB searches have replaced VLV with paged queries.

The only differences with the previous implementation are:
- the total value now show the number of returned item;
- the number of returned entry is limited (this is hard-coded for now).

1. https://github.com/dogtagpki/pki/wiki/CA-List-Certificates-REST-API
2. https://github.com/dogtagpki/pki/wiki/CA-Search-Certificates-REST-API
@fmarco76 fmarco76 requested a review from edewata December 7, 2023 18:18
@fmarco76
Copy link
Member Author

fmarco76 commented Dec 7, 2023

@edewata The total can be fixed to provide the exact number if needed but has to be retrieved from the first query and reported to the servlet so it will require some changes. Is this needed.
For the limit on the returned entry I think it is not a real limitation in the real usage but I wondering if it should be configurable and if we should have different limits for search and returned entries.

@fmarco76 fmarco76 changed the title Ca certs ap iv2 Ca certs api v2 Dec 7, 2023
Copy link
Contributor

@edewata edewata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some comments/questions, but I think overall it's good. Feel free to update or merge as is, then we can address the comments in a new PR.

* {@Code (&(certRecordId=5)(x509Cert.notBefore=934398398))}
*
* @param filter search filter
* @param maxSize max size to return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @param should match the actual method params.

* @return a list of certificates
* @exception EBaseException failed to search
*/
public Iterator<CertRecord> searchCertificates(String filter, int timeLimit, int start, int size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timeLimit doesn't seem to be used. Should we keep it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is implemented in the pagedSeearch. I will add to the call.

Comment on lines 141 to 142
String base64 = CertUtil.toPEM(cert);
certData.setEncoded(base64);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably an opportunity to fix APIv1. Here we could just return the base64-encoded value without the PEM header/footer. I think that would be easier to parse and more consistent with other binary data in JSON in general. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know in other places yet but here we have the chain in base64 the certificate in pem. It is better to have all base64.

Comment on lines 144 to 145
CertPrettyPrint print = new CertPrettyPrint(cert);
certData.setPrettyPrint(print.toString(loc));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pretty-print probably should be generated on the client side because the client already has the cert binaries, and the output would be dependent on client's locale anyway, so I think we can skip this field.

Comment on lines 162 to 166
Date notBefore = cert.getNotBefore();
if (notBefore != null) certData.setNotBefore(notBefore.toString());

Date notAfter = cert.getNotAfter();
if (notAfter != null) certData.setNotAfter(notAfter.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The notBefore and notAfter probably should be generated on the client side too since it's dependent on client's locale and the client already has the cert binaries.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave this for the moment. PrettyPrint is better in the client but the validity could be useful without the need to read the binary. What we could change is the format to be more platform independent (e.g. yyyy-MM-dd HH:mm:ss Z). The client can read or generate from binary

Copy link
Member Author

@fmarco76 fmarco76 Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we have two different format of data, in the certificate list we have time in milliseconds while in a single certificate we have the string. This because the list uses the class CertDataInfo while the single certificate uses the class CertData. These classes are in the same package and have almost the same fields but with different format (e.g. date is Date in one and String in the other) . Not clear why we need to have both. We could merge them. To investigate in a separate PR


int total = results.size();
logger.info("Search results: " + total);
infos.setTotal(total);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably can skip the total since it can be obtained from infos.getEntries().size().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is OK to remove for now. The pageSearch could provide the total number of entries in the DB but the information is not returned from the LDAPSession. If needed we could handle that information to the client. We will consider this in future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done a couple of test and it is not enough to skip the setTotal(), it is reported as total=0 in the final json. I am leaving for now and evaluate in separate PR.


LDAPSearchConstraints cons = new LDAPSearchConstraints();
if (timeLimit > 0) {
cons.setServerTimeLimit(timeLimit);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LDAPSearchConstraints also has a setMaxResults(). I was wondering whether we should just ignore it, set it to size, or add another param for it (e.g. sizeLimit).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another consideration is, IIUC the size is only used internally for paging, but the number of results the client will see won't actually be affected by the size. On the other hand, the sizeLimit will (if it's used to call setMaxResults()).

So maybe we should replace size with a constant or a server-side configurable param, and add a sizeLimit not for paging but for setting the max results.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, size elements are returned from the search. The size is always used in the last query and the elements are returned . If start is provided, than one or more pages are requested until start - 1 elements are retrieved and then a page with size is requested.
If size is not provided there is a limit to 500. I am leaving the limit now and we can decide if this has to be customisable in a separate PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the MaxResults. We could discuss better approach with DS folk.

byte[] p7Bytes = bos.toByteArray();

String p7Str = Utils.base64encode(p7Bytes, true);
certData.setPkcs7CertChain(p7Str);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if we should skip this field since not all clients needs to get the PKCS #7 cert chain, so maybe it can be provided by a different API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this field when the new API is created in a separate PR.

revExts.get(CRLReasonExtension.NAME);
certData.setRevocationReason(ext.getReason().getCode());
} catch (X509ExtensionException e) {
// nothing to do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's log an error so at least we know if something happens.

Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 48 Code Smells

0.0% 0.0% Coverage
2.6% 2.6% Duplication

@fmarco76
Copy link
Member Author

@edewata Thanks!

I have update following some of your comments. The remaining comments need additional work in separate PRs.

@fmarco76 fmarco76 merged commit 1ea0985 into dogtagpki:master Dec 11, 2023
131 checks passed
@fmarco76 fmarco76 deleted the CACertsAPIv2 branch December 11, 2023 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants