Skip to content

[RORDEV-1502] Fixed ror-tools file permissions #1125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import tech.beshu.ror.tools.core.patches.internal.FilePatch.FilePatchMetadata
import tech.beshu.ror.tools.core.patches.internal.RorPluginDirectory.EsPatchMetadata
import tech.beshu.ror.tools.core.utils.EsDirectory
import tech.beshu.ror.tools.core.utils.EsUtil.{findTransportNetty4JarIn, readonlyrestPluginPath}
import tech.beshu.ror.tools.core.utils.FileUtils.modifyFileWithMaintainingOriginalPermissionsAndOwner

private[patches] class RorPluginDirectory(val esDirectory: EsDirectory) {

Expand All @@ -46,11 +47,19 @@ private[patches] class RorPluginDirectory(val esDirectory: EsDirectory) {
}

def backup(file: Path): Unit = {
os.copy(from = file, to = backupFolderPath / file.last, replaceExisting = true)
modifyFileWithMaintainingOriginalPermissionsAndOwner[Path](file, _.toIO) { originalFile =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the usage of modifyFileWithMaintainingOriginalPermissionsAndOwner looks strange too. Becase we don't modify the file

val backedUpFile = backupFolderPath / originalFile.last
os.copy(from = originalFile, to = backedUpFile, replaceExisting = true, copyAttributes = true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have os.copy in copyToPluginPath too. But it's not used. Maybe we should remove it?

backedUpFile
}
}

def restore(file: Path): Unit = {
os.copy(from = backupFolderPath / file.last, to = file, replaceExisting = true)
val backedUpFile = backupFolderPath / file.last
modifyFileWithMaintainingOriginalPermissionsAndOwner[Path](backedUpFile, _.toIO) { backedUpFile =>
os.copy(from = backedUpFile, to = file, replaceExisting = true, copyAttributes = true)
file
}
}

def copyToPluginPath(file: Path): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package tech.beshu.ror.tools.core.patches.internal.filePatchers

import better.files.*
import tech.beshu.ror.tools.core.utils.EsDirectory
import tech.beshu.ror.tools.core.utils.FileUtils.modifyFileWithMaintainingOriginalPermissionsAndOwner

import java.util.UUID
import java.util.jar.{JarFile, JarOutputStream}
Expand All @@ -28,7 +29,8 @@ object JarManifestModifier {

private val patchedByRorVersionPropertyName = "Patched-By-Ror-Version"

def addPatchedByRorVersionProperty(file: File, rorVersion: String): Unit = {
def addPatchedByRorVersionProperty(file: File,
rorVersion: String): Unit = modifyFileWithMaintainingOriginalPermissionsAndOwner[File](file, _.toJava) { file =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it works here, but don't you think it's hard to reason about? Maybe the modifyFileWithMaintainingOriginalPermissionsAndOwner method's interface is not so good?

I mean, the method suggests we modify the jar file, but TBH, we don't have to modify it.
Do you have an idea how to do it better?

val tempJarFile = File(s"temp-${UUID.randomUUID()}.jar")
Using(new JarFile(file.toJava)) { jarFile =>
// Using the better-files temporary files causes problems, probably because of permission issues when copying the file at the end of this method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ object FileUtils {
digest.digest.map("%02x".format(_)).mkString
}

def modifyFileWithMaintainingOriginalPermissionsAndOwner(jar: File)(modifyJar: File => Unit): Unit = {
val originalFileOwner = Files.getOwner(jar.toPath)
val originalFilePermissions = getOriginalPermissions(jar.toPath)
modifyJar(jar)
Files.setOwner(jar.toPath, originalFileOwner)
setOriginalPermissions(jar.toPath, originalFilePermissions)
def modifyFileWithMaintainingOriginalPermissionsAndOwner(file: File)(modifyFile: File => Unit): Unit = {
modifyFileWithMaintainingOriginalPermissionsAndOwner[File](file, identity) { f => modifyFile(f); f }
}

def modifyFileWithMaintainingOriginalPermissionsAndOwner[FILE](file: FILE, toJavaFile: FILE => File)(modifyJar: FILE => FILE): Unit = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I'd be nice if we could get rid of java.io.File from our public methods. Better files lib is so nice, it'd be better to use it instead. Mixing these two worlds doesn't look nice.

So, let's modify the previous interface of the method and adapt other places where we are not so consistent with using better files.

val originalFileOwner = Files.getOwner(toJavaFile(file).toPath)
val originalFilePermissions = getOriginalPermissions(toJavaFile(file).toPath)
val resultFile = modifyJar(file)
Files.setOwner(toJavaFile(resultFile).toPath, originalFileOwner)
setOriginalPermissions(toJavaFile(resultFile).toPath, originalFilePermissions)
}

private def getOriginalPermissions(jarPath: Path): Any = {
Expand Down
Loading