Skip to content
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

fix(follow): handle directories in tar.gz artifacts #716

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

TiagoJMartins
Copy link

@TiagoJMartins TiagoJMartins commented Jan 14, 2025

What type of PR is this?

/kind bug

Any specific area of the project related to this PR?

/area cli

What this PR does / why we need it:

A bit of background on my use-case:

I've configured Falco to load all rules from a given sub-directory within /etc/falco, e.g.: /etc/falco/subdir/rules1.yaml, and packaged multiple rules files into separate rulesfile artifacts in tar.gz format.

During packaging, I'll generate the final directory structure I want and archive it, knowing that falcoctl artifact will extract the contents onto /etc/falco.

The issue is that while falco artifact install works great, falco artifact follow seems to break when processing the file path as it expects base paths to always be files, so it'll always fail when the rules gets updated and then it tries to move a directory.

Which issue(s) this PR fixes:

This PR addresses the issue above by modifying the file path handling in a way that preserves the artifact directory structure, and also slightly modifies the tar.gz extraction logic by only appending to files if it's handling an actual file and not a directory/symlink.

I've added tests for these use cases to ensure that the previous behavior remains unchanged.

The error below is printed when trying to update an artifact pushed as .tar.gz, that includes the file: /rules.csq.d/kubernetes.yaml, which gets installed at /etc/falco/rules.csq.d/kubernetes.yaml (because /rulesfiles is mounted at /etc/falco by the Helm chart).

{
    "destDirectory": "/rulesfiles",
    "fileName": "rules.csq.d",
    "followerName": "foobar/falco-rules/platform:1-kubernetes",
    "level": "ERROR",
    "msg": "Unable to move file",
    "reason": "unable to read file /tmp/falcoctl-1880591248/rules.csq.d: read /tmp/falcoctl-1880591248/rules.csq.d: is a directory",
    "timestamp": "2025-01-14 17:03:32"
}

@poiana
Copy link
Contributor

poiana commented Jan 14, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: TiagoJMartins
Once this PR has been reviewed and has the lgtm label, please assign leogr for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana requested a review from zuc January 14, 2025 20:30
@poiana
Copy link
Contributor

poiana commented Jan 14, 2025

Welcome @TiagoJMartins! It looks like this is your first PR to falcosecurity/falcoctl 🎉

@poiana poiana added the size/XL label Jan 14, 2025
@TiagoJMartins TiagoJMartins force-pushed the fix/allow-directories-in-artifacts branch from 9c6f709 to 1ef75d7 Compare January 14, 2025 20:31
@TiagoJMartins TiagoJMartins marked this pull request as ready for review January 14, 2025 20:33
@poiana poiana requested a review from loresuso January 14, 2025 20:33
@TiagoJMartins
Copy link
Author

TiagoJMartins commented Jan 14, 2025

Marked as WIP as it seems to work but I want to build the Docker image and run a few more tests on my end.

@TiagoJMartins TiagoJMartins force-pushed the fix/allow-directories-in-artifacts branch from 1ef75d7 to 48f8bfb Compare January 15, 2025 11:13
@poiana poiana added size/L and removed size/XL labels Jan 15, 2025
@TiagoJMartins TiagoJMartins force-pushed the fix/allow-directories-in-artifacts branch from 48f8bfb to d1e9252 Compare January 15, 2025 11:49
@TiagoJMartins TiagoJMartins changed the title wip: fix: allow directories in tar.gz artifacts fix: handle directories in tar.gz artifacts Jan 15, 2025
@TiagoJMartins TiagoJMartins changed the title fix: handle directories in tar.gz artifacts fix(follow): handle directories in tar.gz artifacts Jan 15, 2025
@TiagoJMartins
Copy link
Author

TiagoJMartins commented Jan 15, 2025

Tested with this (amd64) image: tiagojmartins/falcoctl:0.10.1 - Docker Hub

@alacuku
Copy link
Member

alacuku commented Jan 15, 2025

Hi @TiagoJMartins, thansk for the PR.

Could you please have a look at the CI? Linting and tests are failing.

@TiagoJMartins TiagoJMartins force-pushed the fix/allow-directories-in-artifacts branch 2 times, most recently from 6424fab to 1ba880a Compare January 15, 2025 15:30
@TiagoJMartins
Copy link
Author

TiagoJMartins commented Jan 15, 2025

Could you please have a look at the CI? Linting and tests are failing.

I think the issues should be fixed now. I'll wait until someone approves the next CI run 👍🏻

@leogr leogr added this to the v0.11.0 milestone Jan 16, 2025
Copy link
Member

@alacuku alacuku left a comment

Choose a reason for hiding this comment

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

Hey @TiagoJMartins, thanks for the contribution!

I left some comments.

Comment on lines +117 to +126
// Always create temporary directories at the root level to avoid nesting issues.
// If TmpDir points to a subdirectory (e.g., /tmp/foo/bar), we use its parent (/tmp/foo)
// to prevent creating temporary directories inside artifact directories.
baseDir := conf.TmpDir
if baseDir != "" {
baseDir = filepath.Clean(baseDir)
if filepath.Base(baseDir) != "." {
baseDir = filepath.Dir(baseDir)
}
}

tmpDir, err := os.MkdirTemp(baseDir, "falcoctl-")
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this? The temporary dir is given by the user, and is the same for all the followers/artifacts.

Copy link
Member

Choose a reason for hiding this comment

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

@TiagoJMartins, could you please provide more info about this change?

Signed-off-by: Tiago Martins <tiago.martins@hotjar.com>
@TiagoJMartins TiagoJMartins force-pushed the fix/allow-directories-in-artifacts branch from 1ba880a to 1e715bb Compare January 17, 2025 15:03
@TiagoJMartins TiagoJMartins requested a review from alacuku January 17, 2025 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

4 participants