🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Data Management / Database Administration › Bash Automation for Database Administration › Lesson 7
TEC-DATA86-09 · Lesson 7 · 12 clock hours · practice

Secure Credential Management and Script Hardening

Objective. Students will apply layered credential-management techniques—environment files, .pgpass, Oracle wallets, restricted file permissions, and secrets scanning—to eliminate plaintext passwords from all scripts in their automation library.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Why Plaintext Passwords Are a Critical Risk

Every time a password appears in a shell script, it is readable by anyone who can view that file, inspect process environment variables, or read shell history logs. In a database environment, a compromised credential can expose entire schemas, customer records, and backups. Professional database administrators treat credential exposure as a high-severity vulnerability—not merely bad style.

This lesson applies layered controls to eliminate plaintext passwords from your Bash automation library. The same defense-in-depth philosophy used in physical security—never relying on a single lock—applies here: no single technique is foolproof, so you combine several.

Layer 1: Environment Files with Restricted Permissions

A `.env` file stores key-value pairs such as `DB_PASS=secret` outside of the script itself. The script sources the file at runtime with `source /path/to/.env` or `. /path/to/.env`. The critical step is setting the file's permissions so only the owning user can read or write it:

```bash

chmod 0600 ~/.env

```

Mode `0600` means owner read/write, no access for group or others. Store the `.env` file outside version-control directories and add it to `.gitignore`. Never commit it to a repository.

Always validate that the file exists and is not world-readable at the top of every script that sources it:

```bash

[ "$(stat -c '%a' ~/.env)" = "600" ] || { echo "Bad .env permissions"; exit 1; }

. ~/.env

```

Layer 2: PostgreSQL `.pgpass` and `scram-sha-256`

PostgreSQL's `.pgpass` file (`~/.pgpass` on Linux) stores connection credentials in the format `hostname:port:database:username:password`. The `libpq` library reads this file automatically when no password is supplied on the command line. Required permissions:

```bash

chmod 0600 ~/.pgpass

```

If permissions are wider than `0600`, PostgreSQL ignores the file entirely.

For stronger authentication, configure `pg_hba.conf` to require `scram-sha-256` instead of `md5` or `trust`:

```

host all dbauser 127.0.0.1/32 scram-sha-256

```

For certificate-based authentication (the strongest client-auth method), use the `cert` method in `pg_hba.conf` and supply a client certificate signed by the server's CA. No password travels over the network at all. Your backup script can then call `pg_dump` without any credential flag—the certificate and private key handle identity.

Layer 3: Oracle Wallets

Oracle Database uses an encrypted wallet (managed with `mkstore` or the ORAPKI utility) to store connection credentials. The `sqlplus /@TNS_ALIAS` syntax instructs SQLPlus to retrieve the password from the wallet rather than accepting it as an argument. The wallet directory must be protected:

```bash

chmod 0700 /path/to/wallet

```

Oracle's wallet approach aligns directly with the Oracle DBA CA exam's security domain, which expects candidates to know how to avoid embedding passwords in scripts and connection strings.

Layer 4: Secrets Scanning

Before committing or deploying any script, run a grep scan across your automation library:

```bash

grep -rn 'password\|PASSWORD\|PGPASSWORD' ./scripts/

```

A clean library returns zero hits. This is a mandatory gate in this lesson: if the scan finds anything, refactor before moving on. In professional pipelines this scan is automated—tools such as `git-secrets` or `truffleHog` run at commit time. For this course, the manual `grep` scan achieves the same learning goal.

Script Hardening Practices

Beyond credential management, harden every script with these header directives:

Physical and Digital Security: A Parallel

OSHA's Lockout/Tagout (LOTO) standard (29 CFR 1910.147) requires workers to isolate and de-energize equipment before servicing it, so that no one else can accidentally re-energize it during the work. The database equivalent is least-privilege service accounts: a backup service account should have `SELECT` and `pg_dump` rights only—not `SUPERUSER`. If that account is compromised, the blast radius is limited, just as LOTO limits injury risk to the worker performing maintenance.

Additional physical controls that complement digital script security include:

Completing the OSHA security checklist in this lesson connects these principles explicitly so you can explain digital security decisions in physical-safety language—a skill valued in team environments where not everyone has a technical background.

Connecting to Exam Objectives

Key terms10

