Skip to content

Prevent start of containers with Host and TS Enabled#2047

Merged
limetech merged 4 commits into
unraid:masterfrom
Squidly271:patch-31
Feb 25, 2025
Merged

Prevent start of containers with Host and TS Enabled#2047
limetech merged 4 commits into
unraid:masterfrom
Squidly271:patch-31

Conversation

@Squidly271

@Squidly271 Squidly271 commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Enhanced container management processes to improve startup operations. The system now verifies container configurations before initiating a start, gracefully bypassing those with specific settings. This update includes error handling for containers running in "host" network mode with specific commands, ensuring only containers meeting optimal criteria are activated, reducing potential issues and enhancing overall stability.

@coderabbitai

coderabbitai Bot commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@Squidly271 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 17 minutes and 53 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 9441db1 and fbf176d.

📒 Files selected for processing (2)
  • emhttp/plugins/dynamix.docker.manager/include/ContainerManager.php (2 hunks)
  • emhttp/plugins/dynamix.docker.manager/include/Events.php (2 hunks)

Walkthrough

This update in ContainerManager.php revises copyright years from 2023 to 2025 and enhances container management. A new instance of DockerClient is created to retrieve Docker container information. The control flow now includes an additional conditional block: when processing a container with a "start" action, if the container’s name exists in the retrieved information and its NetworkMode is "host" with the command /opt/unraid/tailscale, the processing is skipped for that container. Similar updates are made in Events.php to improve error handling for specific container configurations.

Changes

File Change Summary
emhttp/.../ContainerManager.php Updated copyright years; added instantiation of DockerClient and retrieval of Docker container info; introduced conditional logic in container loop to skip start actions for containers with NetworkMode "host" and command /opt/unraid/tailscale
emhttp/.../Events.php Enhanced error handling in 'start' action to prevent starting containers with "host" network mode and command /opt/unraid/tailscale, returning an error message instead.

Sequence Diagram(s)

sequenceDiagram
    participant CM as ContainerManager
    participant DC as DockerClient
    participant C as Container
    CM->>DC: Instantiate DockerClient
    CM->>DC: getDockerContainers()
    DC-->>CM: Return container info
    loop For each container in list
        C->>CM: Start action requested
        note right of CM: Check if container name exists in info
        alt Container with NetworkMode "host" and Cmd "/opt/unraid/tailscale"
            CM->>CM: Skip starting container
        else
            CM->>C: Execute Docker start command
        end
    end
Loading

Possibly related PRs

  • Disallow TS integration on network types host/container #2024: The changes in the main PR and the retrieved PR are related as both involve conditional logic regarding the handling of Docker containers with the "host" network mode and Tailscale integration, specifically addressing how containers are started based on their configurations.

Poem

Oh, I hop through lines of code so bright,
With Docker calls and conditions just right.
Skipping containers with a careful check,
My little paws keep the logic in spec.
From 2023 to 2025, I cheer in delight!
🐇💻 Hop, code, hop!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

@Squidly271 Squidly271 changed the title Disallow Start All on containers with Host and TS enabled Prevent start of containers with Host and TS Enabled Feb 25, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
emhttp/plugins/dynamix.docker.manager/include/ContainerManager.php (2)

22-23: Consider optimizing the Docker container information retrieval.

The code creates a new DockerClient instance and retrieves all container information regardless of which action is being performed, but this information is only used when the action is "start". Consider only retrieving this information when needed.

$containers = DockerUtil::docker("ps -a --filter status='$status' --format='{{.Names}}'",true);
-$dockerClient = new DockerClient();
-$info = $dockerClient->getDockerContainers();

if (file_exists($user_prefs)) {
  // existing code
}

+$info = [];
+if ($action == "start") {
+  $dockerClient = new DockerClient();
+  $info = $dockerClient->getDockerContainers();
+}

31-35: Consider adding a comment explaining the purpose of this check.

The code now skips starting containers that use host networking mode and run Tailscale, but the reason isn't documented. Adding a comment would help future maintainers understand the rationale.

