Add a counter for requests for delivery certificates with/without e164s.

This commit is contained in:
Jon Chambers 2021-06-30 15:21:04 -04:00 committed by Jon Chambers
parent e3afcae7d3
commit b384ed7f5c
2 changed files with 25 additions and 20 deletions

View File

@ -33,7 +33,7 @@ public class CertificateGenerator {
this.serverCertificate = ServerCertificate.parseFrom(serverCertificate); this.serverCertificate = ServerCertificate.parseFrom(serverCertificate);
} }
public byte[] createFor(Account account, Device device, boolean includeE164) throws IOException, InvalidKeyException { public byte[] createFor(Account account, Device device, boolean includeE164) throws InvalidKeyException {
SenderCertificate.Certificate.Builder builder = SenderCertificate.Certificate.newBuilder() SenderCertificate.Certificate.Builder builder = SenderCertificate.Certificate.newBuilder()
.setSenderDevice(Math.toIntExact(device.getId())) .setSenderDevice(Math.toIntExact(device.getId()))
.setExpires(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(expiresDays)) .setExpires(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(expiresDays))

View File

@ -5,17 +5,16 @@
package org.whispersystems.textsecuregcm.controllers; package org.whispersystems.textsecuregcm.controllers;
import static com.codahale.metrics.MetricRegistry.name;
import com.codahale.metrics.annotation.Timed; import com.codahale.metrics.annotation.Timed;
import io.dropwizard.auth.Auth; import io.dropwizard.auth.Auth;
import org.signal.zkgroup.auth.ServerZkAuthOperations; import io.micrometer.core.instrument.Metrics;
import org.slf4j.Logger; import java.io.IOException;
import org.slf4j.LoggerFactory; import java.security.InvalidKeyException;
import org.whispersystems.textsecuregcm.auth.CertificateGenerator; import java.util.LinkedList;
import org.whispersystems.textsecuregcm.entities.DeliveryCertificate; import java.util.List;
import org.whispersystems.textsecuregcm.entities.GroupCredentials; import java.util.Optional;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.util.Util;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
@ -24,22 +23,24 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.IOException; import org.signal.zkgroup.auth.ServerZkAuthOperations;
import java.security.InvalidKeyException; import org.whispersystems.textsecuregcm.auth.CertificateGenerator;
import java.util.LinkedList; import org.whispersystems.textsecuregcm.entities.DeliveryCertificate;
import java.util.List; import org.whispersystems.textsecuregcm.entities.GroupCredentials;
import java.util.Optional; import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.util.Util;
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@Path("/v1/certificate") @Path("/v1/certificate")
public class CertificateController { public class CertificateController {
private final Logger logger = LoggerFactory.getLogger(CertificateController.class);
private final CertificateGenerator certificateGenerator; private final CertificateGenerator certificateGenerator;
private final ServerZkAuthOperations serverZkAuthOperations; private final ServerZkAuthOperations serverZkAuthOperations;
private final boolean isZkEnabled; private final boolean isZkEnabled;
private static final String GENERATE_DELIVERY_CERTIFICATE_COUNTER_NAME = name(CertificateGenerator.class, "generateCertificate");
private static final String INCLUDE_E164_TAG_NAME = "includeE164";
public CertificateController(CertificateGenerator certificateGenerator, ServerZkAuthOperations serverZkAuthOperations, boolean isZkEnabled) { public CertificateController(CertificateGenerator certificateGenerator, ServerZkAuthOperations serverZkAuthOperations, boolean isZkEnabled) {
this.certificateGenerator = certificateGenerator; this.certificateGenerator = certificateGenerator;
this.serverZkAuthOperations = serverZkAuthOperations; this.serverZkAuthOperations = serverZkAuthOperations;
@ -51,8 +52,8 @@ public class CertificateController {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("/delivery") @Path("/delivery")
public DeliveryCertificate getDeliveryCertificate(@Auth Account account, public DeliveryCertificate getDeliveryCertificate(@Auth Account account,
@QueryParam("includeE164") Optional<Boolean> includeE164) @QueryParam("includeE164") Optional<Boolean> maybeIncludeE164)
throws IOException, InvalidKeyException throws InvalidKeyException
{ {
if (account.getAuthenticatedDevice().isEmpty()) { if (account.getAuthenticatedDevice().isEmpty()) {
throw new AssertionError(); throw new AssertionError();
@ -61,7 +62,11 @@ public class CertificateController {
throw new WebApplicationException(Response.Status.BAD_REQUEST); throw new WebApplicationException(Response.Status.BAD_REQUEST);
} }
return new DeliveryCertificate(certificateGenerator.createFor(account, account.getAuthenticatedDevice().get(), includeE164.orElse(true))); final boolean includeE164 = maybeIncludeE164.orElse(true);
Metrics.counter(GENERATE_DELIVERY_CERTIFICATE_COUNTER_NAME, INCLUDE_E164_TAG_NAME, String.valueOf(includeE164)).increment();
return new DeliveryCertificate(certificateGenerator.createFor(account, account.getAuthenticatedDevice().get(), includeE164));
} }
@Timed @Timed