Sub topic 14
Handling credentials safely
This chapter assumes you can already connect with SQL Developer and SQLcl, and that you know the Easy Connect, TNS alias, and saved-connection forms from the earlier chapters. Here you will learn where a password can leak, which storage options are safer, and how to run SQLcl in scripts without a password in the script text.
Where credentials leak
A password is exposed wherever it is written down or passed along in plain form. For Oracle tools, the usual places are:
- The command line. On Linux and macOS, other users on the same host can often see running commands with
ps. A line such assql hr/MyPassword@orclpdbexposes the password for as long as the process runs. - Shell history. The shell stores every command you type, including the password. The history file persists after you log out.
- Script files. A
connect hr/MyPassword@orclpdbline inreport.sqlends up in version control, backups, and chat messages. - Logs and spool output. If you run a script with
set echo on, theconnectline is echoed to the screen and into anyspoolfile. - Saved connection files. SQL Developer and SQLcl can store passwords on disk. This is convenient, but you must protect those files.
- Screenshots and screen sharing. The connection dialog's password field is masked, but a script or terminal is not.
flowchart LRThe diagram below ranks the options this chapter covers, from most to least exposed.
Prefer prompts over typed passwords
The simplest improvement is to leave the password out of the command. If you give SQLcl a user name without a password, it asks for the password and does not echo it.
sql hr@//dbhost.example.com:1521/orclpdb1.example.comThe password prompt appears after the command starts, so the password never enters the process list or the shell history. The same holds for the /nolog form shown earlier in this tutorial: start sql /nolog, then run connect hr@... and answer the prompt.
SQL Developer behaves the same way. If you leave Save Password unchecked in the connection dialog, it asks for the password the first time you open the connection in each session.
Saved credentials in SQL Developer and SQLcl
Both tools can store a password so you do not retype it. Treat this as a trade-off between convenience and exposure.
- SQL Developer. With Save Password checked, the password is stored, encrypted, in your user profile's SQL Developer settings. SQL Developer also offers a master password in its Preferences (Security section; the exact menu path varies by version). With a master password set, you enter it once per session to unlock all stored passwords.
- SQLcl. The
-savepwdoption onconnect -savestores the password in SQLcl's connection store, which lives under a hidden directory in your home directory. Without-savepwd, the connection is saved but you are prompted for the password each time.
SQL> connect -save orclpdb_hr hr@//dbhost.example.com:1521/orclpdb1.example.com
SQL> connect -name orclpdb_hrThe first line saves the connection without the password. The second prompts you, then connects.
Secure external password store
When a scheduled job or script must connect without a person present, the standard Oracle answer is a secure external password store. This is an Oracle wallet file that holds credentials against a connect string. A script then connects with a slash and the alias instead of a user name and password.
The wallet is created with Oracle's mkstore utility, which ships with the Oracle Client and Database software (not with SQLcl). Create a wallet and add a credential:
mkstore -wrl /opt/app/wallet -create
mkstore -wrl /opt/app/wallet -createCredential orclpdb hrmkstore prompts for the wallet password, and then for the password of the hr account. It never takes the account password on the command line in this form. The alias orclpdb must match the alias in your tnsnames.ora.
Next, tell the client where the wallet is. In sqlnet.ora in the same directory as tnsnames.ora:
WALLET_LOCATION =
(SOURCE = (METHOD = FILE)
(METHOD_DATA = (DIRECTORY = /opt/app/wallet)))
SQLNET.WALLET_OVERRIDE = TRUEThen connect with a slash in place of the user name and password:
sql /@orclpdbWhether SQLcl can use the wallet this way depends on the version and on how it finds TNS_ADMIN. Check this against your SQLcl release before you rely on it. Set TNS_ADMIN to the directory that holds tnsnames.ora and sqlnet.ora, then test interactively before moving the connection into a script.
Cloud wallets work differently. The zip file you download from Autonomous Database, used with set cloudconfig, contains connection details and certificates. Its password is for the keystore. Store it with the same care, and do not share it by email.
Scripts without passwords inside them
If you cannot use a wallet, you can still keep the password out of the script text. SQLcl supports the SQL*Plus accept command with hide, which prompts for a value without echoing it.
set verify off
accept db_pwd char prompt 'Password for HR: ' hide
connect hr/&db_pwd@orclpdb
select sys_context('USERENV', 'SESSION_USER') as session_user from dual;This approach needs a person at the keyboard, so it fits ad hoc maintenance scripts, not cron jobs. It also has limits: set verify off stops the substitution from being echoed for SQL statements, but you should still avoid set echo on in scripts that contain connect lines, and be careful where you spool.
For unattended runs, use the wallet approach from the previous section and keep the script free of any credential:
sql -S -L /@orclpdb @report.sqlHere -S is silent mode and -L limits the login to a single attempt, so a bad credential fails fast and does not wait for input. The script report.sql then contains only queries.
Reduce the damage if a credential leaks
Careful storage matters, but so does limiting what a leaked credential can do.
- Use dedicated accounts. Give each script or application its own database user, with only the privileges it needs. Never run reports as
SYSorSYSTEM. - Use separate accounts per environment. A development password should not work in production.
- Rotate. Change passwords on a schedule and whenever someone with access leaves. In SQLcl, the
passwordcommand changes your own password without typing it on a command line. - Do not reuse passwords across accounts or with other systems.
- Watch for lockouts. Many failed attempts from a stale saved password can lock the account with ORA-28000. If an automated job holds an old password, it can lock the account for everyone. Update stored copies when you change the password.
- Keep privileged connections out of saved lists. For SYSDBA connections, type the password every time, and use operating system authentication where it is available.
A short checklist
| Situation | Preferred approach |
|---|---|
| Interactive work on your own laptop | Prompt for the password, or save it and set a master password in SQL Developer |
| Shared or jump-host workstation | Never save passwords; use prompts |
| Ad hoc script run by a person | `accept ... hide`, or connect first and then run the script |
| Scheduled job or service | Secure external password store, dedicated least-privilege account |
| Cloud database | Wallet zip with restricted permissions, separate user per application |
| Shared code repository | No credentials in files; commit only `tnsnames.ora` entries without secrets |
Exercise
You have a nightly cron job that runs sql -S hr/Welcome1@orclpdb @report.sql. Name two places this exposes the password, and describe how you would change the job so no password appears in the command or the script.
Show answer
The password is visible to other users in the process list (ps) while the job runs, and it is stored in plain text in the crontab entry, which often also ends up in backups and shell history. To fix it, create a secure external password store with mkstore -wrl <dir> -create and mkstore -wrl <dir> -createCredential orclpdb hr, point sqlnet.ora at the wallet with WALLET_LOCATION and SQLNET.WALLET_OVERRIDE = TRUE, set TNS_ADMIN, and run sql -S -L /@orclpdb @report.sql. Restrict the wallet directory to the job's operating system account, and use a dedicated least-privilege database user.
Exercise
A colleague saved a SQLcl connection with connect -save prod_hr -savepwd hr@//proddb:1521/prodpdb. What does -savepwd change, and what is the risk on a shared machine?
Show answer
-savepwd stores the password, encrypted, in SQLcl's connection store, so connect -name prod_hr connects without a prompt. On a shared machine, anyone who can read or use that user profile can connect to production as that user. Saving without -savepwd keeps the connection definition but makes SQLcl prompt for the password each time.