Skip to content

Commit e6b9e25

Browse files
committed
feat(amazonq): hook up lsp payload encryption
Certain LSP messages require AES-256-GCM encryption packaged as JWE
1 parent d9f7bb3 commit e6b9e25

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLspService.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import org.slf4j.event.Level
3737
import software.aws.toolkits.core.utils.getLogger
3838
import software.aws.toolkits.core.utils.warn
3939
import software.aws.toolkits.jetbrains.isDeveloperMode
40+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.encryption.JwtEncryptionManager
4041
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.createExtendedClientMetadata
4142
import software.aws.toolkits.jetbrains.services.telemetry.ClientMetadata
4243
import java.io.IOException
@@ -111,6 +112,8 @@ class AmazonQLspService(private val project: Project, private val cs: CoroutineS
111112
}
112113

113114
private class AmazonQServerInstance(private val project: Project, private val cs: CoroutineScope) : Disposable {
115+
private val encryptionManager = JwtEncryptionManager()
116+
114117
private val launcher: Launcher<AmazonQLanguageServer>
115118

116119
private val languageServer: AmazonQLanguageServer
@@ -172,7 +175,11 @@ private class AmazonQServerInstance(private val project: Project, private val cs
172175
}
173176

174177
init {
175-
val cmd = GeneralCommandLine("amazon-q-lsp")
178+
val cmd = GeneralCommandLine(
179+
"amazon-q-lsp",
180+
"--stdio",
181+
"--set-credentials-encryption-key",
182+
)
176183

177184
launcherHandler = KillableColoredProcessHandler.Silent(cmd)
178185
val inputWrapper = LSPProcessListener()
@@ -207,6 +214,9 @@ private class AmazonQServerInstance(private val project: Project, private val cs
207214
launcherFuture = launcher.startListening()
208215

209216
cs.launch {
217+
// encryption info must be sent within 5s or Flare process will exit
218+
encryptionManager.writeInitializationPayload(launcherHandler.process.outputStream)
219+
210220
val initializeResult = try {
211221
withTimeout(Duration.ofSeconds(10)) {
212222
languageServer.initialize(createInitializeParams()).await()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.services.amazonq.lsp.encryption
5+
6+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
7+
import com.nimbusds.jose.EncryptionMethod
8+
import com.nimbusds.jose.JWEAlgorithm
9+
import com.nimbusds.jose.JWEHeader
10+
import com.nimbusds.jose.JWEObject
11+
import com.nimbusds.jose.Payload
12+
import com.nimbusds.jose.crypto.DirectDecrypter
13+
import com.nimbusds.jose.crypto.DirectEncrypter
14+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.EncryptionInitializationRequest
15+
import java.io.OutputStream
16+
import java.security.SecureRandom
17+
import java.util.Base64
18+
import javax.crypto.SecretKey
19+
import javax.crypto.spec.SecretKeySpec
20+
21+
class JwtEncryptionManager(private val key: SecretKey) {
22+
constructor() : this(generateHmacKey())
23+
24+
private val mapper = jacksonObjectMapper()
25+
26+
fun writeInitializationPayload(os: OutputStream) {
27+
val payload = EncryptionInitializationRequest(
28+
EncryptionInitializationRequest.Version.V1_0,
29+
EncryptionInitializationRequest.Mode.JWT,
30+
Base64.getUrlEncoder().withoutPadding().encodeToString(key.encoded)
31+
)
32+
33+
// write directly to stream because utils are closing the underlying stream
34+
os.write("${mapper.writeValueAsString(payload)}\n".toByteArray())
35+
}
36+
37+
fun encrypt(data: Any): String {
38+
val header = JWEHeader(JWEAlgorithm.DIR, EncryptionMethod.A256GCM)
39+
val payload = Payload(mapper.writeValueAsBytes(data))
40+
val jweObject = JWEObject(header, payload)
41+
jweObject.encrypt(DirectEncrypter(key))
42+
43+
return jweObject.serialize()
44+
}
45+
46+
fun decrypt(jwt: String): String {
47+
val jweObject = JWEObject.parse(jwt)
48+
jweObject.decrypt(DirectDecrypter(key))
49+
50+
return jweObject.payload.toString()
51+
}
52+
53+
private companion object {
54+
private fun generateHmacKey(): SecretKey {
55+
val keyBytes = ByteArray(32)
56+
SecureRandom().nextBytes(keyBytes)
57+
return SecretKeySpec(keyBytes, "HmacSHA256")
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.services.amazonq.lsp.model
5+
6+
import com.fasterxml.jackson.annotation.JsonValue
7+
8+
data class EncryptionInitializationRequest(
9+
val version: Version,
10+
val mode: Mode,
11+
val key: String,
12+
) {
13+
enum class Version(@JsonValue val value: String) {
14+
V1_0("1.0"),
15+
}
16+
17+
enum class Mode(@JsonValue val value: String) {
18+
JWT("JWT"),
19+
}
20+
}

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/model/aws/credentials/UpdateCredentialsPayload.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.credentia
55

66
data class UpdateCredentialsPayload(
77
val data: String,
8-
val encrypted: String,
8+
val encrypted: Boolean,
99
)
1010

1111
data class UpdateCredentialsPayloadData(

0 commit comments

Comments
 (0)