2323from tests .config_pydantic import CONFIG_TDF
2424
2525
26- def _get_configuration () -> SDK :
26+ def _get_sdk () -> SDK :
2727 return (
2828 SDKBuilder ()
2929 .set_platform_endpoint (
@@ -44,44 +44,37 @@ def _get_configuration() -> SDK:
4444 )
4545
4646
47- def encrypt_file (input_path : Path , sdk = None ) -> Path :
48- """Encrypt a file and return the path to the encrypted file."""
49- if sdk is None :
50- kas_url = os .environ .get ("OPENTDF_KAS_URL" , "https://default.kas.example.com" )
47+ def _get_sdk_and_tdf_config () -> tuple :
48+ sdk = _get_sdk ()
5149
52- # Build the SDK
53- sdk = _get_configuration ()
50+ # Create KASInfo without public key - let the SDK fetch it
51+ kas_info = KASInfo (
52+ url = CONFIG_TDF .OPENTDF_PLATFORM_URL ,
53+ default = True ,
54+ )
5455
55- # Create KASInfo without public key - let the SDK fetch it
56- kas_info = KASInfo (url = kas_url )
56+ tdf_config = sdk .new_tdf_config (
57+ attributes = ["attr1" , "attr2" ],
58+ kas_info_list = [kas_info ], # KAS info without explicit public key
59+ )
60+ return sdk , tdf_config
5761
58- # Create config with the KASInfo
59- config = sdk .new_tdf_config (
60- attributes = ["attr1" , "attr2" ], kas_info_list = [kas_info ]
61- )
62- else :
63- # If SDK is provided, we need to create a config with attributes and KASInfo
64- # Get the platform URL from the SDK
65- kas_url = sdk .get_platform_url () or "https://default.kas.example.com"
6662
67- # Create KASInfo without public key - let the SDK fetch it
68- kas_info = KASInfo ( url = kas_url )
63+ def encrypt_file ( input_path : Path ) -> Path :
64+ """Encrypt a file and return the path to the encrypted file."""
6965
70- # Create config with attributes and KASInfo
71- config = sdk .new_tdf_config (
72- attributes = ["attr1" , "attr2" ], kas_info_list = [kas_info ]
73- )
66+ # Build the SDK
67+ sdk , tdf_config = _get_sdk_and_tdf_config ()
7468
7569 output_path = input_path .with_suffix (input_path .suffix + ".tdf" )
7670 with open (input_path , "rb" ) as infile , open (output_path , "wb" ) as outfile :
77- sdk .create_tdf (infile .read (), config , output_stream = outfile )
71+ sdk .create_tdf (infile .read (), tdf_config , output_stream = outfile )
7872 return output_path
7973
8074
81- def decrypt_file (encrypted_path : Path , sdk = None ) -> Path :
75+ def decrypt_file (encrypted_path : Path ) -> Path :
8276 """Decrypt a file and return the path to the decrypted file."""
83- if sdk is None :
84- sdk = _get_configuration ()
77+ sdk = _get_sdk ()
8578
8679 output_path = encrypted_path .with_suffix (".decrypted" )
8780 with open (encrypted_path , "rb" ) as infile , open (output_path , "wb" ) as outfile :
@@ -106,7 +99,7 @@ def decrypt_file(encrypted_path: Path, sdk=None) -> Path:
10699def verify_encrypt_str () -> None :
107100 print ("Validating string encryption (local TDF)" )
108101 try :
109- sdk = _get_configuration ()
102+ sdk = _get_sdk ()
110103
111104 payload = b"Hello from Python"
112105
@@ -116,7 +109,7 @@ def verify_encrypt_str() -> None:
116109 default = True ,
117110 )
118111
119- config = sdk .new_tdf_config (
112+ tdf_config = sdk .new_tdf_config (
120113 attributes = ["attr1" , "attr2" ],
121114 kas_info_list = [kas_info ], # KAS info without explicit public key
122115 )
@@ -125,7 +118,7 @@ def verify_encrypt_str() -> None:
125118 from io import BytesIO
126119
127120 output = BytesIO ()
128- sdk .create_tdf (payload , config , output_stream = output )
121+ sdk .create_tdf (payload , tdf_config , output_stream = output )
129122 manifest_bytes = output .getvalue ()
130123 print (f"Manifest returned: { manifest_bytes [:60 ]} ... (truncated)" )
131124 assert manifest_bytes and len (manifest_bytes ) > 0
@@ -160,6 +153,11 @@ def verify_encrypt_file() -> None:
160153 ) from e
161154
162155
156+ def test_verify_encrypt_file ():
157+ """Run the file encryption verification test."""
158+ verify_encrypt_file ()
159+
160+
163161def verify_encrypt_decrypt_file () -> None :
164162 print ("Validating encrypt/decrypt roundtrip (local TDF)" )
165163 try :
@@ -168,17 +166,12 @@ def verify_encrypt_decrypt_file() -> None:
168166 input_file = tmpDir / "plain.txt"
169167 input_file .write_text ("Secret message" )
170168
171- # Build the SDK
172- sdk = _get_configuration ()
173-
174- # Get public key from KAS
175169 try :
176- # Use the encrypt_file function which now gets the public key from KAS
177- encrypted_path = encrypt_file (input_file , sdk )
170+ encrypted_path = encrypt_file (input_file )
178171 print (f"Encrypted file at: { encrypted_path } " )
179172
180173 # Decrypt the file using the same SDK
181- decrypted_path = decrypt_file (encrypted_path , sdk )
174+ decrypted_path = decrypt_file (encrypted_path )
182175 print (f"Decrypted file at: { decrypted_path } " )
183176
184177 # Verify the result
0 commit comments