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

Update CodeWhisperer server to use bearer tokens instead of IAM credentials #539

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions lsp/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/**
out/**
server/aws-lsp-codewhisperer/src/client/codewhispererclient.d.ts
server/aws-lsp-codewhisperer/src/client/token/codewhispererclient.d.ts
2 changes: 2 additions & 0 deletions lsp/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules/
out/
**/bin/
**/obj/
server/aws-lsp-codewhisperer/src/client/codewhispererclient.d.ts
server/aws-lsp-codewhisperer/src/client/token/codewhispererclient.d.ts
6 changes: 4 additions & 2 deletions lsp/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"outFiles": ["${workspaceFolder}/client/vscode/out/**/*.js"],
"env": {
"LSP_SERVER": "${workspaceFolder}/app/aws-lsp-codewhisperer-binary/out/index.js",
"ENABLE_INLINE_COMPLETION": "true"
"ENABLE_INLINE_COMPLETION": "true",
"ENABLE_TOKEN_PROVIDER": "true"
},
"preLaunchTask": "npm: compile"
},
Expand All @@ -59,7 +60,8 @@
"outFiles": ["${workspaceFolder}/client/vscode/out/**/*.js"],
"env": {
"LSP_SERVER": "${workspaceFolder}/app/aws-lsp-s3-binary/out/index.js",
"ENABLE_IAM_PROVIDER": "true"
"ENABLE_IAM_PROVIDER": "true",
"ENABLE_TOKEN_PROVIDER": "false"
},
"preLaunchTask": "npm: compile"
},
Expand Down
46 changes: 36 additions & 10 deletions lsp/app/aws-lsp-codewhisperer-binary/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,46 @@ import {
CodeWhispererServiceProps,
createCodeWhispererService,
} from '@lsp-placeholder/aws-lsp-codewhisperer'
import {
AwsInitializationOptions,
IdeCredentialsProvider,
readEncryptionInitialization,
shouldWaitForEncryptionKey,
} from '@lsp-placeholder/aws-lsp-core'
import { ProposedFeatures, createConnection } from 'vscode-languageserver/node'

const connection = createConnection(ProposedFeatures.all)
const lspConnection = createConnection(ProposedFeatures.all)

const serviceProps: CodeWhispererServiceProps = {
displayName: CodeWhispererServer.serverId,
connection,
if (shouldWaitForEncryptionKey()) {
// Before starting the language server, accept encryption initialization details
// directly from the host. This avoids writing the key to the same channel used
// to send encrypted data.
// Contract: Only read up to (and including) the first newline (\n).
readEncryptionInitialization(process.stdin).then(encryptionInit => {
createServer(lspConnection, encryptionInit.key)
})
} else {
createServer(lspConnection)
}

const service = createCodeWhispererService(serviceProps)
function createServer(connection: any, key?: string): CodeWhispererServer {
const credentialsProvider = new IdeCredentialsProvider(connection, key)

const props: CodeWhispererServerProps = {
connection,
codeWhispererService: service,
}
const serviceProps: CodeWhispererServiceProps = {
displayName: CodeWhispererServer.serverId,
connection,
credentialsProvider,
}

const service = createCodeWhispererService(serviceProps)

export const server = new CodeWhispererServer(props)
const props: CodeWhispererServerProps = {
connection,
codeWhispererService: service,
onInitialize: (props: AwsInitializationOptions) => {
credentialsProvider.initialize(props)
},
}

return new CodeWhispererServer(props)
}
45 changes: 3 additions & 42 deletions lsp/app/aws-lsp-s3-binary/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
AwsInitializationOptions,
EncryptionInitialization,
IdeCredentialsProvider,
readEncryptionInitialization,
shouldWaitForEncryptionKey,
validateEncryptionDetails,
} from '@lsp-placeholder/aws-lsp-core'
import { S3Server, S3ServerProps, S3ServiceProps, createS3ervice } from '@lsp-placeholder/aws-lsp-s3'
import { Readable } from 'stream'
import { ProposedFeatures, createConnection } from 'vscode-languageserver/node'

const lspConnection = createConnection(ProposedFeatures.all)
Expand All @@ -16,50 +14,13 @@ if (shouldWaitForEncryptionKey()) {
// directly from the host. This avoids writing the key to the same channel used
// to send encrypted data.
// Contract: Only read up to (and including) the first newline (\n).
readLine(process.stdin).then(input => {
const encryptionDetails = JSON.parse(input) as EncryptionInitialization

validateEncryptionDetails(encryptionDetails)

createServer(lspConnection, encryptionDetails.key)
readEncryptionInitialization(process.stdin).then(encryptionInit => {
createServer(lspConnection, encryptionInit.key)
})
} else {
createServer(lspConnection)
}

/**
* Read from the given stream, stopping after the first newline (\n).
* Return the string consumed from the stream.
*/
function readLine(stream: Readable): Promise<string> {
return new Promise<string>((resolve, reject) => {
let contents = ''

// Fires when the stream has contents that can be read
const onStreamIsReadable = () => {
while (true) {
const byteRead: Buffer = process.stdin.read(1)
if (byteRead == null) {
// wait for more content to arrive on the stream
break
}

const nextChar = byteRead.toString('utf-8')
contents += nextChar

if (nextChar == '\n') {
// Stop reading this stream, we have read a line from it
stream.removeListener('readable', onStreamIsReadable)
resolve(contents)
break
}
}
}

stream.on('readable', onStreamIsReadable)
})
}

function createServer(connection: any, key?: string): S3Server {
const credentialsProvider = new IdeCredentialsProvider(connection, key)

Expand Down
Loading