Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
feat: Provide metrics for data key rotation job (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayitbeegh authored Mar 13, 2020
1 parent 5f55378 commit a57ecd8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.nike.cerberus.service;

import com.codahale.metrics.Counter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -26,6 +27,7 @@
import com.nike.cerberus.dao.SecureDataVersionDao;
import com.nike.cerberus.domain.*;
import com.nike.cerberus.error.DefaultApiError;
import com.nike.cerberus.metric.MetricsService;
import com.nike.cerberus.record.DataKeyInfo;
import com.nike.cerberus.record.SecureDataRecord;
import com.nike.cerberus.record.SecureDataVersionRecord;
Expand All @@ -48,6 +50,8 @@ public class SecureDataService {
private final ObjectMapper objectMapper;
private final DateTimeSupplier dateTimeSupplier;
private final SecureDataVersionDao secureDataVersionDao;
private final Counter reencryptSuccessCounter;
private final Counter reencryptFailCounter;

protected final Logger log = LoggerFactory.getLogger(getClass());

Expand All @@ -57,12 +61,16 @@ public SecureDataService(
EncryptionService encryptionService,
ObjectMapper objectMapper,
DateTimeSupplier dateTimeSupplier,
SecureDataVersionDao secureDataVersionDao) {
SecureDataVersionDao secureDataVersionDao,
MetricsService metricsService) {
this.secureDataDao = secureDataDao;
this.encryptionService = encryptionService;
this.objectMapper = objectMapper;
this.dateTimeSupplier = dateTimeSupplier;
this.secureDataVersionDao = secureDataVersionDao;
reencryptSuccessCounter =
metricsService.getOrCreateCounter("cms.encryption.reencrypt.success", null);
reencryptFailCounter = metricsService.getOrCreateCounter("cms.encryption.reencrypt.fail", null);
}

@Transactional
Expand Down Expand Up @@ -491,16 +499,20 @@ public void rotateDataKeys(int numberOfKeys, int pauseTimeInMillis, int rotation
try {
reencryptData(id);
counter[0]++;
reencryptSuccessCounter.inc();
} catch (Exception e) {
log.error("Failed to re-encrypt secure data id: {}", id);
reencryptFailCounter.inc();
}
} else if (source == Source.SECURE_DATA_VERSION) {
String versionId = dataKeyInfo.getId();
try {
reencryptDataVersion(versionId);
counter[1]++;
reencryptSuccessCounter.inc();
} catch (Exception e) {
log.error("Failed to re-encrypt secure data version id: {}", versionId);
reencryptFailCounter.inc();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.nike.cerberus.dao.SecureDataVersionDao;
import com.nike.cerberus.domain.SecureData;
import com.nike.cerberus.domain.SecureDataType;
import com.nike.cerberus.metric.MetricsService;
import com.nike.cerberus.record.SecureDataRecord;
import com.nike.cerberus.record.SecureDataVersionRecord;
import com.nike.cerberus.util.DateTimeSupplier;
Expand Down Expand Up @@ -73,6 +74,7 @@ public class SecureDataServiceTest {
@Mock private EncryptionService encryptionService;
@Mock private DateTimeSupplier dateTimeSupplier;
@Mock private SecureDataVersionDao secureDataVersionDao;
@Mock private MetricsService metricsService;
private ObjectMapper objectMapper;

private SecureDataService secureDataService;
Expand All @@ -83,7 +85,12 @@ public void before() {
objectMapper = new ObjectMapper();
secureDataService =
new SecureDataService(
secureDataDao, encryptionService, objectMapper, dateTimeSupplier, secureDataVersionDao);
secureDataDao,
encryptionService,
objectMapper,
dateTimeSupplier,
secureDataVersionDao,
metricsService);
}

@After
Expand Down

0 comments on commit a57ecd8

Please sign in to comment.