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

Conversation

mgoworko
Copy link
Collaborator

@mgoworko mgoworko commented Jun 7, 2025

🐞Fix (ES) Preserve file owner and permissions during ror-tools patch and unpatch operations


  • The file owner could change, when Jar manifest is modified during patching operation
  • but there was also an older, preexisting issue - backup and restore operations did not preserve file owner

@mgoworko mgoworko changed the title [RORDEV-1502] Fixed ror- tools file permissions [RORDEV-1502] Fixed ror-tools file permissions Jun 7, 2025
Copy link

coderabbitai bot commented Jun 7, 2025

📝 Walkthrough

Walkthrough

The changes refactor file modification logic across several components to consistently preserve original file permissions and ownership. The modifyFileWithMaintainingOriginalPermissionsAndOwner utility in FileUtils.scala is generalized to support modifications that may return a new file reference. The backup and restore methods in RorPluginDirectory and the addPatchedByRorVersionProperty method in JarManifestModifier are updated to use this utility, ensuring file attribute preservation during copy and modification operations. No public API signatures are altered except for the generalization of the utility method.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant FileUtils
    participant FileSystem

    Caller->>FileUtils: modifyFileWithMaintainingOriginalPermissionsAndOwner(file, modifyFn)
    FileUtils->>FileSystem: Read file owner and permissions
    FileUtils->>FileUtils: Call modifyFn(file)
    FileUtils->>FileSystem: Restore file owner and permissions
    FileUtils-->>Caller: Return (Unit or new file reference)
Loading
sequenceDiagram
    participant RorPluginDirectory
    participant FileUtils
    participant FileSystem

    RorPluginDirectory->>FileUtils: modifyFileWithMaintainingOriginalPermissionsAndOwner(file, copyFn)
    FileUtils->>FileSystem: Read file owner and permissions
    FileUtils->>FileUtils: copyFn(file) (e.g., os.copy with copyAttributes)
    FileUtils->>FileSystem: Restore file owner and permissions
    FileUtils-->>RorPluginDirectory: Complete backup/restore
Loading
sequenceDiagram
    participant JarManifestModifier
    participant FileUtils
    participant FileSystem

    JarManifestModifier->>FileUtils: modifyFileWithMaintainingOriginalPermissionsAndOwner(file, patchManifestFn)
    FileUtils->>FileSystem: Read file owner and permissions
    FileUtils->>FileUtils: patchManifestFn(file) (modify manifest, use temp files, etc.)
    FileUtils->>FileSystem: Restore file owner and permissions
    FileUtils-->>JarManifestModifier: Complete manifest modification
Loading

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/utils/FileUtils.scala (1)

38-44: Consider exception safety during permission restoration.

The generalized method design is solid, but there's no exception handling around the permission restoration. If modifyJar succeeds but permission restoration fails, the file could be left in an inconsistent state.

Consider wrapping the permission restoration in a try-catch:

  def modifyFileWithMaintainingOriginalPermissionsAndOwner[FILE](file: FILE, toJavaFile: FILE => File)(modifyJar: FILE => FILE): Unit = {
    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)
+   try {
+     Files.setOwner(toJavaFile(resultFile).toPath, originalFileOwner)
+     setOriginalPermissions(toJavaFile(resultFile).toPath, originalFilePermissions)
+   } catch {
+     case ex: Exception =>
+       // Log warning but don't fail the operation
+       System.err.println(s"Warning: Could not restore file permissions: ${ex.getMessage}")
+   }
  }
ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/patches/internal/filePatchers/JarManifestModifier.scala (1)

32-50: Excellent: JAR modification now preserves file permissions.

The refactoring properly wraps the JAR modification logic to preserve original file permissions and ownership. This should resolve the permission issues mentioned in the comment on line 36.

The temp file cleanup happens automatically via moveTo, but consider adding explicit cleanup in failure scenarios:

+   try {
      tempJarFile.moveTo(file)(File.CopyOptions(overwrite = true))
+     file
+   } catch {
+     case ex: Exception =>
+       tempJarFile.delete(swallowIOExceptions = true)
+       throw ex
+   }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 16f4aed and 71c5eab.

📒 Files selected for processing (3)
  • ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/patches/internal/RorPluginDirectory.scala (2 hunks)
  • ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/patches/internal/filePatchers/JarManifestModifier.scala (2 hunks)
  • ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/utils/FileUtils.scala (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/patches/internal/filePatchers/JarManifestModifier.scala (1)
ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/utils/FileUtils.scala (3)
  • FileUtils (25-78)
  • modifyFileWithMaintainingOriginalPermissionsAndOwner (34-36)
  • modifyFileWithMaintainingOriginalPermissionsAndOwner (38-44)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: ror (Run all tests IT_es67x)
  • GitHub Check: ror (Run all tests IT_es80x)
  • GitHub Check: ror (Run all tests IT_es70x)
  • GitHub Check: ror (Run all tests IT_es710x)
  • GitHub Check: ror (Run all tests IT_es810x)
  • GitHub Check: ror (CVE check Job)
  • GitHub Check: ror (Run all tests IT_es90x)
  • GitHub Check: ror (Run all tests IT_es717x)
  • GitHub Check: ror (Run all tests IT_es818x)
  • GitHub Check: ror (Run all tests UNIT)
  • GitHub Check: ror (Run all tests LICENSE)
🔇 Additional comments (5)
ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/utils/FileUtils.scala (1)

34-36: Clean backward compatibility preservation.

The wrapper method maintains the existing API while delegating to the generalized implementation.

ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/patches/internal/RorPluginDirectory.scala (3)

26-26: LGTM: Proper import for the utility function.


50-55: Excellent: File permissions preserved during backup.

The backup operation now correctly preserves the original file's permissions and ownership when creating the backup copy.


58-63: Excellent: File permissions preserved during restore.

The restore operation now correctly preserves the backup file's permissions and ownership when restoring to the original location.

ror-tools-core/src/main/scala/tech/beshu/ror/tools/core/patches/internal/filePatchers/JarManifestModifier.scala (1)

21-21: LGTM: Proper import for the utility function.

Copy link
Collaborator

@coutoPL coutoPL left a comment

Choose a reason for hiding this comment

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

Plus, we have to answer two questions:

  1. Why doesn't the issue break anything? Or maybe it does in some circumstances?
  2. What about the files that now have the wrong owner? How to fix it? Or maybe we don't have to do that?

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.

@@ -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?

os.copy(from = file, to = backupFolderPath / file.last, replaceExisting = true)
modifyFileWithMaintainingOriginalPermissionsAndOwner[Path](file, _.toIO) { originalFile =>
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?

@@ -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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants