From 0ad7f76263f129977bc99ae7a3806cb8b9610296 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Thu, 8 Apr 2021 12:56:41 -0500 Subject: [PATCH] Preserve file permissions in images built by Gradle This commit copies the file mode along with other attributes when copying files from the source archive to the build container while building an image using the Gradle plugin. This preserves file permissions on any resources included in the source archive. Fixes gh-25915 --- .../boot/buildpack/platform/io/ZipFileTarArchive.java | 5 +++-- .../boot/buildpack/platform/io/ZipFileTarArchiveTests.java | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchive.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchive.java index 90d543b0d61..0bab02e1f8c 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchive.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchive.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -80,7 +80,7 @@ public class ZipFileTarArchive implements TarArchive { + "' is not compatible with buildpacks; ensure jar file is valid and launch script is not enabled"); } catch (IOException ex) { - throw new IllegalStateException("File is not readable", ex); + throw new IllegalStateException("File '" + jarFile + "' is not readable", ex); } } @@ -99,6 +99,7 @@ public class ZipFileTarArchive implements TarArchive { tarEntry.setUserId(this.owner.getUid()); tarEntry.setGroupId(this.owner.getGid()); tarEntry.setModTime(NORMALIZED_MOD_TIME); + tarEntry.setMode(zipEntry.getUnixMode()); if (!zipEntry.isDirectory()) { tarEntry.setSize(zipEntry.getSize()); } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchiveTests.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchiveTests.java index 1e267db579b..667c7f14e65 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchiveTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/io/ZipFileTarArchiveTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException * Tests for {@link ZipFileTarArchive}. * * @author Phillip Webb + * @author Scott Frederick */ class ZipFileTarArchiveTests { @@ -77,6 +78,7 @@ class ZipFileTarArchiveTests { assertThat(fileEntry.getLongUserId()).isEqualTo(123); assertThat(fileEntry.getLongGroupId()).isEqualTo(456); assertThat(fileEntry.getSize()).isEqualTo(4); + assertThat(fileEntry.getMode()).isEqualTo(0755); String fileContent = StreamUtils.copyToString(tarStream, StandardCharsets.UTF_8); assertThat(fileContent).isEqualTo("test"); } @@ -88,6 +90,7 @@ class ZipFileTarArchiveTests { zip.putArchiveEntry(dirEntry); zip.closeArchiveEntry(); ZipArchiveEntry fileEntry = new ZipArchiveEntry("spring/boot"); + fileEntry.setUnixMode(0755); zip.putArchiveEntry(fileEntry); zip.write("test".getBytes(StandardCharsets.UTF_8)); zip.closeArchiveEntry();