diff --git a/pom.xml b/pom.xml index 4d7a2ca..279d864 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ adapter-common-lib - 1.2.9 + 1.2.10 jar adapter-common-lib diff --git a/src/main/java/dev/vality/adapter/common/secret/SecretObj.java b/src/main/java/dev/vality/adapter/common/secret/SecretObj.java new file mode 100644 index 0000000..e08bdd0 --- /dev/null +++ b/src/main/java/dev/vality/adapter/common/secret/SecretObj.java @@ -0,0 +1,26 @@ +package dev.vality.adapter.common.secret; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.ToString; + +import java.util.Map; + +/** + * Объект с секретами для сохранения в vault + * path - путь, по которому в vault будут храниться секреты, передданые в объекте. Хранится в options платежа. + * value - значение ключа + * Например, + * SecretObj{ + * 'tinkoff-merchant-882347345', + * {'secret-password','PASSWORD'} + * } + */ + +@Data +@ToString +@AllArgsConstructor +public class SecretObj { + private String path; + private Map values; +} diff --git a/src/main/java/dev/vality/adapter/common/secret/SecretService.java b/src/main/java/dev/vality/adapter/common/secret/SecretService.java index e2facc0..bb2e964 100644 --- a/src/main/java/dev/vality/adapter/common/secret/SecretService.java +++ b/src/main/java/dev/vality/adapter/common/secret/SecretService.java @@ -55,4 +55,12 @@ public interface SecretService { String digest(String serviceName, String data, SecretRef secretRef, DigestAlgorithms digestAlgorithm) throws SecretNotFoundException; + /** + * Сохраняет секреты для терминала + * + * @param serviceName - имя сервиса, для которого сохраняются секреты. Хранится в настройках сервиса. + * @param secretObj - объект с секретами, {@link SecretObj} + */ + void writeSecret(String serviceName, SecretObj secretObj); + } diff --git a/src/main/java/dev/vality/adapter/common/secret/VaultSecretService.java b/src/main/java/dev/vality/adapter/common/secret/VaultSecretService.java index 8dc84e9..af2d8d2 100644 --- a/src/main/java/dev/vality/adapter/common/secret/VaultSecretService.java +++ b/src/main/java/dev/vality/adapter/common/secret/VaultSecretService.java @@ -44,6 +44,11 @@ public class VaultSecretService implements SecretService { return new DigestSigner().sign(data, secret, algorithm); } + @Override + public void writeSecret(String serviceName, SecretObj secretObj) { + vaultTemplate.opsForVersionedKeyValue(serviceName).put(secretObj.getPath(), secretObj.getValues()); + } + private String getSecretString(String serviceName, SecretRef secretRef) throws SecretNotFoundException { var map = vaultTemplate.opsForVersionedKeyValue(serviceName).get(secretRef.getPath()); if (map == null || map.getData() == null || map.getData().get(secretRef.getKey()) == null) { diff --git a/src/test/java/dev/vality/adapter/common/secret/VaultSecretServiceTest.java b/src/test/java/dev/vality/adapter/common/secret/VaultSecretServiceTest.java index d09ea79..a86c301 100644 --- a/src/test/java/dev/vality/adapter/common/secret/VaultSecretServiceTest.java +++ b/src/test/java/dev/vality/adapter/common/secret/VaultSecretServiceTest.java @@ -10,7 +10,6 @@ import org.junit.jupiter.api.Test; import org.springframework.vault.authentication.TokenAuthentication; import org.springframework.vault.client.VaultEndpoint; import org.springframework.vault.core.VaultTemplate; -import org.testcontainers.containers.Container; import org.testcontainers.utility.DockerImageName; import org.testcontainers.vault.VaultContainer; import org.testcontainers.vault.VaultLogLevel; @@ -27,9 +26,14 @@ public class VaultSecretServiceTest { public static final String HMAC_KEY = "hmacKey"; public static final String HMAC_SECRET = "6d6b6c6172657772"; public static final String SERVICE_NAME = "adapter-vtb"; + public static final String TOKEN = "token"; + public static final String TOKEN_VALUE = "token-value"; + public static final String TOKEN_EXP_DATE = "token_exp_date"; + public static final String TOKEN_EXP_DATE_VALUE = "2023-04-20T12:26:17.191286"; private static VaultSecretService vaultService; private static final String TEST_PATH = "test-terminal-123"; + private static final String TEST_TOKEN_PATH = "test-terminal-345"; @BeforeAll public static void setUp() throws IOException, InterruptedException { @@ -49,13 +53,13 @@ public class VaultSecretServiceTest { } @Test - public void testGetSecrets() { + void testGetSecrets() { assertNotNull(vaultService.getSecrets(SERVICE_NAME, TEST_PATH)); assertThrows(SecretPathNotFoundException.class, () -> vaultService.getSecrets(SERVICE_NAME, "kekek")); } @Test - public void testGetSecret() { + void testGetSecret() { assertEquals(SIMPLE_SECRET, vaultService.getSecret(SERVICE_NAME, new SecretRef(TEST_PATH, SIMPLE_KEY)).getValue()); assertThrows(SecretNotFoundException.class, @@ -63,7 +67,7 @@ public class VaultSecretServiceTest { } @Test - public void testHmac() { + void testHmac() { String expected = HmacEncryption.calculateHMacSha256("some_dat", "6d6b6c6172657772"); SecretRef hmacRef = new SecretRef(TEST_PATH, HMAC_KEY); String actual = vaultService.hmac(SERVICE_NAME, "some_dat", hmacRef, HmacAlgorithms.HMAC_SHA_256); @@ -74,10 +78,41 @@ public class VaultSecretServiceTest { } @Test - public void digest() { + void digest() { String expected = DigestUtils.md5Hex("some_da" + SIMPLE_SECRET); String actual = vaultService.digest(SERVICE_NAME, "some_da", new SecretRef(TEST_PATH, SIMPLE_KEY), DigestAlgorithms.MD5); assertEquals(expected, actual); } + + @Test + void writeSingleSecret() { + SecretObj secretObj = new SecretObj(TEST_TOKEN_PATH, Map.of(TOKEN, TOKEN_VALUE)); + vaultService.writeSecret(SERVICE_NAME, secretObj); + + SecretValue secret = vaultService.getSecret(SERVICE_NAME, new SecretRef(TEST_TOKEN_PATH, TOKEN)); + + assertNotNull(secret); + assertEquals(TOKEN_VALUE, secret.getValue()); + + } + + @Test + void writeMultipleSecret() { + SecretObj secretObj = new SecretObj( + TEST_TOKEN_PATH, + Map.of( + TOKEN, TOKEN_VALUE, + TOKEN_EXP_DATE, TOKEN_EXP_DATE_VALUE + ) + ); + vaultService.writeSecret(SERVICE_NAME, secretObj); + + Map secret = vaultService.getSecrets(SERVICE_NAME, TEST_TOKEN_PATH); + + assertNotNull(secret); + assertEquals(TOKEN_VALUE, secret.get(TOKEN).getValue()); + assertEquals(TOKEN_EXP_DATE_VALUE, secret.get(TOKEN_EXP_DATE).getValue()); + + } } \ No newline at end of file