brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: macOS keeps script passwords in the keychain with security find-generic-password -w

macOS
~/tips/tip-macos-security-find-generic-password

security add-generic-password -a "$USER" -s myapi -w

Stores a password in the macOS keychain and reads it back in a script, without a plain text file.

A script that needs an API key should not have the key written in it. On a Mac the keychain is right there, and the security command talks to it from the terminal.

Store the secret once:

security add-generic-password -a "$USER" -s myapi -w

-a is the account and -s the service name; both are required. Ending the command with -w and no value makes it prompt for the password, which the man page recommends, so the secret does not end up in your shell history.

Read it back in a script:

API_KEY=$(security find-generic-password -a "$USER" -s myapi -w)

With -w, only the password is printed, nothing else, so it drops straight into a variable.

Changing the value later? The man page says an item cannot already exist when you add it, unless you pass -U to update it. Without a keychain name it goes into your default keychain.

Caveat: there is an -A option that lets any application read the item without a warning, and the man page itself calls that insecure and not recommended. Leave it off. The point of the keychain is that not every program can read your secrets.

Further reading: security man page (Apple, mirrored)

More macOS tips System Settings > Accessibility > Hover Text > When using the pointer A Smart Folder in Notes collects notes by tag, checklist or date Type # in a Mac note to tag it, then filter by tag in the sidebar tmutil isexcluded ~/Projects/app/node_modules Crop an image in Preview by dragging a frame and clicking Crop All macOS tips (99) All topics

Link