From d625f934de3f10444721ab0e1c04a1e4d2de70f8 Mon Sep 17 00:00:00 2001 From: ZhmakaAS Date: Mon, 18 Apr 2022 17:28:25 +0200 Subject: [PATCH 1/2] zhars/update_python_examples Update python example --- examples/python/encryptor_config_with_zone.yaml | 13 ++++++++++++- examples/python/encryptor_config_without_zone.yaml | 13 ++++++++++++- examples/python/example_with_zone.py | 14 +++++--------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/examples/python/encryptor_config_with_zone.yaml b/examples/python/encryptor_config_with_zone.yaml index b9334ff12..a37979639 100644 --- a/examples/python/encryptor_config_with_zone.yaml +++ b/examples/python/encryptor_config_with_zone.yaml @@ -46,4 +46,15 @@ schemas: - id - email encrypted: - - column: email \ No newline at end of file + - column: email + + - table: test_example_with_zone + columns: + - id + - data + - raw_data + encrypted: + - column: data + data_type: bytes + # base64 bytes value + default_data_value: dGVzdC1kYXRhCg== \ No newline at end of file diff --git a/examples/python/encryptor_config_without_zone.yaml b/examples/python/encryptor_config_without_zone.yaml index 6624600ed..1ac21cc8b 100644 --- a/examples/python/encryptor_config_without_zone.yaml +++ b/examples/python/encryptor_config_without_zone.yaml @@ -39,4 +39,15 @@ schemas: - id - email encrypted: - - column: email \ No newline at end of file + - column: email + + - table: test_example_without_zone + columns: + - id + - data + - raw_data + encrypted: + - column: data + data_type: bytes + # base64 bytes value + default_data_value: ZGVmYXVsdAo= \ No newline at end of file diff --git a/examples/python/example_with_zone.py b/examples/python/example_with_zone.py index 77a5014a1..137fd0074 100644 --- a/examples/python/example_with_zone.py +++ b/examples/python/example_with_zone.py @@ -14,7 +14,7 @@ # coding: utf-8 import argparse import ssl -from sqlalchemy import (Table, Column, Integer, MetaData, select, LargeBinary, Text, cast) +from sqlalchemy import (Table, Column, Integer, MetaData, select, LargeBinary, Text, literal_column) from sqlalchemy.dialects.postgresql import BYTEA from acrawriter import create_acrastruct from common import get_engine, get_default, get_zone, register_common_cli_params @@ -25,7 +25,7 @@ def print_data(zone_id, connection): console""" result = connection.execute( # explicitly pass zone id before related data - select([cast(zone_id.encode('utf-8'), BYTEA), test_table])) + select([literal_column("'{}'".format(zone_id)), test_table])) result = result.fetchall() ZONE_ID_INDEX = 0 print("use zone_id: ", zone_id) @@ -33,8 +33,8 @@ def print_data(zone_id, connection): for row in result: print( "{:<3} - {} - {} - {}\n".format( - row['id'], row[ZONE_ID_INDEX].decode('utf-8'), - row['data'].decode('utf-8', errors='ignore'), row['raw_data'])) + row['id'], row[ZONE_ID_INDEX], + row['data'].decode('utf-8', errors='ignore').rstrip(), row['raw_data'])) def write_data(data, connection, sslcontext=None): @@ -45,10 +45,7 @@ def write_data(data, connection, sslcontext=None): encrypted_data = create_acrastruct( data.encode('utf-8'), key, zone_id.encode('utf-8')) - connection.execute( - test_table.insert(), data=encrypted_data, - zone_id=zone_id.encode('utf-8'), - raw_data=data) + connection.execute(test_table.insert(), data=encrypted_data, raw_data=data) if __name__ == '__main__': @@ -67,7 +64,6 @@ def write_data(data, connection, sslcontext=None): test_table = Table( 'test_example_with_zone', metadata, Column('id', Integer, primary_key=True, nullable=False), - Column('zone_id', LargeBinary, nullable=True), Column('data', LargeBinary, nullable=False), Column('raw_data', Text, nullable=False), ) From 34a13da5029563701d54904fa25e77db9a7989e8 Mon Sep 17 00:00:00 2001 From: ZhmakaAS Date: Tue, 19 Apr 2022 09:30:01 +0200 Subject: [PATCH 2/2] zhars/update_python_examples Fixed after review --- examples/python/encryptor_config_with_zone.yaml | 2 +- examples/python/encryptor_config_without_zone.yaml | 4 ++-- examples/python/example_with_zone.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/python/encryptor_config_with_zone.yaml b/examples/python/encryptor_config_with_zone.yaml index a37979639..1eaa36032 100644 --- a/examples/python/encryptor_config_with_zone.yaml +++ b/examples/python/encryptor_config_with_zone.yaml @@ -56,5 +56,5 @@ schemas: encrypted: - column: data data_type: bytes - # base64 bytes value + # base64 bytes value `test-data` default_data_value: dGVzdC1kYXRhCg== \ No newline at end of file diff --git a/examples/python/encryptor_config_without_zone.yaml b/examples/python/encryptor_config_without_zone.yaml index 1ac21cc8b..b1cf11e7f 100644 --- a/examples/python/encryptor_config_without_zone.yaml +++ b/examples/python/encryptor_config_without_zone.yaml @@ -49,5 +49,5 @@ schemas: encrypted: - column: data data_type: bytes - # base64 bytes value - default_data_value: ZGVmYXVsdAo= \ No newline at end of file + # base64 bytes value `test-data` + default_data_value: dGVzdC1kYXRhCg \ No newline at end of file diff --git a/examples/python/example_with_zone.py b/examples/python/example_with_zone.py index 137fd0074..a3007cf1a 100644 --- a/examples/python/example_with_zone.py +++ b/examples/python/example_with_zone.py @@ -14,7 +14,7 @@ # coding: utf-8 import argparse import ssl -from sqlalchemy import (Table, Column, Integer, MetaData, select, LargeBinary, Text, literal_column) +from sqlalchemy import (Table, Column, Integer, MetaData, select, LargeBinary, Text, literal) from sqlalchemy.dialects.postgresql import BYTEA from acrawriter import create_acrastruct from common import get_engine, get_default, get_zone, register_common_cli_params @@ -25,7 +25,7 @@ def print_data(zone_id, connection): console""" result = connection.execute( # explicitly pass zone id before related data - select([literal_column("'{}'".format(zone_id)), test_table])) + select([literal(zone_id), test_table])) result = result.fetchall() ZONE_ID_INDEX = 0 print("use zone_id: ", zone_id)