DEV Community

Sergey Shinder
Sergey Shinder

Posted on

We wrote mode 755 in an Ansible task and our manifest script would only run for root

Every evening at half past six a script on our integration server sends the day's parcel manifest to one of our carriers. Without it, their hub has parcels it cannot match to anything. The script runs from cron as the integration user, and in October we moved it into an Ansible role with a template task: owner integration, mode: 755.

The next morning the carrier rang. Four hundred and twelve parcels had arrived at their hub overnight with no manifest and were being held. Cron had run the script on time and it had failed with Permission denied. The error went to the integration user's local mailbox, which nobody reads.

ls -l showed the file as --wxrw--wt. Ansible had done exactly what we asked. In YAML 755 is the integer seven hundred and fifty five, and the file module treats an integer mode as a number, not as octal digits. Seven hundred and fifty five in octal is 1363: the sticky bit, write and execute for the owner, read and write for the group, write and execute for everyone else. The owner could execute the script but not read it, and a shell script has to be read by its interpreter. The kernel started bash and bash could not open the file.

It had been tested. After the deploy an engineer ran the script with sudo, it sent a test manifest, and the change was closed. Root does not need read permission to read a file, and needs only one execute bit anywhere to run it. Our test proved the script worked for the one user it would never run as.

The Ansible documentation warns about this in so many words: give a mode with a leading zero, as 0755, or as a quoted string, or expect unexpected results. ansible-lint has a rule for it, risky-octal, and we now run ansible-lint in CI. Every mode in our roles is a quoted string with a leading zero. Each role that installs a script ends with a check that runs it with --dry-run through sudo -u as the user who will actually run it. Cron output for service users now goes to our log pipeline instead of a mailbox.

Testing something as root tells you it works for root. Permissions are the one thing root cannot test.

– Sergey Shinder

Top comments (0)