Skip to content

Commit 66fd946

Browse files
committed
Remove auth data from inspect and toJSON in connection class (#887)
* Remove auth data from inspect and toJSON in connection class * Updated test
1 parent 39bbd77 commit 66fd946

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

lib/Connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class Connection {
231231
// access them with `instance.agent` and `instance.ssl`.
232232
[inspect.custom] (depth, options) {
233233
return {
234-
url: this.url,
234+
url: stripAuth(this.url.toString()),
235235
id: this.id,
236236
headers: this.headers,
237237
deadCount: this.deadCount,
@@ -244,7 +244,7 @@ class Connection {
244244

245245
toJSON () {
246246
return {
247-
url: this.url,
247+
url: stripAuth(this.url.toString()),
248248
id: this.id,
249249
headers: this.headers,
250250
deadCount: this.deadCount,

test/unit/connection.test.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -723,11 +723,11 @@ test('setRole', t => {
723723
t.end()
724724
})
725725

726-
test('Util.inspect Connection class should hide agent and ssl', t => {
726+
test('Util.inspect Connection class should hide agent, ssl and auth', t => {
727727
t.plan(1)
728728

729729
const connection = new Connection({
730-
url: new URL('http://localhost:9200'),
730+
url: new URL('http://user:password@localhost:9200'),
731731
id: 'node-id',
732732
headers: { foo: 'bar' }
733733
})
@@ -741,20 +741,7 @@ test('Util.inspect Connection class should hide agent and ssl', t => {
741741
.replace(/(\r\n|\n|\r)/gm, '')
742742
}
743743

744-
t.strictEqual(cleanStr(inspect(connection)), cleanStr(`{ url:
745-
URL {
746-
href: 'http://localhost:9200/',
747-
origin: 'http://localhost:9200',
748-
protocol: 'http:',
749-
username: '',
750-
password: '',
751-
host: 'localhost:9200',
752-
hostname: 'localhost',
753-
port: '9200',
754-
pathname: '/',
755-
search: '',
756-
searchParams: URLSearchParams {},
757-
hash: '' },
744+
t.strictEqual(cleanStr(inspect(connection)), cleanStr(`{ url: 'http://localhost:9200/',
758745
id: 'node-id',
759746
headers: { foo: 'bar' },
760747
deadCount: 0,
@@ -765,6 +752,34 @@ test('Util.inspect Connection class should hide agent and ssl', t => {
765752
)
766753
})
767754

755+
test('connection.toJSON should hide agent, ssl and auth', t => {
756+
t.plan(1)
757+
758+
const connection = new Connection({
759+
url: new URL('http://user:password@localhost:9200'),
760+
id: 'node-id',
761+
headers: { foo: 'bar' }
762+
})
763+
764+
t.deepEqual(connection.toJSON(), {
765+
url: 'http://localhost:9200/',
766+
id: 'node-id',
767+
headers: {
768+
foo: 'bar'
769+
},
770+
deadCount: 0,
771+
resurrectTimeout: 0,
772+
_openRequests: 0,
773+
status: 'alive',
774+
roles: {
775+
master: true,
776+
data: true,
777+
ingest: true,
778+
ml: false
779+
}
780+
})
781+
})
782+
768783
// https://github.com/elastic/elasticsearch-js/issues/843
769784
test('Port handling', t => {
770785
t.test('http 80', t => {

0 commit comments

Comments
 (0)