-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use custom operators from presidio for vault
- Loading branch information
1 parent
4f2be88
commit cd6d338
Showing
4 changed files
with
105 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import base64 | ||
from typing import Dict | ||
from urllib.parse import urlparse | ||
|
||
import hvac | ||
from presidio_anonymizer.entities import InvalidParamException | ||
from presidio_anonymizer.operators import Operator, OperatorType | ||
|
||
|
||
class VaultEncrypt(Operator): | ||
def _base64ify(self, bytes_or_str): | ||
if isinstance(bytes_or_str, str): | ||
input_bytes = bytes_or_str.encode('utf8') | ||
else: | ||
input_bytes = bytes_or_str | ||
|
||
output_bytes = base64.urlsafe_b64encode(input_bytes) | ||
return output_bytes.decode('ascii') | ||
|
||
def operate(self, text: str, params: Dict = None) -> str: | ||
vault_url = params.get("vault_url") | ||
client = hvac.Client(url=vault_url) | ||
|
||
encrypt_data_response = client.secrets.transit.encrypt_data( | ||
name='orders', | ||
plaintext=self._base64ify(text), | ||
) | ||
|
||
ciphertext = encrypt_data_response['data']['ciphertext'] | ||
return ciphertext | ||
|
||
def validate(self, params: Dict = None) -> None: | ||
vault_url = params.get("vault_url") | ||
if isinstance(vault_url, str): | ||
result = urlparse(vault_url) | ||
if result.scheme and result.netloc: | ||
pass | ||
else: | ||
raise InvalidParamException(f"Invalid input, vault_url must be a valid URL.") | ||
else: | ||
raise InvalidParamException(f"Invalid input, vault_url must be a string.") | ||
|
||
def operator_name(self) -> str: | ||
return "vault_encrypt" | ||
|
||
def operator_type(self) -> OperatorType: | ||
return OperatorType.Anonymize | ||
|
||
|
||
|
||
class VaultDecrypt(Operator): | ||
def operate(self, text: str, params: Dict = None) -> str: | ||
vault_url = params.get("vault_url") | ||
client = hvac.Client(url=vault_url) | ||
|
||
decrypt_data_response = client.secrets.transit.decrypt_data( | ||
name='orders', | ||
ciphertext=text, | ||
) | ||
|
||
encodedtext = decrypt_data_response['data']['plaintext'] | ||
plaintext = base64.b64decode(encodedtext).decode('utf8') | ||
return plaintext | ||
|
||
def validate(self, params: Dict = None) -> None: | ||
vault_url = params.get("vault_url") | ||
if isinstance(vault_url, str): | ||
result = urlparse(vault_url) | ||
if result.scheme and result.netloc: | ||
pass | ||
else: | ||
raise InvalidParamException(f"Invalid input, vault_url must be a valid URL.") | ||
else: | ||
raise InvalidParamException(f"Invalid input, vault_url must be a string.") | ||
|
||
def operator_name(self) -> str: | ||
return "vault_decrypt" | ||
|
||
def operator_type(self) -> OperatorType: | ||
return OperatorType.Deanonymize |
This file was deleted.
Oops, something went wrong.