Decrpyt Credentials on Python code

Hi,

Please share an simple idea on directly passing the orchestrator DB asset from UiPath to my python code as parameter and Decrypt the DB password on python code to access DB connection.

It will be better for performance to access and get all data you need direct from Orcastrator API using below URL

https://docs.uipath.com/orchestrator/reference/api-references

An samples?

@2022learncode you can find below examples on the Reset Project

also find below video playlist include all details you need :slight_smile:

Here’s a github implementation of using Windows SecureStrings in Python.
Has to be run by the same user that owns the password though.

Hi @2022learncode

Plese follow the below steps in UiPath & python code as well

  1. In UiPath, create a Python script activity and pass the orchestrator DB asset as an input parameter to the Python script.
  2. In the Python script, retrieve the input parameter and store it in a variable.
  3. Use the decrypt function from the cryptography library to decrypt the DB password.
  4. Use a Python library such as pyodbc to connect to the DB using the decrypted password and other necessary connection details.

Sample Code:

import pyodbc
from cryptography.fernet import Fernet

def decrypt(key, ciphertext):
f = Fernet(key)
return f.decrypt(ciphertext)

def main(db_asset):
# Retrieve the password from the DB asset and decrypt it
password = decrypt(db_asset[‘Key’], db_asset[‘Password’])

# Connect to the DB using pyodbc
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};'
                            'SERVER=' + db_asset['Server'] + ';'
                            'DATABASE=' + db_asset['Database'] + ';'
                            'UID=' + db_asset['Username'] + ';'
                            'PWD=' + password)

# Do something with the connection
cursor = connection.cursor()
cursor.execute("SELECT * FROM my_table")
rows = cursor.fetchall()
for row in rows:
    print(row)

Regards,
Kaviyarasu N