jdhalbert / bitwarden_secrets_manager_python

Python wrapper for Bitwarden Secrets Manager (BWS) CLI
MIT License
5 stars 2 forks source link

A thank you for the project and a request for help with initialization utilizing Django and Docker #2

Open catbyte-io opened 1 month ago

catbyte-io commented 1 month ago

Hello,

First, thank you so much for making and maintaining this amazing project. There are so few tools for secrets management, and this one does exactly what I am looking for.

I was wondering if you would have an idea on an issue I am experiencing with getting the project initialized. Any help you can provide is greatly appreciated. I have carefully read the documentation and have just not been able to figure it out for months. I am able to access my project secrets using just the BWS CLI from the command line. However, when I try to set up my project using the python wrapper I receive an error. I am certain I am just doing something that is not quite right, or perhaps it is not compatible with how I am trying to use it.

It seems as if it is having trouble accessing the project id. I am using the wrapper in a Django project and building with Docker. The following is how I am attempting to initialize the BWS wrapper in my settings.py:

from pathlib import Path
from bitwarden_secrets_manager_python import BWS

import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/

# Set up Bitwarden Secrets

access_token = os.environ.get("bws_access_token")
cat_bws = BWS(project_name='Project Name', bws_access_token=access_token, bws_path='/usr/local/bin/bws')

I have BWS_ACCESS_TOKEN set in my environment and I have alternatively tried hard coding the token into the code above with the same results.

Here is the error:

catbyte_web          | Traceback (most recent call last):
catbyte_web          |   File "/usr/src/app/catbyte/manage.py", line 22, in <module>
catbyte_web          |     main()
catbyte_web          |   File "/usr/src/app/catbyte/manage.py", line 18, in main
catbyte_web          |     execute_from_command_line(sys.argv)
catbyte_web          |   File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
catbyte_web          |     utility.execute()
catbyte_web          |   File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 382, in execute
catbyte_web          |     settings.INSTALLED_APPS
catbyte_web          |   File "/usr/local/lib/python3.12/site-packages/django/conf/__init__.py", line 89, in __getattr__
catbyte_web          |     self._setup(name)
catbyte_web          |   File "/usr/local/lib/python3.12/site-packages/django/conf/__init__.py", line 76, in _setup
catbyte_web          |     self._wrapped = Settings(settings_module)
catbyte_web          |                     ^^^^^^^^^^^^^^^^^^^^^^^^^
catbyte_web          |   File "/usr/local/lib/python3.12/site-packages/django/conf/__init__.py", line 190, in __init__
catbyte_web          |     mod = importlib.import_module(self.SETTINGS_MODULE)
catbyte_web          |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
catbyte_web          |   File "/usr/local/lib/python3.12/importlib/__init__.py", line 90, in import_module
catbyte_web          |     return _bootstrap._gcd_import(name[level:], package, level)
catbyte_web          |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
catbyte_web          |   File "<frozen importlib._bootstrap>", line 1381, in _gcd_import
catbyte_web          |   File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
catbyte_web          |   File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
catbyte_web          |   File "<frozen importlib._bootstrap>", line 929, in _load_unlocked
catbyte_web          |   File "<frozen importlib._bootstrap_external>", line 994, in exec_module
catbyte_web          |   File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
catbyte_web          |   File "/usr/src/app/catbyte/catbyte/settings.py", line 28, in <module>
catbyte_web          |     cat_bws = BWS(project_name='Project Name', bws_access_token='mytokenhere', bws_path='/usr/local/bin/bws')
catbyte_web          |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
catbyte_web          |   File "/usr/local/lib/python3.12/site-packages/bitwarden_secrets_manager_python/bws.py", line 40, in __init__
catbyte_web          |     self.PROJECT_ID = self._get_project_id()
catbyte_web          |                       ^^^^^^^^^^^^^^^^^^^^^^
catbyte_web          |   File "/usr/local/lib/python3.12/site-packages/bitwarden_secrets_manager_python/bws.py", line 106, in _get_project_id
catbyte_web          |     projects: list[dict] = self.call_and_return_text(
catbyte_web          |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
catbyte_web          |   File "/usr/local/lib/python3.12/site-packages/bitwarden_secrets_manager_python/bws.py", line 193, in call_and_return_text
catbyte_web          |     return json.loads(text) if as_json else text
catbyte_web          |            ^^^^^^^^^^^^^^^^
catbyte_web          |   File "/usr/local/lib/python3.12/json/__init__.py", line 346, in loads
catbyte_web          |     return _default_decoder.decode(s)
catbyte_web          |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
catbyte_web          |   File "/usr/local/lib/python3.12/json/decoder.py", line 337, in decode
catbyte_web          |     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
catbyte_web          |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
catbyte_web          |   File "/usr/local/lib/python3.12/json/decoder.py", line 355, in raw_decode
catbyte_web          |     raise JSONDecodeError("Expecting value", s, err.value) from None
catbyte_web          | json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Thank you again so much for your time and hard work. Please let me know what other information I can provide that is helpful. Best regards, Catbyte

jdhalbert commented 1 month ago

Hi Catbyte,

Thanks for your kind message.

Can you run the following in your terminal and let me know the output?

/your/path/to/bws project list -c no -t your_access_token

catbyte-io commented 1 week ago

Thank you for your patience. I am currently attempting to fix another environment issue that came up that is preventing me from providing this output. I am working on this as I can so that I can get this to you as soon as possible.