forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_unit_encryption_util.py
More file actions
93 lines (81 loc) · 2.85 KB
/
Copy pathtest_unit_encryption_util.py
File metadata and controls
93 lines (81 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2019 Snowflake Computing Inc. All right reserved.
#
import codecs
import glob
import os
import tempfile
from os import path
from snowflake.connector.compat import PY2
from snowflake.connector.constants import (UTF8)
from snowflake.connector.encryption_util import SnowflakeEncryptionUtil
from snowflake.connector.remote_storage_util import \
SnowflakeFileEncryptionMaterial
THIS_DIR = path.dirname(path.realpath(__file__))
def test_encrypt_decrypt_file():
"""
Encrypt and Decrypt a file
"""
encryption_material = SnowflakeFileEncryptionMaterial(
query_stage_master_key='ztke8tIdVt1zmlQIZm0BMA==',
query_id='123873c7-3a66-40c4-ab89-e3722fbccce1',
smk_id=3112)
data = 'test data'
input_fd, input_file = tempfile.mkstemp()
encrypted_file = None
decrypted_file = None
try:
with codecs.open(input_file, 'w', encoding=UTF8) as fd:
fd.write(data)
(metadata, encrypted_file) = SnowflakeEncryptionUtil.encrypt_file(
encryption_material, input_file)
decrypted_file = SnowflakeEncryptionUtil.decrypt_file(
metadata, encryption_material, encrypted_file)
contents = ''
with codecs.open(decrypted_file, 'r', encoding=UTF8) as fd:
for line in fd:
contents += line
assert data == contents, "encrypted and decrypted contents"
finally:
os.close(input_fd)
os.remove(input_file)
if encrypted_file:
os.remove(encrypted_file)
if decrypted_file:
os.remove(decrypted_file)
def test_encrypt_decrypt_large_file(tmpdir, test_files):
"""
Encrypt and Decrypt a large file
"""
encryption_material = SnowflakeFileEncryptionMaterial(
query_stage_master_key='ztke8tIdVt1zmlQIZm0BMA==',
query_id='123873c7-3a66-40c4-ab89-e3722fbccce1',
smk_id=3112)
# generates N files
number_of_files = 1
number_of_lines = 10000
tmp_dir = test_files(tmpdir, number_of_lines, number_of_files)
files = glob.glob(os.path.join(tmp_dir, 'file*'))
input_file = files[0]
encrypted_file = None
decrypted_file = None
try:
(metadata, encrypted_file) = SnowflakeEncryptionUtil.encrypt_file(
encryption_material, input_file)
decrypted_file = SnowflakeEncryptionUtil.decrypt_file(
metadata, encryption_material, encrypted_file)
contents = ''
cnt = 0
with codecs.open(decrypted_file, 'r', encoding=UTF8) as fd:
for line in fd:
contents += line
cnt += 1
assert cnt == number_of_lines, "number of lines"
finally:
os.remove(input_file)
if encrypted_file:
os.remove(encrypted_file)
if decrypted_file:
os.remove(decrypted_file)