-
Notifications
You must be signed in to change notification settings - Fork 17
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 #161 from ing-bank/feature/sts_cache
add cache to sts call
- Loading branch information
Showing
9 changed files
with
129 additions
and
1 deletion.
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
Empty file.
98 changes: 98 additions & 0 deletions
98
src/it/scala/com/ing/wbaa/rokku/proxy/provider/AuthenticationCachedProviderSTSItTest.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,98 @@ | ||
package com.ing.wbaa.rokku.proxy.provider | ||
|
||
import akka.actor.ActorSystem | ||
import akka.stream.ActorMaterializer | ||
import com.amazonaws.services.securitytoken.model.{AssumeRoleRequest, GetSessionTokenRequest} | ||
import com.ing.wbaa.rokku.proxy.config.StsSettings | ||
import com.ing.wbaa.rokku.proxy.data._ | ||
import com.ing.wbaa.testkit.awssdk.StsSdkHelpers | ||
import com.ing.wbaa.testkit.oauth.OAuth2TokenRequest | ||
import org.scalatest.Assertion | ||
import org.scalatest.diagrams.Diagrams | ||
import org.scalatest.wordspec.AsyncWordSpec | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
class AuthenticationCachedProviderSTSItTest extends AsyncWordSpec with Diagrams | ||
with AuthenticationCachedProviderSTS | ||
with StsSdkHelpers | ||
with OAuth2TokenRequest { | ||
override implicit val testSystem: ActorSystem = ActorSystem.create("test-system") | ||
override implicit val system: ActorSystem = testSystem | ||
override implicit val executionContext: ExecutionContext = testSystem.dispatcher | ||
override implicit val materializer: ActorMaterializer = ActorMaterializer()(testSystem) | ||
|
||
override val stsSettings: StsSettings = StsSettings(testSystem) | ||
|
||
implicit val requestId: RequestId = RequestId("test") | ||
|
||
private val validKeycloakCredentials = Map( | ||
"grant_type" -> "password", | ||
"username" -> "testuser", | ||
"password" -> "password", | ||
"client_id" -> "sts-rokku" | ||
) | ||
private val userOneKeycloakCredentials = Map( | ||
"grant_type" -> "password", | ||
"username" -> "userone", | ||
"password" -> "password", | ||
"client_id" -> "sts-rokku" | ||
) | ||
|
||
def withAwsCredentialsValidInSTS(testCode: AwsRequestCredential => Future[Assertion]): Future[Assertion] = { | ||
val stsSdk = getAmazonSTSSdk(StsSettings(testSystem).stsBaseUri) | ||
retrieveKeycloackToken(validKeycloakCredentials).flatMap { keycloakToken => | ||
val cred = stsSdk.getSessionToken(new GetSessionTokenRequest() | ||
.withTokenCode(keycloakToken.access_token)) | ||
.getCredentials | ||
|
||
testCode(AwsRequestCredential(AwsAccessKey(cred.getAccessKeyId), Some(AwsSessionToken(cred.getSessionToken)))) | ||
} | ||
} | ||
|
||
def withAssumeRoleInSTS(testCode: AwsRequestCredential => Future[Assertion]): Future[Assertion] = { | ||
val stsSdk = getAmazonSTSSdk(StsSettings(testSystem).stsBaseUri) | ||
retrieveKeycloackToken(userOneKeycloakCredentials).flatMap { keycloakToken => | ||
val assumeRoleReq = new AssumeRoleRequest().withTokenCode(keycloakToken.access_token) | ||
assumeRoleReq.setRoleArn("arn:aws:iam::account-id:role/admin") | ||
assumeRoleReq.setRoleSessionName("testRole") | ||
val cred = stsSdk.assumeRole(assumeRoleReq).getCredentials | ||
|
||
testCode(AwsRequestCredential(AwsAccessKey(cred.getAccessKeyId), Some(AwsSessionToken(cred.getSessionToken)))) | ||
} | ||
} | ||
|
||
"Authentication Provider STS" should { | ||
"check authentication" that { | ||
"succeeds for valid credentials" in { | ||
withAwsCredentialsValidInSTS { awsCredential => | ||
areCredentialsActive(awsCredential).map { userResult => | ||
assert(userResult.map(_.userName).contains(UserName("testuser"))) | ||
assert(userResult.map(_.userGroups).head.contains(UserGroup("testgroup"))) | ||
assert(userResult.map(_.userGroups).head.contains(UserGroup("group3"))) | ||
assert(userResult.map(_.userGroups).head.size == 2) | ||
assert(userResult.exists(_.accessKey.value.length == 32)) | ||
assert(userResult.exists(_.secretKey.value.length == 32)) | ||
} | ||
} | ||
} | ||
|
||
"fail when user is not authenticated" in { | ||
areCredentialsActive(AwsRequestCredential(AwsAccessKey("notauthenticated"), Some(AwsSessionToken("okSessionToken")))).map { userResult => | ||
assert(userResult.isEmpty) | ||
} | ||
} | ||
|
||
"succeeds for valid role" in { | ||
withAssumeRoleInSTS { awsCredential => | ||
areCredentialsActive(awsCredential).map { roleResult => | ||
assert(roleResult.map(_.userRole).contains(UserAssumeRole("admin"))) | ||
assert(roleResult.map(_.userGroups).contains(Set())) | ||
assert(roleResult.exists(_.accessKey.value.length == 32)) | ||
assert(roleResult.exists(_.secretKey.value.length == 32)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
23 changes: 23 additions & 0 deletions
23
src/main/scala/com/ing/wbaa/rokku/proxy/provider/AuthenticationCachedProviderSTS.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,23 @@ | ||
package com.ing.wbaa.rokku.proxy.provider | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine | ||
import com.ing.wbaa.rokku.proxy.data.{ AwsRequestCredential, RequestId, User } | ||
import scalacache._ | ||
import scalacache.caffeine.CaffeineCache | ||
import scalacache.modes.scalaFuture._ | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.duration.DurationInt | ||
|
||
trait AuthenticationCachedProviderSTS extends AuthenticationProviderSTS { | ||
|
||
private val stsCacheConfig = | ||
Caffeine.newBuilder(). | ||
maximumSize(10000). | ||
build[String, Entry[Future[Option[User]]]] | ||
private implicit val stsCache: Cache[Future[Option[User]]] = CaffeineCache(stsCacheConfig) | ||
|
||
override protected[this] def areCredentialsActive(awsRequestCredential: AwsRequestCredential)(implicit id: RequestId): Future[Option[User]] = { | ||
caching(keyParts = awsRequestCredential)(ttl = Some(stsSettings.cacheTTLInSeconds.second))(super.areCredentialsActive(awsRequestCredential)).flatten | ||
} | ||
} |
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