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

fix: prevent override of client request listeners in CodeWhispererServiceIAM #784

Merged
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@aws/language-server-runtimes/server-interface'
import { AWSError, ConfigurationOptions, CredentialProviderChain, Credentials } from 'aws-sdk'
import { PromiseResult } from 'aws-sdk/lib/request'
import { Request } from 'aws-sdk/lib/core'
import { v4 as uuidv4 } from 'uuid'
import {
CodeWhispererSigv4ClientConfigurationOptions,
Expand Down Expand Up @@ -90,8 +91,13 @@ export class CodeWhispererServiceIAM extends CodeWhispererServiceBase {
]),
}
this.client = createCodeWhispererSigv4Client(options, sdkInitializator)
this.client.setupRequestListeners = ({ httpRequest }) => {
httpRequest.headers['x-amzn-codewhisperer-optout'] = `${!this.shareCodeWhispererContentWithAWS}`
// Avoid overwriting any existing client listeners
const clientRequestListeners = this.client.setupRequestListeners
this.client.setupRequestListeners = (request: Request<unknown, AWSError>) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do this with caching original listeners? Would using similar implementation as for CodeWhispererServiceToken work

onRequestSetup: [
req => {
req.on('build', ({ httpRequest }) => {
const creds = credentialsProvider.getCredentials('bearer') as BearerCredentials
if (!creds?.token) {
throw new Error('Authorization failed, bearer token is not set')
}
httpRequest.headers['Authorization'] = `Bearer ${creds.token}`
httpRequest.headers['x-amzn-codewhisperer-optout'] = `${!this.shareCodeWhispererContentWithAWS}`
})
?

Copy link
Contributor Author

@francescoopiccoli francescoopiccoli Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not tested but I am confident that it would work as well. I am fine with going with that approach if you think it's better, but I would be curious to know why you think it is better, apart from consistency with the the token client implementation.
I did consider that approach before opening the PR, but it felt like it was adding more complexity as you need to extend the options (see here), and you still do cache the original listeners from my understanding (see here)

if (clientRequestListeners) {
clientRequestListeners.call(this.client, request)
}
request.httpRequest.headers['x-amzn-codewhisperer-optout'] = `${!this.shareCodeWhispererContentWithAWS}`
}
}

Expand Down
Loading