Skip to content

Commit

Permalink
Stop dealing with IP if we have tested it (#2186)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonYeMSFT authored Sep 22, 2023
1 parent df0f620 commit f914b81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/postgres/postgresConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

export const invalidCredentialsErrorType: string = '28P01';
export const firewallNotConfiguredErrorType: string = '28000';
export const timeoutErrorType = 'ETIMEDOUT';
18 changes: 15 additions & 3 deletions src/postgres/tree/ClientConfigFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { callWithTelemetryAndErrorHandling } from "@microsoft/vscode-azext-utils";
import { callWithTelemetryAndErrorHandling, parseError } from "@microsoft/vscode-azext-utils";
import { ClientConfig } from "pg";
import { getAzureAdUserSession, getTokenFunction } from "../../azureAccountUtils";
import { localize } from "../../utils/localize";
import { PostgresClientConfigType, getClientConfigs, testClientConfig } from "../getClientConfig";
import { invalidCredentialsErrorType } from "../postgresConstants";
import { firewallNotConfiguredErrorType, invalidCredentialsErrorType, timeoutErrorType } from "../postgresConstants";
import { PostgresServerTreeItem } from "./PostgresServerTreeItem";

export const postgresResourceType = "https://ossrdbms-aad.database.windows.net/";
Expand Down Expand Up @@ -62,7 +62,19 @@ export class PostgresClientConfigFactory {
clientConfig
};
} catch (error) {
// If the client config failed during test, skip and try the next available one.
const parsedError = parseError(error);
if (parsedError.errorType === invalidCredentialsErrorType) {
// If the client config failed with invalid credential error, skip and try the next available one.
} else if (parsedError.errorType === firewallNotConfiguredErrorType || parsedError.errorType === timeoutErrorType) {
// The time out error are common when the firewall rules doesn't grant access from the current IP address.
// If the client is blocked by the firewall, let the user go to Azure Portal to grant access.
throw {
message: localize("mustConfigureFirewall", 'Must configure firewall from Azure Portal to grant access.'),
code: firewallNotConfiguredErrorType
};
} else {
throw error;
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/postgres/tree/PostgresDatabaseTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AzExtParentTreeItem, AzExtTreeItem, createContextValue, GenericTreeItem
import { ThemeIcon } from 'vscode';
import { ext } from '../../extensionVariables';
import { localize } from '../../utils/localize';
import { invalidCredentialsErrorType } from '../postgresConstants';
import { firewallNotConfiguredErrorType, invalidCredentialsErrorType } from '../postgresConstants';
import { runPostgresQuery, wrapArgInQuotes } from '../runPostgresQuery';
import { PostgresClientConfigFactory } from './ClientConfigFactory';
import { PostgresFunctionsTreeItem } from './PostgresFunctionsTreeItem';
Expand Down Expand Up @@ -90,6 +90,9 @@ export class PostgresDatabaseTreeItem extends AzExtParentTreeItem {
});
credentialsTreeItem.commandArgs = [this.parent];
return [credentialsTreeItem];
} else if (this.parent.azureName && parsedError.errorType === firewallNotConfiguredErrorType) {
void context.ui.showWarningMessage(localize('couldNotConnect', 'Could not connect to "{0}": {1}', this.parent.label, parsedError.message), { stepName: 'loadPostgresDatabases' });
return [];
} else {
throw error;
}
Expand Down

0 comments on commit f914b81

Please sign in to comment.