if ( $action == "start") {
+  // Skip starting containers using host networking mode and running Tailscale
+  // to prevent potential network conflicts or security issues
  $key = array_search($ct,array_column($info,"Name"));
  if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale")
    continue;
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between baf0b4d and f7d9fd9.

📒 Files selected for processing (1)
  • emhttp/plugins/dynamix.docker.manager/include/ContainerManager.php (2 hunks)
🔇 Additional comments (1)
emhttp/plugins/dynamix.docker.manager/include/ContainerManager.php (1)

2-3: Updated copyright years correctly.

The copyright years have been updated from 2023 to 2025 for both Lime Technology and Bergware International, keeping the file's copyright information current.

Comment on lines +31 to +35
if ( $action == "start") {
$key = array_search($ct,array_column($info,"Name"));
if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale")
continue;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for container lookup.

The current implementation assumes that array_search() will always find the container name in the $info array. If a container isn't found, $key would be false, potentially causing an undefined index error when accessing $info[$key].

if ( $action == "start") {
  $key = array_search($ct,array_column($info,"Name"));
-  if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale")
+  if ($key !== false && isset($info[$key]) && 
+     $info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale")
    continue;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if ( $action == "start") {
$key = array_search($ct,array_column($info,"Name"));
if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale")
continue;
}
if ( $action == "start") {
$key = array_search($ct,array_column($info,"Name"));
if ($key !== false && isset($info[$key]) &&
$info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale")
continue;
}

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
emhttp/plugins/dynamix.docker.manager/include/Events.php (2)

35-39: Consider using a more specific error response type.

Currently, a successful security block returns a 'success' response with an explanation message. This could be confusing for client code that checks for success/error status.

Consider using an 'error' response type with an appropriate error code:

if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale") {
-	$arrResponse = ['success'=> _('For security reasons, containers with Network Type "Host" should not have Tailscale enabled. Please disable Tailscale in this container or change the Network Type of the container.')];
+	$arrResponse = ['error'=> _('For security reasons, containers with Network Type "Host" should not have Tailscale enabled. Please disable Tailscale in this container or change the Network Type of the container.')];
	break;
}

This makes it clearer that the operation was not completed due to a security policy violation rather than suggesting it was successful.


34-36: Consider optimizing container information retrieval.

The current implementation fetches all Docker containers even when we only need information about one specific container.

If the DockerClient API allows it, consider using a more targeted method to get information about just the specific container:

- $info = $DockerClient->getDockerContainers();
- $key = array_search($container,array_column($info,"Id"));
- if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale") {
+ $containerInfo = $DockerClient->getContainerDetails($container);
+ if ($containerInfo && $containerInfo['NetworkMode'] == "host" && $containerInfo['Cmd'] == "/opt/unraid/tailscale") {

This would be more efficient, especially in environments with many containers.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f7d9fd9 and 9441db1.

📒 Files selected for processing (1)
  • emhttp/plugins/dynamix.docker.manager/include/Events.php (2 hunks)
🔇 Additional comments (1)
emhttp/plugins/dynamix.docker.manager/include/Events.php (1)

2-3: Copyright year updated correctly.

The copyright years have been updated from 2023 to 2025 for both Lime Technology and Bergware International, keeping the copyright information current.

Comment on lines +33 to +41
if ($container) {
$info = $DockerClient->getDockerContainers();
$key = array_search($container,array_column($info,"Id"));
if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale") {
$arrResponse = ['success'=> _('For security reasons, containers with Network Type "Host" should not have Tailscale enabled. Please disable Tailscale in this container or change the Network Type of the container.')];
break;
}
$arrResponse = ['success' => $DockerClient->startContainer($container)];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling in container checking logic.

The security check to prevent containers with Host network mode and Tailscale from starting is a good security improvement. However, there's a potential issue if the container ID isn't found in the array.

Consider adding additional validation:

if ($container) {
	$info = $DockerClient->getDockerContainers();
	$key = array_search($container,array_column($info,"Id"));
+	if ($key !== false) {
		if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale") {
			$arrResponse = ['success'=> _('For security reasons, containers with Network Type "Host" should not have Tailscale enabled. Please disable Tailscale in this container or change the Network Type of the container.')];
			break;
		}
+	}
	$arrResponse = ['success' => $DockerClient->startContainer($container)];
}

This ensures that the array index check only happens when the container is found, preventing potential undefined index errors.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if ($container) {
$info = $DockerClient->getDockerContainers();
$key = array_search($container,array_column($info,"Id"));
if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale") {
$arrResponse = ['success'=> _('For security reasons, containers with Network Type "Host" should not have Tailscale enabled. Please disable Tailscale in this container or change the Network Type of the container.')];
break;
}
$arrResponse = ['success' => $DockerClient->startContainer($container)];
}
if ($container) {
$info = $DockerClient->getDockerContainers();
$key = array_search($container, array_column($info, "Id"));
if ($key !== false) {
if ($info[$key]['NetworkMode'] == "host" && $info[$key]['Cmd'] == "/opt/unraid/tailscale") {
$arrResponse = ['success'=> _('For security reasons, containers with Network Type "Host" should not have Tailscale enabled. Please disable Tailscale in this container or change the Network Type of the container.')];
break;
}
}
$arrResponse = ['success' => $DockerClient->startContainer($container)];
}

@limetech limetech merged commit 78ea6f4 into unraid:master Feb 25, 2025
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