Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 74949ca

Browse files
committed
feat: add allowUnsafeSSL for self-signed certificates
1 parent 332e73c commit 74949ca

7 files changed

Lines changed: 47 additions & 8 deletions

File tree

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
"default": false,
6969
"scope": "resource"
7070
},
71+
"github.allowUnsafeSSL": {
72+
"type": "boolean",
73+
"description": "Allow SSL connnection with unauthorized self-signed certificates. Defaults to false",
74+
"default": false,
75+
"scope": "resource"
76+
},
7177
"github.statusbar.command": {
7278
"type": [
7379
"string",
@@ -233,6 +239,7 @@
233239
"common-tags": "1.7.0",
234240
"conventional-changelog-lint-config-angular": "1.0.0",
235241
"execa": "^0.9.0",
242+
"https": "1.0.0",
236243
"isomorphic-fetch": "2.2.1",
237244
"lru-cache": "^4.1.1",
238245
"pretend": "^1.2.1",

src/configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface Configuration {
88
upstream?: string;
99
customPullRequestDescription: 'off' | 'singleLine' | 'gitEditor';
1010
autoPublish?: boolean;
11+
allowUnsafeSSL?: boolean;
1112
statusBarCommand: string | null;
1213
statusbar: {
1314
refresh: number;

src/provider/client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22
import { Git } from '../git';
3+
import { getConfiguration } from '../helper';
34
import { Tokens } from '../workflow-manager';
45
import { Repository } from './repository';
56
import { User } from './user';
@@ -13,14 +14,16 @@ export async function createClient(git: Git, tokens: Tokens, uri: vscode.Uri,
1314
const protocol = gitProtocol.startsWith('http') ? gitProtocol : 'https:';
1415
const hostname = await git.getGitHostname(uri);
1516
const tokenInfo = tokens[hostname];
17+
const allowUnsafeSSL = !!getConfiguration('github', uri).allowUnsafeSSL;
1618
if (!tokenInfo) {
1719
throw new Error(`No token found for host ${hostname}`);
1820
}
1921
switch (tokenInfo.provider) {
2022
case 'github':
21-
return new GithubClient(protocol, hostname, tokens[hostname].token, logger);
23+
return new GithubClient(
24+
protocol, hostname, tokens[hostname].token, logger, allowUnsafeSSL);
2225
case 'gitlab':
23-
return new GitLabClient(protocol, hostname, tokens[hostname].token, logger);
26+
return new GitLabClient(protocol, hostname, tokens[hostname].token, logger, allowUnsafeSSL);
2427
default:
2528
throw new Error(`Unknown git provider '${tokenInfo.provider}'`);
2629
}

src/provider/github/api.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as https from 'https';
12
import * as LRUCache from 'lru-cache';
23
import {Pretend, Get, Post, Put, Patch, Delete, Headers as Header, Interceptor, IPretendRequestInterceptor,
34
IPretendDecoder} from 'pretend';
@@ -180,11 +181,13 @@ export interface PullRequestStruct {
180181
mergeable?: boolean|null;
181182
}
182183

183-
export function getClient(endpoint: string, token: string, logger: (message: string) => void): GitHub {
184+
export function getClient(endpoint: string, token: string, logger: (message: string) => void,
185+
allowUnsafeSSL = false): GitHub {
184186
return Pretend
185187
.builder()
186188
.interceptor(impl.githubCache())
187189
.requestInterceptor(impl.githubTokenAuthenticator(token))
190+
.requestInterceptor(impl.githubHttpsAgent(!allowUnsafeSSL))
188191
.interceptor(impl.logger(logger))
189192
.decode(impl.githubDecoder())
190193
.target(impl.GitHubBlueprint, endpoint);
@@ -251,6 +254,16 @@ namespace impl {
251254
};
252255
}
253256

257+
export function githubHttpsAgent(rejectUnauthorized: boolean): IPretendRequestInterceptor {
258+
return request => {
259+
if (!request.url.startsWith('https://')) {
260+
return request;
261+
}
262+
request.options.agent = new https.Agent({ rejectUnauthorized });
263+
return request;
264+
};
265+
}
266+
254267
export function githubDecoder(): IPretendDecoder {
255268
return async response => {
256269
if (response.status >= 400) {

src/provider/github/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ export class GithubClient implements Client {
1414

1515
public name = 'GitHub Client';
1616

17-
constructor(protocol: string, hostname: string, token: string, logger: (message: string) => void) {
18-
this.client = getClient(this.getApiEndpoint(protocol, hostname), token, logger);
17+
constructor(protocol: string, hostname: string, token: string, logger: (message: string) => void,
18+
allowUnsafeSSL = false) {
19+
this.client = getClient(this.getApiEndpoint(protocol, hostname), token, logger, allowUnsafeSSL);
1920
}
2021

2122
private getApiEndpoint(protocol: string, hostname: string): string {

src/provider/gitlab/api.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as https from 'https';
12
import {
23
Pretend,
34
Interceptor,
@@ -121,10 +122,12 @@ export interface Project {
121122
merge_requests_enabled: boolean;
122123
}
123124

124-
export function getClient(endpoint: string, token: string, logger: (message: string) => void): GitLab {
125+
export function getClient(endpoint: string, token: string, logger: (message: string) => void,
126+
allowUnsafeSSL = false): GitLab {
125127
return Pretend
126128
.builder()
127129
.requestInterceptor(impl.gitlabTokenAuthenticator(token))
130+
.requestInterceptor(impl.gitlabHttpsAgent(!allowUnsafeSSL))
128131
.requestInterceptor(impl.formEncoding())
129132
.interceptor(impl.logger(logger))
130133
.decode(impl.gitlabDecoder())
@@ -166,6 +169,16 @@ namespace impl {
166169
};
167170
}
168171

172+
export function gitlabHttpsAgent(rejectUnauthorized: boolean): IPretendRequestInterceptor {
173+
return request => {
174+
if (!request.url.startsWith('https://')) {
175+
return request;
176+
}
177+
request.options.agent = new https.Agent({ rejectUnauthorized });
178+
return request;
179+
};
180+
}
181+
169182
export function formEncoding(): IPretendRequestInterceptor {
170183
return request => {
171184
if (request.options.method !== 'GET') {

src/provider/gitlab/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ export class GitLabClient implements Client {
1010

1111
public name = 'GitLab Client';
1212

13-
constructor(protocol: string, hostname: string, token: string, logger: (message: string) => void) {
14-
this.client = getClient(this.getApiEndpoint(protocol, hostname), token, logger);
13+
constructor(protocol: string, hostname: string, token: string, logger: (message: string) => void,
14+
allowUnsafeSSL: boolean) {
15+
this.client = getClient(this.getApiEndpoint(protocol, hostname), token, logger, allowUnsafeSSL);
1516
}
1617

1718
private getApiEndpoint(protocol: string, hostname: string): string {

0 commit comments

Comments
 (0)