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

Add flag to follow symlinks when running opa build #6800

Conversation

tjons
Copy link
Contributor

@tjons tjons commented Jun 7, 2024

Why the changes in this PR are needed?

Per #6495, it is important to users using build systems like babel to allow opa build to include symlinked files in the bundle directory. Bazel and other build tools create sandboxes of symlinked files before building. As it stands today, opa build only includes regular files and directories by default.

What are the changes in this PR?

This PR adds a boolean flag, --follow-symlinks to the opa build command to allow users to build directories with symlinked files, and have the contents of those symlinked files included in the built bundle.

Notes to assist PR review:

I've included tests for the build command but would appreciate a critical eye as to any other areas that could use testing. Also, I've kept the symlink changes as constrained to the build features as possible, but there are many layers of method calls to get to the core filesystem loading logic, so any ideas for cleaning up this code is welcome.

Further comments:

@tjons tjons force-pushed the tjons/6495-symlinks-building-bundles branch from 32341ef to 5de7d4b Compare June 7, 2024 00:48
Copy link

netlify bot commented Jun 7, 2024

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 32341ef
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/666258dc4105820008e9ebae
😎 Deploy Preview https://deploy-preview-6800--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@tjons tjons force-pushed the tjons/6495-symlinks-building-bundles branch from b58f28c to 6ee5e4c Compare June 7, 2024 00:50
Copy link

netlify bot commented Jun 7, 2024

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit b58f28c
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/666258ff5f50be0008760453
😎 Deploy Preview https://deploy-preview-6800--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@tjons tjons force-pushed the tjons/6495-symlinks-building-bundles branch from 8329087 to 6bd3b39 Compare June 7, 2024 15:56
Copy link
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

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

Thank you! 😃
Just a couple of comments/questions.

loader/loader.go Outdated
}

// GetBundleDirectoryLoaderFS returns a bundle directory loader which can be used to load
// files in the directory.
func GetBundleDirectoryLoaderFS(fsys fs.FS, path string, filter Filter) (bundle.DirectoryLoader, bool, error) {
func GetBundleDirectoryLoaderFS(fsys fs.FS, path string, filter Filter, followSymlinks bool) (bundle.DirectoryLoader, bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This will be a breaking change for anyone using this public function directly, although probably unlikely. Given that we also add WithFollowSymlinks(), couldn't callers simply use that on the returned value instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great idea! I didn't think about the library usecase.

loader/loader.go Outdated
@@ -188,6 +190,12 @@ func (fl *fileLoader) WithRegoVersion(version ast.RegoVersion) FileLoader {
return fl
}

// WithFollowSymLinks enables or disables following symlinks when loading files
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// WithFollowSymLinks enables or disables following symlinks when loading files
// WithFollowSymlinks enables or disables following symlinks when loading files


params := newBuildParams()
params.outputFile = path.Join(rootDir, "test.tar.gz")
params.bundleMode = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we drop This? Reading in bundle-mode doesn't seem like a requirement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, I think I might have lead you astray with this coment 🤔 .
If I add params.bundleMode = true here, the test starts to fail.
But if I also add params.followSymlinks = true, it succeeds again, clearly showing that your code is being exercised.

So we need to add

params.bundleMode = true
params.followSymlinks = true

@tjons
Copy link
Contributor Author

tjons commented Jun 30, 2024

@johanfylling thanks for the review! sorry it took me a while to respond to your feedback... I think I have it all covered here! Let me know if anything else is needed before I squash all this :)

Copy link

netlify bot commented Jun 30, 2024

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 3496456
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/6682fa5b3939210008df46e5
😎 Deploy Preview https://deploy-preview-6800--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@tjons tjons force-pushed the tjons/6495-symlinks-building-bundles branch from a84fabf to 568dc5d Compare June 30, 2024 21:18
Copy link
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

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

Thank you for the updates 🙂.
I think I misread the bundle-mode angle before.
Your tests look good, but it doesn't look like they're actually enabling params.followSymlinks = true, so they're showing this is a current feature for non-bundle-mode builds. If we add params.bundleMode = true to the tests, they start failing; and then adding params.followSymlinks = true, TestBuildWithFollowSymlinks will start succeeding again, showing that the your changes are necessary 👍 . The TestBuildWithFollowSymlinksEntireDir test will keep failing, however.

@tjons tjons force-pushed the tjons/6495-symlinks-building-bundles branch from 568dc5d to cb2de1f Compare July 1, 2024 18:47
@tjons tjons force-pushed the tjons/6495-symlinks-building-bundles branch from cb2de1f to 3496456 Compare July 1, 2024 18:50
Copy link
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

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

Great job! Thank you 😃

@johanfylling johanfylling merged commit f37b5cc into open-policy-agent:main Jul 2, 2024
28 checks passed
@tjons tjons deleted the tjons/6495-symlinks-building-bundles branch July 4, 2024 14:31
@tjons tjons restored the tjons/6495-symlinks-building-bundles branch July 11, 2024 01:13
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