-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #347 from hagay3/feature/hagai/addSkuberExecConfig
Feature/hagai/add skuber exec config
- Loading branch information
Showing
10 changed files
with
410 additions
and
237 deletions.
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
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
65 changes: 65 additions & 0 deletions
65
client/src/main/scala/skuber/api/client/token/ExecAuthRefreshable.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,65 @@ | ||
package skuber.api.client.token | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.time.{ZoneId, ZonedDateTime} | ||
import java.util.TimeZone | ||
import org.apache.commons.io.IOUtils | ||
import org.joda.time.{DateTime, DateTimeZone} | ||
import play.api.libs.json.Json | ||
import skuber.api.client.AuthProviderRefreshableAuth | ||
import scala.collection.JavaConverters._ | ||
|
||
// https://kubernetes.io/docs/reference/config-api/kubeconfig.v1/#ExecConfig | ||
final case class ExecAuthRefreshable(config: ExecAuthConfig) extends AuthProviderRefreshableAuth { | ||
override val name = "exec" | ||
|
||
@volatile private var cachedToken: Option[RefreshableToken] = None | ||
|
||
override def refreshToken: RefreshableToken = { | ||
val output = generateToken | ||
val parsed = Json.parse(output).as[ExecCredential] | ||
val refreshableToken = toRefreshableToken(parsed) | ||
cachedToken = Some(refreshableToken) | ||
refreshableToken | ||
} | ||
|
||
def accessToken: String = this.synchronized { | ||
cachedToken match { | ||
case Some(token) if isTokenExpired(token) => | ||
refreshToken.accessToken | ||
case None => | ||
refreshToken.accessToken | ||
case Some(token) => | ||
token.accessToken | ||
} | ||
} | ||
|
||
override def toString = | ||
"""ExecAuthRefreshable(accessToken=<redacted>)""".stripMargin | ||
|
||
override def isTokenExpired(refreshableToken: RefreshableToken): Boolean = { | ||
DateTime.now(DateTimeZone.UTC).isAfter(refreshableToken.expiry) | ||
} | ||
|
||
override def generateToken: String = config.execute() | ||
|
||
private def toRefreshableToken(execCredential: ExecCredential): RefreshableToken = { | ||
val utc = ZoneId.of("UTC") | ||
val now = ZonedDateTime.now(utc) | ||
val expiration = execCredential.status.expirationTimestamp.getOrElse(now.plusYears(1)) | ||
val expirationDateTime = new DateTime(expiration.toInstant.toEpochMilli, DateTimeZone.forTimeZone(TimeZone.getTimeZone(utc))) | ||
|
||
RefreshableToken(execCredential.status.token, expirationDateTime) | ||
} | ||
} | ||
|
||
final case class ExecAuthConfig(cmd: String, | ||
args: List[String], | ||
envVariables: Map[String, String]) { | ||
def execute(): String = { | ||
val process = new java.lang.ProcessBuilder((Seq(cmd) ++ args).toList.asJava) | ||
envVariables.map { case (name, value) => process.environment().put(name, value)} | ||
val output = IOUtils.toString(process.start.getInputStream, StandardCharsets.UTF_8) | ||
output | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
client/src/main/scala/skuber/api/client/token/ExecCredential.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,9 @@ | ||
package skuber.api.client.token | ||
|
||
import play.api.libs.json.{Json, OFormat} | ||
|
||
final case class ExecCredential(status: ExecCredentialStatus) | ||
|
||
object ExecCredential { | ||
implicit val execCredentialFmt: OFormat[ExecCredential] = Json.format[ExecCredential] | ||
} |
10 changes: 10 additions & 0 deletions
10
client/src/main/scala/skuber/api/client/token/ExecCredentialStatus.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,10 @@ | ||
package skuber.api.client.token | ||
|
||
import play.api.libs.json.{Json, OFormat} | ||
import skuber.Timestamp | ||
|
||
final case class ExecCredentialStatus(expirationTimestamp: Option[Timestamp], token: String) | ||
|
||
object ExecCredentialStatus { | ||
implicit val execCredentialStatusFmt: OFormat[ExecCredentialStatus] = Json.format[ExecCredentialStatus] | ||
} |
Oops, something went wrong.