The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Set and Retrieve Passwords From Your Systems Password Manager With The keyring Python Module

TODO: Pull subtitle into page object

NOTE: See also this post:

[[/posts/storing-local-environmental-variables-securely-in-password-managers-instead-of-plaintext-env-files--20eonil1bcsz][Storing Local Environmental Variables Securely In Password Managers Instead Of Plaintext env Files]]

### TL;DR

Set passwords form the command line with:

(NOTE: confirmed in march 2022 that `PASSWORD_NAME USER_NAME` is the proper order)

code_start_default_section code_end_default_section

TODO: Look at the below for setting them to see how it goes in and if it's reversed or not

TODO: Add a python command line version of using the module to add passwords

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Set and Retrieve Passwords From Your Systems Password Manager With The keyring Python Module

-- p

NOTE: See also this post:

-- p

[[/posts/storing-local-environmental-variables-securely-in-password-managers-instead-of-plaintext-env-files--20eonil1bcsz][Storing Local Environmental Variables Securely In Password Managers Instead Of Plaintext env Files]]

-- p

### TL;DR

-- p

Set passwords form the command line with:

-- p

(NOTE: confirmed in march 2022 that `PASSWORD_NAME USER_NAME` is the proper order)

-- code/
-- command

python3 -m keyring set PASSWORD_NAME USER_NAME

-- /code

-- p

Running that will ask you to put in the password.

-- p

Then, retrieve them in scripts with:

-- code/
-- python

import keyring 

keyring.get_password(PASSWORD_NAME, USERNAME)

-- /code

-- p

NOTE: Confirmed that this is the order the parameters need to be in to get keys that were added from inside Keychain Access itself

-- p

### Details

-- p

You can store passwords in your system's credential locker (e.g. Keychain Access on macOs and ... on Windows and ... on Linux) with the <<link|keyring|https://pypi.org/project/keyring/>> Python module.

-- p

__Installation__

-- p

Install it with either `pip3` or `pip` depending on your configuration. E.g.

-- code/
-- python

pip3 install keyring

-- /code

-- p

### Setting Passwords From Scripts

-- p

You can set your passwords in a script with:

-- code/
-- python

import keyring

keyring.set_password("SERVICE", "USERNAME", "PASSWORD")

-- /code

-- p

### Setting Passwords From The Command Line

-- p

You can set passwords directly from the command line with:

-- code/
-- python

python3 -m keyring set SERVICE USERNAME

-- /code

-- p

You'll be prompted to enter your password from there

-- p

### Retrieving Passwords

-- p

And retrieve them with:

-- code/
-- python

import keyring 

keyring.get_password("SERVICE", "PASSWORD")

-- /code

-- p

### Notes

-- p

- As far as I can tell "SERVICE" is just for grouping. I simply use my username. 
- I used to add dates to my passwords, but I stopped doing that. (This is more a reminder to myself that to anyone else)

-- p

Note: if you put something directly in Keychain Access,
the values you put in for `name` and `account` are
used in a backwards order from what you'd expect. i.e.

-- code/
-- python

keyring.get_password(NAME, ACCOUNT)

-- /code

-- p

TODO: Look at the below for setting them to see
how it goes in and if it's reversed or not

-- p

TODO: Add a python command line version
of using the module to add passwords


-- categories
-- Security 

-- metadata
-- date: 2021-06-21 12:04:43
-- id: 20eod6rn
-- status: scratch
-- type: post
-- SCRUBBED_NEO: false
-- site: aws