diff --git a/django_code/apps/bitwarden/utils.py b/django_code/apps/bitwarden/utils.py new file mode 100644 index 0000000..27d6856 --- /dev/null +++ b/django_code/apps/bitwarden/utils.py @@ -0,0 +1,113 @@ +import logging +import os +from datetime import datetime, timezone + +from bitwarden_sdk import BitwardenClient, DeviceType, client_settings_from_dict + + +class bitwardenUtils(): + state_path = "/tmp/state_file.txt" + client = BitwardenClient( + client_settings_from_dict( + { + "apiUrl": os.getenv("API_URL", "https://bitwarden.tryton.geeraert.eu/api"), + "deviceType": DeviceType.SDK, + "identityUrl": os.getenv("IDENTITY_URL", "https://bitwarden.tryton.geeraert.eu/identity"), + "userAgent": "Python", + } + ) + ) + + def __init__(self, access_token, organization_id): + self.access_token = access_token + self.organization_id = organization_id + client = BitwardenClient( + client_settings_from_dict( + { + "apiUrl": os.getenv("API_URL", "https://bitwarden.tryton.geeraert.eu/api"), + "deviceType": DeviceType.SDK, + "identityUrl": os.getenv("IDENTITY_URL", "https://bitwarden.tryton.geeraert.eu/identity"), + "userAgent": "Python", + } + ) + ) + self.client.auth().login_access_token(self.access_token, self.state_path) + + def create_project(self,project_name): + project = self.client.projects().create(self.organization_id, project_name) + return project.data.id + + def generate_strong_password(self): + # Note: using the generator does not require authentication with a server + return client.generators().generate() # using default params + + +#documentation +#https://github.com/external-secrets/bitwarden-sdk-server + +#credentials +#organization_id = "" +#access_token = "" +state_path = "/tmp/state_file.txt" + + +# Create the BitwardenClient, which is used to interact with the SDK +client = BitwardenClient( + client_settings_from_dict( + { + "apiUrl": os.getenv("API_URL", "https://bitwarden.tryton.geeraert.eu/api"), + "deviceType": DeviceType.SDK, + "identityUrl": os.getenv("IDENTITY_URL", "https://bitwarden.tryton.geeraert.eu/identity"), + "userAgent": "Python", + } + ) +) + +# Add some logging & set the org id +logging.basicConfig(level=logging.DEBUG) +client.auth().login_access_token(access_token, state_path) + + + +# -- Example Project Commands -- + +def create_project(project_name): + project = client.projects().create(organization_id, project_name) + return project.data.id + +def create_secret(project_id,secret_name,secret_value,description="description"): + secret = client.secrets().create( + organization_id, + secret_name, + secret_value, + description, + [project_id], + ) + return secret.data.id + +def read_value_secret(secret_id): + return client.secrets().get_by_ids([secret_id]).data.data[0].value + +def read_value_secrets(list_secret_id): + list_secret_value = [] + for secret_value in client.secrets().get_by_ids(list_secret_id).data.data: + list_secret_value.append(secret_value.value) + return list_secret_value + +def delete_secrets(list_secret_id): + try: + client.secrets().delete(list_secret_id) + status=0 + except: + print("Cannot delete the secrets") + status=1 + return status + +def delete_projects(list_project_id): + try: + client.projects().delete(list_project_id) + status=0 + except: + print("Cannot delete the project") + status=1 + return status