Category: Automation

Shell scripting, configuration management and repeatable operations.

  • Test an nginx configuration gate without touching production

    AI-assisted article: explanatory prose generated locally; executable commands supplied by a reviewed recipe and tested separately.

    This recipe tests an nginx configuration gate in isolation, verifying syntax and file access without altering production. It uses the nginx binary to validate configurations via the -t switch, ensuring only valid directives and referenced files are accepted. All steps run in a temporary directory, preserving system integrity and avoiding live server changes.

    Prerequisites

    • Bash, GNU coreutils and nginx supporting the -e switch must already be installed. Run as an unprivileged user, with a writable TMPDIR or home directory. Execute all blocks in order in the same Bash session. No installation, live configuration edits, server startup or reload is performed.

    Create a standalone candidate configuration

    Creates a standalone nginx configuration in a temporary directory. Sets up basic directives like error logging, event blocks, and a server block. The configuration is minimal and valid, ensuring a baseline for testing.

    set -euo pipefail
    DEMO_DIR=$(mktemp -d "${TMPDIR:-$HOME}/dav3-nginx.XXXXXX")
    printf '%s\n' \
      'pid nginx.pid;' \
      'error_log stderr;' \
      'events {}' \
      'http {' \
      '  access_log off;' \
      '  client_body_temp_path client_temp;' \
      '  proxy_temp_path proxy_temp;' \
      '  fastcgi_temp_path fastcgi_temp;' \
      '  uwsgi_temp_path uwsgi_temp;' \
      '  scgi_temp_path scgi_temp;' \
      '  server { listen 127.0.0.1:8080; return 200 "ok"; }' \
      '}' > "$DEMO_DIR/nginx.conf"
    nginx -v
    

    Require the baseline configuration to pass

    Runs nginx -t to validate the configuration. If syntax is correct and files are accessible, it passes; otherwise, it fails and exits. This step ensures the configuration meets nginx's syntax requirements before proceeding.

    if nginx -t -e stderr -p "$DEMO_DIR/" -c "$DEMO_DIR/nginx.conf"; then
        printf '%s\n' 'PASS: valid configuration accepted'
    else
        printf '%s\n' 'ERROR: baseline configuration rejected' >&2
        exit 1
    fi
    

    Prove an unknown directive is rejected

    Introduces a non-existent directive ('this_is_not_a_directive') and tests if nginx rejects it. If accepted, the test fails; if rejected, it confirms nginx correctly validates directive validity.

    cp -- "$DEMO_DIR/nginx.conf" "$DEMO_DIR/invalid.conf"
    printf '%s\n' 'this_is_not_a_directive on;' >> "$DEMO_DIR/invalid.conf"
    if nginx -t -e stderr -p "$DEMO_DIR/" -c "$DEMO_DIR/invalid.conf"; then
        printf '%s\n' 'ERROR: invalid directive was accepted' >&2
        exit 1
    else
        printf '%s\n' 'PASS: invalid directive rejected'
    fi
    

    Prove a missing include is rejected

    Adds a missing include directive to a configuration. If nginx accepts it, the test fails; if it rejects it, it confirms nginx properly checks for referenced files that do not exist.

    cp -- "$DEMO_DIR/nginx.conf" "$DEMO_DIR/missing-include.conf"
    printf '%s\n' 'include definitely-missing.conf;' >> "$DEMO_DIR/missing-include.conf"
    if nginx -t -e stderr -p "$DEMO_DIR/" -c "$DEMO_DIR/missing-include.conf"; then
        printf '%s\n' 'ERROR: missing include was accepted' >&2
        exit 1
    else
        printf '%s\n' 'PASS: missing include rejected'
    fi
    printf 'Demo directory retained: %s\n' "$DEMO_DIR"
    

    Limitations

    • This demonstration checks configuration parsing and referenced-file access, not HTTP responses, upstream reachability, TLS behaviour or a production reload.
    • The deliberately broken cases must fail; the complete demo succeeds only when those failures are correctly detected. No broken configuration is activated.
    • A real change process must validate the intended production configuration with the correct nginx binary, modules and operating context. Do not validate this demo file and then reload an unrelated production configuration.
    • This is an isolated userspace example. It does not demonstrate CloudLinux, AlmaLinux, Plesk, DirectAdmin or cPanel compatibility.

    Pinned sources

    Technical validation record

    Four runs passed. The normal run checks the valid candidate and both invalid candidates. The other runs deliberately break one result to verify that the demo stops with exit code 1. This tests the gate, not a production reload or HTTP serving.

    Case Actual exit Expected exit Result
    normal 0 0 PASS
    baseline-rejected 1 1 PASS
    invalid-directive-accepted 1 1 PASS
    missing-include-accepted 1 1 PASS

    Tested at 2026-09-29T20:50:21.456063+00:00 in an offline, non-root, resource-limited container using nginx@sha256:b972f831f200b19ef0767938224f9711e74cd783718738cd7405d5cabf75c442. No host directories were mounted. Source SHA-256: 42072286df2d324ddf3c6c0fb85eb2ea8f949072f5cea5c6cdeff12f3afbc273. Script SHA-256: 3779351fb6e8c77c3c306cae01a006e177cfd8bea899c886ba2000d7af38d33a.

  • Stop a deployment when artifact checksums fail

    A release copied to another server can be incomplete or differ from the files you intended to deploy. A checksum gate lets your deployment process stop rather than proceed with that bundle. This short exercise demonstrates the gate and checks its failure paths without deploying anything.

    Prerequisites: Bash and GNU coreutils already installed. Use a regular, unprivileged account. Run every block below in order in the same Bash session. Everything happens inside a new demo directory; no installation or network access is needed.

    1. Create a disposable artifact bundle

    Start with two tiny files, including a filename containing a space. The temporary directory is created under TMPDIR when set, otherwise under your home directory. Quoting keeps the filename intact.

    set -euo pipefail
    DEMO_DIR=$(mktemp -d "${TMPDIR:-$HOME}/dav3-demo.XXXXXX")
    cd "$DEMO_DIR"
    printf '%s\n' 'app config' > 'app config.txt'
    printf '%s\n' 'secondary file' > 'secondary.txt'
    

    2. Generate the manifest from known-good files

    Name each artifact explicitly. This avoids accidentally hashing the manifest itself or including unrelated files through a wildcard. In a real pipeline, generate this manifest at the trusted build stage—not after receiving files you suspect are damaged.

    sha256sum -- 'app config.txt' 'secondary.txt' > SHA256SUMS
    

    3. Make successful verification a prerequisite

    Run verification from the bundle directory because the manifest contains relative paths. A mismatch or unreadable listed file produces a nonzero exit status; strict mode also rejects malformed checksum lines.[1]

    if ! sha256sum --check --strict SHA256SUMS; then
        printf '%s\n' 'Verification failed: deployment aborted' >&2
        exit 1
    fi
    printf '%s\n' 'PASS: original artifacts verified'
    

    In your deployment script, place the equivalent check immediately before the deployment operation. Do not suppress errors or enable ignore-missing behavior. This demo deliberately does not include a deployment command.

    4. Prove that changed content is rejected

    The following check expects failure. An unexpected success is itself a test failure and terminates the demo with exit status 1.

    printf '%s\n' 'modified content' > 'app config.txt'
    if sha256sum --check --strict SHA256SUMS; then
        printf '%s\n' 'ERROR: changed content was accepted' >&2
        exit 1
    else
        printf '%s\n' 'PASS: changed content rejected'
    fi
    

    5. Prove that a missing artifact is rejected

    Restore the first file exactly, then remove the second demo file. Do not regenerate the manifest: doing so would change the baseline you are checking against.

    printf '%s\n' 'app config' > 'app config.txt'
    rm -- 'secondary.txt'
    if sha256sum --check --strict SHA256SUMS; then
        printf '%s\n' 'ERROR: missing artifact was accepted' >&2
        exit 1
    else
        printf '%s\n' 'PASS: missing artifact rejected'
    fi
    printf 'Demo directory retained: %s\n' "$DEMO_DIR"
    

    Checksum failure messages during steps 4 and 5 are expected. The complete demo exits successfully only when those expected failures are handled correctly; it is a test, not a script that deploys the intentionally damaged bundle. Your real deployment gate is step 3.

    Limitations

    Checksums compare content, not provenance. An attacker able to replace both payload and manifest can bypass this check. Unlisted files and file permissions are not verified, and verification does not prevent changes afterwards. Protect the manifest and keep the verified release immutable through deployment. This userspace exercise is not a full server, kernel, or deployment-pipeline test.

    Sources

    [1] GNU coreutils checksum options

    Technical validation record

    AI-assisted draft: the initial text came from a local Qwen3 4B Instruct model. An AI assistant corrected the commands and edited the explanation. This is not an unedited local-model result.

    Six execution and fault-injection runs passed on the two pinned container images listed below. Tests ran without network access, as a non-root user, with a read-only root filesystem and CPU/memory limits. Exit code 1 is the expected result for the deliberately broken verification cases, not a failing test.

    Environment Case Actual exit Expected exit Result
    ubuntu Normal run: valid, changed and missing files 0 0 PASS
    ubuntu Fault injection: missed tampering must stop the demo 1 1 PASS
    ubuntu Fault injection: missed missing-file check must stop the demo 1 1 PASS
    almalinux Normal run: valid, changed and missing files 0 0 PASS
    almalinux Fault injection: missed tampering must stop the demo 1 1 PASS
    almalinux Fault injection: missed missing-file check must stop the demo 1 1 PASS

    Test execution time (UTC): 2026-09-27T12:28:51.399277+00:00. Source Markdown SHA-256: aca155c66921f0faec843f6f6f78e1fcda4259a72bd80ea14ce886daacaaa406. Executed script SHA-256: b34fc790415db245b0ef6207e8bf49ae21c831857bc3009be9293bb59e027292.

    Container image: almalinux@sha256:3a3fa7f043b142bc8008c8b308d39b47d2c84008addcd52f9f9a7a82d2a90474

    Container image: ubuntu@sha256:008173c23f95b170204355c12626cb5a965d779a7e1283b09e9cffbb1bf33ca3

    Scope: container userspace only, not a full server, kernel or deployment-pipeline validation. No CentOS or CloudLinux testing is claimed. These results apply to the exact source version and commands recorded above; changes to the commands require new tests.