docker-openldap icon indicating copy to clipboard operation
docker-openldap copied to clipboard

chowning certificates in startup.sh during BATS tests break CI environements

Open fir4 opened this issue 10 years ago • 4 comments

Hey :) First, thanks for taking the time to design that image! It's very practical and complete.

We've been trying to automate build of our own images based on this one in Bamboo, but stumbled on a problem: upon running the tests, and as a result launching the docker image, startup.sh chowns a bunch of stuff in /container including the certificates directly to openldap user.

When tests are ran from a CI environment, this results in a bunch of files in host filesystems to be chowned to openldap's in-container user, rendering them unable to be subsequently removed by the CI system.

I can see a few ways to solve that problem, but i'm not sure which one is the most appropriate:

  • chmod 644 the certificates at startup rather than 600+chown, and set the host directory 600, effectively making the in-container files 644 without giving access to them on host.
  • copy the certificate files on startup rather than affecting the mapped files directly
  • perform BATS magic to circumvent the problem when running tests, such as ro'ing the files when mapping them to docker

I can propose a PR if you'd be so kind as to give some input on which method you consider best. Regards, fir4

fir4 avatar Nov 16 '16 11:11 fir4

Hello, As i remember Openldap is pretty strict on certificate file permissions and required 600 for the key ?

Methods 2 or 3 seems ok :)

Method 2 can be done by adding --copy-service to the run command. In test_helper.bash:

docker run $@ -d $IMAGE_NAME

becomes

docker run $@ -d $IMAGE_NAME --copy-service

on container start /container/service directory and subdirectories will be copied to /container/run/service and these files will be used in startup script. More info: https://github.com/osixia/docker-light-baseimage/

I did something similar to method 3 in our mariadb image: https://github.com/osixia/docker-mariadb/blob/stable/test/test.bats#L49-L50

No sure wich method is better, i let you test and choose the one that fits your needs. Pull requests are welcomed :)

ghost avatar Nov 16 '16 14:11 ghost

Ugh, i tried both, it just doesn't work with read-only filesystems even with chown --quiet, i'm not sure why exactly... As for --copy-service, it works perfect, but it doesn't actually help much since there's the same problem for config/ and database/, which don't benefit from it.

A complete fix would probably require a full test-mode to be implemented in startup.sh to copy database/config data aswell and prevent them from being affected by modifications. I also noticed that current BATS tests will leave orphaned docker containers every time a test fails, due to the fact it stops at first error.

Might just end up dropping the tests from the pipeline instead, 95% of the problems i've been having are with the test system itself rather than the image.... I'll check back on this tomorrow :|...

fir4 avatar Nov 16 '16 16:11 fir4

For the existing database test something like this should do the trick:

docker run -v $PWD/database:/tmp/database -v $PWD/config:/tmp/config osixia/openldap:1.1.7 --cmd "cp -rf /tmp/database /var/lib/ldap" --cmd "cp -rf /tmp/config /etc/ldap/slapd.d" --copy-service

I'll check if it's possible to add an option to clean orphaned docker containers when a test fails. But sometimes it's useful to keep containers to check logs and debug.

ghost avatar Nov 16 '16 17:11 ghost

Opened PR, here's what i did overall:

  • Use --copy-service to copy certificates and use ../run/..
  • Add test.sh in image to copy data from /container/test/ to correct places
  • Make test.sh be called explicitly by BATS when launching the image

Hopefully this makes everything covered by current tests be copied over, and prevents test data files on host from being modified, chowned or chmodded by the container. Once this is done, running BATS in a CI pipeline seems to work just fine, no more lingering permission issues! :+1:

fir4 avatar Nov 17 '16 15:11 fir4