rsa key encryption TypeError: Object of type bytes is not JSON serializable

69 Views Asked by At

**Im getting below error when encoding rsa.key for python connector, i saw item = json.dumps(dict(item)) online but not sure how i add to my existing script.

  Traceback (most recent call last):
  File "D:/Common_Source/execute_snowflake_command/execute_snowflake_command_test.py", line 180, in <module>
    execute_snowflake_command(root_path,'pinterest_dim_campaign')
  File "D:/Common_Source/execute_snowflake_command/execute_snowflake_command_test.py", line 57, in     execute_snowflake_command
    schema=params['schema']
  File "C:\Python37\lib\site-packages\snowflake\connector\__init__.py", line 52, in Connect
    return SnowflakeConnection(**kwargs)
  File "C:\Python37\lib\site-packages\snowflake\connector\connection.py", line 219, in __init__
    self.connect(**kwargs)
  File "C:\Python37\lib\site-packages\snowflake\connector\connection.py", line 414, in connect
    self.__open_connection()
  File "C:\Python37\lib\site-packages\snowflake\connector\connection.py", line 613, in __open_connection
    self._authenticate(auth_instance)
  File "C:\Python37\lib\site-packages\snowflake\connector\connection.py", line 839, in _authenticate
    self.__authenticate(self.__preprocess_auth_instance(auth_instance))
  File "C:\Python37\lib\site-packages\snowflake\connector\connection.py", line 869, in __authenticate
    session_parameters=self._session_parameters,
  File "C:\Python37\lib\site-packages\snowflake\connector\auth.py", line 202, in authenticate
    url, headers, json.dumps(body),
  File "C:\Python37\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:\Python37\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Python37\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Python37\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

Process finished with exit code 1

**Below is the code im trying to run, it used to run fine and suddenly throwing the above error, I just pull the rsa.key and use it to connect to snowflake for data loads

import snowflake.connector
import sys
import os
from os import path
import json
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.hazmat.primitives import serialization
import boto3

def execute_snowflake_command(root_client_folder, etl_name, **runtime_params):
    with open('D:\configurations\snowflake\config.json') as f:
        sf_keys = json.load(f)
    print(sf_keys)

    sf_private_key = sf_keys['SF_PRIVATE_KEY']
    sf_automation_pwd = sf_keys['SF_AUTOMATION_PWD']
    sf_username = sf_keys['SF_USERNAME']

    folder_location_parameters = '{root_client_folder}parameters\\{etl_name}.json'.format(root_client_folder=root_client_folder,etl_name=etl_name)
    folder_location_sql = '{root_client_folder}sql\\{etl_name}.sql'.format(root_client_folder=root_client_folder,etl_name=etl_name)
    # print(folder_location_sql)
    with open(folder_location_parameters) as f:
        etl_params = json.load(f)
    
    # if any ETL param references another ETL param, replace the value
    for key in etl_params:
        etl_params[key] = etl_params[key].format(**etl_params)

    # unpack and merge preset ETL parameters and runtime parameters
    params = dict()
    for specific_param in [etl_params, runtime_params]:    
        params.update(specific_param)

    if('e' in params):
        params['e'] = params['e'].replace('\'','"')

    with open(sf_private_key, "rb") as key:
        p_key= serialization.load_pem_private_key(
            key.read(),
            password=sf_automation_pwd.encode(),
            backend=default_backend()
        )
    print(p_key)
    pkb = p_key.private_bytes(
        encoding=serialization.Encoding.DER,
        format=serialization.PrivateFormat.PKCS8,
        encryption_algorithm=serialization.NoEncryption())

    ctx = snowflake.connector.connect(
        user=sf_username,
        account=params['account'],
        private_key=pkb,
        warehouse=params['warehouse'],
        database=params['database'],
        schema=params['schema']
        )

0

There are 0 best solutions below