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

feat: add keytar word to user agent #130

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/base-commands/twilio-client-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TwilioClientCommand extends BaseCommand {
await super.run();

this.currentProfile = this.userConfig.getProfileById(this.flags.profile);
let keytarFlag = false;

const reportUnconfigured = (verb, message = '') => {
const profileParam = this.flags.profile ? ` --profile "${this.flags.profile}"` : '';
Expand All @@ -47,9 +48,10 @@ class TwilioClientCommand extends BaseCommand {
}
this.currentProfile.apiKey = creds.apiKey;
this.currentProfile.apiSecret = creds.apiSecret;
keytarFlag = true;
}

this.httpClient = new CliRequestClient(this.id, this.logger);
this.httpClient = new CliRequestClient(this.id, this.logger, undefined, keytarFlag);
}

async catch(error) {
Expand Down
6 changes: 4 additions & 2 deletions src/services/cli-http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const NETWORK_ERROR_CODES = new Set(['ETIMEDOUT', 'ESOCKETTIMEDOUT', 'ECONNABORT
const STANDARD_HEADERS = ['user-agent', 'accept-charset', 'connection', 'authorization', 'accept', 'content-type'];

class CliRequestClient {
constructor(commandName, logger, http) {
constructor(commandName, logger, http, keytarFlag = false) {
this.commandName = commandName;
this.logger = logger;
this.http = http || require('axios');
Expand All @@ -26,6 +26,7 @@ class CliRequestClient {
this.http.defaults.proxy = false;
this.http.defaults.httpsAgent = new HttpsProxyAgent(process.env.HTTP_PROXY);
}
this.keytarWord = keytarFlag ? 'keytar' : '';
}

/**
Expand Down Expand Up @@ -67,7 +68,8 @@ class CliRequestClient {
const componentInfo = (headers['User-Agent'] || '').replace(' (', '|').replace(')', '').split('|');
componentInfo.push(`${os.platform()} ${os.release()} ${os.arch()}`);
componentInfo.push(this.commandName);
headers['User-Agent'] = `${pkg.name}/${pkg.version} (${componentInfo.join(', ')})`;
componentInfo.push(this.keytarWord);
headers['User-Agent'] = `${pkg.name}/${pkg.version} (${componentInfo.filter(Boolean).join(', ')})`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a test which verifies that empty pieces don't get added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is currently no test for the user-agent header itself in unit tests. I've manually tested in the release feature branch with API keys in the file that the empty piece doesn't get added. [DEBUG] User-Agent: @twilio/cli-core/5.24.0 (twilio-node/3.64.0, node.js v16.2.0, darwin 20.5.0 x64, debugger:logs:list)


const options = {
timeout: opts.timeout || 30000,
Expand Down
17 changes: 12 additions & 5 deletions test/services/cli-http-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ describe('services', () => {
});

test.it('should make an http request', async () => {
const client = new CliRequestClient('blah', logger, (options) => {
expect(options.url).to.equal('https://foo.com/bar');
return { status: 200, data: 'foo', headers: {} };
});
const client = new CliRequestClient(
'blah',
logger,
(options) => {
expect(options.url).to.equal('https://foo.com/bar');
return { status: 200, data: 'foo', headers: {} };
},
true,
);
expect(client.commandName).to.equal('blah');
expect(client.keytarWord).to.equal('keytar');
const response = await client.request({
method: 'POST',
uri: 'https://foo.com/bar',
Expand All @@ -32,8 +38,9 @@ describe('services', () => {

test.it('should add the correct http agent for proxy', async () => {
process.env.HTTP_PROXY = 'http://someproxy.com:8080';
const client = new CliRequestClient('blah', logger, { defaults: {} });
const client = new CliRequestClient('blah', logger, { defaults: {} }, false);
const httpAgent = client.http.defaults.httpsAgent;
expect(client.keytarWord).to.equal('');
expect(httpAgent.proxy.host).to.equal('someproxy.com');
expect(httpAgent.proxy.port).to.equal(8080);
});
Expand Down