98 lines
3.3 KiB
Python
98 lines
3.3 KiB
Python
import logging
|
|
import os
|
|
from datetime import datetime, timezone
|
|
|
|
from bitwarden_sdk import BitwardenClient, DeviceType, client_settings_from_dict
|
|
|
|
|
|
class bitwardenUtils():
|
|
#documentation
|
|
#https://github.com/external-secrets/bitwarden-sdk-server
|
|
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 verbose(self):
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
|
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 self.client.generators().generate() # using default params
|
|
|
|
def create_secret(self,project_id,secret_name,secret_value,description="description"):
|
|
secret = self.client.secrets().create(
|
|
self.organization_id,
|
|
secret_name,
|
|
secret_value,
|
|
description,
|
|
[project_id],
|
|
)
|
|
return secret.data.id
|
|
|
|
|
|
def read_value_secret(self,secret_id):
|
|
return self.client.secrets().get_by_ids([secret_id]).data.data[0].value
|
|
|
|
def read_value_secrets(self,list_secret_id):
|
|
if not(isinstance(list_secret_id, list)):
|
|
print("this is not a list")
|
|
return 1
|
|
list_secret_value = []
|
|
for secret_value in self.client.secrets().get_by_ids(list_secret_id).data.data:
|
|
list_secret_value.append(secret_value.value)
|
|
return list_secret_value
|
|
|
|
def delete_secrets(self,list_secret_id):
|
|
if not(isinstance(list_secret_id, list)):
|
|
print("this is not a list")
|
|
return 1
|
|
try:
|
|
self.client.secrets().delete(list_secret_id)
|
|
status=0
|
|
except:
|
|
print("Cannot delete the secrets")
|
|
status=1
|
|
return status
|
|
|
|
def delete_projects(self,list_project_id):
|
|
if not(isinstance(list_project_id, list)):
|
|
print("this is not a list")
|
|
return 1
|
|
try:
|
|
self.client.projects().delete(list_project_id)
|
|
status=0
|
|
except:
|
|
print("Cannot delete the project")
|
|
status=1
|
|
return status
|
|
|
|
|