1
1
import * as path from "path" ;
2
2
import * as fs from "fs" ;
3
3
import * as tar from "tar" ;
4
+ import axios , { AxiosRequestConfig } from "axios" ;
5
+ import { Agent } from "https" ;
6
+
4
7
const AdmZip = require ( "adm-zip" ) ;
5
8
import { ExtensionContext , OutputChannel } from "vscode" ;
6
9
10
+ const defaultRequestConfig = {
11
+ headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } ,
12
+ timeout : 30_000 ,
13
+ } satisfies AxiosRequestConfig ;
14
+
15
+ /**
16
+ * Get the version of GGShield
17
+ * @param context The extension context
18
+ * @returns The version of GGShield
19
+ */
20
+ export function getGGShieldVersion ( context : ExtensionContext ) : string {
21
+ return fs
22
+ . readFileSync ( path . join ( context . extensionPath , "ggshield_version" ) , "utf8" )
23
+ . trim ( ) ;
24
+ }
25
+
7
26
/**
8
27
* Get the absolute path to GGShield binary. If it doesn't exist, it will be installed.
9
28
* @param platform The platform of the user
@@ -12,31 +31,30 @@ import { ExtensionContext, OutputChannel } from "vscode";
12
31
* @param outputChannel The output channel to use
13
32
* @returns The absolute path to the GGShield binary
14
33
*/
15
- export function getGGShield (
34
+ export async function getGGShield (
16
35
platform : NodeJS . Platform ,
17
36
arch : string ,
18
37
context : ExtensionContext ,
19
- outputChannel : OutputChannel
20
- ) : string {
21
- const version = fs
22
- . readFileSync ( path . join ( context . extensionPath , "ggshield_version" ) , "utf8" )
23
- . trim ( ) ;
38
+ outputChannel : OutputChannel ,
39
+ allowSelfSigned : boolean ,
40
+ ) : Promise < string > {
41
+ const version = getGGShieldVersion ( context ) ;
24
42
console . log ( `Latest GGShield version: ${ version } ` ) ;
25
43
const ggshieldFolder : string = path . join (
26
44
context . extensionPath ,
27
- "ggshield-internal"
45
+ "ggshield-internal" ,
28
46
) ;
29
47
const ggshieldBinaryPath : string = computeGGShieldPath (
30
48
platform ,
31
49
arch ,
32
50
ggshieldFolder ,
33
- version
51
+ version ,
34
52
) ;
35
53
36
54
// if exists, return the path
37
55
if ( fs . existsSync ( ggshieldBinaryPath ) ) {
38
56
outputChannel . appendLine (
39
- `Using GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.`
57
+ `Using GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.` ,
40
58
) ;
41
59
console . log ( `GGShield already exists at ${ ggshieldBinaryPath } ` ) ;
42
60
return ggshieldBinaryPath ;
@@ -47,28 +65,20 @@ export function getGGShield(
47
65
}
48
66
fs . mkdirSync ( ggshieldFolder ) ;
49
67
// install GGShield
50
- installGGShield ( platform , arch , ggshieldFolder , version ) ;
68
+ await installGGShield (
69
+ platform ,
70
+ arch ,
71
+ ggshieldFolder ,
72
+ version ,
73
+ allowSelfSigned ,
74
+ ) ;
51
75
outputChannel . appendLine (
52
- `Updated to GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.`
76
+ `Updated to GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.` ,
53
77
) ;
54
78
console . log ( `GGShield binary installed at ${ ggshieldBinaryPath } ` ) ;
55
79
return ggshieldBinaryPath ;
56
80
}
57
81
58
- /**
59
- * Get the latest version of GGShield
60
- * @returns The latest version of GGShield
61
- */
62
- export function getGGShieldLatestVersion ( ) : string {
63
- const response = require ( "sync-request" ) (
64
- "GET" ,
65
- "https://api.github.com/repos/GitGuardian/ggshield/releases/latest" ,
66
- { headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } }
67
- ) ;
68
- const data = JSON . parse ( response . getBody ( "utf8" ) ) ;
69
- return data . tag_name ?. replace ( / ^ v / , "" ) ;
70
- }
71
-
72
82
/**
73
83
* Compute the folder name of the GGShield binary
74
84
* @param platform The platform of the user
@@ -79,7 +89,7 @@ export function getGGShieldLatestVersion(): string {
79
89
export function computeGGShieldFolderName (
80
90
platform : NodeJS . Platform ,
81
91
arch : string ,
82
- version : string
92
+ version : string ,
83
93
) : string {
84
94
let archString : string = "" ;
85
95
let platformString : string = "" ;
@@ -119,12 +129,13 @@ export function computeGGShieldFolderName(
119
129
* @param ggshieldFolder The folder of the GGShield binary
120
130
* @param version The version of GGShield
121
131
*/
122
- export function installGGShield (
132
+ export async function installGGShield (
123
133
platform : NodeJS . Platform ,
124
134
arch : string ,
125
135
ggshieldFolder : string ,
126
- version : string
127
- ) : void {
136
+ version : string ,
137
+ allowSelfSigned : boolean ,
138
+ ) : Promise < void > {
128
139
let extension : string = "" ;
129
140
switch ( platform ) {
130
141
case "win32" :
@@ -141,10 +152,15 @@ export function installGGShield(
141
152
const fileName : string = `${ computeGGShieldFolderName (
142
153
platform ,
143
154
arch ,
144
- version
155
+ version ,
145
156
) } .${ extension } `;
146
157
const downloadUrl : string = `https://github.com/GitGuardian/ggshield/releases/download/v${ version } /${ fileName } ` ;
147
- downloadGGShieldFromGitHub ( fileName , downloadUrl , ggshieldFolder ) ;
158
+ await downloadGGShieldFromGitHub (
159
+ fileName ,
160
+ downloadUrl ,
161
+ ggshieldFolder ,
162
+ allowSelfSigned ,
163
+ ) ;
148
164
extractGGShieldBinary ( path . join ( ggshieldFolder , fileName ) , ggshieldFolder ) ;
149
165
}
150
166
@@ -155,7 +171,7 @@ export function installGGShield(
155
171
*/
156
172
export function extractGGShieldBinary (
157
173
filePath : string ,
158
- ggshieldFolder : string
174
+ ggshieldFolder : string ,
159
175
) : void {
160
176
if ( filePath . endsWith ( ".tar.gz" ) ) {
161
177
tar . x ( {
@@ -177,18 +193,29 @@ export function extractGGShieldBinary(
177
193
* @param downloadUrl The URL of the GGShield binary
178
194
* @param ggshieldFolder The folder of the GGShield binary
179
195
*/
180
- function downloadGGShieldFromGitHub (
196
+ async function downloadGGShieldFromGitHub (
181
197
fileName : string ,
182
198
downloadUrl : string ,
183
- ggshieldFolder : string
184
- ) : void {
199
+ ggshieldFolder : string ,
200
+ allowSelfSigned : boolean ,
201
+ ) : Promise < void > {
185
202
console . log ( `Downloading GGShield from ${ downloadUrl } ` ) ;
186
- const response = require ( "sync-request" ) ( "GET" , downloadUrl , {
187
- headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } ,
203
+
204
+ const instance = allowSelfSigned
205
+ ? new Agent ( {
206
+ rejectUnauthorized : false ,
207
+ } )
208
+ : undefined ;
209
+
210
+ const { data } = await axios . get ( downloadUrl , {
211
+ ...defaultRequestConfig ,
212
+ responseType : "arraybuffer" ,
213
+ httpsAgent : instance ,
188
214
} ) ;
189
- fs . writeFileSync ( path . join ( ggshieldFolder , fileName ) , response . getBody ( ) ) ;
215
+
216
+ fs . writeFileSync ( path . join ( ggshieldFolder , fileName ) , data ) ;
190
217
console . log (
191
- `GGShield archive downloaded to ${ path . join ( ggshieldFolder , fileName ) } `
218
+ `GGShield archive downloaded to ${ path . join ( ggshieldFolder , fileName ) } ` ,
192
219
) ;
193
220
}
194
221
@@ -203,7 +230,7 @@ export function computeGGShieldPath(
203
230
platform : NodeJS . Platform ,
204
231
arch : string ,
205
232
ggshieldFolder : string ,
206
- version : string
233
+ version : string ,
207
234
) : string {
208
235
console . log ( `Platform: ${ platform } ; Arch: ${ arch } ` ) ;
209
236
let executable : string = "" ;
@@ -222,6 +249,6 @@ export function computeGGShieldPath(
222
249
return path . join (
223
250
ggshieldFolder ,
224
251
computeGGShieldFolderName ( platform , arch , version ) ,
225
- executable
252
+ executable ,
226
253
) ;
227
254
}
0 commit comments