Skip to content

Commit cd43bcf

Browse files
authored
Give warning for deprecated clientId param (#282)
* renamed clientId to userAgentHeader in connect args * add warning for deprecated clientId param * add backward compatibility
1 parent 538bf7a commit cd43bcf

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

lib/DBSQLClient.ts

+11
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I
160160
* const session = client.connect({host, path, token});
161161
*/
162162
public async connect(options: ConnectionOptions, authProvider?: IAuthentication): Promise<IDBSQLClient> {
163+
const deprecatedClientId = (options as any).clientId;
164+
if (deprecatedClientId !== undefined) {
165+
this.logger.log(
166+
LogLevel.warn,
167+
'Warning: The "clientId" option is deprecated. Please use "userAgentEntry" instead.',
168+
);
169+
if (!options.userAgentEntry) {
170+
options.userAgentEntry = deprecatedClientId;
171+
}
172+
}
173+
163174
this.authProvider = this.createAuthProvider(options, authProvider);
164175

165176
this.connectionProvider = this.createConnectionProvider(options);

lib/utils/buildUserAgentString.ts

+14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@ function getOperatingSystemVersion(): string {
1111
return `${os.type()} ${os.release()}`;
1212
}
1313

14+
function redactInternalToken(userAgentEntry: string): string {
15+
const internalTokenPrefixes = ['dkea', 'dskea', 'dapi', 'dsapi', 'dose'];
16+
for (const prefix of internalTokenPrefixes) {
17+
if (userAgentEntry.startsWith(prefix)) {
18+
return '<REDACTED>';
19+
}
20+
}
21+
return userAgentEntry;
22+
}
23+
1424
export default function buildUserAgentString(userAgentEntry?: string): string {
25+
if (userAgentEntry) {
26+
userAgentEntry = redactInternalToken(userAgentEntry);
27+
}
28+
1529
const extra = [userAgentEntry, getNodeVersion(), getOperatingSystemVersion()].filter(Boolean);
1630
return `${productName}/${packageVersion} (${extra.join('; ')})`;
1731
}

tests/unit/DBSQLClient.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ describe('DBSQLClient.connect', () => {
7979

8080
expect(thriftConnectionStub.on.called).to.be.true;
8181
});
82+
83+
it('should log a warning when deprecated clientId is passed', async () => {
84+
const client = new DBSQLClient();
85+
const logSpy = sinon.spy((client as any).logger, 'log');
86+
87+
const optionsWithDeprecated = {
88+
...connectOptions,
89+
clientId: 'clientId',
90+
};
91+
92+
await client.connect(optionsWithDeprecated as any);
93+
94+
const warningRegex = /Warning: The "clientId" option is deprecated\. Please use "userAgentEntry" instead\./;
95+
const callFound = logSpy.getCalls().some((call) => warningRegex.test(call.args[1]));
96+
97+
expect(callFound).to.be.true;
98+
99+
logSpy.restore();
100+
});
82101
});
83102

84103
describe('DBSQLClient.openSession', () => {

tests/unit/utils/utils.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ describe('buildUserAgentString', () => {
5656
const ua = buildUserAgentString();
5757
checkUserAgentString(ua);
5858
});
59+
60+
it('should redact internal token in userAgentEntry', () => {
61+
const userAgentEntry = 'dkea-internal-token';
62+
const userAgentString = buildUserAgentString(userAgentEntry);
63+
expect(userAgentString).to.include('<REDACTED>');
64+
});
5965
});
6066

6167
describe('formatProgress', () => {

0 commit comments

Comments
 (0)