add write version secret (#58)

Co-authored-by: ggmaleva <ggmaleva@yandex.ru>
This commit is contained in:
Gregory 2023-08-15 18:29:23 +03:00 committed by GitHub
parent 28aa598613
commit 68ec296ea6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 1 deletions

View File

@ -12,7 +12,7 @@
</parent>
<artifactId>adapter-common-lib</artifactId>
<version>1.2.11</version>
<version>1.2.12</version>
<packaging>jar</packaging>
<name>adapter-common-lib</name>

View File

@ -74,4 +74,13 @@ public interface SecretService {
*/
void writeSecret(String serviceName, SecretObj secretObj);
/**
* Сохраняет секреты для терминала
*
* @param serviceName - имя сервиса, для которого сохраняются секреты. Хранится в настройках сервиса.
* @param secretObj - объект с секретами, {@link SecretObj}
* @return Возвращает версию созданного хранилища для терминала
*/
Integer writeVersionSecret(String serviceName, SecretObj secretObj);
}

View File

@ -70,6 +70,13 @@ public class VaultSecretService implements SecretService {
vaultTemplate.opsForVersionedKeyValue(serviceName).put(secretObj.getPath(), secretObj.getValues());
}
@Override
public Integer writeVersionSecret(String serviceName, SecretObj secretObj) {
Versioned.Metadata metadata =
vaultTemplate.opsForVersionedKeyValue(serviceName).put(secretObj.getPath(), secretObj.getValues());
return metadata.getVersion().getVersion();
}
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) {

View File

@ -144,4 +144,23 @@ public class VaultSecretServiceTest {
assertEquals(TOKEN_EXP_DATE_VALUE, secret.get(TOKEN_EXP_DATE).getValue());
}
@Test
void writeMultipleVersionSecret() {
SecretObj secretObj = new SecretObj(
TEST_TOKEN_PATH,
Map.of(
TOKEN, TOKEN_VALUE,
TOKEN_EXP_DATE, TOKEN_EXP_DATE_VALUE
)
);
Integer version = vaultService.writeVersionSecret(SERVICE_NAME, secretObj);
VersionedSecret versionSecrets = vaultService.getVersionSecrets(SERVICE_NAME, TEST_TOKEN_PATH);
assertNotNull(versionSecrets);
assertEquals(version, versionSecrets.getVersion());
assertEquals(TOKEN_VALUE, versionSecrets.getSecretes().get(TOKEN).getValue());
assertEquals(TOKEN_EXP_DATE_VALUE, versionSecrets.getSecretes().get(TOKEN_EXP_DATE).getValue());
}
}