DevSecOps
Find and remove secrets, tighten permissions and sudo, harden SSH and Dockerfiles, and check certificates, checksums and signatures.
Secrets in git
git grep -nIiE 'password|secret|api_key|token'Search the tracked files for likely secrets
git log -p -S DB_PASSWORDEvery commit that added or removed a string, with its diff
git grep DB_PASSWORD $(git rev-list --all)Search every commit, not just the current one
git rm --cached .env && echo .env >> .gitignoreStop tracking a file but keep it on disk
git check-ignore -v .envWhich ignore rule matches a file
FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --index-filter 'git rm -q --cached --ignore-unmatch .env' -- --allRemove a file from every commit
git update-ref -d refs/original/refs/heads/mainDelete the backup ref a rewrite leaves behind
git reflog expire --expire=now --all && git gc --prune=nowForget old commits and drop their objects
git diff --cached -U0 | grep -iE '^\+.*(password|secret|api_key)'Test the staged changes for secrets (the core of a pre-commit hook)
Permissions and accounts
find . -perm -o=wFiles and folders anyone can write to
chmod -R o-w appTake write access away from others, keep the rest
chmod 600 secrets.env | chmod 750 configOwner-only file; folder closed to others
stat -c '%a %U:%G %n' fileMode, owner and group of a file
sudo useradd --system --shell /usr/sbin/nologin webappA service account nobody can log in as
sudo chown -R webapp:webapp app/logsHand a folder and its contents to an account
getent passwd webapp | id -nG deployLook up an account and its groups
sudo rules
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart webappOne command, as root, for one user
echo 'RULE' | sudo tee /etc/sudoers.d/deployWrite a root-owned rule file
sudo chmod 440 /etc/sudoers.d/deployThe mode sudo expects for a rule file
sudo visudo -cf /etc/sudoers.d/deployCheck a rule file's syntax before you log out
sudo -l -U deployWhat a user may run with sudo
SSH server
sshd uses the first value of each keyword; lines after Match apply only to matching connections.
PermitRootLogin noNobody logs in as root
PasswordAuthentication noKeys only (also KbdInteractiveAuthentication no, PermitEmptyPasswords no)
AllowUsers deploy opsOnly these accounts may log in
MaxAuthTries 3 | LoginGraceTime 30Fewer guesses, less time to authenticate
X11Forwarding no | AllowAgentForwarding noTurn off forwarding nobody uses
LogLevel VERBOSELog the fingerprint of the key used for each login
ClientAliveInterval 300 | ClientAliveCountMax 2Close idle sessions
sudo sshd -t | sudo sshd -TTest the config; print the values the server really uses
Certificates
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out ca.keyAn EC private key
openssl req -x509 -new -key ca.key -subj "/CN=Lab Root CA" -days 365 -out ca.crtA self-signed CA certificate
openssl req -new -key api.key -subj "/CN=api.internal.test" -addext "subjectAltName=DNS:api.internal.test,IP:10.0.0.5" -out api.csrA signing request with DNS and IP names
openssl x509 -req -in api.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 90 -copy_extensions copy -out api.crtSign a request with your CA
openssl verify -CAfile ca.crt api.crtCheck a certificate's chain
openssl x509 -in api.crt -noout -subject -issuer -datesWho it is for, who signed it, and when it expires
openssl x509 -in api.crt -noout -ext subjectAltNameThe names a client will accept
openssl x509 -checkend 2592000 -noout -in api.crtExit status 1 if it expires within 30 days
Dockerfiles
FROM python:3.12-slim@sha256:<digest>Pin the base image by digest
RUN apt-get update && apt-get install -y --no-install-recommends libpq5 && rm -rf /var/lib/apt/lists/*Install only what runs, and clean up in the same layer
RUN pip install --no-cache-dir -r requirements.txtNo package cache left in the image
RUN useradd --system --uid 10001 appCreate an unprivileged user
USER appRun everything after this line as that user
printf '.env\n.git\n' > .dockerignoreKeep local secrets out of the build context
RUN --mount=type=secret,id=token cat /run/secrets/tokenUse a secret during one build step without storing it
Checksums and signatures
sha256sum file > SHA256SUMSWrite a checksum list
sha256sum -c SHA256SUMSCheck every file in a list (OK or FAILED)
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out signing.keyA signing key
openssl pkey -in signing.key -pubout -out signing.pubIts public half, to publish
openssl dgst -sha256 -sign signing.key -out SHA256SUMS.sig SHA256SUMSSign a file
openssl dgst -sha256 -verify signing.pub -signature SHA256SUMS.sig SHA256SUMSVerify a signature (Verified OK)
Nothing matches that search.
Cheatsheet for the DevSecOps path · Deboistech · All cheatsheets
