Skip to content

Commit 9593a32

Browse files
authored
VSCODE-117: Fix file pathing for windows connect (#110)
1 parent 73d22ee commit 9593a32

File tree

3 files changed

+6
-151
lines changed

3 files changed

+6
-151
lines changed

src/test/suite/views/webviewController.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ suite('Connect Form View Test Suite', () => {
361361
html: '',
362362
postMessage: (message: any): void => {
363363
assert(message.action === 'file_action');
364-
assert(message.files[0] === './somefilepath/test.text');
364+
assert(message.files[0] === 'somefilepath/test.text');
365365

366366
testConnectionController.disconnect();
367367
done();
@@ -382,7 +382,7 @@ suite('Connect Form View Test Suite', () => {
382382

383383
const fakeVSCodeOpenDialog = sinon.fake.resolves([
384384
{
385-
path: './somefilepath/test.text'
385+
path: '/somefilepath/test.text'
386386
}
387387
]);
388388

@@ -451,7 +451,9 @@ suite('Connect Form View Test Suite', () => {
451451

452452
setTimeout(() => {
453453
assert(fakeVSCodeExecuteCommand.called);
454-
assert(fakeVSCodeExecuteCommand.firstCall.args[0] === 'mdb.connectWithURI');
454+
assert(
455+
fakeVSCodeExecuteCommand.firstCall.args[0] === 'mdb.connectWithURI'
456+
);
455457

456458
done();
457459
}, 50);

src/views/webview-app/connection-model/connection-model.ts

Lines changed: 0 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { format as toURL } from 'url';
2-
3-
import AUTHENICATION_TO_AUTH_MECHANISM from './constants/auth-strategy-to-auth-mechanism';
41
import AUTH_STRATEGIES from './constants/auth-strategies';
52
import READ_PREFERENCES from './constants/read-preferences';
63
import SSL_METHODS from './constants/ssl-methods';
@@ -9,8 +6,6 @@ import SSH_TUNNEL_TYPES from './constants/ssh-tunnel-types';
96
// Defaults.
107
const AUTH_STRATEGY_DEFAULT = AUTH_STRATEGIES.NONE;
118
const READ_PREFERENCE_DEFAULT = READ_PREFERENCES.PRIMARY;
12-
const MONGODB_DATABASE_NAME_DEFAULT = 'admin';
13-
const KERBEROS_SERVICE_NAME_DEFAULT = 'mongodb';
149
const SSL_DEFAULT = SSL_METHODS.NONE;
1510
const SSH_TUNNEL_DEFAULT = SSH_TUNNEL_TYPES.NONE;
1611

@@ -91,148 +86,6 @@ class ConnectionModel {
9186
sshTunnelPassphrase?: string;
9287
}
9388

94-
const getDriverAuthMechanism = (
95-
connectionModel: ConnectionModel
96-
): string | undefined => {
97-
return AUTHENICATION_TO_AUTH_MECHANISM[connectionModel.authStrategy];
98-
};
99-
100-
// eslint-disable-next-line complexity
101-
const getBaseUrlFromConnectionModel = (
102-
connectionModel: ConnectionModel
103-
): string => {
104-
const req: any = {
105-
protocol: 'mongodb',
106-
port: null,
107-
slashes: true,
108-
pathname: '/',
109-
query: {}
110-
};
111-
112-
// In the `mongodb+srv` protocol the comma separated list of host names is
113-
// replaced with a single hostname.
114-
// The format is: `mongodb+srv://{hostname}.{domainname}/{options}`
115-
if (connectionModel.isSrvRecord) {
116-
req.protocol = 'mongodb+srv';
117-
req.hostname = connectionModel.hostname;
118-
} else if (connectionModel.hosts.length === 1) {
119-
// Driver adds sharding info to the original hostname.
120-
// And returnes a list of all coresponding hosts.
121-
// If driver returns a list of hosts which size is equal one,
122-
// we can use hostname attribute that stores unmodified value.
123-
req.hostname = connectionModel.hostname;
124-
req.port = connectionModel.port;
125-
} else {
126-
req.host = connectionModel.hosts
127-
.map((item) => `${item.host}:${item.port}`)
128-
.join(',');
129-
}
130-
131-
if (connectionModel.ns) {
132-
req.pathname = `/${connectionModel.ns}`;
133-
}
134-
135-
// Encode auth for url format
136-
if (connectionModel.authStrategy === AUTH_STRATEGIES.MONGODB) {
137-
req.auth = 'AUTH_TOKEN';
138-
req.query.authSource =
139-
connectionModel.mongodbDatabaseName || MONGODB_DATABASE_NAME_DEFAULT;
140-
} else if (connectionModel.authStrategy === 'SCRAM-SHA-256') {
141-
req.auth = 'AUTH_TOKEN';
142-
req.query.authSource =
143-
connectionModel.mongodbDatabaseName || MONGODB_DATABASE_NAME_DEFAULT;
144-
req.query.authMechanism = getDriverAuthMechanism(connectionModel);
145-
} else if (connectionModel.authStrategy === AUTH_STRATEGIES.KERBEROS) {
146-
req.auth = 'AUTH_TOKEN';
147-
req.query.gssapiServiceName =
148-
connectionModel.kerberosServiceName || KERBEROS_SERVICE_NAME_DEFAULT;
149-
req.query.authMechanism = getDriverAuthMechanism(connectionModel);
150-
} else if (connectionModel.authStrategy === AUTH_STRATEGIES.X509) {
151-
req.auth = 'AUTH_TOKEN';
152-
req.query.authMechanism = getDriverAuthMechanism(connectionModel);
153-
} else if (connectionModel.authStrategy === AUTH_STRATEGIES.LDAP) {
154-
req.auth = 'AUTH_TOKEN';
155-
req.query.authMechanism = getDriverAuthMechanism(connectionModel);
156-
}
157-
158-
if (req.query.readPreference !== undefined) {
159-
req.query.readPreference = connectionModel.readPreference;
160-
}
161-
if (req.query.replicaSet !== undefined) {
162-
req.query.replicaSet = connectionModel.replicaSet;
163-
}
164-
165-
if (connectionModel.sslMethod === SSL_METHODS.NONE) {
166-
req.query.ssl = 'false';
167-
} else {
168-
req.query.ssl = 'true';
169-
}
170-
171-
const reqClone = {
172-
...req
173-
};
174-
175-
return toURL(reqClone);
176-
};
177-
178-
// eslint-disable-next-line complexity
179-
export const getDriverUrlFromConnectionModel = (
180-
connectionModel: ConnectionModel
181-
): string => {
182-
let username = '';
183-
let password = '';
184-
let authField = '';
185-
let result = getBaseUrlFromConnectionModel(connectionModel);
186-
187-
// Post url.format() workaround for
188-
// https://github.com/nodejs/node/issues/1802
189-
if (
190-
connectionModel.authStrategy === 'MONGODB' ||
191-
connectionModel.authStrategy === 'SCRAM-SHA-256'
192-
) {
193-
username = encodeURIComponent(connectionModel.mongodbUsername || '');
194-
password = encodeURIComponent(connectionModel.mongodbPassword || '');
195-
authField = `${username}:${password}`;
196-
} else if (connectionModel.authStrategy === 'LDAP') {
197-
username = encodeURIComponent(connectionModel.ldapUsername || '');
198-
password = encodeURIComponent(connectionModel.ldapPassword || '');
199-
authField = `${username}:${password}`;
200-
} else if (connectionModel.authStrategy === 'X509') {
201-
username = encodeURIComponent(connectionModel.x509Username || '');
202-
authField = username;
203-
} else if (
204-
connectionModel.authStrategy === 'KERBEROS' &&
205-
connectionModel.kerberosPassword
206-
) {
207-
username = encodeURIComponent(connectionModel.kerberosPrincipal || '');
208-
password = encodeURIComponent(connectionModel.kerberosPassword);
209-
authField = `${username}:${password}`;
210-
} else if (connectionModel.authStrategy === 'KERBEROS') {
211-
username = encodeURIComponent(connectionModel.kerberosPrincipal || '');
212-
authField = `${username}:`;
213-
}
214-
215-
// The auth component comes straight after `the mongodb://`
216-
// so a single string replace should always work.
217-
result = result.replace('AUTH_TOKEN', authField);
218-
219-
if (
220-
connectionModel.authStrategy === AUTH_STRATEGIES.KERBEROS ||
221-
connectionModel.authStrategy === AUTH_STRATEGIES.LDAP
222-
) {
223-
result = `${result}&authSource=$external`;
224-
}
225-
226-
if (
227-
connectionModel.authStrategy === AUTH_STRATEGIES.KERBEROS &&
228-
connectionModel.kerberosCanonicalizeHostname
229-
) {
230-
result = `${result}&authMechanismProperties=CANONICALIZE_HOST_NAME:true`;
231-
}
232-
233-
return result;
234-
};
235-
23689
/**
23790
* Enforce constraints for SSL.
23891
* @param {Object} attrs - Incoming attributes.

src/views/webviewController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export default class WebviewController {
100100
action: message.action,
101101
files:
102102
files && files.length > 0
103-
? files.map((file) => file.path)
103+
? files.map((file) => file.path.substr(1))
104104
: undefined
105105
});
106106
});

0 commit comments

Comments
 (0)