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

Conditionally set ownership and permissions in entrypoint #110

Merged
merged 3 commits into from
Oct 20, 2018

Conversation

kocolosk
Copy link
Member

Overview

Three significant changes here:

  1. Check ownership/permissions and only execute chown/chmod when necessary
  2. Switch target permissions to those set by CouchDB itself
  3. Drop the -f flag and the silent || true guard on these commands

The last one is perhaps worth discussion. I couldn't find any explanation for why it was added in the first place, and I didn't see it being used on comparable docker-entrypoint files in other projects. I figured that if we feel these commands are important enough to place in the entry point we probably don't want them to be silently ignored.

GitHub issue number

Fixes apache/couchdb-documentation#109

Checklist

  • Code is written and works correctly;
  • Changes are covered by tests;
  • Documentation reflects the changes;

@kocolosk kocolosk requested a review from wohali October 18, 2018 15:38
@wohali
Copy link
Member

wohali commented Oct 18, 2018

@kocolosk see #73 (comment) for apache/couchdb-documentation#3. I think we can't drop this.

Copy link
Member

@wohali wohali left a comment

Choose a reason for hiding this comment

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

Probably can't drop the -f

@kocolosk
Copy link
Member Author

Oh geez we already had this conversation back in apache/couchdb-documentation#80 as well didn't we? I will add the -f back in for the config stuff.

# we need to set the permissions here because docker mounts volumes as root
chown -fR couchdb:couchdb /opt/couchdb || true
# Check that we own everything in /opt/couchdb and fix if necessary
find /opt/couchdb \! \( -user couchdb -group couchdb \) -exec chown couchdb:couchdb '{}' +
Copy link
Member

Choose a reason for hiding this comment

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

Needs a -f

Copy link
Member Author

Choose a reason for hiding this comment

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

So ... I want to test that one a bit. I've already scoped down the chown so it only executes on files that have incorrect ownership. I'm struggling to think of a case where we have files in /opt/couchdb that are not owned by couchdb and yet we're OK to proceed without that ownership change. I guess the best example would be a config file with 0644 permissions owned by root coming in from a volume mount?

Copy link
Member

Choose a reason for hiding this comment

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

Right, the big issues are config and data files owned by the wrong person. We could conceivably skip changing their ownership as long as we have read/write access....

Another option is to have Couch itself complain when it sees config files it can't read/write, skip all the checks here, and let Couch crash out. We don't do that yet.

It'd be harder to do that for data files since generally we don't start opening db/view files until asked to do so by the user.

# contents of the datadir have the same permissions as they had when they
# were initially created. This should minimize any startup delay.
find /opt/couchdb/data -type d ! -perm 0755 -exec chmod 0755 '{}' +
find /opt/couchdb/data -type f ! -perm 0644 -exec chmod 0644 '{}' +
Copy link
Member

Choose a reason for hiding this comment

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

Should do -f for L37-38

@wohali
Copy link
Member

wohali commented Oct 19, 2018

Missed a couple - once those are fixed this is good to go.

Hopefully all the stat(2) calls that find(1) imposes won't continue to aggravate the problem the Hyperledger people saw over in apache/couchdb#4211.

@kocolosk kocolosk force-pushed the streamline-ownership-and-permissions branch from 61e6cd1 to 3af2fa4 Compare October 20, 2018 13:20
@kocolosk
Copy link
Member Author

kocolosk commented Oct 20, 2018

OK after sleeping on it I went ahead and applied the -f flag everywhere and rebased things into a clean set.

I also tried a smoke test on whether this is actually helping by commenting out all of this code in the entry point and then timing the two chown options manually inside a container running on my laptop:

root@002a9518bc6f:/opt/couchdb# time find /opt/couchdb \! \( -user couchdb -group couchdb \) -exec chown -f couchdb:couchdb '{}' +

real    0m0.015s
user    0m0.000s
sys     0m0.000s
root@002a9518bc6f:/opt/couchdb# time chown -R couchdb:couchdb /opt/couchdb

real    0m3.992s
user    0m0.000s
sys     0m0.370s

Seems like a good idea 😄 There are about 1600 files in a vanilla CouchDB install so anyone with lots of (correctly-permissioned) databases would likely see an even bigger improvement.

Recursive modification of ownership and permissions in the entrypoint
has been implicated in slow container startup times. This change checks
the ownership first and only modifies it if necessary. It is modeled
after similar changes recently applied to a number of other projects
e.g. redis/docker-library-redis#166.
Previously we had been doing a blanket recursive chmod to 770 on
everything in the datadir. This had a few problems:

- The files themselves need not have the executable bit set
- CouchDB itself creates directories and files with 755/644
- Executing lots of chmod operations caused startup delays

This patch makes the execution of chmod conditional, and works to set
the permissions to what they would normally be when CouchDB creates the
the files and directories.
This patch also drops the target permissions from 775/664 to 755/644,
as the latter permissions are the ones set by the CouchDB installation
itself.
@kocolosk kocolosk force-pushed the streamline-ownership-and-permissions branch from 3af2fa4 to be3fd23 Compare October 20, 2018 13:30
@wohali
Copy link
Member

wohali commented Oct 20, 2018

+1 and thanks for the test.

@wohali wohali merged commit 262dc35 into master Oct 20, 2018
@wohali wohali deleted the streamline-ownership-and-permissions branch October 20, 2018 17:21
@kocolosk kocolosk mentioned this pull request Feb 6, 2019
3 tasks
@wohali wohali mentioned this pull request Aug 12, 2019
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.

None yet

2 participants