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

plugins/bundle: use unique temporary files #4786

Merged

Conversation

FredrikAppelros
Copy link
Contributor

In order to use the feature to persist activated bundles to disk in a
cloud environment with shared storage, e.g. Kubernetes with the Amazon
EFS storage driver, each instance of OPA needs to either synchronize
their access to the temporary file using advisory file locks, or use
unique temporary files. If not, then the following situation may occur:

p1: open and trunc tmp file
p1: write to tmp file
p2: open and trunc tmp file
p1: rename tmp file to dst
p2: write to tmp file
p2: rename tmp file to dst

This may then lead to the persisted bundle being truncated or corrupted.

Here the approach of using unique temporary files is chosen because it
avoids the overhead of introducing file locks, and the additional
dependency since Go lacks any such mechanisms in the standard library.

This solution should avoid truncated or corrupt bundles as rename() is
guaranteed to be atomic, even in file systems like NFS.

Fixes: #4782
Signed-off-by: Fredrik Appelros fredrik.appelros@sinch.com

In order to use the feature to persist activated bundles to disk in a
cloud environment with shared storage, e.g. Kubernetes with the Amazon
EFS storage driver, each instance of OPA needs to either synchronize
their access to the temporary file using advisory file locks, or use
unique temporary files. If not, then the following situation may occur:

p1: open and trunc tmp file
p1: write to tmp file
p2: open and trunc tmp file
p1: rename tmp file to dst
p2: write to tmp file
p2: rename tmp file to dst

This may then lead to the persisted bundle being truncated or corrupted.

Here the approach of using unique temporary files is chosen because it
avoids the overhead of introducing file locks, and the additional
dependency since Go lacks any such mechanisms in the standard library.

This solution should avoid truncated or corrupt bundles as `rename()` is
guaranteed to be atomic, even in file systems like NFS.

Fixes: open-policy-agent#4782
Signed-off-by: Fredrik Appelros <fredrik.appelros@sinch.com>
}

dest, err := os.OpenFile(filepath.Join(path, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
dest, err := os.CreateTemp(path, ".bundle.tar.gz.*.tmp")
Copy link
Contributor

Choose a reason for hiding this comment

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

f, err := OpenFile(name, O_RDWR|O_CREATE|O_EXCL, 0600)

this is what's called under the hood, so it's changed a bit -- but I think that's OK.

@srenatus srenatus merged commit 2eb5744 into open-policy-agent:main Jul 12, 2022
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.

Allow bundle persistence in cloud native environments
3 participants