From a57ecd852d95b07b1a02e21b8c5161b99b05d2e6 Mon Sep 17 00:00:00 2001 From: Sean Lin Date: Thu, 12 Mar 2020 18:00:18 -0700 Subject: [PATCH] feat: Provide metrics for data key rotation job (#253) --- .../nike/cerberus/service/SecureDataService.java | 14 +++++++++++++- .../cerberus/service/SecureDataServiceTest.java | 9 ++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cerberus-web/src/main/java/com/nike/cerberus/service/SecureDataService.java b/cerberus-web/src/main/java/com/nike/cerberus/service/SecureDataService.java index d927bda82..b12972a19 100644 --- a/cerberus-web/src/main/java/com/nike/cerberus/service/SecureDataService.java +++ b/cerberus-web/src/main/java/com/nike/cerberus/service/SecureDataService.java @@ -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; @@ -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; @@ -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()); @@ -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 @@ -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(); } } diff --git a/cerberus-web/src/test/java/com/nike/cerberus/service/SecureDataServiceTest.java b/cerberus-web/src/test/java/com/nike/cerberus/service/SecureDataServiceTest.java index 3255ea333..641562076 100644 --- a/cerberus-web/src/test/java/com/nike/cerberus/service/SecureDataServiceTest.java +++ b/cerberus-web/src/test/java/com/nike/cerberus/service/SecureDataServiceTest.java @@ -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; @@ -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; @@ -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