11import * as _ from 'lodash' ;
22import * as Docker from 'dockerode' ;
33import * as path from 'path' ;
4- import * as tarFs from 'tar-fs' ;
54import * as semver from 'semver' ;
65
76import {
@@ -92,36 +91,6 @@ const envArrayToObject = (envArray: string[] | null | undefined) =>
9291const envObjectToArray = ( envObject : { [ key : string ] : string } ) : string [ ] =>
9392 Object . keys ( envObject ) . map ( k => `${ k } =${ envObject [ k ] } ` ) ;
9493
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-
12594/**
12695 * Takes the config for a container, and returns the config to create the
12796 * same container, but fully intercepted.
@@ -133,8 +102,7 @@ export type DOCKER_INTERCEPTION_TYPE =
133102export function transformContainerCreationConfig (
134103 containerConfig : Docker . ContainerCreateOptions ,
135104 baseImageConfig : Docker . ImageInspectInfo | undefined ,
136- { interceptionType, proxyPort, proxyHost, certPath } : {
137- interceptionType : DOCKER_INTERCEPTION_TYPE
105+ { proxyPort, proxyHost, certPath } : {
138106 proxyPort : number ,
139107 proxyHost : string ,
140108 certPath : string
@@ -164,25 +132,20 @@ export function transformContainerCreationConfig(
164132 const hostConfig : Docker . HostConfig = {
165133 ...currentConfig . HostConfig ,
166134 // 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+ ] ,
186149 ...( process . platform === 'linux'
187150 // On Linux only, we need to add an explicit host to make host.docker.internal work:
188151 ? {
@@ -254,8 +217,7 @@ async function connectNetworks(
254217export async function restartAndInjectContainer (
255218 docker : Docker ,
256219 containerId : string ,
257- { interceptionType, proxyPort, certContent, certPath } : {
258- interceptionType : DOCKER_INTERCEPTION_TYPE
220+ { proxyPort, certContent, certPath } : {
259221 proxyPort : number ,
260222 certContent : string
261223 certPath : string
@@ -298,7 +260,6 @@ export async function restartAndInjectContainer(
298260 // We don't need image config - inspect result has *everything*
299261 undefined ,
300262 { // The settings to inject:
301- interceptionType,
302263 certPath,
303264 proxyPort,
304265 proxyHost
@@ -313,11 +274,6 @@ export async function restartAndInjectContainer(
313274 containerDetails . NetworkSettings . Networks
314275 ) ;
315276
316- if ( interceptionType === 'inject' ) {
317- // Inject the overide files & MITM cert into the image directly:
318- await newContainer . putArchive ( packInterceptionFiles ( certContent ) , { path : '/' } ) ;
319- }
320-
321277 // Start everything up!
322278 await newContainer . start ( ) ;
323279}
0 commit comments