.pgpassA PostgreSQL client file (~/.pgpass) that stores hostname, port, database, username, and password so libpq can authenticate without a password on the command line; must have permissions 0600 or PostgreSQL ignores it.
scram-sha-256A challenge-response authentication mechanism for PostgreSQL that hashes credentials during the handshake, providing stronger protection than the older md5 method.
pg_hba.confThe PostgreSQL host-based authentication configuration file that controls which users can connect from which hosts using which authentication methods.
Oracle WalletAn encrypted file-based store used by Oracle Database to hold credentials and certificates, enabling scripts to authenticate with /@TNS_ALIAS without embedding passwords in code.
Least PrivilegeA security principle requiring that an account or process be granted only the minimum permissions needed to perform its specific function, limiting damage if the account is compromised.
chmod 0600A Linux file permission mode that grants read and write access to the owner only, with no permissions for the group or others; required for sensitive files such as .pgpass and .env.
set -euo pipefailA Bash directive placed at the top of scripts that causes the shell to exit on any error (-e), treat unset variables as errors (-u), and catch failures in pipelines (-o pipefail).
Secrets ScanningThe practice of searching scripts and code repositories for patterns that suggest embedded credentials, such as keywords like PASSWORD or PGPASSWORD, to prevent accidental credential exposure.
Certificate AuthenticationA method of proving identity using a cryptographic certificate and private key rather than a password; in PostgreSQL it is configured with the cert method in pg_hba.conf and requires OpenSSL-generated client certificates.
Lockout/Tagout (LOTO)An OSHA-required safety procedure (29 CFR 1910.147) that isolates hazardous energy sources before equipment is serviced; used in this lesson as a physical-security analogy for least-privilege digital controls.
In class: Students audit the five scripts written in previous lessons using a simple grep -rn 'password\\|PASSWORD\\|PGPASSWORD' scan; for any hit, they refactor to load credentials from a sourced .env file with 0600 permissions or from the system keyring, then re-run the scan to confirm zero hits. · Students configure a PostgreSQL scram-sha-256 connection using certificate authentication on the VM, update their backup script to use the cert, and document the pg_hba.conf changes—connecting Oracle DBA CA and DP-300 security exam objectives to the hands-on task. · Students complete an OSHA-aligned security checklist (covering physical server access controls, screen-lock policy, and least-privilege account practices for service accounts) and discuss how digital security controls for scripts parallel physical lockout/tagout principles taught in Course 1.
Materials: Linux VM, OpenSSL, .pgpass, pg_hba.conf, .env file template, grep, OSHA 10 lockout/tagout analogy handout, Oracle DBA CA exam guide section on security
Check: grep scan of all five scripts must return zero plaintext-password hits; pg_hba.conf must use scram-sha-256 or cert method; OSHA checklist must be fully completed with no blank items to pass.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

PostgreSQL CE (EDB) — authentication and security
1. A DBA's backup script contains the line `PGPASSWORD=S3cr3t pg_dump -U dba mydb`. Which corrective action best eliminates the plaintext password?
PostgreSQL CE (EDB) — authentication and security
2. After setting permissions on ~/.pgpass with `chmod 0644`, a DBA finds that pg_dump still prompts for a password even though the .pgpass entry is correct. What is the most likely cause?
PostgreSQL CE (EDB) — pg_hba.conf and authentication methods
3. Which pg_hba.conf authentication method provides stronger password security than md5 by using a challenge-response mechanism that avoids transmitting the password in a recoverable form?
Oracle DBA CA — database security
4. An Oracle DBA wants a scheduled shell script to connect to Oracle Database without embedding a password. Which Oracle feature best addresses this requirement?
PostgreSQL CE (EDB) / General DBA practice — secrets scanning
5. A student runs `grep -rn 'password\|PASSWORD\|PGPASSWORD' ./scripts/` and receives zero output. What does this confirm?
Microsoft DP-300 — securing Azure database solutions
6. In Microsoft Azure SQL, which authentication approach most directly parallels certificate-based authentication in PostgreSQL by eliminating stored passwords for a service identity?
DAMA CDMP Associate — data security; OSHA physical security analogy
7. OSHA's Lockout/Tagout standard (29 CFR 1910.147) requires isolating hazardous energy before servicing equipment so no one else can re-energize it. Which database administration practice most directly mirrors this principle?
PostgreSQL CE (EDB) / General DBA practice — script hardening
8. A student adds `set -euo pipefail` to the top of a Bash backup script. Which of the following behaviors does this directive combination produce?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.