-
Notifications
You must be signed in to change notification settings - Fork 35
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
Vault - Support default TTLs #33
Changes from all commits
c98c47a
1bf5663
51a0be0
1527fc8
fde97de
2d42ebe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,7 +6,6 @@ | |||||
|
||||||
package io.lenses.connect.secrets.providers | ||||||
|
||||||
import java.nio.file.FileSystems | ||||||
import java.nio.file.Paths | ||||||
import java.time.OffsetDateTime | ||||||
import java.util | ||||||
|
@@ -15,6 +14,7 @@ import _root_.io.lenses.connect.secrets.config.VaultProviderConfig | |||||
import _root_.io.lenses.connect.secrets.config.VaultSettings | ||||||
import _root_.io.lenses.connect.secrets.connect._ | ||||||
import com.bettercloud.vault.Vault | ||||||
import com.bettercloud.vault.response.LogicalResponse | ||||||
import io.lenses.connect.secrets.async.AsyncFunctionLoop | ||||||
import io.lenses.connect.secrets.io.FileWriterOnce | ||||||
import io.lenses.connect.secrets.utils.EncodingAndId | ||||||
|
@@ -30,7 +30,6 @@ import scala.util.Try | |||||
|
||||||
class VaultSecretProvider() extends ConfigProvider with VaultHelper { | ||||||
|
||||||
private val separator: String = FileSystems.getDefault.getSeparator | ||||||
private var settings: VaultSettings = _ | ||||||
private var vaultClient: Option[Vault] = None | ||||||
private var tokenRenewal: Option[AsyncFunctionLoop] = None | ||||||
|
@@ -122,7 +121,7 @@ class VaultSecretProvider() extends ConfigProvider with VaultHelper { | |||||
|
||||||
logger.info(s"Looking up value at [$path]") | ||||||
val fileWriter = new FileWriterOnce(Paths.get(settings.fileDir, path)) | ||||||
Try(vaultClient.get.logical().read(path)) match { | ||||||
Try(readSecretFromVault(path)) match { | ||||||
case Success(response) => | ||||||
if (response.getRestResponse.getStatus != 200) { | ||||||
throw new ConnectException( | ||||||
|
@@ -133,8 +132,15 @@ class VaultSecretProvider() extends ConfigProvider with VaultHelper { | |||||
) | ||||||
} | ||||||
|
||||||
val ttl = Option(vaultClient.get.logical().read(path).getLeaseDuration) match { | ||||||
case Some(duration) => Some(now.plusSeconds(duration)) | ||||||
val ttl = Option(response.getLeaseDuration) match { | ||||||
case Some(duration) => | ||||||
if (duration != 0) { | ||||||
Some(now.plusSeconds(duration)) | ||||||
} else if (settings.secretTtlDefault > 0) { | ||||||
Some(now.plusSeconds(settings.secretTtlDefault)) | ||||||
} else { | ||||||
None | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's been a "long time" since I wrote this code (3 weeks! 😆 ) and some of the changes mixed in here were required when combined with other changes (#32 and #34). It was a bit of a mess to try to split these 3 PRs up into cleanly applicable units after the fact and it looks like other unrelated changes have also made their way in too, such as the If I recall correctly, returning I lack time at the moment to untangle this, but will note that we've been running a patched version that combines #32, #33, and #34 for a few weeks now without issues (https://github.com/NextDeveloperTeam/secret-provider/commits/master). Also thanks for all the feedback and hoping to find some time to revisit in more detail in the new year. |
||||||
} | ||||||
case None => None | ||||||
} | ||||||
|
||||||
|
@@ -162,4 +168,20 @@ class VaultSecretProvider() extends ConfigProvider with VaultHelper { | |||||
) | ||||||
} | ||||||
} | ||||||
|
||||||
private def readSecretFromVault(path: String): LogicalResponse = { | ||||||
// Check if the path looks like it's referencing the vault database engine. If so, use the `database()` api of the | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment can be removed I think, it states what the code does line by line without adding value. |
||||||
// vault client to obtain this secret. Otherwise, fall back to the default key-vaule engine. | ||||||
if (path.startsWith("database/creds/")) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to have a test case for database creds. A good place to start from would probably be the test case for kv secrets. |
||||||
val parts = path.split("/") | ||||||
|
||||||
if (parts.length == 3) { | ||||||
vaultClient.get.database().creds(parts(2)) | ||||||
} else { | ||||||
throw new Exception("Database path is invalid. Path must be in the form 'database/creds/<role_name>'") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} else { | ||||||
vaultClient.get.logical().read(path) | ||||||
} | ||||||
} | ||||||
} |
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.
The referenced issue is closed, I suggest the following wording to keep things simple: