-
Notifications
You must be signed in to change notification settings - Fork 29
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
Issue 204 - Bound tokens for incluster configuration #218
Merged
hagay3
merged 4 commits into
hagay3:master
from
jahzielHA:issue-204-bound-service-account
Sep 8, 2022
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
67 changes: 67 additions & 0 deletions
67
client/src/main/scala/skuber/api/client/token/FileTokenAuthRefreshable.scala
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package skuber.api.client.token | ||
|
||
import org.joda.time.DateTime | ||
import skuber.K8SException | ||
import skuber.api.client.{AuthProviderRefreshableAuth, Status} | ||
|
||
import scala.concurrent.duration.{Duration, DurationInt} | ||
import scala.util.{Failure, Success} | ||
|
||
final case class FileTokenAuthRefreshable(config: FileTokenConfiguration) extends TokenAuthRefreshable with FileReaderComponent {} | ||
|
||
final case class FileTokenConfiguration( | ||
cachedAccessToken: Option[String], | ||
tokenPath: Option[String], | ||
refreshInterval: Duration = 5.minutes, | ||
) | ||
|
||
trait TokenAuthRefreshable extends AuthProviderRefreshableAuth { self: ContentReaderComponent => | ||
val config: FileTokenConfiguration | ||
|
||
private val refreshInterval: Duration = config.refreshInterval | ||
@volatile private var cachedToken: Option[RefreshableToken] = config.cachedAccessToken.map(buildRefreshableToken) | ||
|
||
private val tokenPath: String = { | ||
config.tokenPath.getOrElse { | ||
throw new K8SException( | ||
Status(reason = Some("token path not found, please provide the token path for refreshing the token")) | ||
) | ||
} | ||
} | ||
|
||
override def name: String = "file-token" | ||
override def toString: String = """FileTokenAuthRefreshable(accessToken=<redacted>)""".stripMargin | ||
|
||
override def refreshToken: RefreshableToken = { | ||
val refreshedToken = buildRefreshableToken(generateToken) | ||
cachedToken = Some(refreshedToken) | ||
refreshedToken | ||
} | ||
|
||
override def generateToken: String = { | ||
val maybeToken = contentReader.read(tokenPath) | ||
maybeToken match { | ||
case Success(token) => token | ||
case Failure(e) => throw new K8SException(Status(reason = Option(e.getMessage))) | ||
} | ||
} | ||
|
||
override def isTokenExpired(refreshableToken: RefreshableToken): Boolean = | ||
refreshableToken.expiry.isBefore(System.currentTimeMillis) | ||
|
||
override def accessToken: String = this.synchronized { | ||
cachedToken match { | ||
case Some(token) if isTokenExpired(token) => | ||
refreshToken.accessToken | ||
case None => | ||
refreshToken.accessToken | ||
case Some(token) => | ||
token.accessToken | ||
} | ||
} | ||
|
||
private def buildRefreshableToken(accessToken: String): RefreshableToken = { | ||
RefreshableToken(accessToken, DateTime.now.plus(refreshInterval.toMillis)) | ||
} | ||
} | ||
|
26 changes: 26 additions & 0 deletions
26
client/src/main/scala/skuber/api/client/token/package.scala
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package skuber.api.client | ||
|
||
import scala.io.Source | ||
import scala.util.Try | ||
|
||
package object token { | ||
trait ContentReaderComponent { | ||
val contentReader: ContentReader | ||
|
||
trait ContentReader { | ||
def read(filePath: String): Try[String] | ||
} | ||
} | ||
|
||
trait FileReaderComponent extends ContentReaderComponent { | ||
val contentReader: ContentReader = new FileContentReader | ||
|
||
class FileContentReader extends ContentReader { | ||
def read(filePath: String): Try[String] = for { | ||
source <- Try(Source.fromFile(filePath, "utf-8")) | ||
content <- Try(source.getLines().mkString("\n")) | ||
_ <- Try(source.close()) | ||
} yield content | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
client/src/test/scala/skuber/api/client/token/FileTokenAuthRefreshableSpec.scala
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package skuber.api.client.token | ||
|
||
import org.joda.time.DateTime | ||
import org.specs2.mutable.Specification | ||
|
||
import scala.concurrent.duration.DurationInt | ||
import scala.util.Try | ||
|
||
class FileTokenAuthRefreshableSpec extends Specification { | ||
"This is a specification for the 'FileTokenAuthRefreshable' class".txt | ||
|
||
trait MockFileReaderComponent extends ContentReaderComponent { | ||
val contentReader: ContentReader = new MockFileReaderComponent | ||
|
||
class MockFileReaderComponent extends ContentReader { | ||
def read(filePath: String): Try[String] = Try(DateTime.now.toString()) | ||
} | ||
} | ||
|
||
final case class MockFileTokenAuthRefreshable(config: FileTokenConfiguration) extends TokenAuthRefreshable with MockFileReaderComponent {} | ||
|
||
"FileTokenAuthRefreshable" should { | ||
"Retrieve the token if none provided" in { | ||
val initialToken : Option[String] = None | ||
val fileTokenRefreshable = MockFileTokenAuthRefreshable(FileTokenConfiguration(cachedAccessToken = initialToken, tokenPath = Some("/tmp/token"), refreshInterval = 100.milliseconds)) | ||
fileTokenRefreshable.accessToken.nonEmpty must beTrue | ||
} | ||
|
||
"Refresh the token after the refresh interval" in { | ||
val initialToken = "cachedToken" | ||
val fileTokenRefreshable = MockFileTokenAuthRefreshable(FileTokenConfiguration(Some(initialToken), Some("/tmp/token"), 100.milliseconds)) | ||
fileTokenRefreshable.accessToken shouldEqual initialToken | ||
|
||
Thread.sleep(150) | ||
val refreshed = fileTokenRefreshable.accessToken | ||
refreshed shouldNotEqual initialToken | ||
|
||
Thread.sleep(150) | ||
fileTokenRefreshable.accessToken shouldNotEqual refreshed | ||
} | ||
} | ||
} |
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.
tokenPath should be mandatory rather than Optional
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.
Changed