From d615211c708fe96603af88ecfed09621f2a26161 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 10 Aug 2020 18:56:39 +0200 Subject: [PATCH 1/7] feat(scaffolder): use the uid and gid of the parent process for docker --- .../src/scaffolder/stages/templater/helpers.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts index 11bbcd6005..8d3e0eab3f 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts @@ -71,6 +71,9 @@ export const runDockerContainer = async ({ return undefined; }); }); + + const User = `${process.getuid()}:${process.getgid()}`; + const [{ Error: error, StatusCode: statusCode }] = await dockerClient.run( imageName, args, @@ -85,6 +88,7 @@ export const runDockerContainer = async ({ `${await fs.promises.realpath(templateDir)}:/template`, ], }, + User, ...createOptions, }, ); From d24b24a473a5ef0b1d66ecded07a68fecea59c9a Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 10 Aug 2020 19:13:36 +0200 Subject: [PATCH 2/7] feat(scaffolder): Default the user and group id to the same as the host process for docker reasons --- .../stages/templater/helpers.test.ts | 19 +++++++++++++++++++ .../scaffolder/stages/templater/helpers.ts | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.test.ts index e04ba48324..d5dd1cc561 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.test.ts @@ -126,6 +126,25 @@ describe('helpers', () => { ); }); + it('should pass through the user and group id from the host machine', async () => { + await runDockerContainer({ + imageName, + args, + templateDir, + resultDir, + dockerClient: mockDocker, + }); + + expect(mockDocker.run).toHaveBeenCalledWith( + imageName, + args, + expect.any(Stream), + expect.objectContaining({ + User: `${process.getuid()}:${process.getgid()}`, + }), + ); + }); + it('should pass through the log stream to the docker client', async () => { const logStream = new PassThrough(); await runDockerContainer({ diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts index 8d3e0eab3f..40bc0ac0c9 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts @@ -72,6 +72,10 @@ export const runDockerContainer = async ({ }); }); + // Files that are created inside the Docker container will be owned by + // root on the host system on non Mac systems, because of reasons. Mainly the fact that + // volume sharing is done using NFS on Mac and actual mounts in Linux world. + // So we set the user in the container as the same user and group id as the host. const User = `${process.getuid()}:${process.getgid()}`; const [{ Error: error, StatusCode: statusCode }] = await dockerClient.run( From d6a0ad744fb65006c0ea51ced4430fdf1dbe0691 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 10 Aug 2020 20:46:55 +0200 Subject: [PATCH 3/7] chore(scaffolder): set the working directory default as the temp foler --- .../src/scaffolder/stages/templater/helpers.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts index 40bc0ac0c9..12ec1be143 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts @@ -84,6 +84,7 @@ export const runDockerContainer = async ({ logStream, { Volumes: { '/result': {}, '/template': {} }, + WorkingDir: '/tmp', HostConfig: { Binds: [ // Need to use realpath here as Docker mounting does not like From 92f3e58640be342ec9cfe28884e8a26be7fa6f10 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 10 Aug 2020 20:53:56 +0200 Subject: [PATCH 4/7] chores(scaffolder): trying something new --- .../src/scaffolder/stages/templater/helpers.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts index 12ec1be143..7684735519 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts @@ -77,20 +77,21 @@ export const runDockerContainer = async ({ // volume sharing is done using NFS on Mac and actual mounts in Linux world. // So we set the user in the container as the same user and group id as the host. const User = `${process.getuid()}:${process.getgid()}`; - + console.warn('running this new'); const [{ Error: error, StatusCode: statusCode }] = await dockerClient.run( imageName, args, logStream, { Volumes: { '/result': {}, '/template': {} }, - WorkingDir: '/tmp', + WorkingDir: '/home', HostConfig: { Binds: [ // Need to use realpath here as Docker mounting does not like // symlinks for binding volumes `${await fs.promises.realpath(resultDir)}:/result`, `${await fs.promises.realpath(templateDir)}:/template`, + `${__dirname}:/home`, ], }, User, From c533d15336575c64af6565525bdf8c75e54a5189 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 10 Aug 2020 21:17:13 +0200 Subject: [PATCH 5/7] chore(scaffolder): set a home directory for cookiecutter now we have new username and group id --- .../src/scaffolder/stages/templater/helpers.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts index 7684735519..2bda4e0f80 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts @@ -77,24 +77,22 @@ export const runDockerContainer = async ({ // volume sharing is done using NFS on Mac and actual mounts in Linux world. // So we set the user in the container as the same user and group id as the host. const User = `${process.getuid()}:${process.getgid()}`; - console.warn('running this new'); const [{ Error: error, StatusCode: statusCode }] = await dockerClient.run( imageName, args, logStream, { Volumes: { '/result': {}, '/template': {} }, - WorkingDir: '/home', HostConfig: { Binds: [ // Need to use realpath here as Docker mounting does not like // symlinks for binding volumes `${await fs.promises.realpath(resultDir)}:/result`, `${await fs.promises.realpath(templateDir)}:/template`, - `${__dirname}:/home`, ], }, User, + Env: ['HOME=/tmp'], ...createOptions, }, ); From ae56770cf2ce2a0ac9e823570cc8288e97b82ebe Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 10 Aug 2020 21:23:54 +0200 Subject: [PATCH 6/7] chore(scaffolder): fixing some of the docs and making things a little cleanern --- .../src/scaffolder/stages/templater/helpers.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts index 2bda4e0f80..a3dea7ea5a 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.ts @@ -72,11 +72,6 @@ export const runDockerContainer = async ({ }); }); - // Files that are created inside the Docker container will be owned by - // root on the host system on non Mac systems, because of reasons. Mainly the fact that - // volume sharing is done using NFS on Mac and actual mounts in Linux world. - // So we set the user in the container as the same user and group id as the host. - const User = `${process.getuid()}:${process.getgid()}`; const [{ Error: error, StatusCode: statusCode }] = await dockerClient.run( imageName, args, @@ -91,7 +86,13 @@ export const runDockerContainer = async ({ `${await fs.promises.realpath(templateDir)}:/template`, ], }, - User, + // Files that are created inside the Docker container will be owned by + // root on the host system on non Mac systems, because of reasons. Mainly the fact that + // volume sharing is done using NFS on Mac and actual mounts in Linux world. + // So we set the user in the container as the same user and group id as the host. + User: `${process.getuid()}:${process.getgid()}`, + // Set the home directory inside the container as something that applications can + // write to, otherwise they will just flop and fail trying to write to / Env: ['HOME=/tmp'], ...createOptions, }, From 56db3293d83cd1f8061ec9ac2ca20ff66d28da89 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 10 Aug 2020 21:27:35 +0200 Subject: [PATCH 7/7] test(scaffolder/docker): added some tests to check the home cariable --- .../src/scaffolder/stages/templater/helpers.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.test.ts index d5dd1cc561..b7e969cd47 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/templater/helpers.test.ts @@ -126,7 +126,7 @@ describe('helpers', () => { ); }); - it('should pass through the user and group id from the host machine', async () => { + it('should pass through the user and group id from the host machine and set the home dir', async () => { await runDockerContainer({ imageName, args, @@ -141,6 +141,7 @@ describe('helpers', () => { expect.any(Stream), expect.objectContaining({ User: `${process.getuid()}:${process.getgid()}`, + Env: ['HOME=/tmp'], }), ); });