From 652a2e9276e0e359aaaf7339fd4dc02510a7169c Mon Sep 17 00:00:00 2001 From: Christopher Zorn Date: Tue, 31 Aug 2021 15:52:55 -0700 Subject: [PATCH 1/4] Copy file mode by getting original mode via stats Signed-off-by: Christopher Zorn --- .../src/scaffolder/actions/builtin/fetch/template.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts index 425623d154..13dfb2486e 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -262,8 +262,9 @@ export function createFetchTemplateAction(options: { ); await fs.copy(inputFilePath, outputPath); } else { + const statsObj = await fs.stat(inputFilePath); ctx.logger.info( - `Writing file ${location} to template output path.`, + `Writing file ${location} to template output path. mode: ${statsObj.mode}`, ); const inputFileContents = await fs.readFile(inputFilePath, 'utf-8'); await fs.outputFile( @@ -272,6 +273,7 @@ export function createFetchTemplateAction(options: { ? templater.renderString(inputFileContents, context) : inputFileContents, ); + await fs.chmod(outputPath, statsObj.mode); } } } From ed1100739c5988faf260029b625c620d17590565 Mon Sep 17 00:00:00 2001 From: Christopher Zorn Date: Wed, 1 Sep 2021 14:47:58 -0700 Subject: [PATCH 2/4] Tests for fixing #7021 to ensure file permissions are copied when templating Signed-off-by: Christopher Zorn --- .../scaffolder/actions/builtin/fetch/template.test.ts | 11 +++++++++++ .../src/scaffolder/actions/builtin/fetch/template.ts | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts index 752212606f..d676af56ca 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts @@ -146,6 +146,10 @@ describe('fetch:template', () => { mockFetchContents.mockImplementation(({ outputPath }) => { mockFs({ [outputPath]: { + 'an-executable.sh': mockFs.file({ + content: '#!/usr/bin/env bash', + mode: parseInt('100755', 8), + }), 'empty-dir-${{ values.count }}': {}, 'static.txt': 'static content', '${{ values.name }}.txt': 'static content', @@ -211,6 +215,13 @@ describe('fetch:template', () => { fs.readFile(`${workspacePath}/target/a-binary-file.png`), ).resolves.toEqual(aBinaryFile); }); + it('copies files and maintains the original file permissions', async () => { + await expect( + fs + .stat(`${workspacePath}/target/an-executable.sh`) + .then(fObj => fObj.mode), + ).resolves.toEqual(parseInt('100755', 8)); + }); }); describe('copyWithoutRender', () => { diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts index 13dfb2486e..5e4e4406fa 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -272,8 +272,8 @@ export function createFetchTemplateAction(options: { renderContents ? templater.renderString(inputFileContents, context) : inputFileContents, + { mode: statsObj.mode }, ); - await fs.chmod(outputPath, statsObj.mode); } } } From 04aad2dab5a427e93f7548fa16eec9d2093eda6b Mon Sep 17 00:00:00 2001 From: Christopher Zorn Date: Thu, 2 Sep 2021 09:27:10 -0700 Subject: [PATCH 3/4] Issue #7021 changeset Signed-off-by: Christopher Zorn --- .changeset/breezy-gifts-wave.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/breezy-gifts-wave.md diff --git a/.changeset/breezy-gifts-wave.md b/.changeset/breezy-gifts-wave.md new file mode 100644 index 0000000000..2bea8e8cd1 --- /dev/null +++ b/.changeset/breezy-gifts-wave.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Fix issue #7021 scaffolder action fetch:template preserves templates file permissions From a5c71b627c126a1813539481287f4dada15a337b Mon Sep 17 00:00:00 2001 From: Christopher Zorn Date: Thu, 2 Sep 2021 10:12:52 -0700 Subject: [PATCH 4/4] log: change the way the file mode is logged Signed-off-by: Christopher Zorn --- .../src/scaffolder/actions/builtin/fetch/template.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts index 5e4e4406fa..bb568c25e6 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -264,7 +264,7 @@ export function createFetchTemplateAction(options: { } else { const statsObj = await fs.stat(inputFilePath); ctx.logger.info( - `Writing file ${location} to template output path. mode: ${statsObj.mode}`, + `Writing file ${location} to template output path with mode ${statsObj.mode}.`, ); const inputFileContents = await fs.readFile(inputFilePath, 'utf-8'); await fs.outputFile(