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

refactor: move the loginError property from Connection into Login7Handler #1662

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 13 additions & 19 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,10 +937,6 @@
* @private
*/
declare closed: boolean;
/**
* @private
*/
declare loginError: undefined | AggregateError | ConnectionError;
/**
* @private
*/
Expand Down Expand Up @@ -2002,7 +1998,6 @@
}

this.closed = true;
this.loginError = undefined;
}
}

Expand Down Expand Up @@ -3305,7 +3300,6 @@
this.curTransientRetryCount++;

this.clearConnectTimer();
this.loginError = undefined;

this.socket!.removeListener('error', this._onSocketError);
this.socket!.removeListener('close', this._onSocketClose);
Expand Down Expand Up @@ -3346,12 +3340,12 @@
} else {
this.transitionTo(this.STATE.LOGGED_IN_SENDING_INITIAL_SQL);
}
} else if (this.loginError) {
if (isTransientError(this.loginError)) {
} else if (handler.loginError) {
if (isTransientError(handler.loginError)) {
this.debug.log('Initiating retry on transient error');
this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY);
} else {
this.emit('connect', this.loginError);
this.emit('connect', handler.loginError);
this.transitionTo(this.STATE.FINAL);
}
} else {
Expand Down Expand Up @@ -3399,12 +3393,12 @@
});

this.ntlmpacket = undefined;
} else if (this.loginError) {
if (isTransientError(this.loginError)) {
} else if (handler.loginError) {
if (isTransientError(handler.loginError)) {
this.debug.log('Initiating retry on transient error');
return this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY);
} else {
this.emit('connect', this.loginError);
this.emit('connect', handler.loginError);

Check warning on line 3401 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3401

Added line #L3401 was not covered by tests
return this.transitionTo(this.STATE.FINAL);
}
} else {
Expand Down Expand Up @@ -3487,30 +3481,30 @@
try {
tokenResponse = await credentials.getToken(tokenScope);
} catch (err) {
this.loginError = new AggregateError(
const aggrErr = new AggregateError(

Check warning on line 3484 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3484

Added line #L3484 was not covered by tests
[new ConnectionError('Security token could not be authenticated or authorized.', 'EFEDAUTH'), err]);
this.emit('connect', this.loginError);
this.emit('connect', aggrErr);

Check warning on line 3486 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3486

Added line #L3486 was not covered by tests
this.transitionTo(this.STATE.FINAL);
return;
}

// Type guard the token value so that it is never null.
if (tokenResponse === null) {
this.loginError = new AggregateError(
const aggrErr = new AggregateError(

Check warning on line 3493 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3493

Added line #L3493 was not covered by tests
[new ConnectionError('Security token could not be authenticated or authorized.', 'EFEDAUTH')]);
this.emit('connect', this.loginError);
this.emit('connect', aggrErr);

Check warning on line 3495 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3495

Added line #L3495 was not covered by tests
this.transitionTo(this.STATE.FINAL);
return;
}

this.sendFedAuthTokenMessage(tokenResponse.token);

} else if (this.loginError) {
if (isTransientError(this.loginError)) {
} else if (handler.loginError) {
if (isTransientError(handler.loginError)) {
this.debug.log('Initiating retry on transient error');
this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY);
} else {
this.emit('connect', this.loginError);
this.emit('connect', handler.loginError);

Check warning on line 3507 in src/connection.ts

View check run for this annotation

Codecov / codecov/patch

src/connection.ts#L3507

Added line #L3507 was not covered by tests
this.transitionTo(this.STATE.FINAL);
}
} else {
Expand Down
17 changes: 10 additions & 7 deletions src/token/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,13 @@

declare loginAckReceived: boolean;

declare loginError: ConnectionError | undefined;

constructor(connection: Connection) {
super();
this.loginAckReceived = false;
this.connection = connection;
this.loginError = undefined;
}

onInfoMessage(token: InfoMessageToken) {
Expand All @@ -270,7 +273,7 @@
error.isTransient = true;
}

this.connection.loginError = error;
this.loginError = error;
}

onSSPI(token: SSPIToken) {
Expand Down Expand Up @@ -305,27 +308,27 @@

if (authentication.type === 'azure-active-directory-password' || authentication.type === 'azure-active-directory-access-token' || authentication.type === 'azure-active-directory-msi-vm' || authentication.type === 'azure-active-directory-msi-app-service' || authentication.type === 'azure-active-directory-service-principal-secret' || authentication.type === 'azure-active-directory-default') {
if (token.fedAuth === undefined) {
this.connection.loginError = new ConnectionError('Did not receive Active Directory authentication acknowledgement');
this.loginError = new ConnectionError('Did not receive Active Directory authentication acknowledgement');

Check warning on line 311 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L311

Added line #L311 was not covered by tests
} else if (token.fedAuth.length !== 0) {
this.connection.loginError = new ConnectionError(`Active Directory authentication acknowledgment for ${authentication.type} authentication method includes extra data`);
this.loginError = new ConnectionError(`Active Directory authentication acknowledgment for ${authentication.type} authentication method includes extra data`);

Check warning on line 313 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L313

Added line #L313 was not covered by tests
}
} else if (token.fedAuth === undefined && token.utf8Support === undefined) {
this.connection.loginError = new ConnectionError('Received acknowledgement for unknown feature');
this.loginError = new ConnectionError('Received acknowledgement for unknown feature');

Check warning on line 316 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L316

Added line #L316 was not covered by tests
} else if (token.fedAuth) {
this.connection.loginError = new ConnectionError('Did not request Active Directory authentication, but received the acknowledgment');
this.loginError = new ConnectionError('Did not request Active Directory authentication, but received the acknowledgment');
}
}

onLoginAck(token: LoginAckToken) {
if (!token.tdsVersion) {
// unsupported TDS version
this.connection.loginError = new ConnectionError('Server responded with unknown TDS version.', 'ETDS');
this.loginError = new ConnectionError('Server responded with unknown TDS version.', 'ETDS');

Check warning on line 325 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L325

Added line #L325 was not covered by tests
return;
}

if (!token.interface) {
// unsupported interface
this.connection.loginError = new ConnectionError('Server responded with unsupported interface.', 'EINTERFACENOTSUPP');
this.loginError = new ConnectionError('Server responded with unsupported interface.', 'EINTERFACENOTSUPP');

Check warning on line 331 in src/token/handler.ts

View check run for this annotation

Codecov / codecov/patch

src/token/handler.ts#L331

Added line #L331 was not covered by tests
return;
}

Expand Down
Loading