-
Notifications
You must be signed in to change notification settings - Fork 4.9k
secret-persistence: Hashicorp Vault Secret Store #13616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alafanechere
merged 16 commits into
airbytehq:master
from
heap:stella-vault-secret-store
Jun 24, 2022
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4b229e6
Add vault secret persistence
schung507 4c496f3
Add test
schung507 613231e
Add final
schung507 9d600b6
Adjust permissions
schung507 0986a33
Merge branch 'master' into stella-vault-secret-store
schung507 29fe3ff
Merge branch 'master' of https://github.com/airbytehq/airbyte into st…
23925c5
Address PR comments
a7cf4bd
make constructor public
8a44740
Add auth method
20c4bc6
Remove the bettercloud vault token environment variable and add one f…
a326740
Remove unused client
846742f
Update VAULT_AUTH_TOKEN to not default to empty string
bcdf863
Update docs/operator-guides/configuring-airbyte.md
c41e60a
Merge branch 'master' of https://github.com/airbytehq/airbyte into st…
0a7610d
Merge branch 'stella-vault-secret-store' of github.com:heap/airbyte i…
1f34e0f
Formatting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...nce/src/main/java/io/airbyte/config/persistence/split_secrets/VaultSecretPersistence.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
| */ | ||
|
|
||
| package io.airbyte.config.persistence.split_secrets; | ||
|
|
||
| import com.bettercloud.vault.Vault; | ||
| import com.bettercloud.vault.VaultConfig; | ||
| import com.bettercloud.vault.VaultException; | ||
| import io.airbyte.commons.lang.Exceptions; | ||
| import java.util.HashMap; | ||
| import java.util.Optional; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import lombok.val; | ||
|
|
||
| @Slf4j | ||
| final public class VaultSecretPersistence implements SecretPersistence { | ||
|
|
||
| private final String SECRET_KEY = "value"; | ||
| private final Vault vault; | ||
| private final String pathPrefix; | ||
|
|
||
| public VaultSecretPersistence(final String address, final String prefix, final String token) { | ||
| this.vault = Exceptions.toRuntime(() -> getVaultClient(address, token)); | ||
| this.pathPrefix = prefix; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<String> read(final SecretCoordinate coordinate) { | ||
| try { | ||
| val response = vault.logical().read(pathPrefix + coordinate.getFullCoordinate()); | ||
| val restResponse = response.getRestResponse(); | ||
| val responseCode = restResponse.getStatus(); | ||
| if (responseCode != 200) { | ||
| log.error("Vault failed on read. Response code: " + responseCode); | ||
| return Optional.empty(); | ||
| } | ||
| val data = response.getData(); | ||
| return Optional.of(data.get(SECRET_KEY)); | ||
| } catch (final VaultException e) { | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(final SecretCoordinate coordinate, final String payload) { | ||
| try { | ||
| val newSecret = new HashMap<String, Object>(); | ||
| newSecret.put(SECRET_KEY, payload); | ||
| vault.logical().write(pathPrefix + coordinate.getFullCoordinate(), newSecret); | ||
| } catch (final VaultException e) { | ||
| log.error("Vault failed on write", e); | ||
| } | ||
| } | ||
|
|
||
| private static Vault getVaultClient(final String address, final String token) throws VaultException { | ||
| val config = new VaultConfig() | ||
| .address(address) | ||
| .token(token) | ||
| .engineVersion(2) | ||
| .build(); | ||
| return new Vault(config); | ||
| } | ||
|
|
||
| } | ||
64 changes: 64 additions & 0 deletions
64
...src/test/java/io/airbyte/config/persistence/split_secrets/VaultSecretPersistenceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
| */ | ||
|
|
||
| package io.airbyte.config.persistence.split_secrets; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import lombok.val; | ||
| import org.apache.commons.lang3.RandomUtils; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.testcontainers.vault.VaultContainer; | ||
|
|
||
| public class VaultSecretPersistenceTest { | ||
|
|
||
| private VaultSecretPersistence persistence; | ||
| private String baseCoordinate; | ||
|
|
||
| private VaultContainer vaultContainer; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| vaultContainer = new VaultContainer("vault").withVaultToken("vault-dev-token-id"); | ||
| vaultContainer.start(); | ||
|
|
||
| val vaultAddress = "http://" + vaultContainer.getHost() + ":" + vaultContainer.getFirstMappedPort(); | ||
|
|
||
| persistence = new VaultSecretPersistence(vaultAddress, "secret/testing", "vault-dev-token-id"); | ||
| baseCoordinate = "VaultSecretPersistenceIntegrationTest_coordinate_" + RandomUtils.nextInt() % 20000; | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| vaultContainer.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| void testReadWriteUpdate() { | ||
| val coordinate1 = new SecretCoordinate(baseCoordinate, 1); | ||
|
|
||
| // try reading non-existent value | ||
| val firstRead = persistence.read(coordinate1); | ||
| assertThat(firstRead.isEmpty()).isTrue(); | ||
|
|
||
| // write | ||
| val firstPayload = "abc"; | ||
| persistence.write(coordinate1, firstPayload); | ||
| val secondRead = persistence.read(coordinate1); | ||
| assertThat(secondRead.isPresent()).isTrue(); | ||
| assertEquals(firstPayload, secondRead.get()); | ||
|
|
||
| // update | ||
| val secondPayload = "def"; | ||
| val coordinate2 = new SecretCoordinate(baseCoordinate, 2); | ||
| persistence.write(coordinate2, secondPayload); | ||
| val thirdRead = persistence.read(coordinate2); | ||
| assertThat(thirdRead.isPresent()).isTrue(); | ||
| assertEquals(secondPayload, thirdRead.get()); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be made available for the non-test mode? It looks like it will only required to add one env variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should. Would this actually be 2 env vars? One to indicate the auth type and the other to pass in a token?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think this should be it.