-
Notifications
You must be signed in to change notification settings - Fork 8
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 login with role_id and secret_id #303
Open
mhodovaniuk
wants to merge
6
commits into
Banno:main
Choose a base branch
from
mhodovaniuk:login-with-role-id-and-secret-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8927fdb
Support login with role_id and secret_id
mhodovaniuk 4bcb82b
Fix build
mhodovaniuk 2fadd15
Fix build
mhodovaniuk 30aac32
Rename secretId to roleSecretId
mhodovaniuk 0348bc6
Rename secretId to roleSecretId
mhodovaniuk 20f46cc
Pass roleSecretId to login with builder pattern
mhodovaniuk 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 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 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 |
---|---|---|
|
@@ -37,18 +37,8 @@ object Vault { | |
/** | ||
* https://www.vaultproject.io/api/auth/approle/index.html#login-with-approle | ||
*/ | ||
def login[F[_]](client: Client[F], vaultUri: Uri)(roleId: String)(implicit F: Concurrent[F]): F[VaultToken] = { | ||
val request = Request[F]( | ||
method = Method.POST, | ||
uri = vaultUri / "v1" / "auth" / "approle" / "login" | ||
).withEntity(Json.obj(("role_id", Json.fromString(roleId)))) | ||
for { | ||
json <- F.handleErrorWith(client.expect[Json](request) | ||
) { e => | ||
F.raiseError(VaultRequestError(request, e.some, s"roleId=$roleId".some)) | ||
} | ||
token <- raiseKnownError(json.hcursor.get[VaultToken]("auth"))(decoderError) | ||
} yield token | ||
def login[F[_]](client: Client[F], vaultUri: Uri): VaultLoginOperation[F] = { | ||
VaultLoginOperationImpl(client, vaultUri) | ||
} | ||
|
||
|
||
|
@@ -227,9 +217,8 @@ object Vault { | |
} | ||
} | ||
|
||
def loginAndKeepSecretLeased[F[_]: Temporal, A: Decoder](client: Client[F], vaultUri: Uri) | ||
(roleId: String, secretPath: String, duration: FiniteDuration, waitInterval: FiniteDuration): Stream[F, A] = | ||
Stream.eval(login(client, vaultUri)(roleId)).flatMap(token => keepLoginAndSecretLeased[F, A](client, vaultUri)(token, secretPath, duration, waitInterval)) | ||
def loginAndKeepSecretLeased[F[_], A](client: Client[F], vaultUri: Uri): VaultLoginAndKeepSecretLeasedOperation[F, A] = | ||
VaultLoginAndKeepSecretLeasedOperationImpl(client, vaultUri) | ||
|
||
def loginK8sAndKeepSecretLeased[F[_]: Temporal, A: Decoder](client: Client[F], vaultUri: Uri) | ||
(roleId: String, jwt: String, secretPath: String, duration: FiniteDuration, waitInterval: FiniteDuration, loginMountPoint: Uri.Path = path"/auth/kubernetes" ): Stream[F, A] = | ||
|
@@ -269,9 +258,9 @@ object Vault { | |
} | ||
} | ||
|
||
def loginAndKeep[F[_]: Async](client: Client[F], vaultUri: Uri) | ||
(roleId: String, tokenLeaseExtension: FiniteDuration): Stream[F, String] = | ||
Stream.eval(login(client, vaultUri)(roleId)).flatMap(token => keepLoginRenewed[F](client, vaultUri)(token, tokenLeaseExtension)) | ||
def loginAndKeep[F[_]](client: Client[F], vaultUri: Uri): VaultLoginAndKeepOperation[F] = { | ||
VaultLoginAndKeepOperationImpl(client, vaultUri) | ||
} | ||
|
||
|
||
/** | ||
|
@@ -333,4 +322,59 @@ object Vault { | |
override def getMessage(): String = s"Token lease $leaseId could not be renewed any longer" | ||
} | ||
|
||
trait VaultLoginOperation[F[_]] { | ||
def withRoleSecretId(roleSecretId: String): VaultLoginOperation[F] | ||
def apply(roleId: String)(implicit F: Concurrent[F]): F[VaultToken] | ||
} | ||
|
||
final case class VaultLoginOperationImpl[F[_]](client: Client[F], vaultUri: Uri, roleSecretId: Option[String] = None) extends VaultLoginOperation[F] { | ||
override def withRoleSecretId(roleSecretId: String): VaultLoginOperation[F] = copy(roleSecretId = Some(roleSecretId)) | ||
override def apply(roleId: String)(implicit F: Concurrent[F]): F[VaultToken] = { | ||
val request = Request[F]( | ||
method = Method.POST, | ||
uri = vaultUri / "v1" / "auth" / "approle" / "login" | ||
).withEntity( | ||
Json.fromFields( | ||
Seq("role_id" -> Json.fromString(roleId)) ++ | ||
roleSecretId.fold(Seq[(String, Json)]())(sId => Seq("secret_id" -> Json.fromString(sId))) | ||
) | ||
) | ||
for { | ||
json <- F.handleErrorWith(client.expect[Json](request) | ||
) { e => | ||
F.raiseError(VaultRequestError(request, e.some, s"roleId=$roleId, roleSecretId=$roleSecretId".some)) | ||
} | ||
token <- raiseKnownError(json.hcursor.get[VaultToken]("auth"))(decoderError) | ||
} yield token | ||
} | ||
} | ||
|
||
trait VaultLoginAndKeepOperation[F[_]] { | ||
def withRoleSecretId(roleSecretId: String): VaultLoginAndKeepOperation[F] | ||
def apply(roleId: String, tokenLeaseExtension: FiniteDuration)(implicit A: Async[F]): Stream[F, String] | ||
} | ||
|
||
final case class VaultLoginAndKeepOperationImpl[F[_]](client: Client[F], vaultUri: Uri, roleSecretId: Option[String] = None) extends VaultLoginAndKeepOperation[F] { | ||
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. Same comments as VaultLoginOperationImpl. |
||
override def withRoleSecretId(roleSecretId: String): VaultLoginAndKeepOperation[F] = copy(roleSecretId = Some(roleSecretId)) | ||
def apply(roleId: String, tokenLeaseExtension: FiniteDuration)(implicit A: Async[F]): Stream[F, String] = { | ||
val loginOperation = login(client, vaultUri) | ||
Stream.eval(roleSecretId.fold(loginOperation)(loginOperation.withRoleSecretId)(roleId)) | ||
.flatMap(token => keepLoginRenewed[F](client, vaultUri)(token, tokenLeaseExtension)) | ||
} | ||
} | ||
|
||
trait VaultLoginAndKeepSecretLeasedOperation[F[_], A] { | ||
def withRoleSecretId(roleSecretId: String): VaultLoginAndKeepSecretLeasedOperation[F, A] | ||
def apply(roleId: String, secretPath: String, duration: FiniteDuration, waitInterval: FiniteDuration)(implicit T: Temporal[F], D: Decoder[A]): Stream[F, A] | ||
} | ||
|
||
final case class VaultLoginAndKeepSecretLeasedOperationImpl[F[_], A](client: Client[F], vaultUri: Uri, roleSecretId: Option[String] = None) extends VaultLoginAndKeepSecretLeasedOperation[F, A] { | ||
override def withRoleSecretId(roleSecretId: String): VaultLoginAndKeepSecretLeasedOperation[F, A] = copy(roleSecretId = Some(roleSecretId)) | ||
override def apply(roleId: String, secretPath: String, duration: FiniteDuration, waitInterval: FiniteDuration)(implicit T: Temporal[F], D: Decoder[A]): Stream[F, A] = { | ||
val loginOperation = login(client, vaultUri) | ||
Stream.eval(roleSecretId.fold(loginOperation)(loginOperation.withRoleSecretId)(roleId)) | ||
.flatMap(token => keepLoginAndSecretLeased[F, A](client, vaultUri)(token, secretPath, duration, waitInterval)) | ||
} | ||
} | ||
|
||
} |
This file contains 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.
When this is a case class, we can't add new parameters later in a bincompat fashion, so this isn't quite the builder pattern. Making it an abstract class with a private constructor should work. You'll need to implement your own
copy
then. I also don't think this needs to be public.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.
@rossabaker thanks you for getting back to this
May I kindly ask you to provide some examples of similar implementations with "abstract class with a private constructor"?
I tried to get back to the examples that you provided before to get some insights, but it looks like the project was refactored and now it uses case class.