Skip to content
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

Support vault prefix depth config (#73) #74

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object VaultProviderConfig {
val VAULT_CLIENT_PEM: String = "vault.client.pem"
val VAULT_PEM: String = "vault.pem"
val VAULT_ENGINE_VERSION = "vault.engine.version"
val VAULT_PREFIX_DEPTH = "vault.prefix.depth"
val AUTH_METHOD: String = "vault.auth.method"

val VAULT_TRUSTSTORE_LOC: String =
Expand Down Expand Up @@ -147,6 +148,18 @@ object VaultProviderConfig {
Importance.HIGH,
"KV Secrets Engine version of the Vault server instance. Defaults to 2",
)
.define(
VAULT_PREFIX_DEPTH,
Type.INT,
1,
Importance.HIGH,
"""
|Set the path depth of the KV Secrets Engine prefix path.
|Normally this is just 1, to correspond to one path element in the prefix path.
|To use a longer prefix path, set this value
|
|""".stripMargin,
)
// auth mode
.define(
AUTH_METHOD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ case class VaultSettings(
pem: String,
clientPem: String,
engineVersion: Int = 2,
prefixDepth: Int = 1,
appRole: Option[AppRole],
awsIam: Option[AwsIam],
gcp: Option[Gcp],
Expand Down Expand Up @@ -77,6 +78,7 @@ object VaultSettings extends StrictLogging {
val pem = config.getString(VaultProviderConfig.VAULT_PEM)
val clientPem = config.getString(VaultProviderConfig.VAULT_CLIENT_PEM)
val engineVersion = config.getInt(VaultProviderConfig.VAULT_ENGINE_VERSION)
val prefixDepth = config.getInt(VaultProviderConfig.VAULT_PREFIX_DEPTH)

val authMode = VaultAuthMethod.withNameOpt(
config.getString(VaultProviderConfig.AUTH_METHOD).toUpperCase,
Expand Down Expand Up @@ -124,6 +126,7 @@ object VaultSettings extends StrictLogging {
pem = pem,
clientPem = clientPem,
engineVersion = engineVersion,
prefixDepth = prefixDepth,
appRole = appRole,
awsIam = awsIam,
gcp = gcp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ object VaultHelper extends StrictLogging {
logger.info(s"Setting engine version to ${settings.engineVersion}")
config.engineVersion(settings.engineVersion)

logger.info(s"Setting prefix path depth to ${settings.prefixDepth}")
config.prefixPathDepth(settings.prefixDepth)

val vault = new Vault(config.build())

logger.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,47 @@ class VaultSecretProviderTest extends AnyWordSpec with Matchers with BeforeAndAf
response.getData.asScala("value") shouldBe "mock"
}

"should be configured for vault engine prefix depth" in {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a test for explicitly setting the prefix depth, can there also be a test to ensure the default value is 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added a test for the default 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidsloan Does this look good to you?


val props = Map(
VaultProviderConfig.VAULT_ADDR -> "https://127.0.0.1:9998",
VaultProviderConfig.VAULT_TOKEN -> "mock_token",
VaultProviderConfig.AUTH_METHOD -> VaultAuthMethod.TOKEN.toString(),
VaultProviderConfig.VAULT_PEM -> pemFile,
VaultProviderConfig.VAULT_CLIENT_PEM -> pemFile,
VaultProviderConfig.VAULT_PREFIX_DEPTH -> 2,
).asJava

val config = VaultProviderConfig(props)
val settings = VaultSettings(config)

settings.prefixDepth shouldBe 2
val provider = new VaultSecretProvider()
provider.configure(props)
val response = provider.getClient.get.logical.read("secret/hello")
response.getData.asScala("value") shouldBe "mock"
}

"should be configured for default vault engine prefix depth" in {

val props = Map(
VaultProviderConfig.VAULT_ADDR -> "https://127.0.0.1:9998",
VaultProviderConfig.VAULT_TOKEN -> "mock_token",
VaultProviderConfig.AUTH_METHOD -> VaultAuthMethod.TOKEN.toString(),
VaultProviderConfig.VAULT_PEM -> pemFile,
VaultProviderConfig.VAULT_CLIENT_PEM -> pemFile,
).asJava

val config = VaultProviderConfig(props)
val settings = VaultSettings(config)

settings.prefixDepth shouldBe 1
val provider = new VaultSecretProvider()
provider.configure(props)
val response = provider.getClient.get.logical.read("secret/hello")
response.getData.asScala("value") shouldBe "mock"
}

"should get values at a path" in {

val props = Map(
Expand Down
Loading