Skip to content
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

Use RDS utilities from SDK to generate auth token #2370

Merged
merged 5 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ publishChannel=
# Common dependencies
ideProfileName=2020.1
kotlinVersion=1.4.21
awsSdkVersion=2.15.75
awsSdkVersion=2.16.3
coroutinesVersion=1.3.3
ideaPluginVersion=0.6.3
ktlintVersion=0.39.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ import com.intellij.database.dataSource.DatabaseCredentialsAuthProvider
import com.intellij.database.dataSource.LocalDataSource
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.future.future
import software.amazon.awssdk.auth.signer.Aws4Signer
import software.amazon.awssdk.auth.signer.params.Aws4PresignerParams
import software.amazon.awssdk.http.SdkHttpFullRequest
import software.amazon.awssdk.http.SdkHttpMethod
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.rds.RdsUtilities
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.core.utils.info
import software.aws.toolkits.jetbrains.core.credentials.ConnectionSettings
Expand All @@ -31,8 +28,6 @@ import software.aws.toolkits.resources.message
import software.aws.toolkits.telemetry.DatabaseCredentials.IAM
import software.aws.toolkits.telemetry.RdsTelemetry
import software.aws.toolkits.telemetry.Result
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.concurrent.CompletionStage

data class RdsAuth(
Expand All @@ -44,6 +39,8 @@ data class RdsAuth(

// [DatabaseAuthProvider] is marked as internal, but JetBrains advised this was a correct usage
class IamAuth : DatabaseAuthProvider, CoroutineScope by ApplicationThreadPoolScope("RdsIamAuth") {
private val rdsUtilities = RdsUtilities.builder().build()

override fun getId(): String = providerId
override fun getDisplayName(): String = message("rds.iam_connection_display_name")

Expand Down Expand Up @@ -91,28 +88,12 @@ class IamAuth : DatabaseAuthProvider, CoroutineScope by ApplicationThreadPoolSco
)
}

internal fun generateAuthToken(auth: RdsAuth): String {
// TODO: Replace when SDK V2 backfills the pre-signer for rds auth token
val httpRequest = SdkHttpFullRequest.builder()
.method(SdkHttpMethod.GET)
.protocol("https")
.host(auth.address)
.port(auth.port)
.encodedPath("/")
.putRawQueryParameter("DBUser", auth.user)
.putRawQueryParameter("Action", "connect")
.build()

// TODO consider configurable expiration time (but 15 is the max)
val expirationTime = Instant.now().plus(15, ChronoUnit.MINUTES)
val presignRequest = Aws4PresignerParams.builder()
.expirationTime(expirationTime)
.awsCredentials(auth.connectionSettings.credentials.resolveCredentials())
.signingName("rds-db")
.signingRegion(Region.of(auth.connectionSettings.region.id))
.build()

return Aws4Signer.create().presign(httpRequest, presignRequest).uri.toString().removePrefix("https://")
internal fun generateAuthToken(auth: RdsAuth): String = rdsUtilities.generateAuthenticationToken {
it.credentialsProvider(auth.connectionSettings.credentials)
it.region(Region.of(auth.connectionSettings.region.id))
it.hostname(auth.address)
it.port(auth.port)
it.username(auth.user)
}

private fun getCredentials(connection: ProtoConnection): Credentials {
Expand Down