|
| 1 | +/* |
| 2 | + * Copyright 2021-2023 VMware, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package com.vmware.taurus.secrets.service.vault; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 9 | +import com.vmware.taurus.ControlplaneApplication; |
| 10 | +import com.vmware.taurus.datajobs.it.common.BaseIT; |
| 11 | +import com.vmware.taurus.exception.DataJobSecretsSizeLimitException; |
| 12 | +import org.apache.commons.lang3.RandomStringUtils; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.vault.authentication.TokenAuthentication; |
| 18 | +import org.springframework.vault.client.VaultEndpoint; |
| 19 | +import org.springframework.vault.core.VaultTemplate; |
| 20 | +import org.testcontainers.junit.jupiter.Container; |
| 21 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 22 | +import org.testcontainers.vault.VaultContainer; |
| 23 | + |
| 24 | +import java.net.URI; |
| 25 | +import java.net.URISyntaxException; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 31 | + |
| 32 | +@SpringBootTest( |
| 33 | + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, |
| 34 | + classes = ControlplaneApplication.class) |
| 35 | +@Testcontainers |
| 36 | +public class TestVaultJobSecretsServiceIT extends BaseIT { |
| 37 | + |
| 38 | + @Container |
| 39 | + private static final VaultContainer vaultContainer = |
| 40 | + new VaultContainer<>("vault:1.0.2").withVaultToken("root"); |
| 41 | + |
| 42 | + private static VaultJobSecretsService vaultJobSecretService; |
| 43 | + |
| 44 | + @BeforeAll |
| 45 | + public static void init() throws URISyntaxException { |
| 46 | + String vaultUri = vaultContainer.getHttpHostAddress(); |
| 47 | + |
| 48 | + VaultEndpoint vaultEndpoint = VaultEndpoint.from(new URI(vaultUri)); |
| 49 | + TokenAuthentication clientAuthentication = new TokenAuthentication("root"); |
| 50 | + |
| 51 | + VaultTemplate vaultTemplate = new VaultTemplate(vaultEndpoint, clientAuthentication); |
| 52 | + |
| 53 | + vaultJobSecretService = new VaultJobSecretsService(vaultTemplate); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testGetEmptyDataJobSecrets() throws Exception { |
| 58 | + Map<String, Object> result = vaultJobSecretService.readJobSecrets("testJob"); |
| 59 | + Assertions.assertEquals(Collections.emptyMap(), result); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testSetDataJobSecrets() throws Exception { |
| 64 | + Map<String, Object> temp = new HashMap<>(); |
| 65 | + temp.put("key1", "value1"); |
| 66 | + |
| 67 | + Map<String, Object> secrets = Collections.unmodifiableMap(temp); |
| 68 | + |
| 69 | + vaultJobSecretService.updateJobSecrets("testJob2", secrets); |
| 70 | + |
| 71 | + Map<String, Object> readResult = vaultJobSecretService.readJobSecrets("testJob2"); |
| 72 | + Assertions.assertEquals(secrets, readResult); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testUpdateJobSecretsLimit() throws JsonProcessingException { |
| 77 | + Map<String, Object> temp = new HashMap<>(); |
| 78 | + temp.put("key1", "value1"); |
| 79 | + |
| 80 | + Map<String, Object> secrets = Collections.unmodifiableMap(temp); |
| 81 | + |
| 82 | + vaultJobSecretService.updateJobSecrets("testJob2", secrets); |
| 83 | + |
| 84 | + Map<String, Object> largeSecrets = new HashMap<>(); |
| 85 | + largeSecrets.put("key1", null); |
| 86 | + largeSecrets.put( |
| 87 | + "key2", |
| 88 | + RandomStringUtils.randomAlphabetic(VaultJobSecretsService.VAULT_SIZE_LIMIT_DEFAULT)); |
| 89 | + |
| 90 | + assertThrows( |
| 91 | + DataJobSecretsSizeLimitException.class, |
| 92 | + () -> vaultJobSecretService.updateJobSecrets("testJob2", largeSecrets)); |
| 93 | + |
| 94 | + // check secrets were not updated |
| 95 | + Map<String, Object> readResult = vaultJobSecretService.readJobSecrets("testJob2"); |
| 96 | + Assertions.assertEquals(secrets, readResult); |
| 97 | + } |
| 98 | +} |
0 commit comments