1
1
import * as _ from 'lodash' ;
2
2
import * as Docker from 'dockerode' ;
3
3
import * as path from 'path' ;
4
- import * as tarFs from 'tar-fs' ;
5
4
import * as semver from 'semver' ;
6
5
7
6
import {
@@ -92,36 +91,6 @@ const envArrayToObject = (envArray: string[] | null | undefined) =>
92
91
const envObjectToArray = ( envObject : { [ key : string ] : string } ) : string [ ] =>
93
92
Object . keys ( envObject ) . map ( k => `${ k } =${ envObject [ k ] } ` ) ;
94
93
95
- function packInterceptionFiles ( certContent : string ) {
96
- return tarFs . pack ( OVERRIDES_DIR , {
97
- map : ( fileHeader ) => {
98
- fileHeader . name = path . posix . join ( HTTP_TOOLKIT_INJECTED_OVERRIDES_PATH , fileHeader . name ) ;
99
-
100
- // Owned by root by default
101
- fileHeader . uid = 0 ;
102
- fileHeader . gid = 0 ;
103
-
104
- // But ensure everything is globally readable & runnable
105
- fileHeader . mode = parseInt ( '555' , 8 ) ;
106
-
107
- return fileHeader ;
108
- } ,
109
- finalize : false ,
110
- finish : ( pack ) => {
111
- pack . entry ( { name : HTTP_TOOLKIT_INJECTED_CA_PATH } , certContent ) ;
112
- pack . finalize ( ) ;
113
- }
114
- } ) ;
115
- }
116
-
117
- // The two ways to inject the files required for interception into the image.
118
- // If 'mount', the override files should be bind-mounted directly into the image. If
119
- // 'inject', the override files should be copied into the image. 'Mount' is generally
120
- // better & faster, but not possible for builds or injection into remote hosts.
121
- export type DOCKER_INTERCEPTION_TYPE =
122
- | 'mount'
123
- | 'inject' ;
124
-
125
94
/**
126
95
* Takes the config for a container, and returns the config to create the
127
96
* same container, but fully intercepted.
@@ -133,8 +102,7 @@ export type DOCKER_INTERCEPTION_TYPE =
133
102
export function transformContainerCreationConfig (
134
103
containerConfig : Docker . ContainerCreateOptions ,
135
104
baseImageConfig : Docker . ImageInspectInfo | undefined ,
136
- { interceptionType, proxyPort, proxyHost, certPath } : {
137
- interceptionType : DOCKER_INTERCEPTION_TYPE
105
+ { proxyPort, proxyHost, certPath } : {
138
106
proxyPort : number ,
139
107
proxyHost : string ,
140
108
certPath : string
@@ -164,25 +132,20 @@ export function transformContainerCreationConfig(
164
132
const hostConfig : Docker . HostConfig = {
165
133
...currentConfig . HostConfig ,
166
134
// To intercept without modifying the container, we bind mount our overrides and certificate
167
- // files into place:
168
- ...( interceptionType === 'mount'
169
- ? {
170
- Binds : [
171
- ...( currentConfig . HostConfig ?. Binds ?? [ ] ) . filter ( ( existingMount ) =>
172
- // Drop any existing mounts for these folders - this allows re-intercepting containers, e.g.
173
- // to switch from one proxy port to another.
174
- ! existingMount . startsWith ( `${ certPath } :` ) &&
175
- ! existingMount . startsWith ( `${ OVERRIDES_DIR } :` )
176
- ) ,
177
- // Bind-mount the CA certificate file individually too:
178
- `${ certPath } :${ HTTP_TOOLKIT_INJECTED_CA_PATH } :ro` ,
179
- // Bind-mount the overrides directory into the container:
180
- `${ OVERRIDES_DIR } :${ HTTP_TOOLKIT_INJECTED_OVERRIDES_PATH } :ro`
181
- // ^ Both 'ro' - untrusted containers must not be able to mess with these!
182
- ]
183
- }
184
- : { }
185
- ) ,
135
+ // files into place on top of the existing content:
136
+ Binds : [
137
+ ...( currentConfig . HostConfig ?. Binds ?? [ ] ) . filter ( ( existingMount ) =>
138
+ // Drop any existing mounts for these folders - this allows re-intercepting containers, e.g.
139
+ // to switch from one proxy port to another.
140
+ ! existingMount . startsWith ( `${ certPath } :` ) &&
141
+ ! existingMount . startsWith ( `${ OVERRIDES_DIR } :` )
142
+ ) ,
143
+ // Bind-mount the CA certificate file individually too:
144
+ `${ certPath } :${ HTTP_TOOLKIT_INJECTED_CA_PATH } :ro` ,
145
+ // Bind-mount the overrides directory into the container:
146
+ `${ OVERRIDES_DIR } :${ HTTP_TOOLKIT_INJECTED_OVERRIDES_PATH } :ro`
147
+ // ^ Both 'ro' - untrusted containers must not be able to mess with these!
148
+ ] ,
186
149
...( process . platform === 'linux'
187
150
// On Linux only, we need to add an explicit host to make host.docker.internal work:
188
151
? {
@@ -254,8 +217,7 @@ async function connectNetworks(
254
217
export async function restartAndInjectContainer (
255
218
docker : Docker ,
256
219
containerId : string ,
257
- { interceptionType, proxyPort, certContent, certPath } : {
258
- interceptionType : DOCKER_INTERCEPTION_TYPE
220
+ { proxyPort, certContent, certPath } : {
259
221
proxyPort : number ,
260
222
certContent : string
261
223
certPath : string
@@ -298,7 +260,6 @@ export async function restartAndInjectContainer(
298
260
// We don't need image config - inspect result has *everything*
299
261
undefined ,
300
262
{ // The settings to inject:
301
- interceptionType,
302
263
certPath,
303
264
proxyPort,
304
265
proxyHost
@@ -313,11 +274,6 @@ export async function restartAndInjectContainer(
313
274
containerDetails . NetworkSettings . Networks
314
275
) ;
315
276
316
- if ( interceptionType === 'inject' ) {
317
- // Inject the overide files & MITM cert into the image directly:
318
- await newContainer . putArchive ( packInterceptionFiles ( certContent ) , { path : '/' } ) ;
319
- }
320
-
321
277
// Start everything up!
322
278
await newContainer . start ( ) ;
323
279
}
0 commit comments