forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
all for both self certs and password at the same time #6274
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
Merged
IanMatthewHuff
merged 4 commits into
microsoft:master
from
IanMatthewHuff:dev/ianhu/certsAndPassword
Jun 21, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow for both password and self cert server to work together |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
'use strict'; | ||
import { Agent as HttpsAgent } from 'https'; | ||
import { inject, injectable } from 'inversify'; | ||
import * as nodeFetch from 'node-fetch'; | ||
import { URLSearchParams } from 'url'; | ||
|
@@ -19,7 +20,7 @@ export class JupyterPasswordConnect implements IJupyterPasswordConnect { | |
} | ||
|
||
@captureTelemetry(Telemetry.GetPasswordAttempt) | ||
public async getPasswordConnectionInfo(url: string, fetchFunction?: (url: nodeFetch.RequestInfo, init?: nodeFetch.RequestInit) => Promise<nodeFetch.Response>): Promise<IJupyterPasswordConnectInfo | undefined> { | ||
public async getPasswordConnectionInfo(url: string, allowUnauthorized: boolean, fetchFunction?: (url: nodeFetch.RequestInfo, init?: nodeFetch.RequestInit) => Promise<nodeFetch.Response>): Promise<IJupyterPasswordConnectInfo | undefined> { | ||
// For testing allow for our fetch function to be overridden | ||
if (!fetchFunction) { | ||
fetchFunction = nodeFetch.default; | ||
|
@@ -44,11 +45,11 @@ export class JupyterPasswordConnect implements IJupyterPasswordConnect { | |
|
||
if (userPassword) { | ||
// First get the xsrf cookie by hitting the initial login page | ||
xsrfCookie = await this.getXSRFToken(url, fetchFunction); | ||
xsrfCookie = await this.getXSRFToken(url, allowUnauthorized, fetchFunction); | ||
|
||
// Then get the session cookie by hitting that same page with the xsrftoken and the password | ||
if (xsrfCookie) { | ||
const sessionResult = await this.getSessionCookie(url, xsrfCookie, userPassword, fetchFunction); | ||
const sessionResult = await this.getSessionCookie(url, allowUnauthorized, xsrfCookie, userPassword, fetchFunction); | ||
sessionCookieName = sessionResult.sessionCookieName; | ||
sessionCookieValue = sessionResult.sessionCookieValue; | ||
} | ||
|
@@ -65,6 +66,16 @@ export class JupyterPasswordConnect implements IJupyterPasswordConnect { | |
} | ||
} | ||
|
||
// For HTTPS connections respect our allowUnauthorized setting by adding in an agent to enable that on the request | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our services connection stuff was using the allowUnauthorized flag, but the manual password requests were not. So you couldn't connect to a self cert password machine. |
||
private addAllowUnauthorized(url: string, allowUnauthorized: boolean, options: nodeFetch.RequestInit): nodeFetch.RequestInit { | ||
if (url.startsWith('https') && allowUnauthorized) { | ||
const requestAgent = new HttpsAgent({rejectUnauthorized: false}); | ||
return {...options, agent: requestAgent}; | ||
} | ||
|
||
return options; | ||
} | ||
|
||
private async getUserPassword() : Promise<string | undefined> { | ||
// First get the proposed URI from the user | ||
return this.appShell.showInputBox({ | ||
|
@@ -74,14 +85,14 @@ export class JupyterPasswordConnect implements IJupyterPasswordConnect { | |
}); | ||
} | ||
|
||
private async getXSRFToken(url: string, fetchFunction: (url: nodeFetch.RequestInfo, init?: nodeFetch.RequestInit) => Promise<nodeFetch.Response>): Promise<string | undefined> { | ||
private async getXSRFToken(url: string, allowUnauthorized: boolean, fetchFunction: (url: nodeFetch.RequestInfo, init?: nodeFetch.RequestInit) => Promise<nodeFetch.Response>): Promise<string | undefined> { | ||
let xsrfCookie: string | undefined; | ||
|
||
const response = await fetchFunction(`${url}login?`, { | ||
const response = await fetchFunction(`${url}login?`, this.addAllowUnauthorized(url, allowUnauthorized, { | ||
method: 'get', | ||
redirect: 'manual', | ||
headers: { Connection: 'keep-alive' } | ||
}); | ||
})); | ||
|
||
if (response.ok) { | ||
const cookies = this.getCookies(response); | ||
|
@@ -98,6 +109,7 @@ export class JupyterPasswordConnect implements IJupyterPasswordConnect { | |
// First you need a get at the login page to get the xsrf token, then you send back that token along with the password in a post | ||
// That will return back the session cookie. This session cookie then needs to be added to our requests and websockets for @jupyterlab/services | ||
private async getSessionCookie(url: string, | ||
allowUnauthorized: boolean, | ||
xsrfCookie: string, | ||
password: string, | ||
fetchFunction: (url: nodeFetch.RequestInfo, init?: nodeFetch.RequestInit) => Promise<nodeFetch.Response>): Promise<{sessionCookieName: string | undefined; sessionCookieValue: string | undefined}> { | ||
|
@@ -108,12 +120,12 @@ export class JupyterPasswordConnect implements IJupyterPasswordConnect { | |
postParams.append('_xsrf', xsrfCookie); | ||
postParams.append('password', password); | ||
|
||
const response = await fetchFunction(`${url}login?`, { | ||
const response = await fetchFunction(`${url}login?`, this.addAllowUnauthorized(url, allowUnauthorized, { | ||
method: 'post', | ||
headers: { Cookie: `_xsrf=${xsrfCookie}`, Connection: 'keep-alive', 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8' }, | ||
body: postParams.toString(), | ||
redirect: 'manual' | ||
}); | ||
})); | ||
|
||
// Now from this result we need to extract the session cookie | ||
if (response.status === 302) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can remove this if we don't like it. I still have the bug for doing a reconnect that I'll try to fit into this release. But I felt this one liner made the scenario better than it currently is. So I stuck it in with this fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No sounds good to me. Better than what we have now.
In reply to: 296046769 [](ancestors = 296046769)